0% found this document useful (0 votes)
1 views

Design Analysis Algorithm- Unit-4

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Design Analysis Algorithm- Unit-4

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

UNIT-V BACKTRACKING UNIT- BACKTRACKING

ntroduction:
In back tracking technique, we will solve problems in an efficient way, when
othermethods like greedy method and dynamic programming. The solution is based finding on
compared
one
to
XI=I
Q
or more vectors that maximize,
minimize, or satisfy a criterion function P(x1, Kn). Form a solution
. . IF3

at any pomt seems not promising. ignore it. All possible soutions require a set of constraints divided
nto two categories
I. Explicit Constraint: Explicit constraints are rules that restrict each xi to take on values only
4

50
from a given set. Ex: xn= 0 or l.
. Implicit Constraint: Implicit Constraints are rules that determine which of the tuples in the
xat3 xap2

solutions space of I satisfy the criterion function. 24) 29 6


Implicit constraints for this problem are that no two queens can be on the same diagonal.
Back tracking is modified depth first search tree. Backtracking is procedure whereby,
a a
determining that a node can lead to nothing but dead end, we go back (backtrack) to the nodes parent
after s.AAAA Ä.A.X
and proceed with the searchon the next child. State space tree exists implicitly in the algorithm
not constructed.
because it S actually
1
Terminologies which is used in this method:
Solution Space:Alluples that satisfy the explicit constraints definea possible solution space
1J11JJ
for a particular instance T of the poblem.
Fig: Tree organization of the 4-queens solution space
EIample:
In the above figure nodes are numbered as in depth first search. Initially, (x1=1 or 2 or 3 or 4,
it means we can place first queen in either first, second, third or fourth column. Ifxl=l then x2 can be
placed in either 2nd, 3rd or 4th columns. If (x2=2) then, x2 can be placed either in 3rd, or 4th column.
Fig: Tree onganization of a solution space Tf x3=3, then x4=4. So nodes 1-2-3-4-5 is one solution in solution space. It may not be a feasible
solution.
5. Answer States: These solution states S, for which the path from the root node to S defines a
2. Problem State: A problem state is the state that is defined by all the nodes within the tree tuple that is a member of the set of so lutions (i.e., it satisfies the implicit constraints) of the problem.
organization.

Example:
Fig: Problem State

Fig: Tree (Answer States)

Solution States: These are the problem states S for which the path form the root to S defines a
Here are C, D are answer states. (A, C) and (A, C, D) are solution states.
tuple in the solutioa space.
Here, square nodes () indicate solution. For the above solution space, there exists 3 solution states 6.Live Node: A node which has been generated but whose children have not yet been generated
is live node.
These solution states represented in the form of tuples i.e., (ghk,B,D),(A,C,F) and (A,C,G) are the
solution states. Example 1: (1)
This node I is called as live node since the children of node I have not been generated.
Example:
Example 2:

Fig: Solution State

In this, node l is not a live node but node 2, node 3 are live nodes.

4. State Space Tree: Is the set of paths from root node to other nodes. State space tree is the tree
organization of the solution of the so lution space
Example: State space tree ofa 4-queen problem.

blog' anilkumarprathipati.wordpress.com blog: anilkumarprathipati.wordpress.com


UNIT-V BACKTRACKING
BACKTRACKING UNIT-V
Example 3: IAlgorithm Backtrack(k)
2 I[ This sclhema describes the backtracking procss using
k l valucs
// recursion. On entering, the tirst
-

3
4
o rl: n f
thesolution vector
have bren ass igned. ri} and n ae global
Here, 4, 5, 3 are live nodes because the children of these nodes not
yet been generated.
E-Node: The live nodes whose children are currently do
.

(node being expanded).


being generated is called the E-node for (cach s[k] ¬ T(«[1],.... , r[k 1}) -

Example 1: (1) then


This node I is live node and its children 10
if (Bulr[l,z[2]z{k]) # 0)
are currently being generated (expanded).
E-node E-node E-node if (r[!], ={2],...,z[k] is a path to an answer node)
12 then write (r[1: kl);
Example2: 13 if (k < n) then Backtrack(k + 1);
-

11
15
16
Algorithm: Recursive backtracking
Here, node 2 is E-node.
8. Dead Node: It is generated node, that is either not to be expanded further or one for which all Applications of Backtracking
Backtracking is an algorithm design technique that can effectively solve the larger instances of
of its children has been generated.
combinational problems. It follows a systematic approach for
obtaining solution to a problem. The
applications of backtracking include,
1) N-Queens Problem: This is generalizat
ion problem. If we take n=8 then the
problem is called as 8
queens problem. If we take n=4then the problem is called 4 queens
problem. A classic combinational
(a) problem is to place n queens on a n"n chess board so that no two attack, i.,e no two queens are on the
Nodes 1. 2, 3,aredend sodes. Since node 1's children geoerated and node 2, 3 are not expanded. same row, column or diagonal
Assumed that node 2 generated one more node, So, I, 3, 4 are dead nodes. Algorithm of n-queens problem is given below:
1 Algorithm NQueens(k, n)
Using backtracking, this procedure prints all
possible placements of n querns on an n x n
chessboard so that they are nonattacking.
b) for i= 1 to rn do
Fig: Dead nodes
General Method:
The basic idea of backtracking is to build up a vector, one component at a time and to test if Place(k, i) then
whether thc vector being formed has any chance of success.
The major of
advantage this algorithm is that
we can realize the fact that the partial vector
generated does not lead to an optimal solution. In such a situation that vector can be ignored. 11
k]-i
if
Backtracking algorithm determincs the solution by systematically searching the solution space
(k -

u) then write (r|l: nl);


12 else NQueens(k +I, rn);
(i.e set of all feasible solutions) for the given problem.
I3
Backtracking is a depth first search with some bounding function. All solutions using
14
backtrack ing are required to satisfy a complex set of constraints. The constraints may be explicit or
implicit. 15
Algorithm: All solutions to the n-queens problem

blog: anilkumarprathipati wordpress com blog: anilkumarprathipatiwordpress com


UNIT-V BACKTRACKING UNIT-V BACKTRACKING
Algorithm Place(k. i Now the fourth qucen shouk e placed n 4h3 row
and
and ird cohamn but there will be a diagonal

2 // Returris true if n queen can he plard in kth row and


aack from qucen 3. So go back. remove qucen
remove f
place t n the nextpossible.
to next cetur but n
cohurn So
not
Buigo itback
no to
possble, so move back to qucen 2 and
s

3 // ith coluinn. Otherwise it returns false. 1[| is a qucen I and move to next column
// global array wihose first (k 1) values have been set.
Abs(r) rurns the abolute value of r.
I to k -1 do
if ((r) = i) // Two iu the coluun
sane
or
(Abslrji) =Abs(j k})
10 1or iu Lte saune diagonalI
11 then return false;
12 return true;
13
Algorithm: Can a new queen be placed?
(
4Queens problem:
Consider a 44 cheasboard. Let there are 4 qucens The objective a placc there 4 gucens on
Fig: Exanple of Backtrack woiution to the 4qunenm proble
3-1,
4°4 chewboard in much a way that o two queens shoukd be plsced in the sane row, ame cokure or Hence the sohution of to4-qucens's peroblem u ai-2. 24 xd-3, 1emfirst queen
frst cokumn
is

paced in 2d cohuma second qacen is placed in 4th coBumn and third queen is placod
diagonal postion and fourth queen is placod n thrd cohumn
The explicit constraunts are 4 qucens are to be placed en 4°4 chessboards in 44 w*y.
The implicit constraints are no two queers are in the sane r u coàuma or dugonal
Row
Let (xl. 2, 3, x4} be the solution vector where xl cokumn oa which the quren i is place
Frt queei s placed n first row and lirst cohimn.

Row 2

Row3
The second qucen shaould not he in finst row and secocd cohamn it shoks t plcad n cond
row and in vecond, third or hurth codumn It we place in aecond aolums, both will be n ams
diagonal, so place it in third cokumn.
Row 4

B B
4
F: Potioe of the rer that in generated during Racktracking
We are unabie to place qucen J in third row, &go bacik to queern 2 ad pace d umes here q e n preblkm
A clamic aombenaleoriai problem in to place 8 queens on a *8 chess board so that
w
else attack. i,e m two queens are to the same row, cmkuman or diagona
Now, we *il ohe t queem problem by using similar procedure adapted tor 4 quees
probkm The algorithm of s quecrs problerm can be obained by plactng n=8, in N
queens aigornhm
We obere that, or every ekemed on the same daagonal wtuch rus trom the upper ett to the e u r
righ, ech eiemern has the same "res-coamn" value. Abo every elemet on the same agonal
which o a from upper ngm to kower ef bae the same "row*cotum" vale

bio aknarprathspatr wordpress com


bhy anikmarpns thsti worupres Com
UNIT-V BACKTRACKING BACKTRACKING
If two
UNIT.V
queens are
placed at positions (1j) and (k,1). They are on the same
diagonal only if Time complexity: The solution space tree of 8-queens problem contains 8 tuples. After imposing
-J=k-I . .. ) or
implicit constraints, the size of so lution space is reduced to 8! tuples.
itj=k+l.... .....4) The state space tree for the above sohution is given
From (1) and (2) implies
j-Fik.and
j-Fk-i
Two queens lie on the same diagonal iff
--k
But how can we determine whether more than one
queen is lying on the same diagonal? To
answer this question, a technique is
deviced. Assume that the chess board is divided into rows

