UNIT 3 daa
UNIT 3 daa
Backtracking: General method, applications-n-queen problem, sum of subsets problem, graph coloring,
Hamiltonian cycles. Branch and Bound: knapsack problem-Traveling salesperson problem.
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
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
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)
si ->{1, 2, 3, 4, 5, 6, 7, 8}, 1 ≤ i ≤8
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
Wiweight of item i
MCapacity of bag (subset)
Xithe 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:
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
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 nodeis 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.