Daa Unit Iii Backtracking and Branch and Bound
Daa Unit Iii Backtracking and Branch and Bound
By Prof. B.A.Khivsara
Assistant Professor
Department of Computer Engineering
SNJB’s KBJ COE, Chandwad
Backtracking
BACKTRACKING
Principal
Problems searching for a set of solutions or
which require an optimal solution can be
solved using the backtracking method .
To apply the backtrack method, the solution
must be expressible as an n-tuple(x1,…,xn),
where the xi are chosen from some finite set si
The solution vector must satisfy the criterion
function P(x1 , ….. , xn).
3
BACKTRACKING (Contd..)
Suppose there are m n-tuples which are possible
candidates for satisfying the function P.
Then m= m1, m2…..mn where mi is size of set si
1<=i<=n.
The brute force approach would be to form all of
these n-tuples and evaluate each one with P,
saving the optimum.
4
BACKTRACKING (Contd..)
6
BACKTRACKING: Solution Space
Tuples that satisfy the explicit constraints define a solution space.
All paths from the root to other nodes define the state-space of the
problem.
8
BACKTRACKING -Terminology
BOUNDING FUNCTION - will be used to kill
live nodes without generating all their
children.
9
BACKTRACKING -Terminology
BREADTH-FIRST-SEARCH : Branch-and
Bound with each new node placed in a
queue .The front of the queen becomes
the new E-node.
Example :
1 1 1 1
. . 2 2 2
3
. . . .
1 1
2
3
. ,
4
11
BACKTRACKING (Contd..)
We start with root node as the only live node.
The path is ( ); we generate a child node 2.
The path is (1).This corresponds to placing
queen 1 on column 1 .
Node 2 becomes the E node. Node 3 is
generated and immediately killed. (because
x1=1,x2=2).
As node 3 is killed, nodes 4,5,6,7 need not be
generated.
12
Iterative Control Abstraction
( General Backtracking Method)
Procedure Backtrack(n)
{ K 1
while K > 0 do
if there remained an untried X(K) such that X(K)
T ( X (1) , …..X(k-1) ) and Bk X (1) ,…,X(K) ) = true then
if (X (1),…, X(k) ) is a path to an answer node then
print ( X (1) ,…X (k) )
end if
K K+1 // consider next set //
else K K-1 // backtrack to previous set
endif
repeat
end Backtrack
13 }
RECURSION Control Abstraction
( General Backtracking Method)
Procedure RBACKTRACK (k)
{ Global n , X(1:n)
for each X(k) such that
X(k) T ( X (1),..X(k-1) ) and Bk (X(1)..,X(k-1), X(k) )= true
do
if ( X (1) ,….,X(k) ) is a path to an answer node
then print ( X(1),……,X(k) ) end if
If (k<n)
CALL RBACKTRACK (k+1)
endif
repeat
End RBACKTRACK
}
14
EFFICIENCY OF BACKTRACKING
ALGORITHM Depend on 4 Factors
15
N queens problem
using Backtracking
The n-queens problem and solution
17
The n-queens problem and solution
contd..
Observe that
(1,1) (1,2) (1,3) (1,4)
i) For the elements in the
the upper left to lower (2,1) (2,2) (2,3) (2,4)
Right diagonal, the row -
column values are same (3,1) (3,2) (3,3) (3,4)
or row- column = 0,
(4,1) (4,2) (4,3) (4,4)
e.g. 1-1=2-2=3-3=4-4=0
ii) For the elements in the upper right to the lower left diagonal, row
+ column value is the same e.g. 1+4=2+3=3+2=4+1=5
18
The n-queens problem and solution
contd..
Thus two queens
are placed at
positions (i, j) and • i – j = k - l or
• i + j = k+l
(k, l), then they
are on the same
diagonal only if
19
The n-queens problem -Algorithm
Procedure PLACE (k)
{
for i 1 to k-1 do
if X(i) = X(k) // two are in the same column
or ABS(X(i) –X(k)) = ABS(i-k) // in the same diagonal
then Return (false)
end if
repeat
return (true)
end
}
20
The n-queens problem -Algorithm contd..
Procedure N queens(n)
{ X(1) 0 ; k1 // k is the current row,//
while k > 0 do
X(k) X(k) + 1
While X(k) n and not PLACE(k)
X(k) X(k) + 1
repeat
if X(k) n then
if k = n then print(X)
else k k+1;
X(k) = 0
endif
else k k-1
endif
repeat
end NQUEENS
21
Graph Coloring Problem
using Backtracking
GRAPH COLOURING PROBLEM
24
Solving the Graph Colouring Problems
25
Solving the Graph Colouring Problems
(Contd..)
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
A
Graph Coloring
B F
C E
D
State space tree for m colouring
problem with n = 3 and m = 3
1
X(1)=1 X(1)=3
X(1)=2
2
X(2)=1
2 3
3
X(3)=1
2 3
4 5 6
39
Graph Colouring Problem- Algorithm
Procedure MCOLORING (k)
{ global integer m,n,X(1:n);
boolean GRAPH (1:n,1:n)
integer k
loop
Call NEXTVALUE(K)
if X(k) = 0 then exit endif
if k = n then Print X
else call MCOLOURING(k+1)
endif
repeat
end MCOLOURING
}
40
Graph Colouring Problem- Algorithm
(Cont…)
Procedure NEXTVALUE(k)
{
Repeat
{
X(k)( X(k) +1 ) mod(m+1)
if X(k)=0 then return endif
for j 1 to n do
if GRAPH(k,j) and X(k)=X(j) then exit endif
if j = n+1 then return endif
} Until(False)
}
41
Sum of Subset Problem
using Backtracking
Sum-of-Subsets problem
In this problem, we are given a vector of N values, called weights.
The weights are usually given in ascending order of magnitude and
are unique.
For example, W= (2, 4, 6, 8, 10) is a weight vector. We are also
given a value M, for example 20.
The problem is to find all combinations of the weights that exactly
add to M.
In this example, the weights that add to 20 are:
(2, 4, 6, 8); (2, 8, 10); and (4, 6, 10).
Solutions to this problem are often expressed by an N-bit binary
solution vector, X, where a 1 in position i indicates that Wi is part of
the solution and a 0 indicates it is not.
In this manner the three solutions above could be expressed
as: (1,1,1,1,0); (1,0,0,1,1); (0,1,1,0,1)
Sum-of-Subsets problem
We are given ‘n’ positive numbers called weights and
we have to find all combinations of these numbers
whose sum is M. this is called sum of subsets
problem.
We have already seen two graph search strategies, BFS and D-search, in
which the exploration of a new node cannot begin until the node currently
being explored is fully explored.
A D-search-like state space search will be called LIFO (Last In First Out)
search as the list of live nodes is a last-in-first-out list (or stack).
Control Abstraction for Branch
and Bound(LC Method)
LC Method Control Abstarction
Explanation
The search for an answer node can often be
speeded by using an "intelligent" ranking function,
c(. ), for live nodes.
The next £-node is selected on the basis of this
ranking function.
Let T be a state space tree and c( ) a cost
function for the nodes in T. If X is a node in T then
c(X) is the minimum cost of any answer node in
the subtree with root X. Thus, c(T) is the cost of a
minimum cost answer node
LC Method Control Abstarction
Explanation
The algorithm uses two subalgorithms LEAST(X)
and ADD(X) to respectively delete and add a live
node from or to the list of live nodes.
LEAST{X) finds a live node with least c( ). This
node is deleted from the list of live nodes and
returned in variable X.
ADD(X) adds the new live node X to the list of
live nodes.
Procedure LC outputs the path from the answer
node it finds to the root node T.
0/1 knapsack problem using
Branch and Bound
The 0/1 knapsack problem
Positive integer P1, P2, …, Pn (profit)
W1, W2, …, Wn (weight)
M (capacity)
n
maximize Pi X i
i 1
n
subject to Wi X i M Xi = 0 or 1, i =1, …, n.
i 1
The problem is modified:
n
minimize Pi X i
i 1
53
The 0/1 knapsack problem
55
How to find the ranking Function
57
0/1 Knapsack algorithm using BB
0/1 Knapsack Example using
LCBB (Least Cost)
Example (LCBB)
Consider the knapsack instance:
n = 4;
(pi, p2,
p3, p4) = (10, 10, 12, 18);
(wi. w2, w3, w4) = (2, 4, 6, 9) and
M = 15.
0/1 Knapsack State Space tree of
Example using LCBB
0/1 Knapsack State Space tree of
Example using FIFO BB
Traveling Salesman problem (TSP)
using Branch and Bound
The traveling salesperson problem
Given a graph, the TSP Optimization problem
is to find a tour, starting from any vertex,
visiting every other vertex and returning to the
starting vertex, with minimal cost.
63
The basic idea
There is a way to split the solution space
(branch)
There is a way to predict a lower bound for a
class of solutions. There is also a way to find
a upper bound of an optimal solution. If the
lower bound of a solution exceeds the upper
bound, this solution cannot be optimal and
thus we should terminate the branching
associated with this solution.
64
Example- TSP
Example with Cost
Matrix(a) and its
Reduced Cost Matrix (b)
Reduced matrix means
every row and column of
matrix should contain at
least one Zero and all
other entries should be
non negative.
Reduced Matrix for node 2,3…10 of
State Space tree using LC Method
State Space tree of Example using LC
Method