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

ADA_Unit-7

Uploaded by

dhruvidevani22
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)
2 views

ADA_Unit-7

Uploaded by

dhruvidevani22
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/ 29

UNIT-7

BACK TRACKING & BRANCH AND BOUND

Back Tracking
Backtracking
• Backtracking can be defined as a general algorithmic technique that
considers searching every possible combination in order to solve an
optimization problem.
• It is a recursive technique.
• It generates state space tree for all possible solutions.
• It traverse state space tree in the depth first order.
• So, in a backtracking we attempt solving a sub-problem, and if we don't
reach the desired solution, then undo whatever we did for solving that
sub-problem, and try solving another sub-problem.
• All the solutions require a set of constraints divided into two categories:
explicit and implicit constraints.
The N - Queens Problem
• The 𝑁 - queen is the problem of placing N chess queens on an 𝑁 × 𝑁
chessboard so that no two queens attack each other.
• Two queens of same row, same column or the same diagonal can
attack each other.
• K-Promising solution: A solution is called k-promising if it arranges
the k - queens such a way that, they can not threat each other.

Q Q Q
Q Q
1 - Promising 0 - Promising 0 - Promising 0 - Promising
Solution Solution Solution Solution
The 4 - queen Problem
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Q1 Q1 Q1 Q1
Q2 Q3 can’t Q2 Q4 can’t Q2
place so place so
backtrack Q3 backtrack Q3
to Q2 to Q1
Q4
4 – Promising <2, 4, 1, 3>
 Another possible solution is 1 2 3 4
<3, 1, 4, 2>
1 Q1
2 Q2
3 Q3
4 Q4
N - Queen Problem
Number of Possible
Queens Solutions
1 1
2 0
3 0
4 2
5 10
6 4
7 40
8 92
9 352
10 724
4 - Queens State Space Tree
Knapsack Problem
• We are given a certain number of objects and a knapsack.
• Instead of supposing that we have n objects available, we shall
suppose that we have n types of object, and that an adequate number
of objects of each type are available.
• Our aim is to fill the knapsack in a way that maximizes the value of the
included objects.
• We may take an object or leave behind,
but we may not take fraction of an object.
Knapsack Problem
 Initially solution is empty. Weight of
 Left of the semicolon are weights of selected objects. objects w = (2,3,4,5)
 Right of the semicolon is the current total value of load. Corresponding v = (3,5,6,10)
value of each
object
W=8
apply depth first
;0 Capacity of
search & find
Knapsack
max profit.
2;3 3;5 4;6 5 ; 10

2,2; 6 2,3; 8 2,4; 9 2,5; 13 3,3; 10 3,4; 11 3,5; 15 4,4; 12

2,2,2; 9 2,2,3; 11 2,2,4; 12 2,3,3; 13 Store solution if optimal


solution is found
2,2,2,2; 12
Branch and Bound
• Branch and Bound is a systematic method for solving
optimization problems.
• It generates state space tree.
• The branch and bound approach is based on the principle that the
total set of feasible solutions can be partitioned into smaller subsets of
solutions.
• These smaller subsets can then be evaluated systematically until the
best solution is found.
• Bounding Function are used to avoid the generation of subtrees that
do not contain an answer node.
Task/Job Assignment Problem
• Problem Statement: Let there be N workers and N jobs. Any
worker can be assigned to perform any job, incurring some cost
that may vary depending on the work-job assignment. It is required
to perform all jobs by assigning exactly one worker to each job and
exactly one job to each agent in such a way that the total cost of
the assignment is minimized.
Task/Job Assignment Problem
There are two approaches to calculate the cost function:
• For each worker, we choose job with minimum cost from list of
unassigned jobs (take minimum entry from each row).
• For each job, we choose a worker with lowest cost for that job from
list of unassigned workers (take minimum entry from each column).
• Here we will solve using second approach.
• Example:
Task/Job Assignment Problem
• First we will calculate lower Bound (LB), by adding min value from each row.
LB= 2+3+1+4=10
Minimax Principle

