M-Coloring Problem
Can the graph's vertices be colored with at most m colors so no edge joins same-colored vertices?
Open on GeeksforGeeks ↗Intuition
Color vertices one at a time; a color is legal if no already-colored neighbour has it. If a vertex has no legal color, backtrack — the earlier assignment forced a dead end. First full assignment = yes.
Approach
Assign vertex by vertex
At vertex v, try colors 1..m; safe checks colored neighbours only — later vertices don't constrain yet.
Backtrack on dead ends
If no color fits, unwind to the previous vertex and try its next color. The search tree covers all assignments but prunes hard.
Stop at the first success
The question is decision, not enumeration — return True the moment vertex n is passed.
Solution & live demo
Edge cases
Greedy always succeeds; the search finds it immediately without backtracking.
Every ordering dead-ends; the search exhausts and returns False.