Assign colours to vertices so that adjacent vertices differ. The chromatic number is the minimum number of colours.
The complexity
| Question | Complexity |
|---|---|
| Is ? (bipartiteness) | — BFS 2-colouring |
| Is ? | NP-complete, even for planar graphs |
| Compute | NP-hard |
| Approximate | inapproximable within |
| of a tree | 2 (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
| Bound | Statement |
|---|---|
| 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:
| Ordering | Guarantee |
|---|---|
| Arbitrary | |
| Largest degree first | often 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
| Method | Time | |
|---|---|---|
| Try with backtracking + DSATUR | exponential | 50-100 |
| Bitmask DP over independent sets | or | 20 |
| Inclusion-exclusion (Björklund-Husfeldt) | 20-25 | |
| Branch and bound + clique lower bound | exponential | 100+ (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
| Variant | Note |
|---|---|
| Edge colouring | colour the line graph; (Vizing) |
| List colouring | each vertex has its own allowed set; harder than ordinary colouring |
| Equitable colouring | colour classes of nearly equal size |
| Fractional chromatic number | LP relaxation; polynomial for perfect graphs |
| Total colouring | vertices and edges together |
| Colouring with colours, fixed | still 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
- Is it bipartite? → 2 colours, .
- Is it a tree or forest? → 2 colours.
- Is it an interval graph? → , sweep.
- Is it planar? → colours; 5-colouring is constructible in linear time.
- Is ? → bitmask DP.
- Otherwise → DSATUR + branch and bound, or accept a heuristic.
See also: Bipartite Graphs · Maximum Clique · Interval Partitioning