What the greedy colouring algorithm gets wrong Image: Thore Husfeldt, CC BY-SA 3.0

What the greedy colouring algorithm gets wrong

Number the vertices of a graph 1 to n in any order. Give vertex 1 colour 1. Give each later vertex the smallest colour number that none of its already coloured neighbours has. Every vertex gets a colour, adjacent vertices get different colours, and the whole thing takes one pass. This is the greedy colouring algorithm, and it is what everyone tries first. How badly it can fail depends entirely on the order.

The example

Take eight vertices in two rows of four, a1 to a4 above and b1 to b4 below, and join ai to bj whenever i and j differ. This is the cover picture of this post. Every edge runs between the rows, so the graph is bipartite and two colours suffice: all of the top row one colour, all of the bottom row the other.

Now run the greedy algorithm in the order a1, b1, a2, b2, a3, b3, a4, b4. Vertex a1 gets colour 1. Vertex b1 is not adjacent to a1 (the indices are equal), so it also gets colour 1. Vertex a2 is adjacent to b1, which has colour 1, so a2 gets colour 2. Vertex b2 is adjacent to a1 (colour 1) and not to a2, so b2 gets colour 2. Vertex a3 is adjacent to b1 and b2, colours 1 and 2, so a3 gets 3; b3 is adjacent to a1 and a2, colours 1 and 2, so b3 gets 3. In the same way a4 and b4 get colour 4. Four colours for a graph that needs two, and with 2n vertices the same order uses n colours. There is no bound on the waste of the greedy algorithm in terms of what the graph actually needs.

Run it instead in the order a1, a2, a3, a4, b1, b2, b3, b4. The four a vertices are pairwise non-adjacent, so all get colour 1; each b vertex then sees colour 1 on its neighbours and gets colour 2. Two colours. Same algorithm, same graph.

The order that always works

For every graph there is an order in which the greedy algorithm uses exactly the chromatic number of colours, the smallest number possible. The proof is one sentence: take any best colouring, list all the vertices of colour 1 first, then all of colour 2, and so on; the greedy algorithm, run in that order, never needs a colour beyond the one the vertex had. So the greedy algorithm is not weak in itself. What is hard is finding the order, and finding it is exactly as hard as finding the colouring, which for three colours is already NP-complete.

Orders that are cheap to compute still give useful guarantees. Listing the vertices by decreasing degree, which Welsh and Powell proposed in 1967, bounds the number of colours by the largest i such that the ith vertex in that list has degree at least i minus 1. Better is the degeneracy order: repeatedly remove a vertex of smallest degree from what remains, and colour in the reverse order of removal. If every subgraph of the graph has a vertex of degree at most d, this order uses at most d + 1 colours. For planar graphs d is at most 5 by Euler's formula, so the greedy algorithm in degeneracy order colours every map with six colours, and Unit 7 of the Graph Theory Masterclass runs it on a drawn map.

Brooks's theorem

Whatever the order, the greedy algorithm never uses more than Δ + 1 colours, where Δ is the largest degree in the graph, because a vertex has at most Δ coloured neighbours when its turn comes. The bound is reached by two kinds of graph: the complete graph on Δ + 1 vertices, where every vertex is adjacent to every other, and the odd cycle, where Δ = 2 and three colours are needed. In 1941 Rowland Leonard Brooks proved that these are the only cases: every connected graph that is neither complete nor an odd cycle can be coloured with Δ colours. The theorem is sharp, and the greedy algorithm in a cleverly chosen order is one way to prove it, with a special argument for the vertex coloured last. Unit 7 gives the proof for the easy case, a graph with some vertex of degree below Δ, and states the full result.

Brooks's theorem does not say that Δ colours are needed. A star with a thousand leaves has Δ = 1000 and needs two colours. The chromatic number sits somewhere between the size of the largest clique and Δ + 1, and the gap can be enormous in both directions: there are graphs with no triangle at all whose chromatic number is as large as one likes, a theorem of Erdős from 1959 that Unit 11 proves with a random graph.

Timetables are colourings

A university has to schedule examinations. Each examination is a vertex, and two are joined by an edge if some student sits both, so that they cannot be at the same time. A timetable with k examination slots is a colouring with k colours, and the smallest number of slots is the chromatic number. Six examinations with the conflicts of a small example in Unit 7 need three slots, and the proof that two will not do is a triangle: three examinations that pairwise share a student.

Real timetabling software is greedy colouring with good orders and a great deal of local repair afterwards, because the real problem has side conditions, such as room sizes and a rule that no student sits two examinations in a row. Frequency assignment for mobile phone masts is the same problem with transmitters as vertices and interference as edges, and the compiler that turns a program into machine code colours a graph whose vertices are variables and whose edges say that two variables are alive at the same time, so that the colours are the processor's registers. Chaitin's register allocator of 1981 is literally the degeneracy ordering above, with a rule for what to do when the colours run out.

Sudoku

A Sudoku grid is a graph with 81 vertices, one per cell, in which two cells are adjacent if they share a row, a column, or a box. Each cell has 8 + 8 + 4 = 20 neighbours (the 4 is the box cells not already counted in its row and column), so the graph has 81 times 20 over 2 = 810 edges. A completed Sudoku is a colouring of this graph with 9 colours, and the puzzle is to extend a partial colouring. The greedy algorithm in reading order is what a beginner does, and it gets stuck for the same reason as on the bipartite example: an early choice that looked harmless rules out the only colour a later vertex can take. Solvers backtrack, which the greedy algorithm never does.

The point of the eight-vertex example is small and permanent. The algorithm is fine. The order is the whole problem, and the problem is hard.

Study graph theory properly

The Graph Theory Masterclass: weekly units, quizzes, a final exam, and a certificate.
About the program