This document discusses backtracking and branch and bound algorithms. It provides examples of problems that can be solved using these techniques, including the 8-queen problem, sum of subsets, graph coloring, Hamiltonian cycles, traveling salesperson problem, and knapsack problem. It also describes the general methods for backtracking and branch and bound, including generating partial solutions and abandoning solutions that cannot be completed.
This document discusses backtracking and branch and bound algorithms. It provides examples of problems that can be solved using these techniques, including the 8-queen problem, sum of subsets, graph coloring, Hamiltonian cycles, traveling salesperson problem, and knapsack problem. It also describes the general methods for backtracking and branch and bound, including generating partial solutions and abandoning solutions that cannot be completed.
This document discusses backtracking and branch and bound algorithms. It provides examples of problems that can be solved using these techniques, including the 8-queen problem, sum of subsets, graph coloring, Hamiltonian cycles, traveling salesperson problem, and knapsack problem. It also describes the general methods for backtracking and branch and bound, including generating partial solutions and abandoning solutions that cannot be completed.
This document discusses backtracking and branch and bound algorithms. It provides examples of problems that can be solved using these techniques, including the 8-queen problem, sum of subsets, graph coloring, Hamiltonian cycles, traveling salesperson problem, and knapsack problem. It also describes the general methods for backtracking and branch and bound, including generating partial solutions and abandoning solutions that cannot be completed.
Download as DOC, PDF, TXT or read online from Scribd
Download as doc, pdf, or txt
You are on page 1of 5
UNIT VI
Backtracking: General method, applications-8-queen problem, sum of subsets, graph coloring,
Hamiltonian cycles Backtracking - General method: Backtracking is a general algorithm for finding all (or some) solutions to some computational problem that incrementally builds candidates to the solutions, and abandons each partial candidate c (back tracks!) as soon as it determines that c cannot possibly be completed to a "alid solution# Back tracking algorithms try each possibility until they find the right one# $t is a depth-first search of the set of possible solutions# %uring the search, if an alternati"e does not &ork, the search backtracks to the choice point, the place &hich presented different alternati"es, and tries the ne't alternati"e# (hen the alternati"es are e'hausted, the search returns to the pre"ious choice point and tries the ne't alternati"e there# $f there are no more choice points, the search fails. Applications: Back tracking method is used to sol"e the follo&ing problems: 8-queen problem, sum of subsets, graph coloring, Hamiltonian cycles 8-queen problem (n-)ueens *roblem): n queens should be placed in an n'n chess board, in such a &ay that no queen can attack another queen# +he follo&ing algorithm computes solution to n-)ueens problem# ,# -irst suppose that e"ery ne& queen &ill be placed in a ne& ro&# +his ensures that no queen can attack another queen ro&-&ise# +hus n queens can be placed in n different ro& positions# .# /o&, &e ha"e to calculate the column positions of these n queens# +his can be done in the follo&ing &ay# .#, -irst place, first queen in first ro& and first column# .#. 0alculate the column position of the ne't queen in ne't ro&, in the follo&ing &ay# *lace it in the first column of the ne& ro&# $f it collides &ith any of the pre"iously placed queens (either column-&ise or diagonal-&ise) , then, mo"e it one step right# $f it goes out of the board, mo"e pre"ious queen one step right, and goto step .#.# $f queen can be placed in a particular column in that ro&, &ithout any collision, then goto step .#.# $f all n queens are placed in n different column positions, then 1+2*# 2ther&ise goto step .#.# 3 queen collides &ith another queen, column-&ise, if the column positions of the both queens are same# 3 queen collides &ith another queen, diagonal-&ise, if 4%ifference of column positions454%ifference of ro& positions Ho& to sol"e n )ueens *roblem using backtracking: ,# *lace ith queen in first possible column position in ith ro&# .# $f you are not able to place ith queen in any column position in ith ro&, then, backtrack and mo"e (i-,)th queen in (i-,)th ro& one column right# 6# 0ontinue this (steps , and .) until all n queens are placed in n different column positions# Sum of Subsets: Gi"en a set 1 of n ob7ects &ith &eights (&,,&.,8,&n) and a positi"e integer 9,&e ha"e to find a subset 1: of the gi"en set 1, such that, the sum of the elements of subset 1: is equal to 9# -or e'ample, if a gi"en set 1 5 (,, ., 6, ;) and 9 5 <, then there e'ists sets 1=5 (6, .) and 1!5(,, ;) &hose sum is equal to 9# $t can also be noted that some instance of the problem does not ha"e any solution# -or e'ample, if a gi"en set 8 5 (,, 6, <) and 9 5 >, then no subset occurs for &hich the sum is equal to 9 5 ># ,
Ho& to sol"e 1um of 1ubsets *roblem using backtracking: ?et (5 (eight added so far to the subset and +5 +otal remaining &eight in the gi"en set, then, @"ery node in the partial solution state space tree is represented as ((,i,+) 3 node at le"el i, is promising, iff, (A&i m and (A+ m $f at any node, (5m, then the path from root to that node is the solution# $f a node is not promising, then, &e backtrack from that node to its pre"ious node Graph Coloring: Graph coloring is a &ay of coloring the "ertices of a graph such that no t&o ad7acent "ertices share the same color#+he minimum number of colors required to color a graph in this &ay is called its chromatic number# Ho& to sol"e graph coloring problem using backtracking:: 0hromatic number of a graph can be obtained by using the follo&ing algorithm# ,# $nitialiBe cn to ,# CC cn 5 chromatic number .# 3pply cn to "erte' , 6# cc5cnD CC cc5current color ;# &hile(cc can=t be applied to ne't un colored "erte') do cc5cc-,# CCbacktrack <# $f(cc
E) F3pply cc to that un colored "erte'D goto 6DG
H# @lse $f(there are any uncolored "ertices) Fcn5cnA,D goto 6DG ># *rint( 0hromatic number 5!, cn) 8# 1top# /2+@: 3 particular color can=t be applied to a particular "erte', if that "erte' is ad7acent to any of the "ertices that are already colored &ith that color# Hamiltonian cycles: Ho& to sol"e Hamiltonian path problem using backtracking: *roceed in any possible path, if Hamiltonian cycle is not obtained, then backtrack and take di"ersion# PROBLEMS n ueens Problem ,# 1ol"e n queens problem &hen (i) n5; (ii) n5< (iii) n58 Sum of Subsets ,# ?et &5F,,.,6G and m56# -ind all subsets of the set & that sum to m# .# ?et &5F>,,,,,6,.;G and m56,# -ind all subsets of the set & that sum to m# 6# ?et &5F<,,E,,.,,6,,<,,8G and m56E# -ind all subsets of the set & that sum to m# Graph Coloring , #-ind chromatic number for the follo&ing graphs, and sho& ho& those graphs can be colored# . -ig(i): 9ap $ts corresponding graph -ig(ii) -ig(iii) Hamiltonian cycles ,# -ind Hamiltonian cycles for the follo&ing graphs# -ig(i) -ig(ii) -ig(iii) -ig(i") -ig(") 6 !"#$ %## Branch and Bound: General method, applications - +ra"elling sales person, EC, knapsack problem- ?0 Branch and Bound solution, -$-2 Branch and Bound solution General Metho&: @-node is the node that is being e'plored currently# @'ploring a node means generating all its children (branches or li"e-nodes)# $n Branch and Bound +echnique, all branches(li"e nodes) of an @-node are generated, before one of the branches(li"e node) becomes an @-node +here are three types of Branch and Bound techniques# +hey are: ,# ?0BB .#?$-2BB 6#-$-2BB 'pplications: BB can be used to sol"e optimiBation problems such as EC, knapsack problem, tra"eling sales person problem etc# $ra(elling sales person problem)$SPP*: +1** can be sol"ed by using BB, in the follo&ing &ay: ,# 2btain reduced matri'(I) for the gi"en cost matri'# .# 0ompute bound(b) for that reduced matri'# 6# Ioot (@-node5I) consists of all possible paths# ;# ?eft child can be obtained by including a particular edge in the solution set# <# Iight child can be obtained by e'cluding that particular edge from the solution set# H# Ieduce left and right childs and compute bounds# ># +he child &ith minimum bound becomes @-node for the ne't iteration# 8# 0ontinue this until either left or right child becomes empty# J# +hen, solution can be obtained by backtracking from that empty child to root# 9ore e'planations for important steps: ,# -irst, perform ro& reduction FIo& reduction means subtract e"ery ro& &ith minimum element in that ro&# Io& reduction sum(rrs) can be obtained by adding all these minimum elements in each ro&#G +hen, for the resulting matri', perform column reduction F0olumn reduction means subtract e"ery column &ith minimum element in that column# 0olumn reduction sum(crs) can be obtained by adding all these minimum elements in each column#G +he resulting matri' is called reduced matri'K .# Bound b5rrsAcrs
6# ;# $f you include a particular edge(i,7) in the solution set, then, -delete ith ro& and 7th colunmn# -put in 7th ro& and ith column <# $f you e'clude a particular edge(i,7) in the solution set, then, -put in ith ro& and 7th column# ># /ormally, in e"ery step &e &ill be considering only left child and hence only left child becomes empty e"entually# J# (rite do&n all edges &hile you backtrack and find path by connecting them# -or e"ery E in ILi,7M, compute N(i,7)# N(i,7)#5min in ro& i A min in col 7 e'cept E in ILi,7M 0onsider that edge(i,7) for &hich you get ma'imum N(i,7) /ote: .# ; +,- .napsac. problem: Branch and Bound technique is designed for minimiBation problem# But, EC, knapsack problem is a ma'imiBation problem(as &e ha"e to ma'imiBe profit)# 1o, &e ha"e to multiply bounds(lo&er and upper) &ith -,# %o not consider fractions &hile computing upper bound(OB) 0onsider fractions &hile computing lo&er bound(?B) LCBB)Least Cost Branch an& Boun&*: +he li"e node &ith minimum(OB-?B) "alue becomes @-node in the ne't iteration# $f t&o li"e nodes ha"e same (OB-?B) "alue, then the node &ith least OB "alue becomes @-node in the ne't iteration# /#/OBB)Last #n /irst Out Branch an& Boun&*: ?et, ?OB5 ?east Opper Bound, 0OB 5 0urrent Opper Bound, P 0?B 5 0urrent ?o&er Bound, Initialize LUB to infinity. If (CUB<LUB) then LUB=CUB If(CLB>LUB) then kill that node Use Queue to hold all live nodes of E-node. L#/OBB)/irst in /irst Out Branch an& Boun&*: +his is same as ?$-2BB, e'cept that, here, &e use 1tack to hold all li"e nodes of @-node#