State the graph coloring problem and write the algorithm. Apply backtracking technique to solve the 3-colouring problem for the following graph.

image

State the graph coloring problem and write the algorithm. Apply backtracking technique to solve the 3-colouring problem for the following graph.

image

Verified Sources
Sep 12, 2026

The Graph Coloring Problem asks: given a graph G=(V,E)G=(V,E) and an integer kk, determine whether there exists a Proper k-Coloring such that for every edge (u,v)E(u,v)\in E, the assigned colors satisfy c(u)c(v)c(u)\neq c(v). The corresponding Decision Problem—“does GG admit a kk-coloring?”—is known to be computationally hard for general k3k\ge 3 (NP-complete).

A Backtracking Algorithm for kk-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 c:V{1,,k}c:V'\to\{1,\dots,k\} 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 kk-coloring, and (3) apply it to the given graph to solve the 3-Coloring problem.

Graph coloring (backtracking intuition)

Formal statement

Let G=(V,E)G=(V,E) be a simple undirected graph.

  • A Coloring is a mapping c:V{1,2,,k}c:V\to\{1,2,\dots,k\}.
  • A Proper Coloring requires:
(u,v)E,c(u)c(v).\forall (u,v)\in E,\quad c(u)\ne c(v).

For fixed kk, the k-Coloring problem asks:

  • Decision version: does there exist cc satisfying the constraint?

Backtracking algorithm for k-Coloring

  1. 1
    Step 1

    Use an adjacency list or adjacency matrix for G=(V,E)G=(V,E). Also pick a fixed vertex order v1,v2,,vnv_1,v_2,\dots,v_n.

  2. 2
    Step 2

    Create c[v]c[v] for all vertices, initially set to UNCOLORED.

  3. 3
    Step 3

    To assign color xx to a vertex vv, check every neighbor uu of vv. If uu is already colored and c[u]=xc[u]=x, the assignment violates the edge constraint—reject it.

  4. 4
    Step 4

    Let the recursion state be an index ii. If i=ni=n, all vertices are colored consistently, so return success. Otherwise, set v=viv=v_i and try each color x{1,,k}x\in\{1,\dots,k\} that passes the safety test.

  5. 5
    Step 5

    If all kk colors fail for the current vertex, reset c[v]c[v] 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 knk^n assignments, with pruning whenever conflicts arise; thus the worst-case complexity is exponential in nn.

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 1

Pick a color; check neighbors already colored (none yet)."

Assign color to second vertex

Level 2

Try colors; reject those conflicting with the first vertex."

Keep recursing and pruning

Levels 3..n

Each time: try colors consistent with all colored neighbors."

Undo and try alternatives

Backtrack

If no color works for a vertex, return to the previous vertex."

Finish

Solution / Failure

Success 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., E={(1,2),(1,3),...}E=\{(1,2),(1,3),...\}) 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:

  1. Choose a vertex order v1,,vnv_1,\dots,v_n.
  2. Build a recursion tree where each node corresponds to a partial coloring.
  3. Show each attempted assignment and each pruning event (when c[u]=xc[u]=x for a neighbor uu).
  4. Conclude with either:
    • a valid 3-coloring assignment c(v){1,2,3}c(v)\in\{1,2,3\} satisfying c(u)c(v)c(u)\neq c(v) 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

Question 1 of 3
Q1Single choice

In the k-coloring backtracking algorithm, what does Safe(v, x) check?