dp algorithms for all-pairs shortest path

24
DP algorithms for all- pairs Shortest Path Tandy Warnow

Upload: frances-bowers

Post on 31-Dec-2015

58 views

Category:

Documents


2 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: DP algorithms for all-pairs Shortest Path

DP algorithms for all-pairs Shortest Path

Tandy Warnow

Page 2: DP algorithms for all-pairs Shortest Path

“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!

Page 3: DP algorithms for all-pairs Shortest Path

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.

Page 4: DP algorithms for all-pairs Shortest Path

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?

Page 5: DP algorithms for all-pairs Shortest Path

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).

Page 6: DP algorithms for all-pairs Shortest Path

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.)

Page 7: DP algorithms for all-pairs Shortest Path

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?

Page 8: DP algorithms for all-pairs Shortest Path

• 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

Page 9: DP algorithms for all-pairs Shortest Path

• 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?

Page 10: DP algorithms for all-pairs Shortest Path

• 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.

Page 11: DP algorithms for all-pairs Shortest Path

• 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]

Page 12: DP algorithms for all-pairs Shortest Path

• 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}}

Page 13: DP algorithms for all-pairs Shortest Path

• 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!

Page 14: DP algorithms for all-pairs Shortest Path

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?

Page 15: DP algorithms for all-pairs Shortest Path

Running time analysis

• How many entries do we need to compute? O(n3)0 ≤ i ≤ n0 ≤ j ≤ n1 ≤ k ≤ n-1

• How much time does it take to compute each entry, once the “previous” ones are computed? O(n)

Page 16: DP algorithms for all-pairs Shortest Path

Running time analysis (cont)

• O(n3) elements,

• each takes O(n) time to compute, if computed in the correct order!

• so O(n4) time

Page 17: DP algorithms for all-pairs Shortest Path

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}.

Page 18: DP algorithms for all-pairs Shortest Path

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?

Page 19: DP algorithms for all-pairs Shortest Path

• 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

Page 20: DP algorithms for all-pairs Shortest Path

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.

Page 21: DP algorithms for all-pairs Shortest Path

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?

Page 22: DP algorithms for all-pairs Shortest Path

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].

Page 23: DP algorithms for all-pairs Shortest Path

New DP algorithm

For k=0 up to n DO

Compute Q[i,j,k] for each i,j

End for

Page 24: DP algorithms for all-pairs Shortest Path

Running time analysis

• Each entry only takes O(1) time to compute

• There are O(n3) entries

• Hence, O(n3) time.