0% found this document useful (0 votes)
27 views24 pages

Aad Mod 4

Uploaded by

padusindia
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)
27 views24 pages

Aad Mod 4

Uploaded by

padusindia
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/ 24

CST 306: ALGORITHM ANALYSIS & DESIGN Semester VI

Module IV
Lecturer: Dr. Reshmi R Class: CSE-A&B

Syllabus:
Dynamic Programming, Back Tracking and Branch & Bound :
Dynamic Programming The Control Abstraction- The Optimality Principle- Matrix Chain Multiplication-Analysis,
All Pairs Shortest Path Algorithm - Floyd-Warshall Algorithm-Analysis.
Back Tracking and Branch & Bound The Control Abstraction of Back Tracking – The N Queen’s Problem. Branch
and Bound Algorithm for Travelling Salesman Problem..

Federal Institute of Science And Technology (FISAT)

Contents

1.1 Dynamic Programming


AT
1.1.1 General method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1

1.1.2 Control Abstraction - Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2


FIS
1.1.3 Matrix Chain Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2 All pairs shortest path problem - Floyd Warshall Algorithm 10

1.3 Backtracking 13

1.3.1 N-Queens Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1.3.2 Branch & Bounding Technique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

1.3.2.1 Least Cost (LC) Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

1.4 Traveling SalesPerson Problem 19

1.4.1 Backtracking vs Branch and Bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

1.1 Dynamic Programming

1.1.1 General method

Dynamic programming is an algorithm design method that can be used when the solution to a problem can be viewed
as the result of a sequence of decisions.

1
For some of the problems, an optimal sequence of decisions can be found by making the decisions one at a time and
never making an erroneous decision. This is true for all problems solvable by the greedy method. For many other
problems, it is not possible to make stepwise decisions (based only on local information) in such a manner that the
sequence of decisions made is optimal.
One way to solve problems for which it is not possible to make a sequence of stepwise decisions leading to an optimal
decision sequence is to try all possible decision sequences. We could enumerate all decision sequences and then pick
out the best. But the time and space requirements may be prohibitive. Dynamic programming often drastically reduces
the amount of enumeration by avoiding the enumeration of some decision sequences that cannot possibly be optimal.
ln dynamic programming an optimal sequence of decisions is obtained by making explicit appeal to the principle of
optimality.

Definition 1 [Principle of optimality] The principle of optimality states that an optimal sequence of decisions has the
property that whatever the initial state and decision are, the remaining decisions must constitute an optimal decision
sequence with regard to the state resulting from the first decision.

1.1.2

AT
Control Abstraction - Dynamic Programming
FIS

Control abstraction for Dynamic Programming

The development of a dynamic-programming algorithm can be broken into a sequence of four steps.

1. Characterize the structure of an optimal solution.

2. Recursively define the value of an optimal solution.

3. Compute the value of an optimal solution in a bottom-up fashion.

4. Construct an optimal solution from computed information.

Dr. Reshmi R 2 Department of CSE, FISAT


Steps 1-3 form the basis of a dynamic-programming solution to a problem. Step 4 can be omitted if only the value of
an optimal solution is required. When we do perform step 4, we sometimes maintain additional information during the
computation in step 3 to ease the construction of an optimal solution.

The following characteristics properties must be associated with a problem which can be solved using dynamic pro-
gramming.

• Optimal substructure: solution to a given optimization problem can be obtained by the combination of optimal
solutions to its sub-problems.

• Overlapping subproblems: the space of sub-problems must be small, that is, any recursive algorithm solving
the problem should solve the same sub-problems over and over, rather than generating new sub-problems.

The difference between the greedy method and dynamic programming is that in the greedy method only one de-
cision sequence is ever generated. ln dynamic programming, many decision sequences may be generated. However,

AT
sequences containing suboptimal subsequences cannot be optimal (if the principle of optimality holds) and so will not
(as far as possible) be generated. Another important feature of the dynamic programming approach is that optimal
solutions to subproblems are retained so as to avoid recomputing their values.

Divide-and-Conquer vs Dynamic programming:

• Divide-and-conquer algorithms partition the problem into independent subproblems, solve the subproblems re-
cursively, and then combine their solutions to solve the original problem. In contrast, dynamic programming is
FIS
applicable when the subproblems are not independent, that is, when subproblems share subsubproblems.

• Divide-and-conquer is top down approach while Dynamic programming is bottom up approach.

• Dynamic programming typically applies to optimization problems in which a set of choices must be made in
order to arrive at an optimal solution.

• Both techniques split their input into parts, find subsolutions to the parts, and synthesize larger solutions from
smaller ones.

• Divide-and-conquer prefer recursion while Dynamic programming prefer iteration.

• Divide and Conquer splits its input at pre-specified deterministic points (e.g., always in the middle)

• Dynamic Programming splits its input at every possible split points rather than at a pre-specified points. After
trying all split points, it determines which split point is optimal.

1.1.3 Matrix Chain Multiplication

A sequence (chain) A1 , A2 , ..., An of n matrices to be multiplied, and we wish to compute the product A1 .A2 .An .

Matrix multiplication is associative, and so all parenthesizations yield the same product.
For example, if the chain of matrices is A1 , A2 , A3 , A4 , the product A1 .A2 .A3 .A4 can be fully parenthesized in five
distinct ways:
(A1 .(A2 .(A3 .A4 )))

