Backtracking
Backtracking
Backtracking
(Hamiltonian Cycle, Graph Coloring,
Sum of subset and N-Queen)
Lecture – 36-39
Overview
1 2 3 1234561
1265431
1625431
6 5 4
2 3
123541
5 124531
1 4
Backtracking
• Problem 1 : Hamiltonian cycle
1 2 3
Hamiltonian cycle is
4 not exist in this
graph
5 6 7
1 5
Hamiltonian cycle is
2 3 not exist in this graph
because of Articulation
6 point vertex.
4
Backtracking
• Problem 1 : Hamiltonian cycle
1
Hamiltonian cycle is
2 3 not exist in this graph
because of Pendent
vertex.
4
5 6
1 2
5 4
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1 0 1 1 0 1
1 2 2 1 0 1 1 1
3 1 1 0 1 0
3
4 0 1 1 0 1
5 4 5 1 1 0 1 0
Adjacent Matrix
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1 2
1 0 1 1 0 1
2 1 0 1 1 1
3
3 1 1 0 1 0
5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
0 0 0 0 0
1 2
4 3
Backtracking
• Problem 2 : Graph Coloring
• Example: Colored the following graph G by using three colour
(Red, Green and Blue) and draw the state space tree by
using Brute force method.
Root
1 2
4 3
Graph G
4 3
Graph G
4 3
Graph G
4 3
Graph G
4 3
Graph G
S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
Backtracking
S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37
Backtracking
S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37
S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24
Backtracking
S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37
S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24
S K R S K R
55 4 0 42 4 0
S K R S K R
31 4 0 18 4 0
Backtracking
S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37
S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24
S K R S K R S K R S K R
55 4 0 42 4 0 44 4 0 31 4 0
S K R S K R S K R S K R
31 4 0 18 4 0 20 4 0 7 4 0
Backtracking
S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37
S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24
S K R S K R S K R S K R S K R S K R
55 4 0 42 4 0 44 4 0 31 4 0 48 4 0 35 4 0
S K R S K R S K R S K R S K R S K R
31 4 0 18 4 0 20 4 0 7 4 0 24 4 0 11 4 0
Backtracking
S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37
S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24
S K R S K R S K R S K R S K R S K R S K R S K R
55 4 0 42 4 0 44 4 0 31 4 0 48 4 0 35 4 0 37 4 0 24 4 0
S K R S K R S K R S K R S K R S K R S K R
S K R
31 4 0 18 4 0 20 4 0 7 4 0 24 4 0 13 4 0 0 4 0
11 4 0
Backtracking
S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
It was observed that all possible solutions of
this example are between S K R S K R
S K R S K R
18 2 37 7 2 37 0 0 0 0 11 2 37 0 2 37
…..
S K R S K R S K R S K R …. S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24
….
S K R S K R S K R S K R 1 1S1K 1R S K R S K R S K R
55 4 0 42 4 0 44 4 0 31 4 0 48 4 0 35 4 0 37 4 0 24 4 0
S K R S K R S K R S K R S K R S K R S K R
S K R
31 4 0 18 4 0 20 4 0 7 4 0 24 4 0 13 4 0 0 4 0
11 4 0
Backtracking
S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37
S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24
S K R S K R S K R S K R S K R S K R S K R S K R
55 4 0 42 4 0 44 4 0 31 4 0 48 4 0 35 4 0 37 4 0 24 4 0
S K R S K R S K R S K R S K R S K R S K R
S K R
31 4 0 18 4 0 20 4 0 7 4 0 24 4 0 13 4 0 0 4 0
11 4 0
Backtracking
Backtracking
• Problem 3 : Sum of Subset
Algorithm:
Backtracking
• Problems
1. Hamiltonian cycle
2. Graph Coloring
3. Sum of subset
4. N-Queen
Backtracking
• Problem 4 : N Queen
• N-Queen problem is based on chess games.
• The problem is based on arranging the queens on
chessboard in such a way that no two queens can
attack each other.
• The N-Queen problem states as consider a n x n
chessboard on which we have to place n queens
so that no two queens attack each other by being
in the same row or in the same column or on the
same diagonal.
Backtracking
• Problem 4 : N Queen
• 2 – Queen’s problem is not solvable because 2 –
Queens can be placed on 2 x 2 chess board as
shown in below.
Q Q Q Q
Q Q
Q Q
Q Q Q Q
Backtracking
• Problem 4 : N Queen
• How to solve N-Queen Problem.
1. Let us take the example of 4 – Queens and 4 x 4
chessboard.
2. Start with an empty chessboard.
3. Place queen 1 in the first possible position of its row
i.e. on 1st row and 1st column
Q
Backtracking
• Problem 4 : N Queen
• How to solve N-Queen Problem.
4. Then place queen 2 after trying unsuccessful place
(1, 2), (2, 1), (2, 2) at (2, 3) i.e. 2nd row and 3rd
column.
Q
Q
Backtracking
• Problem 4 : N Queen
• How to solve N-Queen Problem.
5. This is a dead end because a 3rd queen cannot be
placed in next column, as there is no acceptable
position for queen 3. Hence algorithm backtracks and
places the 2nd queen at (2, 4) position..
Q Q
Q Q
Backtracking
• Problem 4 : N Queen
• How to solve N-Queen Problem.
6. Then place 3rd queen at (3,2) but it again another
dead lock end as next queen (4th queen) cannot be
placed at permissible position.
Q
Q
Q
Backtracking
• Problem 4 : N Queen
• How to solve N-Queen Problem.
7. Hence we need to backtrack all the way up to queen 1
and move it to (1, 2). viii. Place queen 1 at (1, 2), queen
2 at (2, 4), queen 3 at (3, 1) and queen 4 at (4, 3).
Q Q Q Q
Q Q Q
Q Q
Q
8. Thus the solution is obtained (2, 4, 1, 3) in row wise
manner in x array.
Backtracking
Backtracking
• Problem 4 : N Queen
One of the solution to 8-Queen Problem
Q
Q
Q
Q
Q
Q
Q
Q
Backtracking
• Problem 4 : N Queen
• N Queens are placed on n by n chessboard so that no
two attack (no two queens are on the same row,
column, or diagonal).
• A tree bellow shows the state space tree for n=4.
1
2 18 34 50
3 8 13 19 24 29 35 40 45 51 56 61
4 6 9 11 14 16 20 22 25 27 30 32 36 38 41 43 46 48 52 54 57 59 62 64
5 7 10 12 15 17 21 23 26 28 31 33 37 39 42 44 47 49 53 55 58 60 63 65
Backtracking
• Problem 4 : N Queen
• N Queens are placed on n by n chessboardofso that no
two attack (no two queens are on othe ns same row,
i
column, or diagonal). l ut
s o
• A tree bellow shows the state space b le tree for n=4.
s si n!)
1l p
o O(
a l is
a t le
2 18 th mp 34 50
d a
rve s ex
se hi
3 8 13 ob19 t 24 29 35 40 45 51 56 61
a s
w 16 20 22 25 27 30 32 36 38 41 43 46 48 52 54 57 59 62 64
4 6 9 11 t14
I
5 7 10 12 15 17 21 23 26 28 31 33 37 39 42 44 47 49 53 55 58 60 63 65
Backtracking
Backtracking
• Problem 4 : N Queen
Logic behind the place() of N-Queen Problem
1 2 3 4 5 6 7 8 4. The Queen is available in [i,j] =[4,2]
1 5. The squares that are diagonal to that
queen from upper left to lower right
2 are [3,1], [5,3], [6,4], [7,5], and
3 [8,6]. (Note : All these squares have
4 Q a “row-column” value of 2)
5 6. Similarly, the squares that are
diagonal to that queen from upper
6 right to lower left are [1,5], [2,4],
7 [3,3], and [5,1]. (Note : All these
8 squares have a “row+column” value
of 6)
Backtracking
Backtracking
• Problem 2 : N Queen (Algorithm)
Recursive algorithm for N-Queen
NQueens(k, n)
// Using backtracking, this procedure prints all possible placements of n queens
// on an n x n chessboard so that they are nonattacking.
{
for i := 1 to n do
{
if (Place(k, i))
{
x[k] := i;
if (k = n)
write (x[1:n])
else
NQueens(k+1, n);
}
}
}
Backtracking
• Problem 2 : N Queen (Algorithm)
Recursive algorithm for N-Queen
Algorithm Place( k, i)
// Returns true if a queen can be placed in kth row and ith column. Otherwise it
// returns false. x[] is a global array whose first (k-1) values have been set. abs(r)
// returns the absolute value of r.
{
for j := 1 to k-1 do
if ( (x[j] = i) or // Two in the same column
|| (abs(x[j] - i) = abs(j-k)) ) // or in the same diagonal
then return false;
return true;
}