Assign colours to vertices so that adjacent vertices differ. The chromatic number is the minimum number of colours.

The complexity

QuestionComplexity
Is ? (bipartiteness) — BFS 2-colouring
Is ?NP-complete, even for planar graphs
Compute NP-hard
Approximate inapproximable within
of a tree2 (or 1 if edgeless)
of a planar graph (Four Colour Theorem); deciding is NP-complete
of an interval graph= max clique; greedy by left endpoint
of a chordal graph= max clique; perfect elimination ordering
Edge colouring (Vizing); deciding which is NP-complete
Edge colouring, bipartite (König); constructible in polynomial time

The 2-vs-3 cliff mirrors 2-SAT vs 3-SAT — checking bipartiteness is trivial, one colour more is NP-complete.

Bounds

BoundStatement
at least the max clique size
greedy, any order
Brooks unless is complete or an odd cycle
degeneracy + 1
= independence number
Planar
Perfect graphs

Degeneracy is the useful practical bound: order the vertices by repeatedly removing a minimum-degree vertex, then colour greedily in reverse. Uses at most colours where is the degeneracy — often far below .

Greedy colouring

vector<int> greedyColour(int n, vector<vector<int>>& adj, vector<int>& order) {
    vector<int> colour(n, -1);
    for (int u : order) {
        vector<bool> used(n + 1, false);
        for (int v : adj[u]) if (colour[v] >= 0) used[colour[v]] = true;
        int c = 0;
        while (used[c]) c++;
        colour[u] = c;
    }
    return colour;
}

The ordering determines the quality. Useful orderings:

OrderingGuarantee
Arbitrary
Largest degree firstoften good in practice
Degeneracy (smallest-last) colours
DSATUR (most saturated first)optimal for bipartite and several other classes
Perfect elimination (chordal graphs)optimal

DSATUR — always colour the vertex with the most distinctly-coloured neighbours — is the best simple heuristic and is often optimal on structured graphs.

Exact chromatic number

MethodTime
Try with backtracking + DSATURexponential50-100
Bitmask DP over independent sets or 20
Inclusion-exclusion (Björklund-Husfeldt)20-25
Branch and bound + clique lower boundexponential100+ (sparse)

The inclusion-exclusion

is -colourable iff the number of ways to cover with (possibly overlapping) independent sets is non-zero:

where is the number of independent subsets of — computable for all by a DP. Elegant, and the best known exact algorithm.

Bitmask DP over independent sets

dp[mask] = minimum colours to cover mask; transition over maximal independent submasks. , or with SOS preprocessing of which subsets are independent.

Variants

VariantNote
Edge colouringcolour the line graph; (Vizing)
List colouringeach vertex has its own allowed set; harder than ordinary colouring
Equitable colouringcolour classes of nearly equal size
Fractional chromatic numberLP relaxation; polynomial for perfect graphs
Total colouringvertices and edges together
Colouring with colours, fixedstill NP-complete for
Interval graph colouring= maximum overlap — see Interval Partitioning

Interval graphs are the case that appears most in contests, usually disguised as “minimum number of rooms/machines/platforms”. There, = maximum simultaneous overlap, and a sweep solves it in .

Where colouring appears

  • Register allocation in compilers — the classic application.
  • Scheduling with conflicts — exams, meetings, frequencies.
  • Map colouring — planar, hence .
  • Sudoku — a colouring of a specific 81-vertex graph with 9 colours.
  • Timetabling, frequency assignment, partitioning into independent sets.

The practical checklist

  1. Is it bipartite? → 2 colours, .
  2. Is it a tree or forest? → 2 colours.
  3. Is it an interval graph? → , sweep.
  4. Is it planar? → colours; 5-colouring is constructible in linear time.
  5. Is ? → bitmask DP.
  6. Otherwise → DSATUR + branch and bound, or accept a heuristic.

See also: Bipartite Graphs · Maximum Clique · Interval Partitioning