Dr. Reshmi R 3 Department of CSE, FISAT


(A1 .((A2 .A3 ).A4 ))
((A1 .A2 ).(A3 .A4 ))
(A1 .(A2 .A3 )).A4 )
(((A1 .A2 ).A3 ).A4 )
Note: The matrix-chain multiplication problem, not actually multiplying matrices. Goal is only to determine an order
for multiplying matrices that has the lowest cost. We can multiply two matrices A and B only if they are compatible:
the number of columns of A must equal the number of rows of B. If A is a pq matrix and B is a qr matrix, the resulting
matrix C is a pr matrix. The time to compute C is dominated by the number of scalar multiplications.

The matrix-chain multiplication problem can be stated as follows: given a chain A1 , A2 , ..., An of n matrices, where for
i = 1, 2, ..., n, matrix Ai has dimension pi−1 × pi , fully parenthesize the product A1 .A2 .An in a way that minimizes
the number of scalar multiplications.
Procedure:
Step 1: The structure of an optimal parenthesization:
The first step in the dynamic-programming paradigm is to find the optimal sub-structure and then use it to construct an

AT
optimal solution to the problem from optimal solutions to subproblems.
For the matrix-chain multiplication problem, we can perform this step as follows. For evaluating the product Ai Ai+1 ...Aj ,
the parenthesization of the product Ai Ai+1 ...Aj must split the product between Ak and Ak+1 for some integer k in
the range i ≤ k < j . That is, for some value of k, we first compute the matrices Ai...k and Ak+1...j and then multiply
them together to produce the final product Ai...j . The cost of this parenthesization is thus the cost of computing the
matrix Ai...k , plus the cost of computing Ak+1...j , plus the cost of multiplying them together.
The optimal substructure of this problem is as follows. Suppose that an optimal parenthesization of Ai Ai+1 ...Aj splits
the product between Ak and Ak+1 . Then the parenthesization of the “prefix” subchain Ai Ai+1 ...Ak within this optimal
parenthesization of Ai Ai+1 ...Aj must be an optimal parenthesization of Ai Ai+1 ...Ak .
FIS
A similar observation holds for the parenthesization of the subchain Ak+1 Ak+2 ...Aj in the optimal parenthesization
of Ai Ai+1 ...Aj : it must be an optimal parenthesization of Ak+1 Ak+2 ...Aj .
Thus, we can build an optimal solution to an instance of the matrix-chain multiplication problem by splitting the prob-
lem into two subproblems (optimally parenthesizing Ai Ai+1 ...Ak and Ak+1 Ak+2 ...Aj , finding optimal solutions to
subproblem instances, and then combining these optimal subproblem solutions.

Step 2: A recursive solution


For the matrix-chain multiplication problem, consider subproblems for determining the minimum cost of a parenthe-
sization of Ai Ai+1 ...Aj for 1 ≤ i ≤ j ≤ n. Let m[i, j] be the minimum number of scalar multiplications needed to
compute the matrix Ai...j . m[i, j] can be recursively defined as follows.

• If i = j , the problem is trivial; the chain consists of just one matrix Ai...i = Ai , so that no scalar multiplications
are necessary to compute the product. Thus, m[i, i] = 0 for i = 1, 2, ..., n.

• To compute m[i, j] when i < j , consider the structure of an optimal solution from step 1. Assume that the
optimal parenthesization splits the product Ai Ai+1 ...Aj between Ak and Ak+1 , where i ≤ k < j . Then, m[i, j]
is equal to the minimum cost for computing the subproducts Ai...k and Ak+1...j , plus the cost of multiplying
these two matrices together. Each matrix Ai is pi−1 xpi , i.e, computing the matrix product Ai ...Ak ...Aj takes
pi−1 pk pj scalar multiplications. Thus
m[i, j] = m[i, k] + m[k + 1, j] + pi−1 pk pj
There are only j − i possible values for k, i.e, k = i, i + 1, ..., j − 1. Since the optimal parenthesization must
use one of these values for k, we need only check them all to find the best.

Dr. Reshmi R 4 Department of CSE, FISAT


The recursive definition for the minimum cost of parenthesizing the product Ai Ai+1 ...Aj becomes

0 if i=j
m[i, j] = (1.1)
min{m[i, k] + m[k + 1, j] + p
i−1 pk pj } if i<j

The m[i, j] values give the costs of optimal solutions to subproblems.


Let s[i, j] to be a value of k at which we can split the product Ai Ai+1 ...Aj to obtain an optimal parenthesization.
i.e, s[i, j equals a value k such that m[i, j] = m[i, k] + m[k + 1, j] + pi−1 pk pj .

Step 3: Computing the optimal costs:


Instead of computing the solution to recurrence recursively, we perform the third step of the dynamic-programming
paradigm and compute the optimal cost by using a tabular, bottom-up approach. The following pseudocode
assumes that matrix Ai has dimensions pi−1 xpi for i = 1, 2, ..., n. The input is a sequence p = (p0 , p1 , ...pn ),
where length[p] = n + 1. The procedure uses an auxiliary table m[1...n, 1...n] for storing the m[i, j] costs and
an auxiliary table s[1...n, 1..n] that records which index of k achieved the optimal cost in computing m[i, j]. We

AT
will use the table s to construct an optimal solution.
FIS

Algorithm - Matrix chain order

A simple inspection of the nested loop structure of M AT RIX − CHAIN − ORDER yields a running time
of O(n3 ) for the algorithm. The loops are nested three deep, and each loop index (l, i, and k) takes on at most
n − 1 values. The algorithm requires θ(n2 ) space to store the m and s tables.

Step 4: Constructing an optimal solution


Although MATRIX-CHAIN-ORDER determines the optimal number of scalar multiplications needed to compute a
matrix-chain product, it does not directly show how to multiply the matrices. Each entry s[i, j] records the value of k
such that the optimal parenthesization of Ai Ai+1 ...Aj splits the product between Ak and Ak+1 . The following recur-
sive procedure prints an optimal parenthesization of Ai Ai+1 ...Aj , given the s table computed by MATRIX-CHAIN-
ORDER and the indices i and j.

Dr. Reshmi R 5 Department of CSE, FISAT


Example:
Given the sequence {4, 10, 3, 12, 20, and 7}. The matrices have size 4X10, 10X3, 3X12, 12X20, 20X7. We need to
compute M [i, j], 0 ≤ i, j ≤ 5. We know M [i, i] = 0 for all i.

Solution :
Let the given matrices are A1 = 4X10, A2 = 10X3, A3 = 3X12, A4 = 12X20, A5 = 20X7

AT
Using tabular method, consider a 5X5 table and update entries of m[i, i] = 0.
FIS

Calculation of Product of 2 matrices:

1. m(1, 2) = m1 ↓ X m2 ↓
z }| { z }| {
= 4X10 X 10X3
= 4X10X3 = 120

2. m(2, 3) = m2 ↓ X m3 ↓
z }| { z }| {
= 10X3 X 3X12
= 10X3X12 = 360

3. m(3, 4) = m3 ↓ X m4 ↓
z }| { z }| {
= 3X12 X 12X20
= 3X12X20 = 720

Dr. Reshmi R 6 Department of CSE, FISAT


4. m(4, 5) = m4 ↓ X m5 ↓
z }| { z }| {
= 12X20 X 20X7
= 12X20X7 = 1680

Calculation of Product of 3 matrices:


m[1, 3] = m1.m2.m3

m[1, 3] = 264
AT
• There are two cases by which we can solve this multiplication: (m1Xm2) + m3 and m1 + (m2xm3).

• After solving both cases we choose the case in which minimum output is there.

m[1, 3] = min

m[1, 2] + m[3, 3] + p p p
0 2 3
m[1, 1] + m[2, 3] + p p p
0 1 3
= 120 + 0 + 4 ∗ 3 ∗ 12 = 264
= 0 + 360 + 4 ∗ 10 ∗ 12 = 840
(1.2)

As Comparing both output, 264 is minimum in both cases so we insert 264 in table and (m1Xm2) + m3 this combi-
FIS
nation is chosen for the multiplication(k = 2).

m[2, 4] = m2.m3.m4

• There are two cases by which we can solve this multiplication: (m2Xm3) + m4 and m2 + (m3xm4).

• After solving both cases we choose the case in which minimum output is there.

m[2, 3] + m[4, 4] + p p p
1 3 4 = 360 + 0 + 10 ∗ 12 ∗ 20 = 2760
m[2, 4] = min (1.3)
m[2, 2] + m[3, 4] + p p p
1 2 4 = 0 + 720 + 10 ∗ 3 ∗ 20 = 1320

m[2, 4] = 1320
As Comparing both output, 1320 is minimum in both cases so we insert 1320 in table and (m2 + (m3Xm4)) this
combination is chosen for the multiplication(k = 2).

m[3, 5] = m3.m4.m5

• There are two cases by which we can solve this multiplication: (m3Xm4) + m5 and m3 + (m4xm5).

• After solving both cases we choose the case in which minimum output is there.

m[3, 4] + m[5, 5] + p p p
2 4 5 = 720 + 0 + 3 ∗ 20 ∗ 7 = 1140
m[3, 5] = min (1.4)
m[3, 3] + m[4, 5] + p p p
2 3 5 = 0 + 1680 + 3 ∗ 12 ∗ 7 = 1932

Dr. Reshmi R 7 Department of CSE, FISAT


m[3, 5] = 1140
As Comparing both output, 1140 is minimum in both cases so we insert 1140 in table and (m3Xm4) + m5)) this
combination is chosen for the multiplication(k = 4).