18. 8 X2=2
O
rows columns
And columns say A:
This can be diagrammatically represented as follows
5 OQO0OOO
x-24
K4=7
xa-2
OOO
B B B

Now, assume that, we had placed a queen at


position (5,2).
Now, its diagonal cellsin includes (2,1)4,3N5,4)....if traverse from upper leftalso
to lower
we A78

right). If we subtractright
values these cells say 2-1-=1,4-3=1,5-4-1, get same
to lower left
values, addif the we we

traverse
of
from upper say (2,3) (1,404,1)..we get
values when common we

bits these cells i,e 2+3=5, 1+4-5, 4+1=5. Hence, we say that,
on traversing from upper left to
lower right, if (m,n\a.b) are the diagonal elements(of a cell) than m-n=a-b or on traversing from Bx= B
xs*s
upper right to lower let ifm,n)(a,b) are the diagonal elements(ofa cel) then mt=atb. (13)
The solution of 8 queens problem can be obtained similar to the solution of 4 queens. 12
problem.Xl=3, X2=6, X3=2, X4=7, X5=1, X6-4, X7-8, X8=5,
The solution can be shown as 2) Sum of Subsets Problem
Given a set of n objects with weights
Subset S of the given set S, such that
(wl,w2,.. w3) and positive integer
a M. We have to finda

SS
Sum ofthe elements of subset S' is equal to M.
For example,
sum is equal
if a given set S=(123,) and M=5, then there exists sets S'0,2) and S'=(1,4) whose
to
M.
It also be noted
can
that some instance of the problem does not have any solution.
For
M=7.
example, if given set S=(1,3,5) and M-=7, then subset occurs for which the sum is equal to
a no

