How many colors are enough?
You are coloring a blank map with pencils, and neighboring regions must get different colors. How many colors do you need? The famous answer, the Four Color Theorem: four are always enough, for any map. That short answer took 124 years to prove, and in the end it was not a person but a computer that checked the enormous number of cases. But three colors? Some maps can be done with three; others cannot.
How would you check a given map? The honest way is to try every coloring. With regions there are colorings. For the seven prefectures of the Kantō region around Tokyo, that is , a blink for a computer. For all 47 prefectures of Japan it is , about . At a hundred million per second, that takes millions of years.
Here we watch, move by move, a method that does not try everything yet misses nothing: backtracking. Down the road waits the most famous open question in computer science.
Redraw the map as dots and lines
First, redraw the map. Make each region a dot, called a vertex, and connect two regions that share a border with a line, called an edge. That is a graph. Now the problem is: color the vertices with three colors so that the two ends of every edge differ. Shapes and areas no longer matter, only who touches whom.
Plenty of other problems have the same shape. Take an exam timetable: subjects are vertices, and two subjects are joined if some student takes both. Read colors as time slots, and a 3-coloring is a timetable with three slots in which nobody has two exams at once.
Graph coloring works quietly in many places. The apps on your phone may well have been produced by a compiler that solved this very problem. It joins variables that are in use at the same time and treats the processor's few storage slots, the registers, as colors; when the colors run out, some variables are moved out to memory. IBM researchers including John Cocke and Gregory Chaitin turned this into a working method in 1981–82, for a compiler for a new kind of processor design, and its ideas and refinements are still in use.
Mobile networks face the same puzzle: nearby base stations on the same frequency interfere, so stations become vertices, nearby pairs become edges, and frequencies become colors. Scheduling is the timetable example itself, and even Sudoku is one: make the 81 cells vertices, join cells in the same row, column or box, and a solution is a 9-coloring.
Modeling, a central idea of Informatics I, is exactly what we did with maps and timetables: turn them into vertices and edges and one tool solves problems that look different. Choosing what the vertices and edges are is the modeling.
The other simulations in this collection share the same roots. Search Algorithms with the 8-Puzzle hunts for shortest paths, The Tower of Hanoi and Recursion builds moves by recursion, and Connected Components with a Falling-Block Puzzle follows same-colored neighbors. All of them are graph ideas in disguise.
Proving that four colors suffice
In 1852 Francis Guthrie, who had studied under Augustus De Morgan at University College London, is said to have noticed while coloring a map of the counties of England that four colors seemed to be enough. He asked his brother Frederick to pass the question to De Morgan, who wrote about it the same day to the Irish mathematician William Rowan Hamilton. Hamilton replied three days later that he was not likely to try it any time soon.
In 1878 Arthur Cayley asked the London Mathematical Society whether the problem had been solved, and in 1879 Alfred Kempe published a proof. It was widely accepted and went unchallenged for eleven years, until in 1890 Percy Heawood found the flaw and, in the same paper, proved that five colors always suffice. Kempe's trick of swapping two colors along a chain survived and was used in later proofs.
The question was settled in 1976 by Kenneth Appel and Wolfgang Haken at the University of Illinois. They narrowed the problem down to about 1,500 configurations, one of which must appear in any map, and used around 1,200 hours of computer time to check that none of them can block a 4-coloring. The mathematics department stamped its mail with the postmark "Four colors suffice." But many mathematicians were uneasy: can something no human can check by hand count as a proof? In the mid-1990s Neil Robertson, Daniel Sanders, Paul Seymour and Robin Thomas produced a proof with only 633 configurations, still checked by computer, and in 2005 Georges Gonthier and Benjamin Werner verified the entire proof formally in a proof-checking program called Coq.
The theorem also raises the question of what a proof is. The Four Color Theorem was the first major proof that could not be checked without a computer, and research on having computers verify proofs grew out of such questions. By the way, Hamilton, the first person De Morgan wrote to, is the inventor of the quaternions you meet in this collection's 3D Rotation Lab.
Follow the string back
Back to three colors.
So how does backtracking work? Picture yourself in a maze, unrolling a ball of string, the same trick Theseus used, in the Greek myth, to find his way back out of the Labyrinth with Ariadne's thread. At a fork you pick a path; at a dead end you follow the string back to the last fork and try another.
On a graph, you take the vertices one at a time in a fixed order and try red, blue and yellow in turn. A color is ruled out if an already-colored neighbor has it; if some color is allowed, you fix it and move on to the next vertex. If no color is allowed, you have hit a dead end: go back to the previous vertex and switch it to its next untried color.
How many possibilities? Coloring vertices with three colors can be done in ways. The first vertex branches three ways, each of those three ways again, and so on, so level of the search tree holds nodes, and the tree has at most
nodes. The right-hand side comes from a classic trick: call the sum and work out ; all the middle terms cancel, leaving . The whole tree is only about 1.5 times the number of leaves, , so it is that sets the cost.
Counting shows why brute force fails: triples with each extra vertex.
The key is never to explore beyond a dead end. If the first two vertices already clash, no way of coloring the rest can fix it. Cutting off such hopeless branches usually skips the vast majority of the colorings.
A cleverer variant: when you fix a color, cross it off the possible colors of every uncolored neighbor. If some vertex's list becomes empty, go back at once. This is forward checking: it spots a dead end before you actually walk into it.
When the answer is "impossible"
Backtracking only throws away partial colorings in which two neighbors already share a color. No completion can repair that. So if any proper coloring exists, the search must reach it somewhere. If every branch is exhausted without one, there truly is none.
Time for a quiz. Take a "wheel": a hub vertex surrounded by a rim, with the hub joined to every rim vertex. Can it be 3-colored? The answer depends on how many vertices the rim has.
Graphs that cannot be done. Four vertices all joined to each other form the graph , which needs four different colors. More fun is the wheel: a hub joined to every vertex of a rim. Color the hub red; then the rim must use only blue and yellow, alternating blue, yellow, blue, yellow… around the circle. With an odd number of rim vertices, the alternation clashes when you come back to the start. A rim of 6 works; a rim of 5 does not.
There is even a link to physics. In graphs whose edges are drawn uniformly at random, as the edges get denser the graph is believed to switch abruptly from "almost surely 3-colorable" to "almost surely not" when the average number of edges per vertex, which is called the average degree, passes about 4.7. That value was worked out with the tools physicists use for phase transitions, like water freezing into ice. The random graphs in this simulation are planar, like maps, so the switch-over point is not the same, but you can still watch colorability disappear as the density rises.
Easy to check, hard to find
If someone hands you a coloring, checking it is quick: look at each edge once. It is like a jigsaw puzzle: shown the finished picture, you can tell at a glance whether every piece fits, but assembling it from a heap of pieces is hard work. The analogy borrows only that gap between checking and finding. But nobody knows a method that finds a coloring for every graph in time growing like a polynomial in the number of vertices. Among the problems whose answers can be checked quickly, the hardest are called NP-complete, and 3-coloring is a classic one. A fast method for any one of them would give fast methods for all of them. Why? The problems whose answers can be checked quickly make up the class NP, and every one of them can be rewritten as a 3-coloring problem with an amount of work that grows only like a polynomial in its size; that is the substance of Cook's and Karp's work. Rewrite, solve the 3-coloring quickly, and you have solved the original. The 3-term DNF reduction below is a concrete example of such a rewriting, going the other way: it turns 3-coloring into a learning problem. Whether that is truly impossible is the famous P versus NP question. Whoever finds a fast method would get more than the million-dollar prize we'll meet just below. Much of today's cryptography relies on certain problems being hard to solve quickly, so the foundations of information security would be shaken. Note that NP-completeness is about the nastiest possible graphs; many graphs you actually meet, like the ones here, are solved in a flash.
The theory of computational hardness began in 1971, when Stephen Cook introduced NP-completeness. Leonid Levin reached the same idea independently in the Soviet Union. In 1972 Richard Karp showed that 21 problems, graph coloring among them, are all NP-complete: one of the first demonstrations that many natural problems are probably intractable. In 2000 the Clay Mathematics Institute named P versus NP one of seven Millennium Prize Problems, each with a one-million-dollar prize.
Cryptography has a famous twist. In 1986 Oded Goldreich, Silvio Micali and Avi Wigderson showed how to convince someone that you know a 3-coloring of a graph without revealing a single color of it. You hide the colors in locked boxes and open only the two ends of one edge chosen by the other person. Repeat many times, renaming the colors every round, and a cheat will eventually be caught, while the coloring itself leaks nothing. Such zero-knowledge proofs are now real technology for proving that something is correct without revealing the details, used for example in cryptocurrencies. Again the key is that finding is hard while checking is easy.
For the curious: the 3-term DNF reduction
From a graph with vertices, build -bit examples. For each vertex , the string with a 0 only at position is a positive example, with label 1. For each edge , the string with 0s at and is a negative example, with label 0. Given a coloring, make one term per color: is the AND of the variables of all vertices not colored . Writing R, B and Y for red, blue and yellow, the formula classifies every example correctly: vertex 's example satisfies the term of 's own color, and an edge's example satisfies no term, since its ends differ in color. Conversely, any 3-term formula consistent with all examples yields a coloring. So finding a 3-term DNF consistent with examples is as hard as 3-coloring.
The 3-term DNF reduction was used in 1988 by Leonard Pitt and Leslie Valiant to show that learning certain rules from examples is computationally hard. Valiant had founded the "PAC" theory of learning in 1984, and he received the Turing Award in 2010.
Color it yourself
- The opening graph is the map of the Kantō prefectures. Tap a vertex to change its color, which cycles red → blue → yellow → none. A green frame and "Valid 3-coloring!" mean a proper 3-coloring.
- Press Clear all colors, then 🧩 Solve by backtracking at the top of the panel. Read the Event log on the right as colors are tried, set and undone. One step goes one event at a time.
- Predict first: under Choose a graph, which of Wheel (rim of 6) and Wheel (rim of 5) can be 3-colored? Then press ⚡ Solve at once to check.
- On the rim-5 wheel, solve with Forward checking off and then on, and compare the counts on the right: Colors placed and Backtracks from dead ends. Try Order of vertices too.
- Move Random: edges per vertex (average) from 2 to 5, pressing Make a random graph and solving each time. How often do you get "Checked every case: no 3-coloring exists"?
- Advanced: scroll to Going further (university): reduction to 3-term DNF at the end of the panel. Give two neighbors the same color, or leave one vertex blank. Which examples get ✗?
Honest warning: the search is slowed to a few steps per second so you can follow it; ⚡ Solve at once shows its real speed.
From one student's question
Maps and timetables become graphs, and coloring them with three colors is the 3-coloring problem. Backtracking fixes colors one by one and retreats at dead ends; by cutting hopeless branches it avoids trying everything, yet misses nothing. Checking an answer is easy while finding one is hard: 3-coloring is a classic NP-complete problem, and nobody knows whether a fast general method exists.
A student's question while coloring a map went through a wrong proof and a 124-year detour to a computer-assisted proof, and the theory of hardness that grew up along the way now supports compilers, communications and cryptography. To ponder: the Four Color Theorem says every map can be colored with four colors. Does that make it easy to decide whether a map can be colored with three? Surprisingly, no: even for maps, deciding 3-colorability is known to be NP-complete. Why such a gap between four and three?
Sources and further reading
The facts in this article were checked mainly against the following.
- MacTutor, "The four colour theorem" (University of St Andrews): the Guthrie brothers, De Morgan's letter to Hamilton, Cayley, Kempe, Heawood, Appel and Haken
- A. B. Kempe, "On the Geographical Problem of the Four Colours," American Journal of Mathematics, 1879
- K. Appel and W. Haken, "Every planar map is four colorable. Part I: Discharging," Illinois Journal of Mathematics, 1977
- N. Robertson, D. Sanders, P. Seymour and R. Thomas, "The Four-Colour Theorem," Journal of Combinatorial Theory, Series B, 1997
- S. A. Cook, "The complexity of theorem-proving procedures," STOC, 1971
- R. M. Karp, "Reducibility among Combinatorial Problems," 1972
- Clay Mathematics Institute, "P vs NP": the Millennium Prize Problem
- L. Pitt and L. G. Valiant, "Computational limitations on learning from examples," Journal of the ACM, 1988
- L. G. Valiant, "A theory of the learnable," Communications of the ACM, 1984
- G. J. Chaitin, "Register allocation & spilling via graph coloring," ACM SIGPLAN Notices, 1982
- O. Goldreich, S. Micali and A. Wigderson, "Proofs that yield nothing but their validity or all languages in NP have zero-knowledge proof systems," Journal of the ACM, 1991
- R. Mulet, A. Pagnani, M. Weigt and R. Zecchina, "Coloring random graphs," Physical Review Letters, 2002 (arXiv): the threshold of about 4.69
Further reading (encyclopedias):
- Wikipedia, "Four color theorem": the 633 configurations, the Coq verification, the "Four colors suffice" postmark
- Wikipedia, "Karp's 21 NP-complete problems"
- Wikipedia, "Zero-knowledge proof": use in cryptocurrencies