Interactive Graph Theory Learning
Interactive Graph Theory Learning
Guest User
Using app without sign in
Graph coloring and chromatic number solver
Colors vertices so no adjacent vertices share same color
Select an algorithm and generate steps to begin visualization
Graph coloring assigns colors to vertices so that no two adjacent vertices share a color, using as few colors as possible. The minimum number needed is the chromatic number, and computing it is NP-hard for general graphs.
The greedy algorithm orders the vertices and gives each one the smallest color not used by its already-colored neighbors, guaranteeing at most one more color than the maximum degree. Orderings such as Welsh-Powell (by decreasing degree) or DSatur (by saturation, the number of distinct neighbor colors) often use far fewer colors in practice. Exact coloring uses backtracking with pruning, feasible only for small graphs.
Coloring schedules exams so no student has two at once, allocates CPU registers in compilers, assigns radio frequencies without interference, and colors maps so neighboring regions differ. The four color theorem for planar graphs is one of the most celebrated results in mathematics. Bipartite checking is exactly 2-colorability.
Greedy colouring is three lines and always produces a valid colouring. What it does not produce is necessarily a minimal one, and the vertex order decides how close it gets.
GreedyColoring(graph, order):
color = {}
for each vertex v in order:
used = { color[n] : n in neighbors(v), n colored }
c = smallest positive integer not in used
color[v] = c
return color
// Welsh-Powell: order by descending degree
// DSatur: repeatedly pick the uncolored vertex with the
// most distinctly-colored neighbors (saturation),
// breaking ties by degreeGreedy never uses more than max degree plus one colours, because when you reach a vertex it has at most that many neighbours and so at most that many forbidden colours. That is a genuine guarantee, but it can be far from the true chromatic number. DSatur is the practical improvement: choosing the most constrained vertex next is exactly the heuristic that avoids painting yourself into a corner.
Colour a five-cycle greedily in alphabetical order, then check the result against the true chromatic number.
Example graph: Undirected cycle A-B, B-C, C-D, D-E and E-A.
The colouring is A 1, B 2, C 1, D 2, E 3, using three colours, and brute force confirms the chromatic number of a five-cycle really is 3. Greedy happened to be optimal here. The reason three are needed at all is that the cycle has odd length: colours must alternate around a cycle, and an odd cycle brings you back to the start needing a colour different from the one already there. Every even cycle needs only 2.
Time: O(V + E) greedy, NP-hard exactly · Space: O(V)
Greedy colouring examines each vertex once and inspects each edge twice, once from each endpoint, so it is O(V + E) with O(V) space for the colour array. That cost buys a valid colouring using at most max degree plus one colours, never a guaranteed minimum. Computing the actual chromatic number is NP-hard, and even approximating it within a factor of V to the power 1 minus epsilon is NP-hard, which is unusually strong: for most problems some decent approximation exists, and for colouring essentially none does. Deciding 2-colourability is the exception and is easy, since it is exactly the bipartiteness test at O(V + E). Deciding 3-colourability is already NP-complete.
Pick by how many colours you expect to need and whether you require the true minimum.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Bipartite check | You only need to know whether 2 colours suffice. A different and far easier problem. | O(V + E) |
| DSatur | The practical default. Picks the most saturated vertex next and is often optimal or near optimal on real graphs. | O(V^2) |
| Welsh-Powell | You want something better than arbitrary order with almost no extra code. Sorts by descending degree. | O(V^2) |
| Exact branch and bound | You genuinely need the chromatic number and the graph is small. | exponential |
| Maximal clique | You want a lower bound. A clique of size k forces at least k colours. | O(3^(V/3)) |
Related algorithms: Bipartite Check, Maximal Clique, Chordality Check