The
sum ofsubsets problem can be solved by using the back tracking approach. In this implicit
tree is created, which is a binary tree. The root of the tree is selected in such a way that it represents
that no decision is yet taken on any input. We assume that, the elements of the given set are arranged
increasing order.

blog: anilkumarprathipati.wordpress.com blog: anilkumarprathipati wordpress.com


UNIT-V
The left child of the root node indicates BACKTRACKING
that, we have to include the first element and
UNIT-V
child of the root node indicates that, we have to
exclude the first element and so on for other
right BACKTRACKING
Each node stores the sum of the nodes. Subset (Empty}Sum=0 Initially subset is 0
partial solution element. If at any stage, the number equals to
then the search is successful. At this time search will
terminate or continues if all the "M 5
solutions need to be obtain. The dead end in the tree occurs possible 5, 10
exists.
only when either of the two
inequalities
The sum of S' is too 5, 10, 12
large.
The sum ofs' is too small.
5, 10, 12, 13 4 0 Sum exceeds N=30,Hence Backtrack
Thus we take back one step and continue the search.
ALGORITHM: 5,10, 12, 15 | Not Feasible_
Algorithrn SumOfSubfs, k, r} 5,10, 12, 18 Not feasible
// Find all subsets of w/l : n that sum to mi. The values of clji, , 10
3//1Sj<k, have alrealy been determined. s 2 , 10, 13
List ends. Backrack_
i *ri
4//aud 2j-k u:j. T'he uij|s are in noudex:easing order.
r =

I t is assumed tbat «|l] m and i n wi m. 5, 10, 13, 15 33


Not feasible. Backtrack
, 10
// Generate left clild. Note: s +wk} m since Be-1 is true. 5, 10, 15 0 Solution obtained
We can
represent various solutions to sum of
if(s t wki= mi) then write {r{1 : k]}; // Subset found subset by a state space tree as,

// There is no recursive call here as wij. > 0, 1SJSi.


else if (s t uwk
12 then
+wik + 1i Sn} =
SumOfSub(s 1kj,k + 1,r u[k});
Generate right child and evaluate g
if ((s +r- »k] 2 m) and (s + w{k +1j < T)) then

k=U
SumOfSub(s,k + 1,r - tk]);
2
X2l X2-0

19 10
Algorithm: Recursive backtracking algorithm for sum of subsets
X X3=0 X3 x-0 xo1
Example: 27
Let m=31 and w= {7, 11, 13, 24} draw a portions of state space tree.
Solution: Initially we will pass some subset (0, 1, 55). The sum of all the weights from w is 55, i.e.,
=0 xat1 x4=0

7+11+13+24-55. Hence the portion of state -space tree can be 28) 1 30 12


Here Solution A={l1, 1, 1, 0} i.e., subset {7, 11, 13 B
And Solution B={1, 0, ie. subset {7, 24}
xs1 xst x$=0
0,1
Satisfy given condition-31; (30) 20)
Example: Consider a set S={5, 10, 12, 13, 15, 18} and N=30 A
x-1

3) Graph Coloring
Let b e a graph and m be a given positive integer. The graph coloring problem is to find if
the nodes of G
can be colored in such a
way that no two adjacent nodes have the same color, yet only
m
colors are
used. This is termed the m-colorability decision problem. The m-colorability
optimization problem asks for the smallest integer for which the graph G
integer is referred to as the chromatic number of the graph.
m canbe colored. This

blog: anilkumarprathipati wordpress.com blog: anilkumarprathipati.wordpress.com 0


BACKTRACKING
UNIT-V
UNIT-V BACKTRACKING 1Algorithm NextValue(k} valucs in
have beeu assigned integer
b) 2 [1),..tik-
such that adjaceut vertices have dist inct
//the range {I, n
is determincd in the rauge
integers. A value for rik| uexi higlhest numbered color
lie
0, mj. «lk| is assigned from the adjacent vertices
while maintaining distiuctuess
)3 / of vertex k. If no such color exists,
then rik is 0.

