0% found this document useful (0 votes)
11 views74 pages

Alg Unit-4

The document covers advanced algorithmic techniques including backtracking and branch-and-bound, focusing on problems such as the n-Queens problem, Hamiltonian circuit, subset-sum, and various optimization problems like the 15-puzzle, assignment, knapsack, and traveling salesman problem. It explains the principles of constructing solutions through state-space trees, evaluating promising nodes, and pruning non-promising branches. Additionally, it includes experiential learning activities to engage students in these concepts through board games.

Uploaded by

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

Alg Unit-4

The document covers advanced algorithmic techniques including backtracking and branch-and-bound, focusing on problems such as the n-Queens problem, Hamiltonian circuit, subset-sum, and various optimization problems like the 15-puzzle, assignment, knapsack, and traveling salesman problem. It explains the principles of constructing solutions through state-space trees, evaluating promising nodes, and pruning non-promising branches. Additionally, it includes experiential learning activities to engage students in these concepts through board games.

Uploaded by

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

UNIT IV : BACKTRACKING AND BRANCH &

BOUND
• Backtracking: nQueens problem (L3) - Hamiltonian
Circuit Problem (L3) - Subset Sum Problem (L3) -
Branch and Bound: Solving 15 Puzzle problem (L3) -
Assignment problem (L3) - Knapsack Problem (L3) -
Travelling Salesman Problem (TSP) (L3).

• Experiential Learning (Not for Examination)


Create physical or virtual board games simulating the n-
Queens problem or Hamiltonian circuits where students
play the role of decision-makers.
BACKTRACKING
• Elements of Backtracking
• nQueens problem
• Hamiltonian Circuit Problem
• Subset Sum Problem
Backtracking
3

 The principal idea of backtracking is to construct solutions one


component at a time and evaluate such partially constructed
candidates as follows.
 If a partially constructed solution can be developed further
without violating the problem’s constraints, it is done by taking
the first remaining legitimate option for the next component.
 If there is no legitimate option for the next component, no
alternatives for any remaining component need to be
considered.
 In this case, the algorithm backtracks to replace the last
component of the partially constructed solution with its next
option.
State-space tree
4

 Its root represents an initial state before the search


for a solution begins.
 The nodes of the first level in the tree represent the choices
made for the first component of a solution, the nodes of
the second level represent the choices for the second
component, and so on.
 A node in a state-space tree is said to be promising if it
corresponds to a partially constructed solution that may still
lead to a complete solution; Otherwise it is called non-
promising.
State-space tree
5

 Leaves represent either non-promising dead ends or complete


solutions found by the algorithm.
 In the majority of cases, a state-space tree for a backtracking
algorithm is constructed in the manner of depth first search.
 If the current node is promising, its child is generated by
adding the first remaining legitimate option for the next
component of a solution, and the processing moves to this child.
 If the current node turns out to be non promising, the algorithm
backtracks to the node’s parent to consider the next possible
option for its last component; if there is no such option, it
backtracks one more level up the tree, and so on.
 Finally, if the algorithm reaches a complete solution to the
problem, it either stops (if just one solution is required) or
continues searching for other possible solutions.
n-Queens Problem
6

 The problem is to place n queens on an n × n chessboard so that no


two queens attack each other by being in the same row or in the
same column or on the same diagonal.
4-Queens Problem
7

 For n = 4, There is solution to place 4 queens in 4 × 4


