You are on page 1of 8

BACKTRACKING

CSE 2201
BACKTRACKING
 Solution: Some sequence of choices.

We assume that a solution to the problem can be constructed from component


solutions, xi. So the solution can be expressed as (x1, x2,...,xn) and a partial
solution is (x1, x2,...,xi) where i < n.

Problem Nature:
 To make a series of decisions, among various choices.
 Lack of enough information to know what to choose.
 Each decision leads to a new set of choices.
GRAPH COLORING
 M-coloring problem: Color undirected graph with ≤m colors (chromatic
number)
 2 adjacent vertices : Different colors
 The Four Color Theorem : Any map on a plane (planer graph) can be colored
with no more than four colors.
GRAPH COLORING
GRAPH COLORING
 isSafe(n, c){
 for(j=0; j<n; j++)
 if((ADJ[n][j]==1) && (c==cs[j]) && (cs[j]!=0))
 return 0;
 return 1;
}
GRAPH COLORING
 GC(node){ //Return 1 on successful assignment
 for(color=1; color<=M; color++){
 if( isSafe(node, color) ){
 cs[node] = color //Assigned color to current node
 if(node == N) //Successfully assigned to all nodes
return 1
 else
 GC(++node); // Search for solution of the next node
 }
 }
 }
COMPLEXITY
 Generate all possible configurations of colors. Since each node can be
coloured using any of the m available colours, the total number of colour
configurations possible are m^V. 
 Check safety and assign O(m^V). 
There are total O(m^V) combination of colors. So time complexity is O(m^V).
The upperbound time complexity remains the same but the average time
taken will be less.
THANK YOU

You might also like