Dynamic Programming Expected Outcomes n Students should be
Dynamic Programming
Expected Outcomes n Students should be able to n n Solve the all pairs shortest paths problem by dynamic programming, that is the Floyd’s algorithm Solve the transitive closure problem by dynamic programming, that is the Warshall’s algorithm Solve the knapsack problem by dynamic programming Solve the matrix chain multiplication problem by dynamic programming
Transitive Closure The transitive closure of a directed graph with n vertices can be defined as the nxn matrix T = {tij}, in which the element in the ith row (1 ≤ i ≤ n) and the jth column (1 ≤ j ≤ n) is 1 if there exists a nontrivial directed path (i. e. , a directed path of a positive length) from the ith vertex to the jth vertex; otherwise, tij is 0. Graph traversal-based algorithm and Warshall’s algorithm 3 3 1 1 2 4 0 1 0 0 0 1 Adjacency matrix 1 0 0 2 4 Transitive closure 0 1 0 1 1 1 0 1 0 1
Warshall’s Algorithm • Main idea: Use a bottom-up method to construct the transitive closure of a given digraph with n vertices through a series of nxn boolean matrices: T(0), …, T(k-1), T(k) , …, T(n) The question is: how to obtain T(k) from T(k-1) ? T(k) : tij(k) = 1 in T(k) , iff there is an edge from i to j; or there is a path from i to j going through vertex 1 and/or 2; or. . . there is a path from i to j going through 1, 2, … and/or k 3 1 2 0 1 0 0 1 4 T(0) 0 1 0 3 0 1 0 0 Does not allow an intermediate node 1 4 2 T(1) 0 0 1 1 1 0 0 0 1 0 0 3 2 T(2) 0 0 1 0 0 0 1 1 1 4 1 1 0 1 0 1 Allow 1 to be an Allow 1, 2 to be an intermediate node 3 1 4 2 T(3) 0 0 1 0 0 0 1 1 0 1 3 0 1 Allow 1, 2, 3 to be an intermediate node 4 2 T(4) 0 0 1 1 1 1 0 1 0 1 Allow 1, 2, 3, 4 to be an intermediate node
Warshall’s Algorithm In the kth stage: to determine T(k) is to determine if a path exists between two vertices i, j using just vertices among 1, …, k tij(k) = 1: { tij(k-1) = 1 or (tik(k-1) = 1 and tkj(k-1) = 1) k i (path using just 1 , …, k-1) (path from i to k and from k to i using just 1 , …, k-1) kth stage Rule to determine whether tij (k) should be 1 in T(k) : • If an element tij is 1 in T(k-1) , it remains 1 in T(k). j • If an element tij is 0 in T(k-1) , it has to be changed to 1 in T(k) iff the element in its row i and column k and the element in its column j and row k are both 1’s in T(k-1).
Compute Transitive Closure Time Complexity Space Complexity less space?
Floyd’s Algorithm: All pairs shortest paths problem: In a weighted graph, find shortest paths between every pair of vertices. dij(k) Applicable to: undirected and directed weighted graphs; no negative weight. = length of the shortest path from i to j with each vertex numbered no higher than k. Same idea as the Warshall’s algorithm : construct solution through series of matrices D(0) , D(1), …, D(n) Example: 3 4 1 1 6 1 0 5 5 2 3 4 4 6 0 1 3 0 weight matrix 0 1 0 6 5 4 4 0 1 3 0 distance matrix
Floyd’s Algorithm D(k) : allow 1, 2, …, k to be intermediate vertices. In the kth stage, determine whether the introduction of k as a new eligible intermediate vertex will bring about a shorter path from i to j. dij(k) = min{dij(k-1) , dik(k-1) + dkj(k-1)} for k 1, dij(0) = wij dik(k-1) i k dkj(k-1) dij(k-1) j kth stage
Floyd Algorithm Time Complexity: O(n 3), Space ?
Floyd algorithm (less space)
Constructing a shortest path n For k=0 n For k>=1
Print all-pairs shortest paths
Example: 2 4 3 1 3 8 -4 2 7 5 6 1 4 -5
Shortest path from 1 to 2 in 2 4 3 1 3 8 -4 2 7 5 6 1 4 -5
The Knapsack Problem n The problem n n Find the most valuable subset of the given n items that fit into a knapsack of capacity W. Consider the following subproblem P(i, j) n n n Find the most valuable subset of the first i items that fit into a knapsack of capacity j, where 1 i n, and 1 j W Let V[i, j] be the value of an optimal solution to the above subproblem P(i, j). Goal: V[n, W] The question: What is the recurrence relation that expresses a solution to this instance in terms of solutions to smaller subinstances?
The Knapsack Problem n The Recurrence n Two possibilities for the most valuable subset for the subproblem P(i, j) n n It does not include the ith item: V[i, j] = V[i-1, j] It includes the ith item: V[i, j] = vi+ V[i-1, j – wi] V[i, j] = max{V[i-1, j], vi+ V[i-1, j – wi] }, if j – wi 0 V[i-1, j] if j – wi < 0 { V[0, j] = 0 for j 0 and V[i, 0] = 0 for i 0
Dynamic Matrix Multiplication n n If we have a series of matrices of different sizes that we need to multiply, the order we do it can have a big impact on the number of operations For example, if we need to multiply four matrices M 1, M 2, M 3, and M 4 of sizes 20 5, 5 35, 35 4, and 4 25, depending on the order this can take between 3100 and 24, 500 multiplications
Dynamic Matrix Multiplication Problem n n n Input: a series of matrices M 1, M 2, …, MN with different sizes s 1 s 2, s 2 s 3, …, s. N+1 Output: the order of multiplication of these matrices such that the total number of multiplications is minimized Can you give a solution to this problem? n Try all possibilities to find the order, but n It is time consuming because the number of all possibilities is a Catalan number C(N-1) where
Dynamic Matrix Multiplication: Idea n We look at the ways n n to pair the matrices and then combine these into groups of three, and then four keep track of the efficiency of these partial results
Characterize the optimal solution n n Denote M[i: j] (i<j)as Mi. Mi+1…Mj, our problem is to find the optimal multiplication order of M[1: N]. Consider M[i: j] (i<j) and i k<j: n n The optimal value for M[i: j] must be the minimum one of the optimal values for M[i: k] plus for M[k+1: j] plus sisk+1 sj+1 for i k<j. A key property: n If M[i: j] is divide into M[i: k] and M[k+1: j], then the optimal order for M[i: j] contains the optimal order for M[i: k] and M[k+1: j].
Construct the recurrence relation for value of the optimal solution n Let cost[i: j] be the number of multiplications of the optimal solution for M[i: j]: n n n cost[i: j] = min i k<j{cost[i: k]+cost[k+1: j]+ sisk+1 sj+1} cost[i: i] = 0 cost[1: N]: minimum number of multiplications of the problem
Algorithm: bottom-up computation of the cost[1: n] for i = 1 to N do costi, i = 0 end for i = 1 to N-1 do for j = 1 to N-i do loc = i + j temp. Cost = for k = j to loc-1 do if temp. Cost > costj, k + costk+1, loc + sj*sk+1*sloc+1 then temp. Cost = costj, k + costk+1, loc + sj*sk+1*sloc+1 temp. Trace = k end if end for k costj, loc = temp. Cost tracej, loc = temp. Trace end for j end for i
Algorithm – Construct an Optimal Solution Get. Order( first, last, order ) if first < last then middle = tracefirst, last Get. Order( first, middle, order ) Get. Order( middle+1, last, order ) orderposition = middle position = position + 1 end if
Time Complexity n O( n 3)
- Slides: 28