Graph Coloring asks you to paint every vertex of a graph so that no edge connects two vertices of the same color, using as few colors as possible. Finding the true minimum is NP-hard, but a simple greedy pass gives a fast, reasonable coloring — and it is a clean interview exercise in processing things in order and picking the smallest available option.
Problem. Given an undirected graph and an ordering of its vertices, assign each vertex a color (an integer 0, 1, 2, ...) so that no two adjacent vertices share a color. Use few colors. Return the color of every vertex.
Example: vertices A, B, C, D, E with edges A-B, A-C, B-D, B-E, C-D, D-E, processed in order
A, B, C, D, E. A valid greedy result is A=0, B=1, C=1, D=0, E=2 — three colors.
The slow way first
You could try every possible assignment of colors to vertices and keep the one that uses the fewest while staying valid. With k colors and n vertices that is k^n combinations — exponential, and hopeless for anything but tiny graphs. The exact chromatic number really is NP-hard, so brute force is the honest baseline, not a practical method.
The question to ask: can I make one decision per vertex and never look back? For a quick, good-enough answer, yes — and that is exactly what greedy does.
The idea: smallest unused color
Walk the vertices in the given order. For the current vertex v, look at the vertices it connects to that are already colored, and collect the set of colors they use. Then pick the smallest non-negative integer that is not in that set and give it to v. Move on. Because we only ever avoid colors that conflict, the result is always valid.
The greedy choice is local: it never reconsiders an earlier vertex. That makes it fast, but the color count depends on the ordering — a different order can use fewer (or more) colors.
Walk through it
Step through the animation. The current vertex glows, its already-colored neighbors are marked, and we read off the smallest color they leave free. A has no colored neighbors, so it takes 0. B touches A=0, so it takes 1. C touches A=0, so it takes 1. D touches B=1 and C=1, so 0 is free. E touches B=1 and D=0, so the smallest free color is 2. The running colors used counter ends at three.
Pseudocode
color = empty map
for v in order:
used = empty set
for nb in neighbors(v):
if nb already has a color:
add color[nb] to used
c = 0
while c is in used:
c = c + 1
color[v] = c # smallest color no neighbor uses
return colorThe Python solution
def greedy_coloring(graph, order):
color = {}
for v in order:
used = set()
for nb in graph[v]:
if nb in color:
used.add(color[nb])
c = 0
while c in used:
c += 1
color[v] = c
return colorgraphis an adjacency map (vertex → list of neighbors);orderis the sequence we process vertices in.- For each
vwe buildused, the set of colors taken by its already-colored neighbors (if nb in color). - The
while c in usedloop walks0, 1, 2, ...and stops at the first color not inused— the smallest available. - We assign
color[v] = cand never revisit it. One decision per vertex.
Complexity
| Case | Time | Notes |
|---|---|---|
| Exact minimum (brute force) | exponential (moderate) | NP-hard, try all assignments |
| Greedy (this solution) | O(V + E) (moderate) | each edge inspected a constant number of times |
O(V) (moderate)Greedy touches each vertex once and each edge a constant number of times, so it runs in O(V + E). It does not guarantee the fewest possible colors, but it never uses more than maxDegree + 1.
When this pattern shows up
Greedy coloring is the model answer for register allocation, scheduling without conflicts (exam timetables, meeting rooms by color), and map/region coloring. The reusable move: process items in some order and pick the smallest valid option for each, tracking what neighbors already took.
Greedy is order-dependent — it can use more colors than necessary on a bad ordering. If an interviewer wants fewer colors, mention ordering heuristics like processing highest-degree vertices first (the Welsh-Powell idea).
Practice
When we reach vertex D (neighbors B=1 and C=1), what color does greedy assign, and why?
1. How does greedy pick the color for the current vertex?
2. Why does the greedy result depend on the vertex ordering?
3. What is the time complexity of the greedy coloring pass?
4. What is the most colors greedy can use on a graph?