repeat
10
1 Next highest color.
r[ki= {c[k+1) mod (n + 1);All//colors have been used
return; //
2
if (z[k} =l 0)tothen
n ddo
13 for j=
14 // Check if this color is
Figure. An Ex. Of graph coloring 15 // distinct from adjacent colors.
if((G[k.j! # 0) and (:[k} = zj])}
Algorithm mColoring(k) I f (k.j) isand edgeand if adj.
This algorithm was forined usiug the recursive backtrackiug 18 //vertives have thesame color.
schema. The graph is represented by its boolean adjaceney 19 then break;
uatrix G1:76,1: 7]. such
All assiguinents of 1,2.... , m to the
20
vertices of the graph that adjacent verticesindex
are 21 if G =n+1) then return;// New colur founud
distinct integers are printed. k is the
ASsignexl
of the next vertex io color. until (false); // Otherwise iry to find auother color.

Algorithm: Finding next color


repeat
10 {// Generate all kgal asiguments for zik.
NextValue(k); / / A ssign to zikj a legal color.
12 if (z[kj = 0) then return;// No new color passible
been
13 if (k = r) then
A t most in colors lave vertices.
// used to color the n

15 write (il :n})


16 else mColoring(k + l1
17 until (false);
18
Algorithm: Finding all m-colorings of graph
Fig. A map and it's planar graph representation
To color the above graph chromatic number is 4. And the order of coloring is X1=1, X2=2,
X3=3, X4=4, X5=1| children
Nextcolor to determine the
Time Complexity: At each internal node O(mn) time is spent by
correspondingto legal coloring. Hen the total time is bounded by,

. m"
n.m°)

12
blog: anilkumarprathipati.wordpress.com
blog: anilkumarprathipati.wordpress.com7
UNIT-V
49Hamitonian Cycle be BACKTRACKING UNIT-V
Let G =
(V, E)
a connected
BACKTRACKING
graph with n vertices. A Hamitonian cyce is a round-trip Root
along n edges ofG
starting position In other wordspath
that vists every vertex once and ret
uns to its
Hamtoan cycle begns a some vertex Vi E if a
G and the vertices of G are
01.1 12 Va+L then the edges (Vi Vi+) are in E, 1 visited in the order
and VL which are
and theK-n,
Vi are distinct except for VI
equal
Gvenag ph GVE}we bave to find the Hamitonian circuit using
backtracking approach
we start out erch fon any
zditrary vertex, say L This vertex 'x' becomes the
root of our implicit
Tee The Dext
adjaceat vertexs seected oa the asis of alpbabetical i or numerical order.
sage an arbrry r t a ay ¥ mekes a cytle with any verter other than verter 'o' then If at any
dead end is eacbed In this case we backtrack one
we say that
step and again the search begins
t t r vetea i sbould be oted thaz, after
backtracking the element from the
by selecting (d)

be removed The sexth partial soution must Next vertex s' is selected.
IEwe choose vertex I ' then we do not get the Hamiltonian cycle.
using back1racking is successful if a Hamikonian cycle is obtained. Root
Euge Consider a gh G{VE, * e bave tw find the Hamikonian circuit using backtracking
d

Cae

Soktion italy we start out search wich vertea 'I' the vertex 1' becomes the root of our ()
Dead end
Root The vertex adjacent to 5 is 2, 3, 4 but they are already visited. Thus, we get the dead end. So,
we backtrack one step and remove the vertex '5' from our partial
(a) so lution.
Nerr ae choose verter 2' adjacent to "1, asi comes frst in umerical order (2, 3.4). The vertex adjacent to '4' are 5,3,1 from which vertex S' has already beenchecked and
with vertex 1' but by choosing vertex "l' we do not get the Hamitonian cycle. So, we again
we are
left
Root backtrack one step.
Hence we select the vertex '5' adjacent to 3.
Root

} s eieced whicà s adjacent to '2' and which comes first in mumerical order
Net rerna

Roo

()

end
Dead
The vertex adjacent to '5' are (2,3,4) so vertex 4 is selected.

to 3 which comes fsst ia numerical order (4, 5). 4


Next we seect verter '4' dacent
13 blog: anilkumarpra thpati.wordpress.com
log ciKraprathpatiwordpre3s.com
UNT- BACKTRACKING UNIT-V BACKTRACKING
Root Root

(i)

Dead end
The vertex adjacent to "4' are (1, 3, 5) so vertex' l ' is sclectod. Hence we get the Hamiltonian
O'solutio
Fig Construction of Hamilton Cyele using Backtrcking
vertex 1' is visited only once, 1- 2- 3-5- 4- 1.
vertex other
cyele as all the than the start Algorithm Hamiltonian{k)
the rerursive formulation of
Root
/ This algorithm uses
// backtrack ing to find all the Hamiltoninn cycles
//of a graph. The graph is stored as au aljaceucy
natrix G[l: u,1 : . All eycles begin at uode 1.
6
repeat
/ / Generate valucs for rlkj
NextValue(k); // Assign a legal next value to z[k).
10 if (r[k] =0) then return;
if (kn) then write (r[1 : n});
12 clsc Hamiltonian(k + 1);
13 }until (falsc);
14
Dead end
(h) Algorithm: Finding all Hamiltonian cyeles
O Solution
below. The number above each
The final implicit tree for the Hamiltonian circuit is shown
node indicates the order in which thesc nodes are visitcd.

15 blog: anilkumarprothipati.wordpress.com
blog: anilkumarprathipati.worodpress.com
UNIT-V
BACKTRACKING
1
2
Algorit hm NextValue(k)
3
I/zl:k- 1} is a path of k -1 distinct vertices. If r[k| =0, theu
novertex has as yet been axsigned to
r[k). After execution,
/ zk is assigned to the next highest
umbered vertex which
does uot
alrcady appear in r{l: k- 1} aud is conuerted by
6 a n edge to r k 1. Otherwise ik|
-

= 0. If k = n, then
1/ in addition r{k is counected to r[!.
9 repeat
10
rk= (r[k 1) mod (n + i); // Next vertex.
12 if (r[k] =
0) then return
if (Gz[k - 1}.z[k]} #0) then
14 { / / Is there an edge?
15 for j = I to k - do if (r{=r[ki) then break;
16 /7 Check for distiuctuess.
17 if i = k) then // f true, then the vertex is distinct.
18 if ((k < ra) or ({k =n) and G[r7|,r[1] #0))
19 then rcturn;
20
21 until (false);
22 }
Algorithm: Generating a next vertex
UNIT-V1 BRANCH AND BOUND UNIT-V1 BRANCH AND BOUND

Introduction:
Branch
and Bound refers to all state space search methods in which all children of the E- Next, delete element from
an 3 and these livequeue
3, 7, 8 are children of
and
take it E-node.
generate
nodes are killedasby bounding
children of node
the
functions. So we wil not
Node are generated before any other live node becomes the E-Node.
include in the queue.
Branch and Bound is the generalization of both graph search strategies, BFS and D-search.
A BFS like state space search is called as FIFO (First in first out) search as the list of
ive nodes in a first in first out.
. A D-search like state space search is called as LIFO (last in first out) search as the list of
14 5 6
Again delete an element an from queue. Take it as E-node. generate the children of 4.
live nodes in a last in first out list. Node9 is generated and killed by boundary function.
Live node is a node that has been generated but whose children have not yet been generated.

-ndeis aive node whose children


a node curently being expanded.
are currently being explored. In other words, an E-node is 5 6
Dead node is a generated anode that is not be expanded or explored any further. All children of a
Next, delete an elcment from queue. Generate children of nodes 5, i.e., nodes 10 and 11
in queue is 6. The child of node 6 is 12 and it
dead node have already been expanded. are generated
the
and by boundary
of
function,which
last node
is the
Here we will use 3 ypes of search strategies: satisfies conditions the problem, answer node, so search terminates.
1. FIFO (First In Frst Out) LIFO Branch and Bound Search
For this we will use a data structure called stack. Initially stack is empty.
2. LIFO (Last In
First Out)
3. LC (Least Cost) Search Example
EITE Branch and Bound Search:
For this we will use a data structure called Queue. nitially Queue is empty.

Example:

stack

Generate children of node I and place these live nodes into stack.

Assume the node 12 is ananswer node (solution)


stack
In FIFO scarch, first we will take E-node as a node
Next we generate the children 1. We will
of node all these live nodes in
place a
queue Remove clement from stack andgeneratethechildren of it, place those nodes into stack.
2 is removed rom stack. The children of 2 are 5, 6. The content of stack is,

234
Now we will delete an element from queue, Le. node 2, next generate children of node 2
and place in this queue.

L34s 6 SLacs

log anilkumarprathipari wardpresS Com blog anilkumarprathipati wordpress.com


UNIT-VI BRANCH AND BOUND UNIT-V1 BRANCH AND BOUND

Again remove an clement from stack, i.,e node 5 is removed and nodes generated by 5 . Least-cost()
are 10, 11 which are killed by bounded function, so we will not place 10, 11 into stack. 2. Add_node().
Least-cost0 finds a live node with least c(). This node is deleted from the list of live nodes and
returned.
Add_node0 to delete and add a live node from or to the list of live nodes.
Add_node(x)adds the new live node x to the list of live nodes. The list of live nodes be
implemented as a min-heap.
stack

Delete an element from stack. i,e node 6. Generate child of node 6, i,e 12, which is the BOUNDING
answer node, so search process terminates. A branch and bound method searches a state space tree using any search mechanism in
which all the children of the E-node are generated before another node becomes the E-
LCLeast Cost) Branch and Bound Search
I n both FFO and LIFO Brunch and Bound the selection rnules for the next E-node in rigid node.
and blind. The selection rule for the next E-node does not give any A good bounding helps to prune (reduce) efficiently the tree, leading to & faster
preferences to a node that has of
a very good chance ofgetting the scarch to an answer node quickly. exploration space. Each time a new answer node is found, the value of
the solution
In this we wil use ranking function orcost function. We generate the children of E-node, among upper can be updated.
these live nodes; we select a node which has minimum cost. By using ranking function we will Branch and bound algorithms are used for optimization problem where we deal directly
calculate the cost of each node. only with minimization problems. A maximization problem is easily converted toa
minimization problem by changing the sign of the objective function.

APPLICATION; O/1 KNAPSACK PROBLEM (LCBB)


L-2 There aren objects given and capacity of knapsack is M. Select some objects to fill the
knapsack in such a way that it should not exceedthe capacity of Knapsack and maxinmum profit
can be carned. The Knapsack problem is maximization problem It means we will always seckk
for maximum pix1 (where pi represents profit of object xi).
A branch bound technique is used to find solution to the knapsack problem. But we
cannot directly apply the branch and bound technique to the knapsack problem. Because the
branch bound deals only the minimization problems. We modify the
L L-S
minimization problem. The modifies problem is,
knapsack problem to the

Leo
R O O0 minimiz 2Piti
L-S
snbject to
r, `
Initially we will takc node I as E-node. Generate children of node 1, the children are 2, 3,
= 0 or 1, 1 < i n
4. By using ranking function we will
calculate thecost of 2, 3, 4 nodes is =2, =3, ë 4
respectively. Now we will select a node which has minimum cost i,e node 2. For node 2, the
children are 5, 6. Between 5 and 6 we will seclect the node 6 since its cost minimum. Generate
sclect 12 since its cost (=I) is minimum. More
children of node 6 i,e 12 and 13. We will node
over 12 is the answer node. So, we terminate scarch process.
Control Abstraction for LC-search
in Ifx in
Let t be a state space tree and c() a cost for
function the nodes t. is node t, then
a
c(x) is the minimum cost of any answer node in the sub trec with root x. Thus, c(t) is the cost ofa
minimum-cost answer node in t.
LC search uses &to find an answer node. The algorithm uses two functions

blog: anilkumarprathipati wordpresS com blog: anilkumarprathipati wordpress.com


UNIT-VI BRANCH AND BOUND UNIT-VI BRANCH AND BOUND

Algoritha Reduce(p. w, n, m, I1, 72) For node 2, x=1, means we should place first item in the knapsack.
Variables are described in the discussion. U=10+10+12=32, make it as -32
// as
L=10+10*12+ (3/9*|8) = 32+638, we make t as -38
/ Pil/wlij 2 pli + 1i/wji t l}, 1<i<n. For node 3, x1=0, means we should not place first item in the knapsack.
U10+12=22, make it as -22
I1:= 12 :=0; L=10+12+ (5/9*18) = 10+12+10=32, we make it as -32

:Lbb(9, 0); 32
klargest j such that wj1|+ +ui <n;
for i = 1 to k do
-3
if (Ubb 0. (i})< g) >thenthen
I1=Iufi}: 2
else if (Lbb(0, {#}) y) q= Lbb{0,{i}; 2
for i= k +1 to n do Upper mumber = L
Lower number = U

if (Ubb({i}. P) < g) then /2 12u {i}; Next we will calculate difference ofupper bound and lower bound for nodes 2,3
else if (Lbb{{i},0) > q) then q :=Lbb({i},); For node 2, U-L=-32+38=6
For node 3, U-L=-22+32=10
Choose node 2, since it has minimum difference value of 6,

Algorithm: KNAPSACK PROBLEM


Erample: Consider the instance M=15, n=4, (P1, Pz», P3, pe) = 10, 10, 12, 18 and (wi, W2, ws

wa)-(2,4, 6, 9).
Solution: knapsack problem can be solved by using branch and bound technique. In this problem
we will calculate lower bound and upper bound for each node by weight ratio. Ater that,
tem profits and weights with respect profit
the of
Arange
place the first item in the knapsack. Remaining weight of knapsack is l5-2=13. Place nexttem
W2 m knapsack and the remaining weight of knapsack is 13-49, Place next item wJ, in knapsack
then the remaining weight of knapsack is 9-6-3. No fraction are allowed in calculation of upper -36
bound so Wa, cannot be placed in knapsack. 22
Proiit pitp2t p3,=10+10+12
So, Upper bound=32
since fractions allowed in Upper umber = L
To caculate Lower bound we can place W4 in knapsack are

calculation of lower bound. Lower number = UJ


Lower bound=10+10+12+ (3/9*18)-32+6-38 Now we will calculate lower bound and upper bound of node 4 and 5. Calculate difference of
for only
Knapsack is maximization problem but branch bound technique applicable
is
lower and upper bound of nodes 4 and 5.
problems. In order to convert maximization problem into minimization problem
we
minimization For node 4, U-L=-32+38-6
have to take negative sign for upper bound and lower bound. For node S, U-L=-22+36=14
Therefore, upper bound (U) =-32 Choose node 4, since it has minimum difference value of 6
Lower bound (L)=-38
We choose the path, which has minimized difference of upper
bound and lower bound. If
we choose the path by comparing upper bounds
and we discard node
the difference is equal then
with maximum upper bound.
Now we will calculate upper bound and lower bound for
nodes 2, 3

6
blog: anilkumarpra thipati wordpress.com
blog: anilkumarprathipati wordpress.com
BRANCH AND BOUND
UNIT-VI
UNIT-VI1 BRANCH AND BOUND

bound of node 8 and 9. Calculate difference of


Now we will calculate lower bound and upper
8 and 9.
lower and upper bound of nodes
_12 For node 8, U-L=-38+38=0

For node 9, U-L=20+20=0


of nodes 8 and 9. Discard the
Here, the difference is same, so compare upper bounds
9 since, it has n a x imum
32 Choose node 8, discard node
node, which has maximum upper bound.
22 upper bound.
Consider the path from 1>2947>8
X =l
X2=1
X3=0
X=l
is ( xI, X2, X3, K4HI, I,0, )
knapsack problem
The solution for 0/1
Maximum profit is:
-38,
-32 -38
pixi-10l+10°1+12*0+18°1
10+10+18=38.
6
L
Upper nunmber =

Lower number = U FIFO Branch-and-Bound Solution


Now, let us trace through the FIFOBB algorithm using the same knapsack instance as in
Now we will cakculate lower bound and upper bound of node 6 and 7. Calculate difference of above Example. Initially the root node, node 1 of following Figure, is the E-node and the queue
kower and upper bound of nodes 6 and 7. of live nodes is empty. Since this is not a solution node, upper is initialized to u() = -32. We
For node 6, U-L-32+38=6 the of left to
For node 7, U-L=-38+38=0 assume children a node generated
that
are
value of
2
right. Nodes and 3
are generatedand added

Choose node 7, sinceit has minimun difference value of 0.


to the queue (in order). The upper remains unchanged.
node. Its children, nodes 4 and 5, are generated and added to the
Node 2 becomes the next E-
queuc.
-332 Node 3, the next-node, is expanded. Its children nodes are
to the queue. Node 7 is
generated; Node 6 gets added
immediately killed as L (7) > upper. Node 4 is
and 9 are generated and added to the queue. Then Upper is
expanded next. Nodes 8
updated to u(9) -38, Nodes 5 and 6
=

are the next two nodes to B-nodes. Neither is expanded as for each, L >
-38 become Node 8 is
the next E-node. Nodes 10 and Il are generated; Node 10 is infeasible and so upper.killed. Node 11
-32 has L
(11)>upper and so is also
When node 12 is generated,
killed. Node 9 is expanded next.
12
Upper and ans are updated to -38 and 12 respectively. Node
joins the
queue of live nodes. Node 13 is killed before it can get onto the
-38. L (13)> upper. The
queue of live nodes
-32 22 as
only remaining live node is node 12. It has no
children and the search
terminates. The value ot upper and the path rom node 12 to the root is
4 output. So solution is
X=1, X2=1, X3=0, X4=1.
38 38
-32 38

-38, 20
-SR K20
Upper number =

Lower umber = U

blog anilkumarprathpati wordpress co


iog anknoprsthpi ROrdpress com
BRANCH AND BOUND
UNIT-V1
BRANCH AND BOUND
=
UNIT-VI1
2. Calculate the reduced cost matrix for every node.
entries in i and column j ofA to a.
a) If path (,j) is considered then change all
row

b) Set AG,1) to a.
reduction and column reduction except for rows and columns containing
c) Apply row
to reduce the matrix.
only a. Let r is the total amount subtracted
d) Find (S)= ¢(R)+A(L)+r.
-38
2 until all nodes are visited.
Repeat step
-32
Example: Find the LC branch and bound solution for
cost matrix IS as tollows.
the travelling sales person problem whose
o 6
20 30 10 11
16 4 2
3
22 S 2 4
A Thecost matrix is3
19 6 18
16 4 7 16

Step I: Find the reduced cost matr

method:
-20 Apply now reduction
from all values in the 1 row.
Deduct 10 (which is the minimum)
-38 -20 Deduct 2 (which is the minimum) from all values n the 2 row,

10 12 13 Deduct 2 (which is the minimum) from all values n the 3 r o w .

is the minimum) from all values in the 4


roow.
Deduct 3 (which
LL values in the 5 row.
riunber (which is the minimum) from all
=
uper Deduct 4
lower number = U
142 0
PERsON PROBLEM reduced cost matrix I3
APPLICATON: TRAVELLING SALES an instance of the traveling salesperson
The resulting row wise
3
Let G (V, E) be a directed graph defining
= E, and let IVM =n, without loss of 3 12
Cij equal the cost of edge (i, j), Cij= if (i, j)
o 2
problem. Let vertex 1.
we can assume that every tour starts and ends at Row wise reduction sum= 10+2+2+3+4=21.
generality,
above matix:
Now apply column reduction for
the
Procedure for solving travelling sales person problem
is re uced. This
Deduct 1 (which is the minimum) from all values in the column.
row and column
Deduct 3 (which is the minimum) from all
values in the 2 column.
1. Reduce the given cost matrix. A matrix is reduced if every co 10 1 7
can be done as lollows 12 11 2 0
Row Reduction: first row, subtract it from all elements of first row,
next
column wise reduced cost matrix (A)= 3
a) Take the minimum element from Similarly The resulting 5 3 12 0D
the second row and subtract it
fYom second row.
take minmum
element from 11 0 3 12
for all rows.
apply the same procedure rows. Column WIse reduction sum =It0+3+0+0=4.
b) Find the sum which
of elements, for
were
subtracted from reduction. wise reduction + column wise reduction sum.
the matrix obtained after row
=

C) APply column reductions Cumulative reduced sum row


4=25. cost matrix.
Column Reduction:
minimum element from first coumn, subtract it
from all elements of first
This is the cost of a root this is the
i.e. node 1, becausebound initially reduced
d) Take the subtract it from second ,
take minimum element from
the second column and The lower bound for node is 25 and
upper is
column, next next visit 2, 3, 4 and 5 vertices. So, consider to explore the paths (1,
the procedure
same for all columns. Starting from node 1, we can

column. Similarly apply subtracted from columns 2), (1,3), (1, 4, (1,5).
of elements, which were
e) Find thethesum
cumulative sum of row wise
reduction and column wise reduction. The tree organization up to this as follows;
t) Obtain sum.
sum=Row wise reduction s u m + C o l u m n wise reduction Variable I indicate the next node to visit.
Cumulative reduced anda as upper
the starting state as lower bound
reduced sum to
Associate the cumulative

bound. blog: anilkumarprathpati.wordpress.com

com
biog' anilkumarprathipati wordpress.
UNIT-VI BRANCH AND BOUND BRANCH AND BOUND
UNIT-VI

Step 2
Now comsider the path (1, 2)
Change all entries ofrow 1 and colurmn 4 ofA to o and also set A(4,1) to o.

Change all entries ofrow I and column 2 ofA to n and also set A (2, 1) to oo.
12 0
03 2
11 2 0 3 12 0
0 0
0 2 I1 0 0
15 12 0 Apply row and column reduction for the rows and columns whose rows and column are not
11 0 12
Apply row and column reduction for the rows and columns whose rows and column are not completely
completely . Then the resultant matrix is 12 11 o 0

Then theresultant matrix is =


0 3 2
X)
3 12 0
11 0 0
Row reduction sum = 00

Column reduction sum =0


Cumulative reduction(r) =0 +0=0
Row reduction sum=0 +0 +0+0 =0 Therefore, as c(S)= {R)+A(1,4)+T
Column reduction sum =0 +0+0+0=0 (S)=25 +0 +025.
Cumulative reduction(r) Now Consider the path (1, 5)
0+0=0
=

Change all entries of row I and column 5 ofA to o and also set A(5,1) to o
There fore, as (S)= ¢(R)+A(1,2)+r > CS= 25 + 10 -0 35.
Now consider the path (1, 3)
Change all entries of row I and column 3 of A to o and also set A (3, 1) to o. 12 11 2
0 3 0
12 20 15 3 12
2 0 0 12
15 3 0 Apply row and column reduction for the
completelyyo
rows and columns whose rows and column are not

1I 0 12

Apply row and column reduction for the rows and columns whose rows and column are not 10 a 9 0

completely Then the resultant matrix is =


0 3 0
12 0 9
2 0
0 0 12
Row reduction sum =5
Then the resultant matrix is =
3 0 2 Column reduction sum = 0
4 3
Cumulative reduction(r)=5 +0=0
0 0 12
Therefore, as (S)= (R)*A(1,5)+r
Row reduction suin =0
Coumn recduction surn= 11 (S)- 25+1+5 31.
The tree organizarion up to this as follows:
Cumulative reduction(r) 0 +11=F11
=

Thercfore, as (S)= (R}+A(1,3)+r


25 (1)
c(S)= 25 +t 17 +11 = 53.
i =2 i-3 i4
35 2 53(3) )25 31
Numbers outside the node are ê values

Now consider the path (1, 4)

blog: anilkumirprathipati.noradpress. rom 11 blog: anilkumarprathipati wordpress.com 12


UNIT-V1
BRANCH AND BOUND
The cost of UNIT-VI
the between (1, 2) 35,
between (1, 4) is minimum Hence (1, 3) 53, ( 1, 4) 25, (1, 5) 31. The cost
=
=
BRANCH AND BOUND
path
=
=

the matrix obtained for of


path (1, 4) is considered asthe
reduced cost matrix. Therefore, as c(S)= (R+A(4,3)+r
CS}= 25 + 12 +13 50.
Now consider the
12 path (4, 5)
A= 0
Change all entries of row 4 and column 5 ofA to
3 a 2
o and also set A(5,1) to o.
12 0 12
The new possible
paths are (4, 2). (4, 3) and (4, 5).
Now consider the
path (4, 2)
Change all entries of row 4 and column 2 o 0 0
of A
to o and also set A(2,1) toco. Apply row and column reduction for the rows
o
and coumns whose
completely oo rows and column are not

Then the resultant matrix is = 0 3


11 o 0
Apply row and co lumn reduction for the rows and columns whose rows and
column o 0 0
completely are not
Row reduction sum =l1
Column reduction sum = 0
Cumulative reduction(r) = 11 +0=Fll
Then the resutant matrix is = Therefore,
0c 2 as c(S)= E(R}+A(4,5)tr
c(S)= 25 +0+11 =36.
The tree organization up to this as
Row reduction sum=0 follows:
Cohumn reduction sum = 0 25(1
Cuulative reduction(r) = 0 +0-0
Therefore, as (S)= &{R}+A(4,2)+ i=2 i = 3 / i=4 i=5
(S 25 +3 +0 28.

Now consider the path (4, 3) 35(2 53(3 4)25 (5)31


Change all entries of row 4 and column 3 of A to o and also set A(3,1) to co.
i2=2 i=3 i=5
12 0
3 o o 2 28(6) 507) (8 ) 36
Numbers outside the node are values
Apply row and colurnn reduction for the rows and columns whose rows and column are not
The cost of the between (4, 2) 28, (4, 3)= 50,
completely
=
(4, 5) 36. The cost of the path between
=

(4, 2) is minimum. Hence the matrix obtained for path (4, 2) is considered as reduced cost
matrix.

Then the resultant matrix is= cco 11 0 0


A= 0 00
0
Row reduction
sum
Column reduction sum = 11
=2
The new possible paths are (2, 3) and (2, 5).
Cumulative reduction(r) =2 +11=13
blog: anilkumarprathipati.wordpress,.com 13 blog: aniikumarprathipatiwordpress.com A
BRANCH AND BOUND
UNIT-VI
UNIT-V1 BRANCH AND BOUND

Now Consider the path (2 3):


25 (1
Change all entries of row 2 and column 3 ofA to o and also set A(3,1) to a.
i=5
i=2 3 ij=4
2
53(3 (4)25 5)31
352
Apply row and column reduction for the rows and coumns whose rows and column are not 22 i=3 i2=5
completely (8) 36
28(6 507)
Then the resultant matrix is= i=3 i=5
0
10 28
Row reduction sum =13
Cotumn reduction sum = 0
529
Cumulative re uction(r) = 13 +0-13 Numbers outside the node are values
Therefore, as &(S)= CR)rA(23)T (2. 3) 52 and (2, 5) 28. The cost of the path between (2, 5)is
=

of the between
=

The cost
(S}28+ 11 +13 =52. minimum. Hence the matrix obtained for path (2, 5) is considered as reduced cost matrix.

A
Now Consider the path (2,2 and column 5 of A to and also set A(5,1) to .
0
Change all entriesof row The new possible path is (5, 3).

Now consider the path (5, 3):


Change all entries of row 5 and cohumn 3 of A to o and also set A(3,1) to o. Apply row and
coumn reduction for the rows and coh1mns whose rows and cohumn are not completely o
and coumn are not
and column reduction for he rows and columns whose rows
Apply row

completely Then the resutant matrix is =

Then the resultant matrix is


=

Row reduction sum 0


Column reduction sum= 0
Cumulative reduction(r) = 0 +0=0
Row reduction sum ==0
C o h u n reduction s u m = 0 Therefore, as (S= d(R)+A(5,3)tr
Cumulative reductionfr) = 0 +0=0 (S28 0 +0 =28.
(S)= 28 +0+0= 28. The path travelling sales person problem is:
Therefore, as (S}F {R)+A{2.5)T
The tree organization p to this as follows 14-25-31:
The minimum cost of the path is: 10-2+6+7+3=28.

bicg: anikumarpra thipati.wordpress.co 6


15
blag ariikumarprathipati wordpress.com
UNIT-VI1
BRANCH AND BOUND
The overall tree
organization is as
follows:
25 1
-2 i=3 i4
35(2 53(3 25 5)31
i i2=3 i=5
28 (6)
50(7) (8 ) 36
i=3 i=5
52 (9
(10)28
i4=3

2811)
Numbers outside the node are values

, E3

-
3

blog: anilkurarprathipati.wordpress.com
17

You might also like