Now Product of 4 matrices:


m[1, 4] = m1.m2.m3.m4

• There are three cases by which we can solve this multiplication:

1. (m1Xm2Xm3)Xm4
2. m1X(m2Xm3Xm4)
3. (m1Xm2)X(m3Xm4)
AT
• After solving both cases we choose the case in which minimum output is there.





m[1, 3] + m[4, 4] + p0 p3 p4
m[1, 4] = min m[1, 2] + m[3, 4] + p0 p2 p4
= 264 + 0 + 4 ∗ 12 ∗ 20 = 1224
(1.5)
= 120 + 720 + 4 ∗ 3 ∗ 20 = 1080
FIS


= 0 + 1320 + 4 ∗ 10 ∗ 20 = 2120

m[1, 1] + m[2, 4] + p p p
0 1 4

m[1, 4] = 1080
As Comparing both output, 1080 is minimum in both cases so we insert 1080 in table and (m1Xm2)X(m3Xm4)) this
combination is chosen for the multiplication(k = 2).

m[2, 5] = m2.m3.m4.m5

• There are three cases by which we can solve this multiplication:

1. (m2Xm3Xm4)Xm5
2. m2X(m3Xm4Xm5)
3. (m2Xm3)X(m4Xm5)

• After solving both cases we choose the case in which minimum output is there.



 m[2, 4] + m[5, 5] + p1 p4 p5 = 1320 + 0 + 10 ∗ 20 ∗ 7 = 2720

