M-Coloring Problem
M-Coloring Problem is a GFG problem (Medium). You get an undirected graph with v vertices, numbered from 0, as a list of edges, and a number m.
- Return
trueif every vertex can get one ofmcolors so that no edge joins two vertices of the same color, andfalseotherwise.
The graph has at most 10 vertices. That small size is what makes trying colors one vertex at a time fast enough, even though the worst case is exponential.
- 1 <= v <= 10
- 1 <= E = edges.size() <= v * (v - 1) / 2
- 0 <= edges[i][j] <= v - 1
- 1 <= m <= v
Intuition
No formula answers this (it is NP-complete once m is 3 or more), so the solution is a careful search. Color the vertices in order 0, 1, 2, …, try colors 1 to m at each one, and back up the moment a vertex has no legal color.
- Check only colored neighbors: a neighbor with no color yet cannot clash; it is checked on its own turn.
- Back up on a dead end: if nothing fits a vertex, an earlier choice caused it, so return to the previous vertex and give it its next color.
- Stop at the first success: the answer is yes or no, so one complete coloring ends the search.
"Assign each item one of a few labels so no two related items share one" is constraint backtracking: place, check, recurse, undo. N-Queens and Sudoku have the same shape with a different check.
Approach
Before reading on: when you give a vertex a color, which other vertices does that choice restrict, and what has to be undone if the choice leads nowhere? Aim for a search that drops a branch as soon as it clashes.
Build neighbor lists
Turn edges into adj, adding every edge in both directions because the graph is undirected. Checking a color then means scanning one vertex's neighbors instead of all v vertices.
Try each color at the current vertex
For node, go through colors 1 to m. A color is allowed when no neighbor already has it. Uncolored neighbors hold 0, which never matches a real color.
Paint, go deeper, erase on failure
Set color[node] = c and call place(node + 1). If it returns true, pass true straight up. If not, set color[node] back to 0 and try the next color.
Decide at the two ends
Reaching node == v means every vertex has a legal color, so return true. Running out of colors at a vertex returns false to the caller, which then tries its own next color.
Code in Python, C++ and Java
Common pitfalls
Adding each edge in one direction only
for a, b in edges:
adj[a].append(b)for a, b in edges:
adj[a].append(b)
adj[b].append(a)Vertex 1 then never sees its neighbor 0. On the triangle [[0, 1], [1, 2], [0, 2]] with m = 2 every vertex gets color 1 and the code returns true; the answer is false.
Returning true without checking the rest
color[node] = c place(node + 1) return True
color[node] = c
if place(node + 1):
return True
color[node] = 0Success needs every later vertex colored too. On the same triangle with m = 2, vertex 2 finds no color, but the true from vertex 0 is already on its way up.
Edge cases
Both ends of that edge would need color 1, so the search fails at the second one and returns false.
A vertex always has a color its neighbors are not using, so the search never backs up and returns true on the first pass.
Complexity
M-Coloring Problem FAQ
How is the m coloring problem related to the chromatic number?
This problem fixes m and asks yes or no. The chromatic number is the smallest m for which the answer is yes, so you can find it by running this check for m = 1, 2, 3, … and stopping at the first true.
Is m = 2 a special case?
Yes. Two colors work exactly when the graph is bipartite, and a BFS that gives each neighbor the opposite color checks that in O(v + E) with no backing up.
Does the order of the vertices matter?
Not for the answer, only for speed. Coloring the vertices with the most neighbors first tends to reach dead ends sooner, which cuts more branches.