Discrete Mathematics and Combinatorics
Graph Coloring and the Problem of Scheduling Exams
Turn an exam timetable into a coloring puzzle, solve it by hand, and learn why the fewest time slots can be hard to find.
Key takeaways
- A proper coloring assigns colors to vertices so that no edge joins two vertices of the same color.
- The chromatic number is the minimum number of colors, and it is proved by showing both a coloring and why fewer colors fail.
- A group of mutually conflicting courses gives a lower bound, while a greedy procedure gives an upper bound.
- The order in which a greedy method colors vertices can change the result significantly.
- No efficient general method for finding the chromatic number is known, since the problem is NP-hard.
To schedule exams so that no student has two at once, draw a point for each course and join two points whenever some student takes both. Then give each point a color so that joined points get different colors. Each color is a time slot, and the fewest colors that work is the fewest slots you need.
That minimum is called the chromatic number of the network. Finding it exactly can be very hard for large examples, but good schedules and useful bounds are often within easy reach.
Building the conflict graph
A graph is a set of vertices (points) with edges (links) between some pairs. Say six courses are offered: Algebra, Biology, Chemistry, Drama, Economics and French, abbreviated A to F. Enrollment records show these pairs sharing at least one student:
A–B A–C B–C B–D
C–E D–E D–F E–F One shared student is enough to create an edge. Fifty shared students create the same single edge, because the clash is just as absolute.
A proper coloring gives every vertex a color so that the two ends of each edge differ. Courses with the same color share no students and can sit in the same slot.
Solving the example
First, find a lower bound. A, B and C all conflict with one another, so they need three different slots. A set of vertices that are all joined to each other is called a clique, and the chromatic number is at least the size of the largest one.
Next, try to succeed with three colors. Color A as slot 1, B as slot 2 and C as slot 3. Course D conflicts with B, so give it slot 1. E conflicts with C (slot 3) and D (slot 1), so it takes slot 2. F conflicts with D (slot 1) and E (slot 2), so it takes slot 3.
| Slot | Courses |
|---|---|
| 1 | Algebra, Drama |
| 2 | Biology, Economics |
| 3 | Chemistry, French |
Check the pairs sharing a slot: none of A–D, B–E or C–F is an edge. Three slots work and two cannot, so the chromatic number is exactly 3.
Notice the shape of the argument. A schedule shows that three is enough, and the clique shows that three is needed. You need both halves.
The greedy method and its pitfalls
The simplest algorithm takes vertices in some order and gives each the lowest-numbered color not used by its already colored neighbors.
If the largest degree, meaning the most edges at any vertex, is Δ (delta), greedy never needs more than Δ + 1 colors. At the moment a vertex is colored, its neighbors block at most Δ colors.
Order matters a great deal. Take courses a1, a2, a3 and b1, b2, b3, where each a-course conflicts with each b-course except the one with the same number. Two slots obviously suffice: all the a-courses, then all the b-courses.
Run greedy in the order a1, b1, a2, b2, a3, b3:
a1: color 1
b1: no conflict with a1, so color 1
a2: conflicts with b1, so color 2
b2: conflicts with a1 but not a2, so color 2
a3: conflicts with b1 (1) and b2 (2), so color 3
b3: conflicts with a1 (1) and a2 (2), so color 3 Three slots, when two would do. With more courses built the same way, the waste grows without limit.
Two common improvements are to color vertices from highest degree to lowest, and the DSatur rule of Daniel Brélaz: always color next the vertex whose neighbors already use the most different colors, breaking ties by degree. Neither guarantees the optimum on every graph. For more on this theme, see greedy algorithms.
Bounds worth knowing
| Type of graph | Chromatic number |
|---|---|
| No edges | 1 |
| Has an edge and no odd cycle | 2 |
| Cycle of odd length | 3 |
| n vertices all joined | n |
| Planar graph | at most 4 |
A graph that can be colored with two colors is bipartite, and you can test this quickly. Deciding whether three colors are enough is already NP-complete, meaning no efficient method is known and finding one would settle the P versus NP question.
Brooks' theorem sharpens the greedy bound: a connected graph with largest degree Δ can be colored with Δ colors unless it is a complete graph or an odd cycle.
A large clique forces many colors, but the converse fails. Jan Mycielski gave a construction of graphs with no triangles at all whose chromatic numbers are as large as you like. Conflicts can force extra slots through global structure that no small group reveals.
A second kind of lower bound
Cliques are not the only way to show that more slots are needed. The courses in one slot form an independent set, a group with no edges among them. If no independent set has more than m vertices, then V courses need at least V/m slots, rounded up.
In the six-course example, A, B, C are mutual conflicts and so are D, E, F. An independent set can take at most one from each trio, so m = 2. Six courses at two per slot need at least 3 slots, confirming the earlier result.
The idea is stronger than cliques in some cases. Take five courses in a ring, each clashing with its two neighbors. The largest clique has only 2 courses, which suggests 2 slots might do. But no independent set has more than 2 vertices, and 5/2 rounds up to 3.
Repairing a coloring by swapping
Suppose a greedy run has used one slot more than you hoped. You can sometimes remove it without starting over. Pick a course v in the extra slot, and two other slots, say red and blue.
Start at each red neighbor of v and collect every red or blue course you can reach through conflicts between red and blue courses. Each such cluster is a chain, and exchanging red and blue throughout it keeps every conflict satisfied. If none of these chains contains a blue neighbor of v, swap them all. Now v has no red neighbor, so v can become red.
This device, the Kempe chain, comes from early work on map coloring. It does not always succeed, but it is cheap to try.
Real timetables have more rules
Actual exam offices face constraints that plain coloring ignores.
- Room capacity. A slot can hold only so many students, so color classes have size limits.
- Spacing. Students prefer not to sit two exams back to back, or three in one day.
- Fixed slots. Some exams are pinned to a date in advance, which amounts to pre-coloring a few vertices.
- Availability. A course may be barred from certain slots, giving each vertex its own list of allowed colors.
Rules that must never be broken are called hard constraints. Preferences are soft constraints, scored by a penalty that software tries to reduce. Practical systems usually combine a coloring heuristic with repeated local adjustments, such as swapping two courses and keeping the change if the penalty falls.
Other things that are secretly coloring
Radio transmitters close enough to interfere need different frequencies. A compiler assigning variables to a processor's registers must separate variables that are needed at the same time. A standard Sudoku is a graph with 81 vertices, joined when two cells share a row, column or block, and solving it means extending a partial coloring with 9 colors.
You can also color edges instead of vertices. In a round-robin league, teams are vertices, matches are edges and rounds are colors, since no team plays twice in a round. With an even number n of teams, n − 1 rounds suffice. Vizing's theorem says that a simple graph with largest degree Δ needs either Δ or Δ + 1 edge colors.
Counting colorings and proved conjectures
With k colors available, a triangle can be properly colored in k(k − 1)(k − 2) ways. For any finite graph the count is a polynomial in k, the chromatic polynomial. Ronald Read conjectured that the absolute values of its coefficients rise and then fall. June Huh proved this, in work published in 2012, using ideas from algebraic geometry.
Another long-standing question concerned perfect graphs, in which every induced subgraph (a chosen set of vertices with all edges among them) has chromatic number equal to its largest clique size. Claude Berge conjectured a description of them in terms of forbidden odd cycles and their complements. Maria Chudnovsky, Neil Robertson, Paul Seymour and Robin Thomas proved it, with publication in 2006. Interval conflicts, such as room bookings by start and end time, give perfect graphs, and there a simple greedy method in order of start time is optimal.
Common mistakes
- Adding edges between courses that merely share a lecturer or room without deciding whether that is a real conflict.
- Claiming a schedule is optimal without a lower bound.
- Assuming the chromatic number equals the largest clique size.
- Trusting one greedy run. Trying several orders is cheap and often saves a slot.