m[2, 5] = min m[2, 3] + m[4, 5] + p1 p3 p5 = 360 + 1680 + 10 ∗ 12 ∗ 7 = 2880 (1.6)


= 0 + 1140 + 10 ∗ 3 ∗ 7 = 1350

m[2, 2] + m[3, 5] + p p p
1 2 5

m[2, 5] = 1350
As Comparing both output, 1350 is minimum in both cases so we insert 1350 in table and (m2X(m3Xm4Xm5)) this
combination is chosen for the multiplication(k = 2).

Dr. Reshmi R 8 Department of CSE, FISAT


Now Product of 5 matrices:

• There are four cases by which we can solve this multiplication:

1. (m1Xm2Xm3Xm4)Xm5

AT
2. m1X(m2Xm3Xm4Xm5)
3. (m1Xm2Xm3)Xm4Xm5
4. m1Xm2X(m3Xm4Xm5)

• After solving both cases we choose the case in which minimum output is there.






m[1, 4] + m[5, 5] + p0 p4 p5 = 1080 + 0 + 4 ∗ 20 ∗ 7 = 1544
= 264 + 1680 + 4 ∗ 12 ∗ 7 = 2016

m[1, 3] + m[4, 5] + p p p
0 3 5
(1.7)
FIS
m[1, 5] = min


 m[1, 2] + m[3, 5] + p0 p2 p5 = 120 + 1140 + 4 ∗ 3 ∗ 7 = 1344


= 0 + 1350 + 4 ∗ 10 ∗ 7 = 1630

m[1, 1] + m[2, 5] + p p p
0 1 5

m[1, 5] = 1344
As Comparing both output, 1344 is minimum in both cases so we insert 1344 in table and (m1Xm2X(m3Xm4Xm5)) this
combination is chosen for the multiplication(k = 2).