MAX (player) 3

MIN
(opponent) 3 2 2

3 12 8 2 4 6 14 5 2
Minimax Principle
• It is a decision rule that minimize the maximum loss and
maximize the minimum profit(gain).
Minimax Principle
• The key to the Minimax algorithm is a back and forth between the
two players, where the player whose "turn it is" desires to select the
move with the maximum score.
• In turn, the scores for each of the available moves are determined by
the opposing player deciding which of its available moves has the
minimum score.
• It uses a simple recursive computation of the minimax values of each
successor state.
• The recursion proceeds all the way down to the leaves of the tree,
and then the minimax values are backed up through the tree as the
recursion unwinds.
X X O
Human Max X O
O
WIN

X X O X X O X X O
Computer Min X O X X O X O
O X O O X
WIN WIN

X X O X X O X X O X X O
Human Max X O X X O X X O X O O
O O O O O O X O X
WIN WIN

X X O X X O
Computer Min X O X X O O
X O O X O X
Minimax Principle
23-11-2022

Travelling Salesman Problem


• Given a set of cities and distance between every pair of vertices the
problem is to find the shortest possible route that visits every city
once and returns to the starting city.

B C

Travelling Salesman Problem


3
• Let us look at a situation that
there are 5 cities, Which are
represented as NODES
• There is a Person at NODE-1
• This PERSON HAS TO REACH EACH
4 NODES ONE AND ONLY ONCE AND
2 COME BACK TO ORIGINAL
(STARTING)POSITION.
• This process has to occur with
minimum cost or minimum
distance travelled.

1 5

7
23-11-2022

Travelling Salesman Problem


Example: Solve Travelling Salesman Problem using Branch and
Bound Algorithm in the following graph.

Travelling Salesman Problem


Step 1: Write the initial cost matrix.

8
23-11-2022

Travelling Salesman Problem


Step 1: Reduce matrix.
•To reduce a matrix, perform the row reduction and column reduction of
the matrix separately.
•A row or a column is said to be reduced if it contains at least one entry
‘0’ in it.
If the row already contains an entry ‘0’, then
There is no need to reduce that row.

Travelling Salesman Problem


Step 1: Reduce matrix. Row Reduction
If the row already contains an entry ‘0’, then There is no need to reduce that row.
If the row does not contains an entry ‘0’, then-
•Reduce that particular row.
•Select the least value element from that row.
•Subtract that element from each element of that row.
•This will create an entry ‘0’ in that row, thus reducing that row.
Row-reduced matrix

4
5
6
2

9
23-11-2022

Travelling Salesman Problem


Step 1: Reduce matrix. Column Reduction
If the column already contains an entry ‘0’, then There is no need to reduce that column.
If the column does not contains an entry ‘0’, then-
•Reduce that particular column.
•Select the least value element from that column.
•Subtract that element from each element of that column.
•This will create an entry ‘0’ in that column, thus reducing that column.
Column-reduced matrix

0 0 1 0

Travelling Salesman Problem


Step 1: Reduce matrix.
Total Reduction cost = Row Reduction cost + column Reduction cost
= 17 + 1
= 18

4
5
6
2
0 0 1 0

10
23-11-2022

Travelling Salesman Problem


Step 2: We consider all other vertices one by one.
Choosing To Go To Vertex-B:(Path A → B)
From the reduced matrix of step-01, M[A,B] = 0
Set row-A and column-B to ∞
Set M[B,A] = ∞

Travelling Salesman Problem


Step 2: We consider all other vertices one by one.
Choosing To Go To Vertex-B:(Path A → B)

Row Reduction Column Reduction

-
13
0
0

5 - 0 0
Cost(2) = Cost(1) + Sum of reduction elements + M[A,B]
= 18 + (13 + 5) + 0
= 36

11
23-11-2022

Travelling Salesman Problem


Step 2: We consider all other vertices one by one.
Choosing To Go To Vertex-C:(Path A → C)
From the reduced matrix of step-01, M[A,C] = 7
Set row-A and column-C to ∞
Set M[C,A] = ∞

