Sub topic 13
From Two Colours to Three: Where It Gets Hard
This chapter assumes you know the BFS two-colouring algorithm from earlier chapters and its O(V + E) running time. You should also have a rough idea of what NP-complete means: no known polynomial-time algorithm exists, and finding one would give a polynomial-time algorithm for every problem in NP.
Two-colouring is solved in linear time. Adding a single colour changes that completely. Deciding whether a graph can be properly coloured with three colours is NP-complete. This chapter explains what changes between two colours and three.
Why Two Colours Leave No Choices
In two-colouring, the colour of the first vertex settles everything else in its component. If u is colour 0, every neighbour must be colour 1, every neighbour of those must be colour 0, and so on. The algorithm never guesses. Each edge (u, v) says colour[v] = 1 - colour[u], so one value fixes the other.
Because of this, BFS can commit to a colour the moment it reaches a vertex. If a conflict appears later, no other choice would have avoided it, and the odd cycle theorem tells you the graph is simply not bipartite.
Why Three Colours Do
With three colours, the edge constraint colour[u] != colour[v] no longer fixes v. If u is red, v can be green or blue. A local choice can look fine and still cause a conflict many edges away, and no simple local test detects this in advance.
The obvious exact method is backtracking: try a colour for each vertex and undo it when a later vertex has no legal colour left.
def k_colour(n, adj, k):
"""Return a proper k-colouring as a list, or None. Exponential in the worst case."""
colour = [-1] * n
def place(u):
if u == n:
return True
for c in range(k):
if all(colour[v] != c for v in adj[u]):
colour[u] = c
if place(u + 1):
return True
colour[u] = -1
return False
return colour if place(0) else None
triangle = [[1, 2], [0, 2], [0, 1]]
k4 = [[1, 2, 3], [0, 2, 3], [0, 1, 3], [0, 1, 2]]
print(k_colour(3, triangle, 2)) # None
print(k_colour(3, triangle, 3)) # [0, 1, 2]
print(k_colour(4, k4, 3)) # NoneIn the worst case this explores up to 3^V partial assignments. Better exact algorithms exist, running in about O(1.33^V), but they are all still exponential.
The Formal Picture
Karp showed in 1972 that graph colouring is NP-complete. The standard proof reduces 3-SAT to 3-colouring. Small "gadget" subgraphs stand for true, false, and each clause, and a gadget can be coloured only if its clause is satisfied. A fast 3-colouring algorithm would therefore solve 3-SAT quickly.
There is a neat algebraic way to see the difference. Two-colouring is a system of equations x_u XOR x_v = 1 over {0, 1}. Linear systems like this can be solved efficiently, and the parity union-find structure from an earlier chapter is exactly such a solver. The constraint "u and v differ" over three values is not linear in this way, so the method does not carry over.
| Number of colours k | Decision problem | Best known exact method |
|---|---|---|
| 1 | Polynomial: the graph has no edges | Check E = 0 |
| 2 | Polynomial | BFS/DFS, O(V + E) |
| 3 | NP-complete, even for planar graphs with maximum degree 4 | Exponential time |
| k ≥ 3 | NP-complete | Exponential time |
What to Do in Practice
- If two colours are enough for your model (conflict splitting, two shifts, two teams), use the linear-time algorithm.
- If you need three or more colours, use a heuristic such as greedy colouring in DSatur order (most constrained vertex first) when an approximate answer is acceptable.
- For exact answers on moderate sizes, encode the problem for a SAT or integer-programming solver instead of writing your own backtracking.
Exercise
Someone proposes adapting BFS to three colours: give each newly reached vertex the smallest colour not used by its already-coloured neighbours, and report "not 3-colourable" if no colour is free. Why is this not a correct decision procedure?
Show answer
The colour picked for a vertex is a guess, not a forced value. A different earlier choice might have left a colour free later. When this greedy BFS gets stuck, that only shows its particular choices failed, not that no 3-colouring exists. With two colours the choice was forced, so a conflict was a real proof, an odd cycle. With three colours a conflict proves nothing.
Exercise
Express 2-colourability of the edge set {(0,1), (1,2), (2,0)} as equations over {0, 1}, and show they have no solution.
Show answer
The equations are x0 XOR x1 = 1, x1 XOR x2 = 1, and x2 XOR x0 = 1. XOR-ing all three gives 0 on the left, because each variable appears twice, and 1 XOR 1 XOR 1 = 1 on the right. Since 0 = 1 is impossible, the triangle is not 2-colourable.