Order of matrix multiplication is (A1 (A2 (A3 (A4 .A5 )))


Cost of matrix multiplication is 1344

Dr. Reshmi R 9 Department of CSE, FISAT


1.2 All pairs shortest path problem - Floyd Warshall Algorithm

Floyd - Warshall algorithm considers the “intermediate” vertices of a shortest path, where an intermediate vertex of a
simple path p = v1 , v2 , ....vl is any vertex of p other than v1 or vl , that is, any vertex in the set v2 , v3 , ..., vl − 1.
Let V = {1, 2, ..., n} are set of vertices of the given graph, consider a subset {1, 2, ..., k} of vertices for some k. For
any pair of vertices i, j ∈ V , consider all paths from i to j whose intermediate vertices are all drawn from {1, 2, ..., k},
and let p be a minimum-weight path from among them. The Floyd- Warshall algorithm exploits a relationship between
path p and shortest paths from i to j with all intermediate vertices in the set {1, 2, ..., k − 1}. The relationship depends
on whether or not k is an intermediate vertex of path p.

• If k is not an intermediate vertex of path p, then all intermediate vertices of path p are in the set {1, 2, ..., k − 1}.
Thus, a shortest path from vertex i to vertex j with all intermediate vertices in the set {1, 2, ..., k − 1} is also a
shortest path from i to j with all intermediate vertices in the set {1, 2, ..., k}.

AT
• If k is an intermediate vertex of path p, then we break p down into i →p1 k →p2 j, p1 is a shortest path from i
to k with all intermediate vertices in the set {1, 2, ..., k}. Because vertex k is not an intermediate vertex of path
p1 , we see that p1 is a shortest path from i to k with all intermediate vertices in the set 1, 2, ..., k − 1. Similarly,
p2 is a shortest path from vertex k to vertex j with all intermediate vertices in the set 1, 2, ..., k − 1.
FIS
Path p is a shortest path from vertex i to vertex j , and k is the highest-numbered intermediate vertex of p. Path p1 , the
portion of path p from vertex i to vertex k, has all intermediate vertices in the set 1, 2,..., k-1. The same holds for path
p2 from vertex k to vertex j .

(k)
Let dij be the weight of a shortest path from vertex i to vertex j for which all intermediate vertices are in the set
{1, 2, ..., k}. When k = 0, a path from vertex i to vertex j with no intermediate vertex numbered higher than 0 has no
(0)
intermediate vertices at all. Such a path has at most one edge, and hence dij = wij .
A recursive definition of the above is given as:

(k)
w
ij if k=0
dij = (1.8)
min{d(k−1) , d(k−1) + d(k−1) } if k ≥ 0
ij ik kj

(n)
Because for any path, all intermediate vertices are in the set {1, 2, ..., n}, the matrix D(n) = dij gives the final answer:
(n)
dij = δ(i, j) for all i, j ∈ V .

Algorithm :

Dr. Reshmi R 10 Department of CSE, FISAT


Complexity:
The running time of the Floyd-Warshall algorithm is determined by the triply nested for loops of lines 3–6. Because
each execution of line 6 takes O(1) time, the algorithm runs in time O(n3 ).

Example:

AT
FIS
Let D(k) is the distance matrix, of shortest path weights with k as intermediate vertex.
Π is predecessor matrix,
Π = (πij ), where πi,j is NIL if either i = j or there is no path from i to j, and
otherwise πi,j is the predecessor of j on some shortest path from i.

D(0) is the initial distance matrix and Π(0) is initial predecessor matrix.

Step 1: Let k = 1. Find D(1) and Π(1)


(1) (0) (0) (0)
dij = min{dij , di1 + d1j }
Finding shortest distance from vertex 2 to other vertices(3,4,5), when intermediate vertex, k = 1
(1)
As there is no path from 2 to 1, there won’t be any change in d2j , when k=1.
(1) (0) (0) (0)
d23 = min{d23 , d21 + d13 } = min{∞, ∞ + 8} = ∞
(1) (0) (0) (0)
d24 = min{d24 , d21 + d14 } = min{∞, ∞ + ∞} = ∞
(1) (0) (0) (0)
d25 = min{d25 , d21 + d15 } = min{∞, ∞ + (−4)} = ∞

Finding shortest distance from vertex 3 to other vertices(2,4,5), when intermediate vertex, k = 1

Dr. Reshmi R 11 Department of CSE, FISAT


(1) (0) (0) (0)
d32 = min{d32 , d31 + d12 } = min{4, ∞ + 3} = 4; π32 = 3
(1) (0) (0) (0)
d34 = min{d34 , d31 + d14 } = min{∞, ∞ + ∞} = ∞
(1) (0) (0) (0)
d35 = min{d35 , d31 + d15 } = min{∞, ∞ + (−4)} = ∞

Finding shortest distance from vertex 4 to other vertices(2,3,5), when intermediate vertex, k = 1
(1) (0) (0) (0)
d42 = min{d42 , d41 + d12 } = min{∞, 2 + 3} = 5; π42 = 1
(1) (0) (0) (0)
d43 = min{d43 , d41 + d13 } = min{(−5), 2 + 8} = −5
(1) (0) (0) (0)
d45 = min{d45 , d41 + d15 } = min{∞, 2 + (−4)} = −2 π45 = 1

Finding shortest distance from vertex 5 to other vertices(2,3,4), when intermediate vertex, k = 1

(1)
As there is no path from 5 to 1, there won’t be any change in d2j , when k=1.
(1) (0) (0) (0)
d52 = min{d52 , d51 + d12 } = min{∞, ∞ + 3} = ∞
(1) (0) (0) (0)
d53 = min{d53 , d51 + d13 } = min{∞, ∞ + 8} = ∞
(1) (0) (0) (0)
d54 = min{d54 , d51 + d14 } = min{6, ∞ + ∞} = 6

AT
Step 2: Let k = 2. Find D(2) and Π(2) , from D(1)
Note : Repeat all the steps as shown in step 1 by considering the distance matrix D(1) and intermediate vertex, k = 2.
FIS
(2) (1) (1) (1)
dij = min{dij , di2 + d2j }

Step 3: Let k = 3. Find D(3) and Π(3) , from D(2)


Note : Repeat all the steps as shown in step 1 by considering the distance matrix D(2) and intermediate vertex, k = 3.
(3) (2) (2) (2)
dij = min{dij , di3 + d3j }

Step 4: Let k = 4. Find D(4) and Π(4) , from D(3)


Note : Repeat all the steps as shown in step 1 by considering the distance matrix D(3) and intermediate vertex, k = 4.
(4) (3) (3) (3)
dij = min{dij , di4 + d4j }

Dr. Reshmi R 12 Department of CSE, FISAT


Step 5: Let k = 5. Find D(5) and Π(5) , from D(4)
Note : Repeat all the steps as shown in step 1 by considering the distance matrix D(4) and intermediate vertex, k = 5.
(5) (4) (4) (4)
dij = min{dij , di5 + d5j }

1.3 Backtracking
AT
Distance Matrix D(5) is the shortest path weight matrix (All pairs shortest path).

The problems which deal with searching for a set of solutions or which ask for an optimal solution satisfying some
FIS
constraints can be solved using the backtracking formulation. In many applications of the backtrack method, the desired
solution is expressible as an n—tuple (x1 , ..., xn ), where the xi , are chosen from some finite set Si . Often the problem
to be solved calls for finding one vector that maximizes (or minimizes or satisfies) a criterion function P (x1 , ..., xn ).
The basic idea is to build up the solution vector one component at a time and to use modified criterion functions
Pi (x1,...,xi ) (sometimes called bounding functions) to test whether the vector being formed has any chance of success.
The major advantage of this method is this: if it is realized that the partial vector (x1 , ..., xi ) can in no way lead to an
optimal solution, then mi + 1...mn possible test vectors can be ignored entirely.
Many of the problems we solve using backtracking require that all the solutions satisfy a complex set of constraints.
For any problem these constraints can be divided into two categories: explicit and implicit.

Definition 2 (Explicit Constraints) Explicit constraints are rules that restrict each xi to take on values only from a
given set.

Common examples of explicit constraints are:


xi ≥ 0 or Si ={all non negative real numbers}
xi = 0 or 1 or Si = {0, 1}
li ≤ xi ≤ ui or Si = {a : li ≤ a ≤ uis }
The explicit constraints depend on the particular instance I of the problem being solved. All tuples that satisfy the
explicit constraints define a possible solution space for I.

Dr. Reshmi R 13 Department of CSE, FISAT


Definition 3 (Implicit Constraints) The implicit constraints are rules that determine which of the tuples in the solution
space of I satisfy the criterion function. Thus implicit constraints describe the way in which the xi must relate to each
other.

Example: Eight queens on an 8x8 chessboard, queens were placed such that no two "attack” takes place, that is, no
two queens are on the same row, column, or diagonal.

Backtracking - Algorithm:

AT
FIS
Recursive backtracking algorithm

General iterative backtracking method

Dr. Reshmi R 14 Department of CSE, FISAT


1.3.1 N-Queens Problem

The n—queens problem is a generalization of the 8-queens problem.


’N’ queens are to be placed on an nxn chessboard so that no two attack; i.e,

• no two queens are on the same row,column, or diagonal.

The solution space consists of all n! permutations of the n—tuple (1, 2, ..., n).

8-queens problem:

AT
A classic combinatorial problem is to place eight queens on an 8X8 chessboard so that no two "attack,” that is, so that
no two of them are on the same row, column, or diagonal.
Let us number the rows and columns of the chessboard 1 through 8. The queens can also be numbered 1 through 8.
Since each queen must be on a different row, assume that queen i is to be placed on row i. All solutions to the 8-queens
problem can therefore be represented as 8-tuples (x1 , ..., x8 ), where xi is the column on which queen i is placed.
Explicit constraints using this formulation are Si = {1, 2, 3, 4, 5, 6, 7, 8}, 1 ≤ i ≤ 8. Therefore the solution space
consists of 8X8 tuples.

Implicit constraints for this problem are that no two xi can be the same (i.e., all queens must be on different columns)
FIS
and no two queens can be on the same diagonal.

The first of these two constraints implies that all solutions are permutations of the 8—tuple (1, 2, 3, 4, 5, 6, 7, 8). This
realization reduces the size of the solution space from 8X8 tuples to 8! tuples.

The solution is expressed as an 8-tuple, (4, 6, 8, 2, 7, 1, 3, 5).

Dr. Reshmi R 15 Department of CSE, FISAT


Algorithm - N Queens

AT
FIS

Algorithm - Can a new queen be placed?

A possible tree organization for the case n = 4. A tree such as this is called a permutation tree. The edges are labeled
by possible values of xi . Edges from level 1 to level 2 nodes specify the values for xi . Thus, the leftmost subtree
contains all solutions with x1 = 1; its leftmost subtree contains all solutions with x1 = 1 and x2 = 2, and so on. Edges
from level i to level i + 1 are labeled with the values of xi . The solution space is defined by all paths from the root
node to a leaf node. There are 4! = 24 leaf nodes in the tree.

Dr. Reshmi R 16 Department of CSE, FISAT


Tree organization of the 4-queens solution space. Nodes are numbered as in depth first search.

AT
Terminology regarding tree organizations of solution spaces:
Each node in this tree defines a problem state. All paths from the root to other nodes define the state space of the
problem. Solution states are those problem states ’s’ for which the path from the root to s defines a tuple in the solu-
tion space (represented by leaf node). Answer states are those solution states s for which the path from the root to s
defines a tuple that is a member of the set of solutions (i.e., it satisfies the implicit constraints) of the problem. The tree
organization of the solution space is referred to as the state space tree.

The state space tree organizations in which the tree organizations are independent of the problem instance being solved
are called static trees.
FIS
For some problems it is advantageous to use different tree organizations for different problem instances. In this case
the tree organization is determined dynamically as the solution space is being searched. Tree organizations that are
problem instance dependent are called dynamic trees.

Once a state space tree has been conceived of for any problem, this problem can be solved by systematically gen-
erating the problem states, determining which of these are solution states, and finally determining which solution states
are answer states.
There are two fundamentally different ways to generate the problem states. Both of these begin with the root node and
generate other nodes.
A node which has been generated and all of whose children have not yet been generated is called a live node.
The live node whose children are currently being generated is called the E-node (node being expanded).
A dead node is a generated node which is not to be expanded further or all of whose children have been generated.
In both methods of generating problem states, there will be a list of live nodes.
In the first of these two methods as soon as a new child ’C’ of the current E-node, R is generated, this child will become
the new E-node. Then ’R’ will become the E-node again when the subtree C has been fully explored. This corresponds
to a depth first generation of the problem states.
In the second state generation method, the E-node remains the E-node until it is dead.
In both methods, bounding functions are used to kill live nodes without generating all their children.
Depth first node generation with bounding functions is called backtracking. State generation methods in which the
E-node remains the E-node until it is dead, lead to branch-and-bound methods.

Example (working of backtracking on 4-queens problem):

Dr. Reshmi R 17 Department of CSE, FISAT


Bounding function:- if (x1 , x2 , ..., xi ) is the path to the current E-node, then all children nodes with parent-child la-
beling xi+1 are such that (x1 , ..., xi+1 ) represents a chessboard configuration in which no two queens are attacking.
Start with the root node as the only live node. This becomes the E-node and the path is (). We generate one child. Let
us assume that the children are generated in ascending order. Thus, node 2 is generated and the path is now (1). This
corresponds to placing queen 1 on column 1.
Node 2 becomes the E-node. Node 3 is generated and immediately killed. The next node generated is node 8 and the
path becomes (1,3). Node 8 becomes the E-node. However, it gets killed as all its children represent board configura-
tions that cannot lead to an answer node. We backtrack to node 2 and generate another child, node 13. The path is now
(1, 4).
The figure given below shows the board configurations as backtracking proceeds. It shows graphically the steps that
the backtracking algorithm goes through as it tries to find a solution. The dots indicate placements of a queen which
were tried and rejected because another queen was attacking. In (b) the second queen is placed on columns 1 and 2
and finally settles on column 3. In (c) the algorithm tries all four columns and is unable to place the next queen on a
square. Backtracking now takes place. In (d) the second queen is moved to the next possible column, column 4 and the
third queen is placed on column 2. The boards in (e), (f), (g), and (h) show the remaining steps that the algorithm goes

AT
through until a solution is found.
FIS
Example of a backtrack solution to the 4-queens problem

Portion of the tree that is generated during backtracking

Dr. Reshmi R 18 Department of CSE, FISAT


The efficiency of both the backtracking algorithms depends very much on four factors:

• the time to generate the next xk

• the number of xk satisfying the explicit constraints

• the time for the bounding functions Bk and

• the number of xk satisfying the Bk .

1.3.2 Branch & Bounding Technique

The term branch-and-bound 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.
In branch-and bound terminology, a BFS-like state space search will be called FIFO (First In First Out) search as the
list of live nodes is a first-in-first-out list (or queue). A D-search-like state space search will be called LIFO (Last In

1.3.2.1
AT
First Out) search as the list of live nodes is a last-in-first-out list (or stack).

Least Cost (LC) Search

In both LIFO and FIFO branch—and—bound the selection rule for the next E—node does not give any preference to
a node that has a very good chance of getting the search to an answer node quickly.The search for an answer node can
often be speeded by using an "intelligent" ranking function for live nodes. The next E—node is selected on the basis
of this ranking function.The ideal way to assign ranks would be on the basis of the additional computational effort (or
cost) needed to reach an answer node from the live node. For any node x, this cost could be
FIS
1. the number of nodes in the subtree x that need to be generated before an answer node is generated

2. the number of levels the nearest answer node (in the subtree x) is from x

The difficulty with using either of these ideal cost functions is that computing the cost of a node usually involves a
search of the subtree x for an answer node. Hence, by the time the cost of a node is determined, that subtree has been
searched and there is no need to explore x again. For this reason, search algorithms usually rank nodes only on the
0
basis of an estimate, g (x) of their cost.
Let be an estimate of the additional effort needed to reach an answer node from x. Node x is assigned a rank using
0 0
a function such that c (x) = f (h(x)) + g (x), where h(x) is the cost of reaching x from the root and f () is any
nondecreasing function.
0 0
A search strategy that uses a cost function c (x) = f (h(x)) + g (x) to select the next E-node would always choose for
its next E-node a live node with least Hence, such a search strategy is called an LC-search (Least Cost search).

1.4 Traveling SalesPerson Problem

Let G = (V, E) be a directed graph with edge costs cij . The variable cij is defined such that cij > 0 for all i and j
and cij = ∞, If (i, j) ∈
/ E. Let |V | = n and assume n > 1. A tour of G is a directed simple cycle that includes every
vertex in V . The cost of a tour is the sum of the cost of the edges on the tour. The traveling salesperson problem is to
find a tour of minimum cost.

Dr. Reshmi R 19 Department of CSE, FISAT


Without loss of generality, we can assume that every tour starts and ends at vertex 1. So, the solution space S is given
by S = {1, π, 1|π, is a permutation of(2, 3, ..n)}. Then |S| = (n − 1)!. The size of S can be reduced by restricting S
so that (1, i1 , i2 , ..., in−1 , 1) ∈ S iff (ij , ij+1 ) ∈ E, 0 ≤ j ≤ n − 1, and i0 = in = 1.

State space tree for the traveling salesperson problem with n = 4 and i0 = i4 = 1

AT
The tree organization for the case of a complete graph with |V | = 4 is shown above. Each leaf node L is a solution node
and represents the tour defined by the path from the root to L. Node 14 represents the tour i0 = 1, i1 = 3, i2 = 4, i3 = 2
and i4 = 1

Procedure to solve Traveling saleperson problem:


To use LCBB to search, the traveling salesperson state space tree, we need to define a cost function c(.) and two other
0 0
functions c (.) and u(.) such that c (.) ≤ c(.) ≤ c(.) for all nodes r. The cost c(.) is such that the solution node with
FIS
least c(.) corresponds to a shortest tour in G.
One choice for c(.) is :
c(A) = {length of tour defined by the path from the root to A, if A is a leaf;
cost of a minimum-cost leaf in the subtree A, if A is not a leaf}

0
A better c (.) can be obtained by using the reduced cost matrix corresponding to G.
A row (column) is said to be reduced iff it contains at least one zero and all remaining entries are non-negative. A
matrix is reduced iff every row and column is reduced.

Consider a cost matrix of a given graph G,

This corresponds to a graph with five vertices. Since every tour on this graph includes exactly one edge (i, j) with
i = k, 1 ≤ k ≤ 5, and exactly one edge (i, j) with j = k, 1 ≤ k ≤ 5, subtracting a constant t from every entry in one
column or one row of the cost matrix reduces the length of every tour by exactly t.
A minimum—cost tour remains a minimum-cost tour following this subtraction operation. If t is chosen to be the
minimum entry in row i(column j), then subtracting it from all entries in row i (column j) introduces a zero into row i

Dr. Reshmi R 20 Department of CSE, FISAT


(column j). Repeating this as often as needed, the cost matrix can be reduced.

The total amount subtracted from the columns and rows is a lower bound on the length of a minimum-cost tour and
can be used as the ’c’ value for the root of the state space tree. Subtracting 10, 2, 2, 3, 4, 1, and 3 from rows 1, 2, 3,
4, and 5 and columns 1 and 3 respectively of the given cost matrix which yields the reduced matrix. The total amount
subtracted is 25.
Hence, all tours in the original graph have a length at least 25.

AT
Let A be the reduced cost matrix for node R.
Let S be a child of R such that the tree edge (R, S) corresponds to including edge (i, j) in the tour.
If S is not a leaf, then the reduced cost matrix for S may be obtained as follows:

1. Change all entries in row i and column j of A to ∞. This prevents the use of any more edges leaving vertex i or
entering vertex j.
FIS
2. Set A(j, 1) to ∞. This prevents the use of edge (j, 1).

3. Reduce all rows and columns in the resulting matrix except for rows and columns containing only ∞.

Let the resulting matrix be B.


Steps (1) and (2) are valid as no tour in the subtree s can contain edges of the type (i, k) or (k, j) or (j, 1) (except for
edge (i, j)). If r is the total amount subtracted in step (3) then
0 0
c (S) = c (R) + A(i, j) + r.
0 0
For leaf nodes, c (.) = c (.) is easily computed as each leaf defines a unique tour. For the upper bound function u,
u(R) = ∞ for all nodes.
The reduced matrices corresponding to these nodes are shown below:

The reduced matrix for Path 1,3:

• setting all entries in row 1 and column 3 to ∞,

Dr. Reshmi R 21 Department of CSE, FISAT


• setting the element at position (3, 1) to ∞,

0
• reducing column 1 by subtracting by 11. The c () for node 3 is therefore 25 + 17 (the cost of edge (1,3) in the
reduced matrix) + 11 = 53.

0
The matrices and c () value for nodes 2, 4, and 5 are obtained similarly.
The value of upper is unchanged and node 4 becomes the next E-node.
The portion of the state space tree that gets generated is shown below.

AT
The children of E-node (node 4) nodes 6, 7, and 8 are generated.
0
The live nodes at this time are nodes 2, 3, 5, 6, 7, and 8. Node 6 has least c () value and becomes the next E—node.
FIS

Nodes 9 and 10 are generated. Node 10 is the next E-node.

Dr. Reshmi R 22 Department of CSE, FISAT


0
The solution node, node 11, is generated. The tour length for this node is c (11) = 28 and upper is updated to 28. For
0
the next E-node, node 5, c (5) = 31 > upper. Hence, LCBB terminates with 1, 4, 2, 5, 3, 1 as the shortest length tour.

AT
FIS

State space tree generated by procedure LCBB

1.4.1 Backtracking vs Branch and Bound

• Backtracking is the modified process of the brute force approach where the technique efficiently searches for a
solution to the problem among all available options.
The branch-N-bound technique is an algorithm used to solve the problem of optimization by breaking it down
into smaller subproblems and by bounding function it eliminates the subproblem that doesn’t contain an optimal
solution.

• Backtracking is a general algorithm for finding all the solutions to some computational problems, notably con-
straint satisfaction problems, that incrementally builds possible candidates to the solutions and abandons a candi-
date as soon as it determines that the candidate cannot possibly be completed to finally become a valid solution.

Dr. Reshmi R 23 Department of CSE, FISAT


Branch and bound is an algorithm design paradigm for discrete and combinatoric optimisation problems, as well
as mathematical optimisation. A branch-and-bound algorithm consists of a systematic enumeration of candidate
solutions.

• Backtracking is used to find all possible solutions available to a problem. When it realises that it has made a bad
choice, it undoes the last choice by backing it up. It searches the state space tree until it has found a solution for
the problem.
Branch-and-Bound is used to solve optimisation problems. When it realises that it already has a better optimal
solution that the pre-solution leads to, it abandons that pre-solution. It completely searches the state space tree
to get optimal solution.

• Backtracking can be useful where some other optimization techniques like greedy or dynamic programming fail.
Such algorithms are typically slower than their counterparts.
Branch and bound builds the state space tree and find the optimal solution quickly by pruning few of the tree
branches which does not satisfy the bound.

• Backtracking traverses the state space tree by DFS(Depth First Search) manner.

AT
Branch-and-Bound traverse the tree in any manner, DFS or BFS.

• Backtracking involves feasibility function.


Branch-and-Bound involves a bounding function.

• Applications of backtracking includes N Queen Problem, Graph coloring problem, Hamiltonian cycle problem.
Applicatios of branch and bound includes Job sequencing problem, Traveling salesman problem, Knapsack prob-
lem Knapsack Problem, Sum of subsets problem
FIS

Dr. Reshmi R 24 Department of CSE, FISAT

You might also like