GRAPH COLORING USING
BACKTRACKING
BY:
P. Shashidhar
18E11A05B7
What is graph coloring?
• The objective is to minimize the number of colors while coloring a
graph.
• The smallest number of colors required to color a graph G is called its
chromatic number of that graph.
• Graph coloring problem is a NP Complete problem.
• No efficient algorithm is available to implement graph coloring mainly
we use Greedy and Backtracking algorithm.
Method to Color a Graph
1. Arrange the vertices of graph in the same order.
2. Choose the first vertex and color it with the first color.
3. Then choose next vertex and color it with the lowest numbered color
that has not been colored on any vertices which is adjacent to it.
4. If all the adjacent vertices are colored with the color, assign the new
color to it.
5. Repeat the same process until all the vertices are colored.
6. If any complication occurs backtrack before step and repeat the
process.
Algorithm:
1.Create a recursive function that takes current index, number of
vertices and output color array.
2.If the current index is equal to number of vertices. Check if the output
color configuration is safe, i.e check if the adjacent vertices do not
have same color. If the conditions are met, print the configuration and
break.
3.Assign a color to a vertex (1 to m).
4.For every assigned color recursively call the function with next index
and number of vertices
5.If any recursive function returns true break the loop and returns true.
GraphColor(int k){
for(int c=1;c<=m;c++);
if(isSafe(k,c)){
X[k]=c;
if((k+1)<n)
GraphColor(k+1);
else
print x[];return;
}
}
isSafe(int k,int c){
for(int i=0;i<n;i++){
if(G[k][i]=2 && c==x[i]){
return false;
}
}
return true;
}
Applications:
• Making schedule or Time table.
• Sudoku.
• Register allocation.
• Map coloring.
• Job allocation in CPU.
THANK YOU

Graph coloring using backtracking

  • 1.
  • 2.
    What is graphcoloring?
  • 3.
    • The objectiveis to minimize the number of colors while coloring a graph. • The smallest number of colors required to color a graph G is called its chromatic number of that graph. • Graph coloring problem is a NP Complete problem. • No efficient algorithm is available to implement graph coloring mainly we use Greedy and Backtracking algorithm.
  • 4.
    Method to Colora Graph 1. Arrange the vertices of graph in the same order. 2. Choose the first vertex and color it with the first color. 3. Then choose next vertex and color it with the lowest numbered color that has not been colored on any vertices which is adjacent to it. 4. If all the adjacent vertices are colored with the color, assign the new color to it. 5. Repeat the same process until all the vertices are colored. 6. If any complication occurs backtrack before step and repeat the process.
  • 8.
    Algorithm: 1.Create a recursivefunction that takes current index, number of vertices and output color array. 2.If the current index is equal to number of vertices. Check if the output color configuration is safe, i.e check if the adjacent vertices do not have same color. If the conditions are met, print the configuration and break. 3.Assign a color to a vertex (1 to m). 4.For every assigned color recursively call the function with next index and number of vertices 5.If any recursive function returns true break the loop and returns true.
  • 9.
    GraphColor(int k){ for(int c=1;c<=m;c++); if(isSafe(k,c)){ X[k]=c; if((k+1)<n) GraphColor(k+1); else printx[];return; } } isSafe(int k,int c){ for(int i=0;i<n;i++){ if(G[k][i]=2 && c==x[i]){ return false; } } return true; }
  • 10.
    Applications: • Making scheduleor Time table. • Sudoku. • Register allocation. • Map coloring. • Job allocation in CPU.
  • 11.