IT265 DAA Backtracking and Branch Bound
IT265 DAA Backtracking and Branch Bound
CHANGA
BackTracking
Department of Information Technology
BackTracking
2
Department of Information Technology
Backtracking Approach
It requires less possibilities to find desired solution.
Form a solution and check at every step if this has
any chance of success or not. If the solution at any
point seems not-success (not promising) than ignore
it.
Backtracking
Backtracking (animation)
dead end
?
dead end
dead end
?
start ? ?
dead end
dead end
success!
5
Department of Information Technology
Backtracking
Constraints
Explicit Constraint
Implicit Constraint
N Queen’s Problem
The N queen’s problem can be stated as follows:
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.
For example , consider 2-Queen’s problem which is not solvable.
Q Q Q Q Q
Q Q Q Q Q
N Queen’s Problem
But 4 Queen’s Problem is solvable.
Note that no two queens can attack each other.
Let’s take 4-queens and 4 X 4 chessboard.
Now we start with empty chessboard.
Department of Information Technology
4 Queen’s Problem
Try all the 16 places for the each queen
Total possibilities =16C4 = 1820
First improvement : One queen in One
Row
Total possibilities = 4 x 4 x 4 x 4 = 44 = 256
Second improvement : No two queens in
same column
Total possibilities = 4 x 3 x 2 x 1 = 4! = 24
4 Queen’s Problem
Step 1: Place queen 1 in the first possible position of its row. i.e. on (1,1) .
Q
Department of Information Technology
N Queen’s Problem
Step 1: Place queen 1 in the first possible position of its row. i.e. on (1,1) .
Q
Department of Information Technology
4 Queen’s Problem
Q
Q
4 Queen’s Problem
Q
Q
Step 3: Thus this is the Dead End because a 3rd queen cannot be placed in next
row, as there is no acceptable position for queen 3 . Hence algorithm
backtracks to previous queen (i.e. queen 2)
queen 2 on the position (2,4) Q
Q
Department of Information Technology
4 Queen’s Problem
queen 2 on the position (2,4)
Q Q
Q Q
Q
Step 4: Then place 3rd queen at (3,2) but it is again another dead end as
next queen 4th cannot be placed at permissible position.
Department of Information Technology
4 Queen’s Problem
Q
Q
Q Q Q Q
Q Q Q
Q Q
Q
Place queen 1 at (1,2),queen 2 at (2,4), queen 3
at (3,1) and queen 4 at (4,3).
Thus solution is obtained (2,4,1,3) in row
wise manner.
(Q1, Q2, Q3, Q4) = (2, 4, 1, 3)
Department of Information Technology
4 Queen’s Problem
Two solutions for 4 Queen Problem
Q Q
Q Q
Q Q
Q Q
(Q1,Q2,Q3,Q4) = (3,1,4,2)
(Q1,Q2,Q3,Q4) = (2,4,1,3)
Department of Information Technology
4-Queen Problem
Department of Information Technology
4 Queen Problem
Explicit Constraint :
Si= {1,2,3,4}, 1<= i <=4
Here the solution space is 44
Implicit Constraint :
No two Si can be same OR All queens are in different
column
Reduces the solution space 44 to 4!
No two queens can be on the same diagonal
Questions….
(1,3,5,2,4)
(1,4,2,5,3)
(4,2,5,3,1)
(2,4,1,3,5)
(1,3,5,2,4)
(1,4,2,5,3)
(5,3,1,4,2)
(3,1,4,2,5)
(3,5,2,4,1)
(5,2,4,1,3)
(2,5,3,1,4)
(4,1,3,5,2)
Backtracking Approach
Bounding function
No two queens placed on the same column xi’s are
distinct
No two queens placed on the same diagonal how to
test?
The same value of “row-column” or “row+column”
Supposing two queens on (i,j) and (k,l)
i-j=k-l (i.e., j-l=i-k) or i+j=k+l (i.e., j-l=k-i)
So, two queens placed on the same diagonal iff
|j-l| = |i-k|
Department of Information Technology
N- Queen Algorithm….
Invoked : NQueens(1,n)
void NQueens(int k, int n)
// Using backtracking, this procedure prints all
// possible placements of n queens on an n x n
// chessboard so that they are non attacking.
{
for (int i=1; i<=n; i++) {
if (Place(k, i)) {
x[k] = i;
if (k==n) { for (int j=1;j<=n;j++)
cout << x[j] << ' '; cout << endl;}
else NQueens(k+1, n);
}
}
}
N- Queen Algorithm….
Problem space
Any subset of S.
Department of Information Technology
{1,2,3,4,5}
Department of Information Technology
3 3 8
4 2 7
Department of Information Technology
Questions….
1 2 3
2 3 5
3 4 6
4 5 10
Algorithm Backtrack ()
// This is recursive backtracking algorithm.
// a[k] is a solution vector. On entering (k-1) remaining next values can be
computed.
// T ( a1,a2,….,an) be the set of all values for an , such that ( a1,a2,….,an) is a
path to problem state.
// Bk is a bounding function.
{
For (each an that belongs to T (a1,a2,….,an) do )
{ if (Bk (a1,a2,….,an) = true) then // feasible sequence
{ if ((a1,a2,….,an) is a path to answer node ) then
print (a1,a2,….,an) ;
if ( k < n ) then
Backtrack ( k+1 ) ; // find the next set
} }}
Charotar Institute of Technology, Changa 39
Branch and Bound Department of Information Technology
J1 J2 J3 J4
A 11 12 18 40
B 14 15 13 22
C 11 17 19 23
D 17 14 20 28
J1 J2 J3 J4
A 11 12 18 40 Range : [58…73]
B 14 15 13 22
C 11 17 19 23
D 17 14 20 28
Upper Bound :
Top Left to Bottom Right Diagonal
11+15+19+28 = 73
Top Right to Bottom Left Diagonal
40+13+17+17 = 87
Lower Bound :
Row wise Minimum
11+13+11+14 = 49
Column wise Minimum
11+12+13+22 = 58
11+14+13+22 58 65 78*
=60
J1 J2 J3 J4
A 11 12 18 40
B 14 15 13 22
C 11 17 19 23
D 17 14 20 28
11+14+13+22 58 65 78*
=60
11+14+13+22 58 65 78*
=60
60 58 65* 78*
J1 J2 J3 J4
P1 9 2 7 8
P2 6 4 3 7
P3 5 8 1 8
P4 7 6 9 4
Differences