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

UNIT 3 daa

The document discusses backtracking and branch and bound algorithms, focusing on their applications in solving combinatorial problems such as the N-Queens problem, sum of subsets problem, graph coloring, and Hamiltonian cycles. It explains the general method of backtracking, detailing specific problems and their constraints, as well as introducing branch and bound as a systematic approach for optimization problems like the knapsack problem. The document highlights the differences between backtracking and branch and bound, emphasizing their respective use cases and methodologies.

Uploaded by

sathiya.set
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

UNIT 3 daa

The document discusses backtracking and branch and bound algorithms, focusing on their applications in solving combinatorial problems such as the N-Queens problem, sum of subsets problem, graph coloring, and Hamiltonian cycles. It explains the general method of backtracking, detailing specific problems and their constraints, as well as introducing branch and bound as a systematic approach for optimization problems like the knapsack problem. The document highlights the differences between backtracking and branch and bound, emphasizing their respective use cases and methodologies.

Uploaded by

sathiya.set
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT-III Backtracking& Branch and Bound

Backtracking: General method, applications-n-queen problem, sum of subsets problem, graph coloring,
Hamiltonian cycles. Branch and Bound: knapsack problem-Traveling salesperson problem.

1.Backtracking (General method):

Many problems are difficult to solve algorithmically. Backtracking makes it possible to solve at least some large
instances of difficult combinatorial problems.

Suppose you have to make a series of decisions among various choices, where

 You don’t have enough information to know what to choose


 Each decision leads to a new set of choices.
 Some sequence of choices ( more than one choices) may be a solution to your problem.

Applications of Backtracking:

 N Queens Problem
 Sum of subsets problem
 Graph coloring
 Hamiltonian cycles.

1.N-Queens Problem:

 It is a classic combinatorial problem. The eight queen’s puzzle is the problem of placing eight queens
puzzle is the problem of placing eight queens on an 8×8 chessboard so that no two queens attack each
other.
 That is so that no two of them are on the same row, column, or diagonal. The 8-queens puzzle is an
example of the more general n-queens problem of placing n queens on an n×n ches

Here queens can also be numbered 1 through 8

Each queen must be on a different row Assume queen ‘i’ is to be placed on row ‘i’

All solutions to the 8-queens problem can therefore be represented a s s-tuples(x1, x2, x3—x8)

xi->the column on which queen ‘i’ is placed

si ->{1, 2, 3, 4, 5, 6, 7, 8}, 1 ≤ i ≤8

Therefore the solution space consists of 88 s-tuples.


 The implicit constraints for this problem are that no two xi’s can be the same column and no two queens
can be on the same diagonal.
 By these two constraints the size of solution pace reduces from 88 tuples to 8! Tuples. Form example
si(4,6,8,2,7,1,3,5)

Inthesamewayforn-queensaretobeplacedon ann×nchessboard,thesolutionspaceconsists of all n!


Permutations of n-tuples (1,2,-------------------------------n
Some solution to the8-Queens proble

Algorithmfornewqueenbeplaced Allsolutionstothen·queens problem


AlgorithmPlace(k,i) AlgorithmNQueens(k,n)
//Returntrueifaqueencanbeplacedinkth row & ith //itsprintsallpossibleplacementsofn- queens on an n×n
column chessboard.
//Otherwisereturn false {
{ fori:=1tondo{ ifPla
forj:=1 tok-1 do ce(k,i)then
if(x[j]=iorAbs(x[j]-i)=Abs(j-k))) then return { X[k]:=I;
false if(k==n)thenwrite(x[1:n]); else
returntrue NQueens(k+1, n);
} }
}}

Sum of Subsets Problem:


Definition: Given n distinct +ve numbers (usually called weights), desire (want) to find all combinations of these
numbers whose sums are m. this is called sum of subsets problem.
To formulate this problem by using either fixed sized tuples or variable sized tuples.
Backtracking solution uses the fixed size tuple strategy.
For example:
If n=4 (w1, w2, w3, w4)=(11,13,24,7) and m=31.
Then desired subsets are (11, 13, 7) & (24, 7).
The two solutions are described by the vectors (1, 2, 4) and (3, 4).
In general all solution are k-tuples (x1, x2, x3---xk) 1 ≤ k ≤ n, different solutions may have different sized tuples.
Explicit constraints requires xi ∈ {j / j is an integer 1 ≤ j ≤ n }
Implicit constraints requires:

