Graph Coloring: Finding the Chromatic Number

simulator intermediate ~10 min
Loading simulation...
Greedy coloring assigns colors in vertex order, using the first available color.

A greedy coloring algorithm processes vertices in order and assigns each the smallest color not used by its neighbours. The result uses at most Δ+1 colors, where Δ is the maximum degree, but may not achieve the chromatic number.

Formula

χ(G) ≤ Δ(G) + 1 — greedy upper bound (Brook's theorem: χ ≤ Δ for connected graphs that are not complete or odd cycles)
χ(Kₙ) = n — complete graph on n vertices needs n colors

Coloring Maps and Beyond

The graph coloring problem has its roots in cartography: can you color a map so that no two neighbouring countries share the same color? In 1852, Francis Guthrie conjectured that four colors always suffice for any planar map — a claim that took 124 years and a computer to prove. But graph coloring extends far beyond maps into scheduling, wireless frequency allocation, compiler optimization, and combinatorial puzzles like Sudoku.

Greedy Coloring Algorithm

The simplest approach is greedy: process vertices one by one and assign each the smallest color not already used by its neighbours. This runs in O(V + E) time and guarantees at most Δ+1 colors, where Δ is the maximum vertex degree. However, the result depends heavily on vertex ordering — a bad order can use far more colors than necessary, while an optimal order achieves the chromatic number.

The Chromatic Number

The chromatic number χ(G) is the minimum number of colors needed for a proper coloring. Computing it exactly is NP-hard, meaning no known polynomial-time algorithm exists for general graphs. Special cases are easier: bipartite graphs need 2 colors, trees need 2, complete graphs on n vertices need n, and planar graphs need at most 4 (by the four color theorem). Brooks' theorem sharpens the greedy bound to Δ for graphs that aren't complete or odd cycles.

From Theory to Silicon

One of the most important practical applications of graph coloring is register allocation in compilers. Variables that are simultaneously alive in a program form an interference graph; coloring this graph with k colors corresponds to assigning k CPU registers. When k colors aren't enough, the compiler must 'spill' variables to memory — a costly operation. Better coloring heuristics directly translate to faster compiled code.

FAQ

What is graph coloring?

Graph coloring assigns labels (colors) to vertices such that no two adjacent vertices share the same color. The chromatic number χ(G) is the minimum number of colors needed. It is one of the fundamental problems in graph theory, related to map coloring, scheduling, and register allocation.

Why is finding the chromatic number hard?

Determining the chromatic number is NP-hard in general. Even deciding whether a graph can be 3-colored is NP-complete. Practical approaches use greedy heuristics, backtracking, or SAT solvers. Only special graph classes (bipartite, planar, perfect) have efficient exact algorithms.

What is the four color theorem?

The four color theorem states that every planar graph (a graph that can be drawn on a plane without edge crossings) can be properly colored with at most four colors. Proved in 1976 by Appel and Haken with computer assistance, it was the first major theorem proved by computer.

What are practical applications of graph coloring?

Graph coloring is used in scheduling (exams, tasks), frequency assignment in wireless networks, register allocation in compilers, and Sudoku solving. Any problem where resources must be assigned to conflicting entities maps to graph coloring.

Sources

Embed

<iframe src="https://homo-deus.com/lab/graph-theory/graph-coloring/embed" width="100%" height="400" frameborder="0"></iframe>
View source on GitHub