Aad 4
Aad 4
• 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
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
▪ 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
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
▪ 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
if(D[i, j] > D[i, k] + D[k, j] )
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
o Find the matrix D2
▪ Keep the 2nd row, 2nd column and diagonal elements of D1 as such
▪ D2(1,3) = min{ D1(1,3), D1(1,2) + D1(2,3) } = min{-4, 9+2) = -4
▪ D2(1,4) = min{ D1(1,4), D1(1,2) + D1(2,4) } = min{α, 9+2) = 11
▪ D2(3,1) = min{ D1(3,1), D1(3,2) + D1(2,1) } = min{α, 5+6) = 11
▪ D2(3,4) = min{ D1(3,4), D1(3,2) + D1(2,4) } = min{α, 5+2) = 7
▪ D2(4,1) = min{ D1(4,1), D1(4,2) + D1(2,1) } = min{α, α+6) = α
▪ D2(4,3) = min{ D1(4,3), D1(4,2) + D1(2,3) } = min{1, α+2) = 1
o D4 represents the shortest distance between each pair of the given graph
6
• Comparison of Divide and Conquer and Dynamic Programming strategies
• 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
7
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.
8
• 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) Pi(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
9
▪ Following is a tree organization (permutation tree/State Space Tree) for 4-queen
problem without considering the last implicit constraint.
10
o Backtracking: Depth first node generation with bounding functions.
• Breadth First generation of the problem states:
o The -E-node remains the E-node until it is dead.
o Branch-and-bound methods: Breadth first node generation method with
bounding function is called Branch and Bound method. There are two alternatives
▪ Breadth First Generation Method: Each new node is placed into a queue.
When all the children of the current-E-node have been generated, the next
node at the front of the queue becomes the new E-node.
▪ 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.
11
State Space Tree of 4 Queens Problem
o Backtracking Control Abstraction
▪ (x1, x2.,…… xi) be a path from the root to a node in a state space tree.
▪ Generating Function T(x1, x2.,…… xi) be the set of all possible values for xi+1 such that
(x1, x2.,…… xi+1) is also a path to a problem state.
▪ T(x1, x2.,…… xn) = φ
▪ Bounding function Bi+1(x1, x2.,…… xi+1) is false for a path (x1, x2.,…… xi+1) from the
root node to a problem state, then the path cannot be extended to reach an answer node.
▪ Thus the candidates for position i+1of the solution vector (x1, x2.,…… xn) are those
values which are generated by T and satisfy Bi+1.
▪ The recursive version is initially invoked by Backtrack(1).
Backtracking Control Abstraction
Algorithm Backtrack(k)
{
for (each x[k] ϵ T(x[1], ....... x[k-1])
{
if(Bk(x[1], x[2], ..........., x[k]) != 0) then
{
if(x[1], x[2], ........... , x[k] is a path to an answer node)
then write(x[1:k])
if(k<n) then Backtrack(k+1)
}
}
}
12
o All the possible elements for the kth 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 x ith 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 2 equation implies:
nd
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
}
13
o Place(k,i) returns true if the kth queen can be placed in column i.
▪ i should be distinct from all previous values x[1],x[2], .............. , x[k-1]
▪ And no 2 queens are to be on the same diagonal
▪ Time complexity of Place() is O(k)
o NQueen() is initially invoked by NQueen(1,n)
▪ Time complexity of NQueen() is O(n!)
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
• Solution
• The adjacency matrix is
14
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
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
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
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
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