Exp9 16010120126 B3 Graph Colouring
Exp9 16010120126 B3 Graph Colouring
Experiment No. 09
Grade: AA / AB / BB / BC / CC / CD /DD
---------------------------------------------------------------------------------------------------------------------
Objective: To learn the Backtracking strategy of problem solving for Graph Colouring problem
CO to be achieved:
CO 2 Analyze and solve problems for divide and conquer strategy, greedy method, dynamic
programming approach and backtracking and branch & bound policies.
Historical Profile:
Page 1 of 5 AOA Sem IV Jan-May 2022
K. J. Somaiya College of Engineering
(A Constituent College of Somaiya Vidyavihar University)
Given an undirected graph and a number m, determine if the graph can be colored with at most
m colors such that no two adjacent vertices of the graph are colored with same color. Here
coloring of a graph means assignment of colors to all vertices.
nput:
1) A 2D array graph [V][V] where V is the number of vertices in graph and graph[V][V] is
adjacency matrix representation of the graph.
Output:
An array color [V] that should have numbers from 1 to m. color[i] should represent the color
assigned to the ith vertex. The code should also return false if the graph cannot be colored with
m colors.
Following is an example graph can be colored with 3 colors.
static int V = 4;
if (isSafe(graph, color))
{
printSolution(color);
return true;
}
return false;
}
boolean[][] graph = {
{ false, true, true, true },
{ true, false, true, false },
{ true, true, false, true },
{ true, false, true, false },
};
int m = 3;
Conclusion:
In this experiment, we have learnt Implementation of Graph Colouring using backtracking
approach. We have also understood the algorithm and implemented the same on java.
Additionally, we have compared the time and space complexity of the program.