Graph Coloring
Graph coloring gives every vertex a color so that no edge joins two vertices of the same color. Any valid coloring is easy to find greedily; finding the fewest colors, the chromatic number, is NP-hard.
What Graph Coloring Means
Graph coloring (vertex coloring) assigns a color to each vertex so that the two ends of every edge get different colors. The smallest number of colors that works is the chromatic number, written χ(G). The colors are just labels; only whether two are equal matters.
Colors stand for things that must not clash. In exam timetabling, vertices are exams, edges join exams with a shared student, and colors are time slots. Compilers color an interference graph to assign variables to registers; radio networks color transmitters to assign frequencies; a Sudoku is a 9-coloring of a fixed graph with some colors given.
- Edge ends must differ
- Chromatic number: the fewest colors that work
Greedy Coloring
- Fix an order of the vertices
- For each vertex, collect the colors of its already colored neighbours
- Give it the smallest color not in that set
This greedy graph coloring algorithm always produces a proper coloring and never needs more than Δ + 1 colors, where Δ is the largest degree: a vertex has at most Δ neighbours, so at least one of Δ + 1 colors is free. With adjacency lists it runs in O(V + E).
On the square A-B-C-D with diagonal A-C, order A, B, C, D gives A 0, B 1, C 2, D 1: three colors. The result depends heavily on the order. On the path P-Q-R-S, visiting P, S, Q, R uses 3 colors where 2 are enough. On some bipartite graphs (crown graphs) a bad order makes greedy use n/2 colors where 2 suffice.
- Always proper, at most Δ + 1 colors
- Not always the fewest colors
Better Orders: Welsh-Powell and DSATUR
The Welsh-Powell algorithm (1967) sorts vertices by degree, largest first, and then colors greedily. High-degree vertices are the hardest to fit, so placing them early usually saves colors. It also gives the bound χ ≤ max over i of min(dᵢ + 1, i), with degrees d₁ ≥ d₂ ≥ ….
The DSATUR algorithm (Brélaz, 1979) chooses the order as it goes: the next vertex is the uncolored one whose neighbours already use the most different colors (its saturation), with ties broken by degree. It always colors the most constrained vertex next, and it is exact on bipartite graphs, cycles and wheels.
Neither is guaranteed optimal on every graph. They are heuristics that give good upper bounds quickly.
| Method | Order | Time | Fewest colors? |
|---|---|---|---|
| Greedy | Fixed, given | O(V + E) | No; at most Δ + 1 |
| Welsh-Powell | Degree, largest first | O(V log V + E) | No, usually better |
| DSATUR | Highest saturation next | O(V²), or O((V + E) log V) with a heap | No, exact on bipartite graphs |
| Backtracking | Search all choices | Exponential | Yes |
- Welsh-Powell: big degrees first
- DSATUR: most constrained vertex next
Terms, operations, and practical uses
Coloring terms
- Proper coloringEvery edge joins two different colors. Checked edge by edge in O(E).
- Chromatic numberχ(G), the fewest colors in any proper coloring of G.
- Color classAll vertices with one color. Each class is an independent set: no edges inside it.
Algorithms
- Greedy coloringVisit vertices in a fixed order and give each the smallest color its colored neighbours lack. At most Δ + 1 colors.
- DSATURColor next the vertex whose neighbours show the most distinct colors; ties by degree.
- k-coloringDecide whether k colors suffice, by backtracking. NP-complete for k ≥ 3.
Bounds and cases
- Clique lower boundq mutually adjacent vertices need q colors, so χ ≥ the largest clique.
- Brooks' theoremχ ≤ Δ for every connected graph except complete graphs and odd cycles.
- Self-loopAn edge from a vertex to itself. No proper coloring exists.
Code in Python, C++ and Java
names = "ABCD"
g = [[1, 2, 3], [0, 2], [0, 1, 3], [0, 2]]
color = [-1] * 4
for u in range(4):
used = {color[v] for v in g[u] if color[v] >= 0}
c = 0
while c in used:
c += 1
color[u] = c
for u in range(4):
print(names[u], "->", color[u])
print("Colors used:", max(color) + 1)#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
string names = "ABCD";
vector<vector<int>> g = {{1, 2, 3}, {0, 2}, {0, 1, 3}, {0, 2}};
vector<int> color(4, -1);
for (int u = 0; u < 4; u++) {
set<int> used;
for (int v : g[u]) if (color[v] >= 0) used.insert(color[v]);
int c = 0;
while (used.count(c)) c++;
color[u] = c;
}
for (int u = 0; u < 4; u++) cout << names[u] << " -> " << color[u] << '\n';
cout << "Colors used: " << *max_element(color.begin(), color.end()) + 1 << '\n';
}import java.util.*;
class Main {
public static void main(String[] args) {
String names = "ABCD";
int[][] g = {{1, 2, 3}, {0, 2}, {0, 1, 3}, {0, 2}};
int[] color = {-1, -1, -1, -1};
for (int u = 0; u < 4; u++) {
Set<Integer> used = new HashSet<>();
for (int v : g[u]) if (color[v] >= 0) used.add(color[v]);
int c = 0;
while (used.contains(c)) c++;
color[u] = c;
}
for (int u = 0; u < 4; u++) System.out.println(names.charAt(u) + " -> " + color[u]);
System.out.println("Colors used: " + (Arrays.stream(color).max().getAsInt() + 1));
}
}Step through it
Running on edges A-B, B-C, C-D, D-A, A-C; order A, B, C, D
The Chromatic Number and Its Bounds
A lower bound comes from any clique: q vertices that are all adjacent need q colors. In the square-with-diagonal graph, the triangle A-B-C needs 3, and greedy used 3, so χ = 3. When a lower bound meets an upper bound from some valid coloring, the chromatic number is proved.
Some values are known exactly. χ ≤ 2 exactly when the graph has no odd cycle (it is bipartite); BFS checks this in O(V + E), see bipartite graph checking and the is graph bipartite problem. Odd cycles need 3. Brooks' theorem: χ ≤ Δ unless the graph is a complete graph or an odd cycle. Every planar graph needs at most 4 colors (the four color theorem, Appel and Haken, 1976).
- Clique size ≤ χ ≤ any valid coloring
- Bipartite ⇔ no odd cycle ⇔ χ ≤ 2
Exact k-Coloring by Backtracking
To decide whether k colors are enough, color vertices one at a time, try each color not used by a colored neighbour, and undo the choice when a later vertex has no color left. This is the m coloring problem, a standard use of backtracking. The worst case is O(kⱽ).
Pruning helps a lot in practice: pick the most constrained vertex next (as DSATUR does), fix the first vertex's color to avoid trying renamed copies of the same coloring, and stop as soon as any vertex has no free color. Still, deciding k-colorability is NP-complete for every k ≥ 3, so no known method is fast on all graphs; see P, NP and NP-complete.
- Try, recurse, undo on a dead end
- NP-complete for k ≥ 3
Edge Cases
A self-loop joins a vertex to itself, so no proper coloring exists; check for it first. Parallel edges add nothing. Disconnected parts can reuse the same colors, so χ of the whole graph is the largest χ of its parts, not the sum. A graph with vertices but no edges needs 1 color.
Test with complete graphs (χ = n), even and odd cycles (2 and 3), a path visited ends-first, isolated vertices and a self-loop. Report what was computed: a greedy count is an upper bound, not the chromatic number.
- Self-loop: impossible
- Components share colors