No two be the same & that the sum of the corresponding wi’s be m
i.e., (1, 2, 4) & (1, 4, 2) represents the same. Another constraint is xi<xi+1 1 ≤ i ≤ k
Wiweight of item i
MCapacity of bag (subset)
Xithe element of the solution vector is either one or zero.
Xi value depending on whether the weight wi is included or not.
If Xi=1 then wi is chosen.
If Xi=0 then wi is not chosen.

The above equation specify that x1, x2, x3, --- xk cannot lead to an answer node if this condition is
not satisfied.
The equation cannot lead to solution

Graph Coloring:
Let G be a undirected graph and ‘m’ be a given +ve integer. The graph coloring problem is assigning colors to the
vertices of an undirected graph with the restriction that no two adjacent vertices are assigned the same color yet only
‘m’ colors are used.
The optimization version calls for coloring a graph using the minimum number of coloring.
The decision version, known as K-coloring asks whether a graph is colourable using at most k-colors.
Note that, if ‘d’ is the degree of the given graph then it can be colored with ‘d+1’ colors.
The m- colorability optimization problem asks for the smallest integer ‘m’ for which the graph G can be colored.
This integer is referred as “Chromatic number” of the graph.
Example:

Above graph can be colored with 3 colors 1, 2, & 3.


The color of each node is indicated next to it.
3-colors are needed to color this graph and hence this graph’ Chromatic Number is 3.
A graph is said to be planar iff it can be drawn in a plane (flat) in such a way that no two edges cross each other.
Example:2
Adjacency matrix is

Solution:

Hamiltonian Cycles:
Def: Let G=(V, E) be a connected graph with n vertices. A Hamiltonian cycle is a round trip path along n-edges
of G that visits every vertex once & returns to its starting position.
It is also called the Hamiltonian circuit.
Hamiltonian circuit is a graph cycle (i.e., closed loop) through a graph that visits each node exactly once.
A graph possessing a Hamiltonian cycle is said to be Hamiltonian graph.

Example:
In graph G, Hamiltonian cycle begins at some vertiex v1 ∈ G and the vertices of G are visited in

the order v1,v2,---vn+1, then the edges (vi, vi+1) are in E, 1 ≤ i ≤ n.

The above graph contains Hamiltonian cycle: 1,2,8,7,6,5,4,3,1

The above graph contains no Hamiltonian cycles.


There is no known easy way to determine whether a given graph contains a Hamiltonian cycle.
By using backtracking method, it can be possible
Backtracking algorithm, that finds all the Hamiltonian cycles in a graph.
The graph may be directed or undirected. Only distinct cycles are output.

Branch & Bound:

Branch & Bound (B & B) is general algorithm (or Systematic method) for finding optimal solution of various
optimization problems, especially in discrete and combinatorial optimization.
The B&B strategy is very similar to backtracking in that a state space tree is used to solve a problem.
The differences are that the B&B method
Does not limit us to any particular way of traversing the tree.
It is used only for optimization problem
It is applicable to a wide variety of discrete combinatorial problem.
B&B is rather general optimization technique that applies where the greedy method & dynamic programming
fail.

The term B&B refers to all state space search methods in which all children of the “E-node” are generated before
any other “live node” can become the “E-node”
Live nodeis a node that has been generated but whose children have not yet been generated.
E-node:is a live node whose children are currently being explored.
Dead node:is a generated node that is not to be expanded or explored any further. All children of a dead node have
already been expanded

BFS:like state space search will be called FIFO (First In First Out) search as the list of live nodes is “First-in-first-
out” list (or queue).
D-search (DFS);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).

Knapsack Problem:
Consider the instance: M = 15, n = 4, (P1, P2, P3, P4) = (10, 10, 12, 18) and (w1, w2, w3, w4) = ( 2, 4, 6, 9).
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.
Place first item in knapsack. Remaining weight of knapsack is 15 – 2 = 13. Place next item w2 in knapsack and the
remaining weight of knapsack is 13 – 4 = 9.
Place next item w3 in knapsack then the remaining weight of knapsack is 9 – 6 = 3. No fractions are allowed in
calculation of upper bound so w4 cannot be placed in knapsack.
Profit = P1 + P2 + P3 = 10 + 10 + 12 So,
Upper bound = 32
To calculate lower bound we can place w4 in knapsack since fractions are allowed in calculation of lower bound.
Lower bound = 10 + 10 + 12 + ( 3 X 18) = 32 + 6 = 38
9
Knapsack problem is maximization problem but branch and bound technique is applicable for only minimization
problems. In order to convert maximization problem into minimization problem we have to take negative sign for
upper bound and lower bound.

You might also like