1. Introduction
The M-Coloring Problem is a classic graph problem that can be solved using Backtracking.
In this problem, we are given:
- An undirected graph containing
Vvertices. - An adjacency list
adj[][]representing the graph. - An integer
m, representing the maximum number of colors available.
Our task is to determine whether we can color all vertices using at most m different colors.
Main condition: No two adjacent vertices should have the same color.
If we can color every vertex without violating this condition, return true. Otherwise, return false.
2. What is Graph Coloring?
Graph coloring means assigning colors to the vertices of a graph while following certain rules.
For example, consider a graph with four vertices:
- Vertex 0 is connected to vertices 1, 2, and 3.
- Vertex 1 is connected to vertices 0 and 3.
- Vertex 2 is connected to vertices 0 and 3.
- Vertex 3 is connected to vertices 0, 1, and 2.
One possible coloring is:
| Vertex | Color |
|---|---|
| 0 | Red (1) |
| 1 | Blue (2) |
| 2 | Blue (2) |
| 3 | Green (3) |
Notice that vertices 1 and 2 can have the same color because they are not connected to each other.
However, vertices 0 and 1 cannot have the same color because there is an edge between them.
3. What is Backtracking?
Backtracking is an algorithmic technique that solves problems by trying different possible choices.
The process works like this:
- Select an item.
- Try one possible choice.
- Check whether the choice is valid.
- If valid, move to the next item.
- If the choice leads to a problem, undo it.
- Try another possible choice.
In the M-Coloring Problem, we try assigning different colors to each vertex.
If a color is already used by one of its adjacent vertices, we cannot assign that color.
If no color works, we go back to the previous vertex and try another color.
This process continues until:
- All vertices are successfully colored, or
- All possible combinations have been tried.
4. C# Solution
class Solution
{
public bool graphColoring(List<List<int>> adj, int m)
{
int V = adj.Count;
int[] color = new int[V];
return Solve(0, adj, color, m, V);
}
private bool Solve(int node, List<List<int>> adj,
int[] color, int m, int V)
{
// Base condition
if (node == V)
return true;
// Try all available colors
for (int c = 1; c <= m; c++)
{
bool isSafe = true;
// Check adjacent vertices
foreach (int neighbour in adj[node])
{
if (color[neighbour] == c)
{
isSafe = false;
break;
}
}
// Assign color if valid
if (isSafe)
{
color[node] = c;
// Move to the next vertex
if (Solve(node + 1, adj, color, m, V))
return true;
// Backtracking
color[node] = 0;
}
}
return false;
}
}5. Code Explanation Step by Step
Step 1: Find the number of vertices
int V = adj.Count;adj.Count gives the total number of vertices in the graph.
For example, if the adjacency list contains four vertices, then:
V = 4;Step 2: Create a color array
int[] color = new int[V];This array stores the color assigned to each vertex.
Initially, all values are 0.
Here:
0means no color assigned.1means Color 1.2means Color 2.3means Color 3.
For four vertices:
color[] = [0, 0, 0, 0]Step 3: Start the recursive function
return Solve(0, adj, color, m, V);We start coloring from vertex 0.
The Solve() function handles assigning colors to vertices recursively.
Parameters:
node: Current vertex.adj: Graph adjacency list.color: Stores assigned colors.m: Maximum available colors.V: Total number of vertices.
Step 4: Base condition
if (node == V)
return true;This is the stopping condition.
If node == V, it means we have successfully assigned colors to all vertices.
For example, if V = 4, the vertices are:
0, 1, 2, 3After successfully coloring vertex 3, the recursive function reaches node = 4.
Therefore, it returns true.
Step 5: Try all available colors
for (int c = 1; c <= m; c++)This loop tries every available color.
For example, if:

Join the conversation! Your thoughts help the community grow.