Module IV
Module IV
Module IV
Dynamic Programming, Back Tracking and Branch & Bound
o Dynamic Programming Control Abstraction
The Optimality Principle
Matrix Chain Multiplication-Analysis
All Pairs Shortest Path Algorithm - Floyd-Warshall Algorithm-Analysis
o The Control Abstraction of Back Tracking – The N Queen’s Problem
o Branch and Bound Algorithm for Travelling Salesman Problem
Dynamic Programming
o 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.
o Dynamic Programming is mainly an optimization over plain recursion.
o Wherever we see a recursive solution that has repeated calls for same inputs, we can optimize
it using Dynamic Programming.
o The idea is to simply store the results of subproblems, so that we do not have to re-compute
them when needed later.
o This simple optimization reduces time complexities from exponential to polynomial.
o For example, if we write simple recursive solution for Fibonacci Numbers, we get exponential
time complexity and if we optimize it by storing solutions of subproblems, time complexity
reduces to linear.
o In dynamic programming an optimal sequence of decisions is obtained by making explicit
appeal to the principle of optimality.
1 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
2. Optimal Substructure
A given problem has Optimal Substructure Property, if the optimal solution of the
given problem can be obtained using optimal solutions of its sub-problems.
For example, the Shortest Path problem has the following optimal substructure
property: If a node x lies in the shortest path from a source node u to destination
node v, then the shortest path from u to v is the combination of the shortest path
from u to x, and the shortest path from x to v.
We can multiply two matrices A and B if and only if they are compatible: The number of
columns of A must be equal to the number of rows of B.
If A is a pXq matrix and B is a qXr matrix, the resulting matrix C is a pX r matrix.
The time to compute C is pqr.
We shall express costs in terms of the number of scalar multiplications
Example:
Consider 3 matrices A1, A2 and A3. Its dimensions are 10X100, 100X5, 5X50
respectively.
Number of scalar multiplications for
o ( (A1A2) A3) is 7500
o (A1 (A2A3)) is 75000
Thus, computing the product according to the first parenthesization is 10 times faster.
2 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
Matrix Chain Multiplication : Dynamic Programming Method
Step 1: The structure of an optimal parenthesization
o Ai. .j denote the matrix that results from evaluating the product AiAi+1 . . . Aj where
i<= j
o If i< j, we must split the problem into two subproblems (Ai Ai+1 . . . Ak and
Ak+1Ai+1 . . . Aj ), for some integer k in the range i<= k < j.
o That is, for some value of k, we first compute the matrices Ai. .k and Ak+1. .j. Then
multiply them together to produce the final product Ai. .j .
o Total cost = Cost of computing the matrix Ai. .k+ Cost of computing Ak+1. .j+ Cost
of multiplying them together.
}
}
return m[][] and s[][]
}
3 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
o Matrix Ai has dimensions pi-1 X pi for i =1,2, . . . n.
o Input to this algorithm is a sequence p = ( p0, p1, . . . . pn ), where p.length = n + 1.
o The procedure uses 2 auxiliary tables
o m[1 .. n , 1...n] for storing the cost of matrix multiplication
o s[1..n-1 , 2 ...n] records which index of k achieved the optimal cost in computing
m[i,j] .
Time Complexity
We are generating n(n-1)/2 number of elements in matrix m[].
To calculate each element it will take atmost n time.
So the time complexity = O( n.n(n-1)/2) = O(n3)
o Examples
1. Using Dynamic Programming, find the fully parenthesized matrix product for multiplying
the chain of matrices< A1 A2 A3 A4 A5 A6 > whose dimensions are <30X35>,
<35X15>, <15X5>, <5X10>, <10X20> and <20X25> respectively
2. Given a chain of 4 matrices <A1,A2,A3,A4> with dimensions
<5X4>,<4X6>,<6X2>,<2X7> respectively. Using Dynamic programming find the
minimum number of scalar multiplications needed and also write the optimal
multiplication order.
3. Find an optimal paranthesization of a matrix-chain product whose sequence of
dimensions is 4x10,10x3,3x12,12x20,20x7
4 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
Floyd Warshall Algorithm
Inputs are the adjacency matrix of the given graph and total number of vertices
Algorithm FloydWarshall(cost[][], n)
{
for i=1 to n do
for j=1 to n do
D[i, j] = cost[i, j]
for k := 1 to n do
for i := 1 to n do
for j := 1 to n do
D[i, j] = min{D[i, j] , D[i, k] + D[k, j] }
Return D
}
Time Complexity
Floyd Warshall Algorithm consists of three loops over all the nodes. Each loop has
constant complexities.
Hence, the time complexity of Floyd Warshall algorithm = O(n3), where n is the
number of nodes in the given graph.
Example
Consider the following directed weighted graph. Using Floyd Warshall Algorithm, find
the shortest path distance between every pair of vertices
Solution
o Remove all self loops and parallel edges(keeping the lowest weight edge) of the
given graph
o Write the adjacency matrix
5 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
o D4 represents the shortest distance between each pair of the given graph
6 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
Comparison of Divide and Conquer and Dynamic Programming strategies
o Both techniques split their input into parts, find sub-solutions to the parts, and synthesize
larger solutions from smaller ones.
o Divide and Conquer splits its input at pre-specified deterministic points (e.g., most probably in
the middle)
o Dynamic Programming splits its input at every possible split points rather than at pre-specified
points. After trying all split points, it determines which split point is optimal.
o Divide & Conquer algorithm partition the problem into disjoint subproblems. Solve the
subproblems recursively and then combine their solution to solve the original problems.
o Dynamic Programming is used when the subproblems are not independent, e.g. when they
share the same subproblems. In this case, divide and conquer may do more work than
necessary, because it solves the same sub problem multiple times.
o Dynamic Programming solves each subproblems just once and stores the result in a table so
that it can be repeatedly retrieved if needed again.
Back Tracking
o Backtracking method expressed the solution as n-tuple (x1, x2.,…… xn), where xi‟s are chosen
from some finite set Si.
o The problem to be solved calls for finding one vector that maximizes (or minimizes or
satisfies) a criterion function P(x1, x2.,…… xn).
o Examples: Sorting the array of integers in a[l: n]
The solution to the problem is expressed an n-tuple, where xi is the index of the ith
smallest element.
The criterion function P is: a[xi] ≤ a[xi+1], for 1≤ i < n.
The set Si ={1,2, . . . . .n}
Different methods for solving this problem
Brute Force approach
o Suppose mi is the size of set Si.
o The number of tuples (with size n) that are possible candidates for satisfying the
function P is: m = m1 x m2 x m3 . . . . . . x mn
o Brute Force approach evaluates each one with P, and save those which yield the
optimum.
7 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
Backtracking algorithm
o It yields the same answer with far fewer than m trials.
o Its basic idea is to build up the solution vector one component at a time and to use
modified criterion functions (bounding functions) P i(x1, x2.,…… xi ) to test
whether the vector being formed has any chance of success.
o The major advantage of this method is that if it is realized that the partial vector
(x1, x2.,…… xi) can in no way lead to an optimal solution, then mi+1 x mi+2. . . . .
. x mn possible test vectors can be ignored entirely.
o Backtracking method require that all the solutions satisfy a complex set of constraints. These
constraints can be divided into two categories:
Explicit Constraints
Explicit constraints are rules that restrict each xi to take on values only from a given
set
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.
Example:
Implicit Constraints
These are rules that determine which of the tuples in the solution space of I satisfy the
criterion function (Bounding Function).
o N-Queens Problem
n queens are to be placed on a n x n chessboard so that no two attack. That is, no two
queens are on the same row, column, or diagonal.
Number the rows and columns of the chessboard 1through n.
The queens can also be numbered 1through n.
Since each queen must be on a different row, we can assume that queen i is to be placed
on row i.
All solutions to the n-queens problem can therefore be represented as n-tuples(x1,
x2.,…… xn), where xi is the column on which queen i is placed.
Explicit constraint: Si = {1,2,3, . . . . n }, 1≤ i ≤n
The solution space contains |S1| x |S2| x. . . . . . . x |Sn| = nn tuples.
Implicit constraints:
No two xi‟s can be the same(i.e. all queens must be on different columns)
o The solution space contains |S1| x |S2| x . . . . . . . x |Sn| = n(n-1) . . . . .1 = n! tuples
o It reduces the solution space from nn to n!.
No two queens can be on the same diagonal.
1 2 3 4
1 Q1
2 Q2
3 Q3
4 Q4
8 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
Following is a tree organization (permutation tree/State Space Tree) for 4-queen
problem without considering the last implicit constraint.
9 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
D-search(depth search): Each new node is placed into a stack. When all the
children of the current-E-node have been generated, the next node at the top of
the stack becomes the new E-node.
At the conclusion of the process at least one answer node is always generated or all
answer nodes are generated if the problem requires us to find all solutions.
10 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
11 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
th
o All the possible elements for the k position of the tuple that satisfy Bk are generated one by
one, and adjoined to the current vector (x1, x2.,…… xk-1 ).
o Each time xk is attached, a check is made to determine whether a solution has been found.
Then the algorithm is recursively invoked.
o When the for loop is exited, no more values for xk exist and the current copy of Backtrack
ends. The last unresolved call now resumes.
o This algorithm causes all solutions to be printed. If only a single solution is desired, then a flag
can be added as a parameter to indicate the first occurrence of success.
N-Queens Problem(Cond…)
o Consider an n x n chessboard and try to find all possible way to place n non-attacking
queens.
o (x1, x2.,…… xn ) be the solution vector. Queen i is placed in ith row and xith column. xi will
all be distinct since no two queens can be placed in the same column.
o There are 2 type of diagonals
Positive Diagonal
Diagonal from upper left to lower right
Every element on the same diagonal has the same row-column value
Suppose 2 queens are place at position (i,j) and (k,l), then i-j = k-l
Negative Diagonal
Diagonal from upper right to lower left
Every element on the same diagonal has the same row+column value
Suppose 2 queens are place at position (i,j) and (k,l), then i+j = k+l
The 1st equation implies: i-k = j-l
The 2nd equation implies: i-k = l - j
Combining these two, we will get : | i-k | = | j-l |
Absolute value of column difference is equal to the absolute value of row difference.
Algorithm NQueens(k,n)
{
for i=1 to n do
{ if Place(k,i) then
{ x[k] = i
if(k==n) then write(x[1:n])
else NQueens(k+1, n)
}
}
}
Algorithm Place(k,i)
{
for j=1 to k-1 do
{
if( (x[j]==i) or ( Abs(j-k)==Abs(x[j]-i) ) then
Return false
}
Return true
}
12 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
o Examples
Show the state space tree for 4 Queens problem. Show the steps in solving 4 Queens
problem using backtracking method to print all the solutions
13 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
Least() finds a live node with least ĉ. This node is deleted from the list of live
nodes and returned.
Add(x) adds the new live node x to the list of live nodes.
LCSearch outputs the path from the answer node to the root t.
LCSearch terminates only when either an answer node is found or the entire
state space tree has been generated and searched.
The control abstraction for LC, FIFO and LIFO are same. The only difference is
the implementation of the list of live nodes.
FIFO Search scheme:
o The list of live nodes is implemented as queue.
o Least() and Add(x) being algorithms to delete an element from
and add an element to the queue.
LIFO Search scheme:
o The list of live nodes is implemented as stack.
o Least() and Add(x) being algorithms to delete an element from
and add an element to the stack.
LC-Search Scheme:
o Add(x) is an algorithm to add elements to the list. Least() returns
a live node with least ĉ(.) from the list.
Solution
The adjacency matrix is
14 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
Total cost reduced = 2+2+1+1+0+0+0+0 = 6
The state space tree is
Cost reduced = r = 5
M2 is the matrix for node 2
Cost of node 2 = Cost of node 1 + M1[a, b] + r = 6 + 0 + 5 = 11
Find the matrix and cost of node 3
o Set row a and column c elements are α
o Set M1[c, a]= α
o The resultant matrix is
Cost reduced = r = 2
15 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
M3 is the matrix for node 3
Cost of node 3 = Cost of node 1 + M1[a, c] + r = 6 + 3 + 2 = 11
Find the matrix and cost of node 4
o Set row a and column d elements are α
o Set M1[d, a]= α
o The resultant matrix is
Cost reduced = r = 6
M4 is the matrix for node 4
Cost of node 4 = Cost of node 1 + M1[a, d] + r = 6 + 5 + 6 = 17
o Now the state space tree is
Now the live nodes are 2, 3 and 4. Minimum cost is for node 2 and 3. Choose
one node(say node 2) as the next E-node.
o Generate the child node of node 2
16 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
o Perform row reduction, then column reduction
Cost reduced = r = 2
M5 is the matrix for node 5
Cost of node 5 = Cost of node 2 + M1[b, c] + r = 11 + 5 + 2 = 18
Find the matrix and cost of node 6
o Set row b and column d elements are α
o Set M2[d, a]= α
o The resultant matrix is
Cost reduced = r = 0
M6 is the matrix for node 6
Cost of node 6 = Cost of node 2 + M1[b, d] + r = 11 + 0 + 0 = 11
Now the live nodes are 5, 6, 3 and 4. Choose one node which having minimum
cost(say node 6) as the next E-node.
17 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
o Generate the child node of node 6
Cost reduced = r = 0
M7 is the matrix for node 7
Cost of node 7 = Cost of node 6 + M6[d, c] + r = 11 + 0 + 0 = 11
18 CS KTU Lectures
CST 306 - Algorithm Analysis and Design(S6 CSE)
Now the live nodes are 5, 7, 3 and 4. Choose one node which having minimum
cost(say node 7) as the next E-node. Node 7 having no child node.
Now we can say that node 1 to node 7 path is the Traveling salesperson path.
19 CS KTU Lectures