1 / 24

DP algorithms for all-pairs Shortest Path

DP algorithms for all-pairs Shortest Path. Tandy Warnow. “Shortest Path”. Given graph G=(V,E) with positive weights on the edges (w:E->R + ), and given two vertices a and b.

Télécharger la présentation

DP algorithms for all-pairs Shortest Path

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DP algorithms for all-pairs Shortest Path Tandy Warnow

  2. “Shortest Path” • Given graph G=(V,E) with positive weights on the edges (w:E->R+), and given two vertices a and b. • Find the “shortest path” from a to b (where the length of the path is the sum of the edge weights on the path). Perhaps we should call this the minimum weight path!

  3. Greedy algorithm • Start at a, and greedily construct a path that goes to w by adding vertices that are closest to the current endpoint, until you reach b. • Problem: it doesn’t work correctly! Sometimes you can’t reach b at all, and would have to backtrack… and sometimes you get a path that doesn’t have the minimum weight.

  4. Designing a DP solution • How are the subproblems defined? • Where is the answer stored? • How are the boundary values computed? • How do we compute each entry from other entries? • What is the order in which we fill in the matrix?

  5. Two DP algorithms • Both are correct. Both produce correct values for all-pairs shortest paths. • The difference is the subproblem formulation, and hence in the running time. • The reason both algorithms are given is to teach you how to do DP algorithms! • But, be prepared to provide one or both of these algorithms, and to be able to apply it to an input (on some exam, for example).

  6. Dynamic programming First attempt: let {1,2,…,n} denote the set of vertices. Subproblem formulation: • M[i,j,k] = min length of any path from i to j that uses at most k edges. All paths have at most n-1 edges, so 1 ≤ k ≤ n-1.)

  7. First DP approach • M[i,j,k] = min length of any path from i to j that uses at most k edges. • How to set the boundary case (k=1)? • How to set M[i,j,k] from other entries?

  8. How to set the boundary case (k=1)? • Easy: M[x,y,1] = • 0 if x=y • w(x,y) if x and y are different, and (x,y) is an edge, and • infinity otherwise

  9. How to set M[i,j,k] from other entries? • Consider a minimum weight path from i to j that has at most k edges. How many edges does it have?

  10. How to set M[i,j,k] from other entries, for k>1? • Consider a minimum weight path from i to j that has at most k edges. • Case 1: The minimum weight path has at most k-1 edges. • Case 2: The minimum weight path has exactly k edges.

  11. How to set M[i,j,k] from other entries, for k>1? • Case 1: The minimum weight path has at most most k-1 edges. Then M[i,j,k] = M[i,j,k-1]

  12. How to set M[i,j,k] from other entries, for k>1? • Case 2: The minimum weight path has exactly k edges. Then we break the path into two paths: one with k-1 edges, and one with 1 edge. And so: • M[i,j,k] = min{M[i,x,k-1] + w(x,j): x in V-{i,j}}

  13. Since the minimum weight path from i to j with at most k>0 edges will satisfy one of these two properties, we can set M[i,j,k] from other entries, as follows: • M[i,j,k] = min{ min{M[i,x,k-1] + w(x,j): x in V}, M[i,j,k-1]} Note that the third index goes down!

  14. Finishing the design • Where is the answer stored? • How are the boundary values computed? • How do we compute each entry from other entries? • What is the order in which we fill in the matrix? • Running time?

  15. Running time analysis • How many entries do we need to compute? O(n3) 0 ≤ i ≤ n 0 ≤ j ≤ n 1 ≤ k ≤ n-1 • How much time does it take to compute each entry, once the “previous” ones are computed? O(n)

  16. Running time analysis (cont) • O(n3) elements, • each takes O(n) time to compute, if computed in the correct order! • so O(n4) time

  17. Next DP approach • Try a new subproblem formulation! • Q[i,j,k] = minimum weight of any path from i to j that uses internal vertices drawn from {1,2,…,k}.

  18. Designing a DP solution • How are the subproblems defined? • Where is the answer stored? • How are the boundary values computed? • How do we compute each entry from other entries? • What is the order in which we fill in the matrix?

  19. Q[i,j,k] = minimum weight of any path from i to j that uses internal vertices drawn from {1,2,…,k}. • Boundary cases: Q[i,j,0] for all i,j • Solution to minimum found in Q[i,j,n] for minimum weight path from i to j • Once again, O(n3) entries in the matrix

  20. Solving subproblems • Q[i,j,k] = minimum weight of any path from i to j that uses internal vertices drawn from {1,2,…,k}. • The minimum cost such path either includes vertex k or does not include vertex k.

  21. Solving subproblems • Q[i,j,k] = minimum weight of any path from i to j that uses internal vertices drawn from {1,2,…,k}. • If the minimum cost path P includes vertex k, then you can divide P into the path P1 from i to p, and P2 from p to j. • What is the weight of P1? • What is the weight of P2?

  22. Solving subproblems • Q[i,j,k] = minimum weight of any path from i to j that uses internal vertices drawn from {1,2,…,k}. • P is a minimum cost path from I to j that uses vertex k, and has all internal vertices from {1,2,…k}. • Path P1 from i to p, and P2 from p to j. • The weight of P1 is Q[i,p,p-1] (why??). • The weight of P2 is Q[p,j,p-1] (why??). • Thus the weight of P is Q[i,p,p-1] + Q[p,j,p-1].

  23. New DP algorithm For k=0 up to n DO Compute Q[i,j,k] for each i,j End for

  24. Running time analysis • Each entry only takes O(1) time to compute • There are O(n3) entries • Hence, O(n3) time.

More Related