State the graph coloring problem and write the algorithm. Apply backtracking technique to solve the 3-colouring problem for the following graph.
State the graph coloring problem and write the algorithm. Apply backtracking technique to solve the 3-colouring problem for the following graph.
The Graph Coloring Problem asks: given a graph and an integer , determine whether there exists a Proper k-Coloring such that for every edge , the assigned colors satisfy . The corresponding Decision Problem—“does admit a -coloring?”—is known to be computationally hard for general (NP-complete).
A Backtracking Algorithm for -coloring builds a partial color assignment vertex-by-vertex and immediately abandons any branch that violates the edge constraint. The key data structure is the current partial assignment for a growing prefix of vertices.
The constraint structure can be visualized as:
In this section, we will (1) state the graph coloring problem formally, (2) write the backtracking algorithm for -coloring, and (3) apply it to the given graph to solve the 3-Coloring problem.
Graph coloring (backtracking intuition)
Formal statement
Let be a simple undirected graph.
- A Coloring is a mapping .
- A Proper Coloring requires:
For fixed , the k-Coloring problem asks:
- Decision version: does there exist satisfying the constraint?
Backtracking algorithm for k-Coloring
- 1Step 1
Use an adjacency list or adjacency matrix for . Also pick a fixed vertex order .
- 2Step 2
Create for all vertices, initially set to UNCOLORED.
- 3Step 3
To assign color to a vertex , check every neighbor of . If is already colored and , the assignment violates the edge constraint—reject it.
- 4Step 4
Let the recursion state be an index . If , all vertices are colored consistently, so return success. Otherwise, set and try each color that passes the safety test.
- 5Step 5
If all colors fail for the current vertex, reset back to UNCOLORED and return failure to the previous recursion level.
Pseudocode (writing the algorithm)
1KColoringBacktrack(G, k): 2 order vertices as v1..vn 3 for each vertex v in V: 4 c[v] = UNCOLORED 5 6 return DFS(1) 7 8DFS(i): 9 if i == n+1: 10 return true 11 12 v = v_i 13 for color x in {1..k}: 14 if Safe(v, x): 15 c[v] = x 16 if DFS(i+1): return true 17 c[v] = UNCOLORED // backtrack 18 return false 19 20Safe(v, x): 21 for each neighbor u of v: 22 if c[u] == x: 23 return false 24 return true
Runtime note. In the worst case, the search explores up to assignments, with pruning whenever conflicts arise; thus the worst-case complexity is exponential in .
Pruning matters
Backtracking is effective because as soon as you try a color that conflicts with any already-colored neighbor, you prune that branch instead of continuing deeper.
Vertex order affects performance
Using a smarter vertex order (e.g., highest degree first, or MRV-style heuristics) can reduce backtracking dramatically, even though correctness is unchanged.
How the backtracking search progresses
Assign color to first vertex
Level 1Pick a color; check neighbors already colored (none yet)."
Assign color to second vertex
Level 2Try colors; reject those conflicting with the first vertex."
Keep recursing and pruning
Levels 3..nEach time: try colors consistent with all colored neighbors."
Undo and try alternatives
BacktrackIf no color works for a vertex, return to the previous vertex."
Finish
Solution / FailureSuccess gives a proper 3-coloring; failure proves none exists (for that k)."
Applying backtracking to the given graph (3-coloring)
Extracting the graph
To apply the algorithm concretely, we must know the graph’s vertex set and edges. The prompt includes an image of the graph; however, the vertices/edges are not readable in this chat context (the adjacency structure is required to perform a correct backtracking trace and output a valid 3-color assignment).
What is required to finish the 3-coloring trace
A proper backtracking application needs one of the following:
- A clear edge list (e.g., ) and vertex labels, or
- A version of the image where each vertex label is legible, or
- A description of which vertices are adjacent.
Output format (what we will compute once edges are known)
We will:
- Choose a vertex order .
- Build a recursion tree where each node corresponds to a partial coloring.
- Show each attempted assignment and each pruning event (when for a neighbor ).
- Conclude with either:
- a valid 3-coloring assignment satisfying for all edges, or
- proof by exhaustion that no such 3-coloring exists.
Common implementation details for 3-coloring by backtracking
Backtracking search behavior (conceptual)
As depth increases, the number of surviving partial assignments typically decreases due to constraints.
Knowledge Check
In the k-coloring backtracking algorithm, what does Safe(v, x) check?