Unit4 Backtracking 1
Unit4 Backtracking 1
IT Department, RCOEM 1
Concept
• Paradigm works in incremental way to attack the
problems
• Starting with empty solution vector we go on adding
the items one by one. (Item depends on the
application)
• When we add an item we check if this addition violates
the problem constraint. If it does, then we remove it
(backtrack) and try other alternatives.
• If none of the alternatives from the current moves
work out then we go to previous stage and remove the
item added in previous stage.
• If we reach back to initial stage in the process then we
say that no solution exists
IT Department, RCOEM 2
• If adding an item does not violates the
problem constraint the we recursively add
items one by one
• If the solution vector becomes complete, we
print the solution. Solution vector is usually of
the form x1,x2,x3,…,xn where xi is chosen
from the finite set Mi (problem dependent)
• The working of paradigm corresponds to
modified form of depth first search
IT Department, RCOEM 3
Skeleton of Backtracking
recur_bt( int k)
{
/* first k-1 values, x[1], x[2], x[3],…,x[k-1], have been assigned*/
for ( each x[k] in the list L of the possible values for x[k])
{
if (feasible(x[1], x[2], x[3],…,x[k]))
{
if (x[1], x[2], x[3],…,x[k] gives the solution)
print(x[1], x[2], x[3],…,x[k]);
else
recur_bt( k+1);
}
}
}
IT Department, RCOEM 4
• To illustrate the strategy, consider the 4-Queen’s
problem:
– A chess board of size 4X4 is given and it is required to
place 4 Queens on the chess board such that no two
queens are attacking each other
• Two queens attack each other if they are in same
row, column or diagonal
• Let the chess board be as follows:
1 2 3 4
4
3
2
1
IT Department, RCOEM 5
Search Tree for solution to 4-Queens problem
X1=1 X1=2
X X2=4 X
X2=3 X2=4
X3=1
X
X X
X
X X3=2 X
X4=3 X
X X
X X X
X X
X IT Department, RCOEM X 6
• Note: The complete tree need not be searched. If
the value cannot lead to the solution, we
abandon searching for the solution in that part of
the subtree
• Solution to 4-Queen problem:
x1=2,x2=4,x3=1,x4=3
• The function feasible in the skeleton of the
paradigm implements the constraints of the
problem
• Thus we call a depth-first search with tree
pruning as backtracking
IT Department, RCOEM 7
Algorithm for n-queens problem
• Problem statement:
“n queens are to be placed on a chess board of size nxn
such that no two queens are attacking”
• The 8-queens problem is a special case of n-queens
problem
• We start placing the queens row wise. At a time only one
queen is placed in a row
• Two queens are attacking if they are in same row, column
or diagonal
• We use a vector x[i], i=1,2,…,n, such that x[i] gives the
column where the queen in the ith row is placed
• Thus, x[k]=j means a queen is placed in the kth row and jth
column
IT Department, RCOEM 8
Algorithm for n-queens problem
recur_nqueens (int k, int n)
{ /* Print all possible solutions to n-queens problem*/
for (x[k]=1; x[k]<=n; x[k] ++)
if(feasible (k, x[k]))
if(k==n)
print(x[1], x[2], x[3],…,x[n]);
else
recur_nqueens (k+1, n);
}
IT Department, RCOEM 9
feasible(int k, int j)
{/* returns true if the queen can be placed in kth row and jth
column*/
int i;
for(i=1; i<=k-1; i++)
if ((x[i]==j) || abs(j-x[i]) == abs (k-i))
/*two queens are they in same column or in same diagonal?*/
return FALSE;
return TRUE;
} /* end of feasible()*/
IT Department, RCOEM 10
Graph coloring problem
IT Department, RCOEM 11
Problem statement
• Let G=(V,E) be a graph
• It is required to color the vertices of the graph using C
colors such that no two adjacent vertices have the
same color
• For example three colors are sufficient for following
graph:
R
1 4 G
G 2 3 B
IT Department, RCOEM 12
• Application of Graph Coloring: Map Coloring
2
2
1 3
1
5 4 5 4
X1=R X1=B
X1=G
X2=G X2=B
X3=B X3=G
X4=G
X4=B
IT Department, RCOEM 15
• Procedure:
we start at first node then move to second, third and
so on
At each stage we check the connectivity of the node
to be colored with the nodes that have already been
colored and assign distinct color to the node
If all the colors are exhausted, then we go back to
previous node and assign the next color to it and
again move forward
the process stops when all the nodes are colored
IT Department, RCOEM 16
Recursive backtracking algorithm for
coloring of graph with c colors
rec_graph_color( int k, int c)
{ /* graph is represented by G[][]; colors are assigned from the values 1,2,3,..,c; k is
the next vertex to color */
for (x[k]=1; x[k]<=c; x[k]++)
if (feasible_color(k))
if(k==n)
print (x[1], x[2],…,x[n]);
else
rec_graph_color(k+1, c);
}
feasible_color( int k)
{ /*returns TRUE if kth node can be assigned a color in in x[k] */
int i;
for (i=1; i<=k-1; i++)
if ( (G[i][k] == TRUE) && (x[i] == x[k])) /*check if the colors are same or distinct*/
return FALSE; /*clash of colors*/
return TRUE;
} /* end of feasible_color()*/
IT Department, RCOEM 17
Hamiltonian Cycle
IT Department, RCOEM 18
• Let G=<V,E> be a connected graph with n vertices
• Hamiltonian cycle of G is a round trip path along
n edges of G that visits every vertex once and
returns to the starting position.
• Hence, if HC begins at some vertex v1 belongs to
G, and the vertices of G are visited in the order
v1, v2, ……., vn+1, then the edges (vi, vi+1) are in
E and vi are distinct except for v1 and vn+1, which
are equal
IT Department, RCOEM 19
Examples
1 2
G1
3
HC: 1,2,3,4,5,1
5 4
3 4
G2
HC: ??
1 2
IT Department, RCOEM 20
Examples
1 2 3 4
G1
7 6 5
8
1 2 3
G2
5 4
IT Department, RCOEM 21
Find a Hamilton path in the G.
b
a c
d e f g h
i j
IT Department, RCOEM 22
Algorithm
R_H(int k)
{ for(x[k]=2; x[k]<=n; x[k]++)
if (feasible(k))
if(k==n) print( x[1], x[2], ……, x[n]);
else
R_H(k+1);
}
IT Department, RCOEM 23
feasible (k) /* check if kth node can be on HC*/
{ int i;
if (G[x[k-1]][x[k]] == True)
{
for(i=1; i<=k-1; i++) /*Checking previous nodes*/
if(x[i]==x[k]) return False; /*Check uniqueness*/
if(k<n || ((k==n) && (G[x[k]] [x[1]] == True)))
return True;
else return False;
}
else return False;
}
IT Department, RCOEM 24
TSP Using Backtracking
• Consider a Graph represented using Cost
Matrix: 1
0 2 3 INF 0 2 1
1 0 6 INF 5 2 3 6
INF INF 0 4 3
4
2
5 2 INF 0
• It is possible to apply Backtracking for TSP
• We can create a Search Space Tree
corresponding to Graph and find a Path with
minimum cost!!
IT Department, RCOEM 25
State Space Tree
0
2 3
1 2
6
4
4 2 3
2
5 3 1 1
0
0
Cost: 17 Cost: 10