Travelling Salesman Problem


Step 2: We consider all other vertices one by one.
Choosing To Go To Vertex-C:(Path A → C)

Row Reduction Column Reduction

-
0
0
0

0 0 - 0
Cost(3) = Cost(1) + Sum of reduction elements + M[A,C]
= 18 + 0 + 7
= 25

12
23-11-2022

Travelling Salesman Problem


Step 2: We consider all other vertices one by one.
Choosing To Go To Vertex-D:(Path A → D)
From the reduced matrix of step-01, M[A,D] = 3
Set row-A and column-D to ∞
Set M[D,A] = ∞

Travelling Salesman Problem


Step 2: We consider all other vertices one by one.
Choosing To Go To Vertex-D:(Path A → D)

Row Reduction Column Reduction

-
0
5
0
0 0 0 -
Cost(4) = Cost(1) + Sum of reduction elements + M[A,D]
= 18 + 5 + 3
= 26

13
23-11-2022

Travelling Salesman Problem


Step 2: We consider all other vertices one by one.

A
Thus, we have-
Cost(2) = 36 (for Path A → B)
Cost(3) = 25 (for Path A → C)
Cost(4) = 26 (for Path A → D) B C D
36 25 26
We choose the node with the lowest cost.
Since cost for node-3 is lowest, so we prefer to visit node-3.
Thus, we choose node-3 i.e. path A → C.

Travelling Salesman Problem


Step 3: Explore the vertices B and D from node-3.
Choosing To Go To Vertex-B: Node-5 (Path A → C → B)
From the reduced matrix of step-02, M[C,B] = ∞
Set row-C and column-B to ∞
Set M[B,A] = ∞

Cost(3) = 25

14
23-11-2022

Travelling Salesman Problem


Step 3: Explore the vertices B and D from node-3.
Choosing To Go To Vertex-B: Node-5 (Path A → C → B)

Row Reduction Column Reduction

-
13
-
8
0 - - 0
Cost(5) = cost(3) + Sum of reduction elements + M[C,B]
= 25 + (13 + 8) + ∞
=∞

Travelling Salesman Problem


Step 3: Explore the vertices B and D from node-3.
Choosing To Go To Vertex-D: Node-6 (Path A → C → D)
From the reduced matrix of step-02, M[C,D] = 0
Set row-C and column-D to ∞
Set M[D,A] = ∞

Cost(3) = 25

15
23-11-2022

Travelling Salesman Problem


Step 3: Explore the vertices B and D from node-3.
Choosing To Go To Vertex-D: Node-6 (Path A → C → D)

Row Reduction Column Reduction

-
0
-
0
0 0 - -
Cost(6) = cost(3) + Sum of reduction elements + M[C,D]
= 25 + 0 + 0
= 25

Travelling Salesman Problem


Step 3: Explore the vertices B and D from node-3.
Thus, we have-
•Cost(5) = ∞ (for Path A → C → B) A
•Cost(6) = 25 (for Path A → C → D)

We choose the node with the lowest cost. B C D


Since cost for node-6 is lowest, so we 36 25 26
prefer to visit node-6.
Thus, we choose node-6 i.e. path C → D.
B D
∞ 25

16
23-11-2022

Travelling Salesman Problem


Step 4: Explore the vertices B from node-6.
Choosing To Go To Vertex-B: Node-7 (Path A → C → D → B)
From the reduced matrix of step-03, M[D,B] = 0
Set row-D and column-B to ∞
Set M[B,A] = ∞

Cost(6) = 25

Travelling Salesman Problem


Step 4: Explore the vertices B from node-6.
Choosing To Go To Vertex-B: Node-7 (Path A → C → D → B)

Row Reduction Column Reduction

-
-
-
-
- - - -
Cost(7) = cost(6) + Sum of reduction elements + M[D,B]
= 25 + 0 + 0
= 25

17

You might also like