Cs 331 Design and Analysis of Algorithms Dynamic

Cs 331 Design and Analysis of Algorithms Dynamic

1 CS 331 DESIGN AND ANALYSIS OF ALGORITHMS DYNAMIC PROGRAMMING Dr. Daisy Tang Dynamic Programming 2 Idea: Problems can be divided into stages Solution is a sequence of decisions, and the decision at the current stage is based on the results of the previous stage Example: Fibonacci Numbers 3 F(0) 0 F(1) 1 F(n) F(n 1) F(n 2) n 2 Assume n = 5 F(5) F(4) F(3) F(3) F(2) F(2) F(1) F(2) F(1) F(1) F(0) F(1) F(0) F(1) F(0) If We Use Divide-and-Conquer 4 Divide the problem into subproblems. Solve each subproblem independently and then combine the solutions. For example, F(3) is first calculated in the left subtree and it will be calculated again in the right subtree Subproblems are solved independently But Dynamic Programming 5 Works on phases Based on the results in previous 0 1 1 2 3 5 8 . phase, we can calculate result for the current phase Can be solved in a for-loop F[0] = 0 F[1] = 1 For i 2 to n F[i] = F[i-1] + F[i-2]; Greedy vs. Dynamic Programming 6 Consider shortest path problem Suppose we wish to find a shortest path from vertex i to vertex j. Let Ai be the vertices adjacent from vertex i. Which of the vertices in Ai should be the second vertex on the path? There is no way to make a decision at this time and guarantee that future decisions leading to an optimal sequence can be made. If on the other hand we wish to find a shortest path from vertex i to all other vertices in G, then at each step, a correct decision can be made based on the principle of optimality. Greedy vs. Dynamic Programming 7 Greedy: For problems that an optimal solution can be found by making the decisions one at a time and never making an erroneous decision [e.g., Knapsack] Consider only 1 decision sequence Dynamic programming: For problems that it is not possible to make stepwise decisions based only on local information Consider many decision sequences Try all possibilities but eliminates impossible cases based on the principle of optimality Principle of Optimality 8 Principle of optimality: An optimal sequence of decisions has the property that whatever the initial decision is, the remaining decisions must constitute an optimal decision sequence with regard to the state resulting from the first decision eliminates “not optimal” subsequences Examples 9 The shortest path problem satisfies the Principle of Optimality Subpath of a shortest path is also a shortest path The longest path problem does not satisfy the Principle of Optimality Consider a ring graph The subpath of a longest path might not be a longest path 1. Multistage Single-Source Single- Destination Shortest Path Problem 10 The Problem: Given: m columns (or stages) of n nodes each, a source node, a destination node. Assume edges only exist between stages. Goal: to find the shortest path from source (0, 1) to the destination (m-1, 1) Stage number Node number of that stage Straightforward vs. Dynamic Programming 11 Straightforward way: Try all possible paths Complexity: O(n m2 ) Dynamic programming: Let M(i, j) = length of the shortest path from the source (0,1) to (i, j) c(i, k, j) = distance from (i, k) to (i+1, j) Shortest path from want to find M(m-1, 1) source (0,1) to destination (m-1,1) Define functional equation: Define the relation between current stage and the result of previous stage n M (i, j) minM (i 1,k) c(i 1,k, j) k 1 M (0,1) 0 Example 12 3 4 1,1 2,1 3,1 2 1 2 3 2 1 4 5 2 2 3 0,1 1,2 2,2 3 3,2 4,1 1 5 1 3 2 5 4 Source 1,3 3 2,3 3,3 Destination 4 1 1 2 3 M (2,1) min1 3,2 4,1 2 33 1 2 3 Remember M (2,2) min11,2 2,1 3 21 the previous 1 2 3 node of this M (2,3) min1 3,2 5,1 4 41 shortest path 1 2 3 M (3,1) min3 4,2 5,4 3 71,2,3 1 2 3 M (3,2) min3 2,2 3,4 5 51,2 1 2 3 Can be used M (3,3) min 3 2,2 1,4 1 3 2 for constructing the shortest path 1 2 3 at the end M (4,1) min7 2,5 3,3 4 73 13 The shortest path is: (0,1) (1,1) (2,2) (3,3) (4,1) M: node( j) \ stage(i) 0 1 2 3 4 1 0 1 3 7 7 2 2 2 5 3 1 4 3 Algorithm: assign initially M(0,1)=0, M(1,1) =1, M(1,2) =2, M(1,3)=1; for i = 2 to (m – 2) // stages for j = 1 to n // nodes n M (i, j) minM (i 1,k) c(i 1,k, j) k 1 calculate and return M(m – 1,1); 14 Time Complexity 15 There are O(mn) M(i, j)’s to compute, each M(i, j) takes O(n) computation. So, total complexity is O(mn2). total # of nodes in the graph Let N = m n nodes max # of stages max # of nodes in each stage mn2 = mn n = N n Note: if use the Dijkstra’s algorithm, the complexity is O(N2) Revisit Dynamic Programming 16 The problem can be divided into stages. Need to make decisions at each stage. The decision for the current stage (based on the results of the previous stage) is independent from that of the previous stages. e.g. node 3,1 of the multistage shortest path problem, it is calculated from m(2,1), m(2,2), and m(2,3) but it is independent from how node 2,1 (2,2 or 2,3) makes its decision. Note that we find shortest paths from the source to all three nodes 2,1 and 2,2 and 2,3, but some of them may not be used on the shortest path at all. But every decision must be optimal, so that the overall result is optimal. This is called “Principle of Optimality”. Problem 2: All Pairs Shortest Paths 17 The problem: Given G = (V, E) a directed graph with n nodes Cost, a cost adjacency matrix for G: Costii = 0 Costij = cost of <i,j> if <i,j> E(G) otherwise Goal: to determine a matrix A, s.t. Aij = the length of the shortest path from i to j 1≤ i , j ≤ n Use Dijkstra’s algorithm, call it for n times: Time complexity is O(n3) The Dynamic Programming way: k Let Aij = the length of the shortest path from i to j going through no vertex of index greater than k n Want to find Aij 0 Observe that Aij = Cost Define functional equation: k k-1 k-1 k-1 Aij = min {Aij , Aik + Akj } 18 4 Example: v v 1 6 2 3 2 11 v3 0 4 11 A1 minA0 , A0 A0 4 A0 6 0 2 12 12 11 12 A1 minA0 , A0 A0 11 3 0 13 13 11 13 1 0 0 0 A21 minA21 , A21 A11 6 1 0 0 0 A23 minA23 , A21 A13 2 1 0 0 0 0 4 11 A31 minA31 , A31 A11 3 A1 6 0 2 1 0 0 0 A32 minA32 , A31 A12 7 3 7 0 19 2 1 1 1 A12 min A12 , A12 A22 4 2 1 1 1 A13 min A13 , A12 A23 6 2 1 1 1 A21 min A21 , A22 A21 6 2 1 1 1 A23 min A23 , A22 A23 2 0 4 6 2 1 1 1 A31 min A31 , A32 A21 3 2 A 6 0 2 2 1 1 1 A32 min A32 , A32 A22 7 3 7 0 3 2 2 2 A12 min A12 , A13 A32 4 3 2 2 2 A13 min A13 , A13 A33 6 3 2 2 2 A21 min A21 , A23 A31 5 3 2 2 2 A23 min A23 , A23 A33 2 3 2 2 2 0 4 6 A31 min A31 , A33 A31 3 3 A 3 min A 2 , A 2 A 2 7 A 5 0 2 32 32 33 32 3 7 0 20 Algorithm: procedure AllPairs (n, Cost, A) // n, Cost are input, A is output { for i 1 to n do for j 1 to n do A(i,j) ← Cost(i,j); for k 1 to n do for i 1 to n do for j 1 to n do A(i,j) ← min {A(i,j), A(i,k)+ A(k,j)}; } There are O(n) Ak’s to compute, each takes O(n2) computation, total complexity of AllPair is O(n3). 21 In order to print shortest paths for all pairs of vertices, we modify the algorithm: procedure AllPaths (n, Cost, A, P) // n, Cost are input, A and P are output // P(i,j) contains the intermediate node from i to j { for i = 1 to n do for j = 1 to n do procedure PrintOut (P, i, j) A(i,j) = Cost(i,j); { P(i,j) = j; if P(i,j) = j for k = 1 to n do then print(j); for i = 1 to n do else let k = P(i,j); for j = 1 to n do call PrintOut(P, i, k); if (A(i,j) > A(i,k)+ A(k,j) ) call PrintOut(P, k, j); then P(i,j) = k; } A(i,j) = A(i,k)+ A(k,j) ; for i = 1 to n do for j = 1 to n do print(i); Call PrintOut(P, i, j); } 22 In-Class Exercise #1 23 Solve the all pairs shortest path problem for the graph with the following weight matrix.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    42 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us