chessboard. the four-queens problem solved by the backtracking
technique.
4-Queens Problem
8
4-Queens Problem
9
4-Queens Problem
10
4-Queens Problem
11
8-Queens Problem
12
HAMILTONIAN CIRCUIT PROBLEM
13

 A Hamiltonian circuit (also called a Hamiltonian cycle,


Hamilton cycle, or Hamilton 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 a
Hamiltonian graph.
Hamilton Circuit Example
14
Find Hamilton cycle from vertex 1 using backtracking
15
Subset-Sum Problem
16

 The subset-sum problem finds a subset of a given set A =


{a1, . . . , an} of n positive integers whose sum is equal to a
given positive integer d.
 For example, for A = {1, 2, 5, 6, 8} and d = 9, there are
two solutions: {1, 2, 6} and {1, 8}. Of course, some
instances of this problem may have no solutions.
 We will assume that a1< a2 < . . . < an.
 A = {3, 5, 6, 7} and d = 15 of the subset-sum problem.
 The number inside a node is the sum of the elements
already included in the subsets represented by the node.
State Space Tree Construction
17

 The state-space tree can be constructed as a binary


tree
Example
18
Practice
19

Construct the state space tree for the given subset sum
problem
A = {1, 2, 5, 6, 8} and d = 9
BRANCH AND BOUND

 Elements of Branch and Bound


 Solving 15 Puzzle problem
 Assignment problem
 Knapsack Problem
 Travelling Salesman Problem (TSP)
BRANCH AND BOUND
3

 An optimization problem seeks to minimize or maximize


some objective function, usually subject to some
constraints.
 An optimal solution is a feasible solution with the
best value of the objective function.
 Compared to backtracking, branch-and-
bound requires two additional items:
🞑a way to provide, for every node of a state-space tree, a bound
on the best value of the objective function on any solution that
can be obtained by adding further components to the partially
constructed solution represented by the node.
(This bound should be a lower bound for a minimization problem
and an upper bound for a maximization Problem)
BRANCH AND
BOUND
4

 If this information is available, we can compare a node’s


bound value with the value of the best solution seen so
far.
 If the bound value is not better than the value of the best
solution seen so far—i.e., not smaller for a minimization
problem and not larger for a maximization problem—the
node is non-promising and can be terminated (some
people say the branch is “pruned”).
 Indeed, no solution obtained from it can yield a better
solution than the one already available.
 This is the principal idea of the branch-and-bound
technique.
BRANCH AND
BOUND
5

 In general, we terminate a search path at the current


node in a state-space tree of a branch and bound
algorithm for any one of the following three reasons:
 1. The value of the node’s bound is not better than the
value of the best solution seen so far.
 2. The node represents no feasible solutions because
the constraints of the problem are already violated.
 3. The subset of feasible solutions represented by the
node consists of a single point (and hence no further
choices can be made)—in this case, we compare the
value of the objective function for this feasible solution
with that of the best solution seen so far and update the
latter with the former if the new solution is better.
Solving 15-Puzzle problem
Solving 15-Puzzle problem
Solving 15-Puzzle problem

Possible
arrangements
Solving 15-Puzzle problem
7
Solving 15-Puzzle problem
Solving 15-Puzzle problem
Solving 15-Puzzle problem
Solving 15-Puzzle problem
Solving 15-Puzzle problem
Assignment
6
Problem
ASSIGNMENT
7
PROBLEM
 There are n people who need to be assigned to
execute n jobs, one person per job. (That is, each
person is assigned to exactly one job and each job
is assigned to exactly one person.)
 The cost that would accrue if the ith person is

assigned to the jth job is a known quantity C [i, j]


for each pair
i, j = 1, 2, . . . , n.
 The problem is to find an assignment with the

minimum total cost.


ASSIGNMENT PROBLEM -
Example
8
Assignment
Problem
9

 The assignment problem can be solved using branch


and bound.
 We have to find a lower bound of each node in the
state space tree i.e. the sum of the smallest
elements in each of the matrix’s rows.
 The lower bound of the root node is 2 + 3+ 1+ 4 = 10.
 This is not the cost of optimal solution or cost of any
legitimate selection (3 and 1 came from the same
column of the matrix); it is just a lower bound on the
cost of any legitimate selection.
Constructing state-space
tree
10

 Rather than generating a single child of the last


promising node as we did in backtracking, we will
generate all the children of the most promising
node among non terminated leaves in the
current tree. This variation of the strategy is called
the best-first branch-and-bound.
Constructing state-space
tree
11

 Live node – not terminated leaf node.


 In assignment problem choose a most promising live
node (With smallest lower bound) and explore it
i.e. find the possible choices along with its lower
bound.
 Initially we have root node with lower bound 10.
 In the first level we have four choices for Person1.
 Person one can be assigned with any one of the four
jobs and corresponding lower bounds are
calculated.
12
13
Constructing state-space
tree
14

 Now we have four live nodes. Among them P1 →


J2 has the lower bound (Most promising node).
 Explore the that node. In this level 3 choices for
Person2.
 P2 → J1, P2→J3 and P2→J4
15
16
Assignment
Problem
17

 Now, as we inspect each of the live leaves of the last


state-space tree—nodes 1, 3, 4, 6, and 7, their lower-
bound values are not smaller than 13, the value of
the best selection seen so far (leaf 8).
 we terminate all of them and recognize the solution
represented by leaf 8 as the optimal solution to the
problem.
Practice
Problem
18

 Solve the following instance of assignment


problem using Branch and Bound.

Job A Job B Job C Job D

Machine I 10 5 12 8

Machine II 12 10 14 15

Machine III 19 7 13 11

Machine IV 11 8 11 9
19
Knapsack
Problem
Knapsack
Problem
20

 Given n items of known weights w1, w2, . . . , wn and


values v1, v2, . . . , vn and a knapsack of capacity
W, find the most valuable subset of the items that
fit into the knapsack.
 Knapsack Problem can be solved by using branch
and bound.
 It is convenient to order the items of a given instance
in descending order by their value-to-weight ratios.
State Space
Tree
21

 Each node on the ith level of this tree, 0 ≤ i ≤ n,


represents all the subsets of n items that include a
particular selection made from the first i ordered
items.
 This particular selection is uniquely determined by
the path from the root to the node: a branch going to
the left indicates the inclusion of the next item, and a
branch going to the right indicates its exclusion.
 We record the total weight w and the total value v of
this selection in the node, along with some upper
bound ub on the value of any subset that can be
Upper
Bound
22
Constructing State Space
Tree
23

 At the root of the state-space tree no items have


been selected as yet.
 Hence, both the total weight of the items already

selected w and their total value v are equal to 0. The


value of the upper bound computed by $100.
 UB = v+(W-w) (v1/w1)

= 0+10(10)
w=0 v= 0
= 100
UB = 100
Node 1: Node 2:
UB = v+(W-w)(v2/w2) UB = v+(W-w)(v2/w2)
= 40+(10-4) (6) = 0+(10) (6)
= 40+36 = 0+60
=76 =60

24
Node 4:
UB = v+(W-w)(v3/w3)
= 40+(10-4) (5)
= 40+30
25
= 70
Node 5:

UB = v+(W-w)(v4/w4)
= 65+(10-9) (4)
= 65+4
= 69

Node 6:
UB = v+(W-w)(v4/w4)
= 40+(10-4) (4)
= 40+ 24
26
= 64
Node 8:
UB = v+(W-w)(v5/w5)
= 65+(10-9) (0)
= 65+0
= 65

27
The remaining live nodes 2
and
6 have smaller upper-bound
values than the value of the
solution represented by node
8.

Hence, both can be


28
terminated making the
subset {1, 3} of node 8 the
Practice
29
Problem
Solve the following instance of the knapsack problem
using Branch and Bound
🞑 Given that the capacity of the knapsack W = 60, the
weights and profits of each item is as following

Items Weight Profit


A 40 280
B 10 100
C 20 120
D 24 120
30
Travelling Salesman
Problem
Travelling Salesman
Problem
31

 Travelling Salesman Problem (TSP): Given a set of n


cities and distance between every pair of cities, the
problem is to find the shortest possible route that visits
every city exactly once and returns to the starting point.
 TSP can be solved by using Branch and Bound.
 The lower bound can be calculated as
 For each city i, 1≤ i ≤ n, find the sum Si of the distances
from city i to the two nearest cities; compute the sum S
of these n numbers, divide the result by 2, and, if all the
distances are integers, round up the result to the nearest
integer:
Travelling Salesman
Problem
32

 For any subset of tours that must include particular


edges of a given graph, we can modify lower bound
accordingly.

lb = [(1+ 3) + (3 + 6) + (1+ 2) + (3 + 4) + (2 + 3)]/2 =


14.
•To reduce the amount of potential work, we take
advantage of two observations.
•First, without loss of generality, we can consider only
tours that start at a.
•Second, because our graph is undirected, we can
generate
only tours in which b is visited before c.

➢At level one there are four


options for the salesman from city
‘a’,
33
he can visit b or c or d or e.
➢So there are four children for the node 0.
Node 1 Node 3

LBat Node 1:
Lb=[(1+ 3) + (3 + 6) + (1+ 2) + (3 + 4) + (2 + 3)]/2 = 28/2=14
LBat Node 3:
Lb=[(1+
34 5) + (3 + 6) + (1+ 2) + (5 + 3) + (2 + 3)]/2 = 31/2 =16
Node 4

LBat Node 4:
Lb=[(1+ 8) + (3 + 6) + (1+ 2) + (3 + 4) + (2 + 8)]/2 = 38/2=19
35
•There are three live nodes namely 1, 3 and 4.
•Among whichnode 1 has the most promising
node. We can explore the node 1.
•At level 2, there are three options for the
salesman, he can visit c or d or e.
•36So there are three children for node 1.
LBat Node 5:
Lb=[(1+
37 3) + (3 + 6) + (1+ 6) + (3 + 4) + (2 + 3)]/2 = 32/2=16
LBat Node 6:
Lb=[(1+
38 3) + (3 + 7) + (1+ 2) + (3 + 7) + (2 + 3)]/2 = 32/2=16
LBat Node 7:
Lb=[(1+
39 3) + (3 + 9) + (1+ 2) + (3 + 4) + (2 + 9)]/2 = 37/2=19
✓ Live nodes : 3, 4, 5, 6 and 7.
✓ In which Nodes 3, 5 and 6 are most promising nodes. We
will explore node 5.
✓ Node five represents the path a → b → c. Now salesman
have two options either he can visit d or e.
✓ In addition, after visiting n − 1= 4 cities, a tour has no
choice but to visit the remaining unvisited city and return
to the starting
LBat Node 8:
Lb=[(8+
41 3) + (3 + 6) + (6+ 4) + (3 + 4) + (3 + 8)]/2 = 48/2= 24
LBat Node 9:
Lb=[(5+
42 3) + (3 + 6) + (6+ 2) + (3 + 5) + (3 + 2)]/2 = 38/2= 19
❖ Node 8 and 9 represents solution with the cost of 24 and 19 respectively.
❖ Node 9 ( Tour : a → b → c → e → d → a) has better solution (cost : 19) compared
to Node 8 (Tour : a → b → c → d → e → a) with cost 24.
❖ Live Nodes: 3, 4, 6 and 7
❖ Live nodes 3 and 6 has lower bound than solution represented by node 9.
❖ Now we are exploring the node 6 (Path : a → b → d)
43
❖ Now salesman have two options either he can visit c or e.
LBat Node 10:
Lb=[(8+
44 3) + (3 + 7) + (4+ 2) + (4 + 7) + (8 + 2)]/2 = 48/2= 24
LBat Node 11:
Lb=[(1+
45 3) + (3 + 7) + (1+ 2) + (3 + 7) + (3 + 2)]/2 = 32/2= 16
Optimal
Solution
46

 Node 10 and 11 representsthe solution.


 Node 10 :
🞑 Tour : a → b → d → c → e → a
🞑 Cost : 24

 Node 11:
🞑 Tour : a → b → d → c → e → a
🞑 Cost : 16

 Node 11 has better solution compared to all solutions so far.


 No Live nodes (3, 4 and 7) has better lower bound.
 So Node 11 representsthe optimal solution.
🞑 Tour : a → b → d → c → e → a
🞑 Cost : 16
47

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy