introduction to algorithms all-pairs shortest paths my t. uf

24
Introduction to Algorithms All-Pairs Shortest Paths My T. Thai @ UF

Upload: jason-oliver

Post on 18-Jan-2018

244 views

Category:

Documents


0 download

DESCRIPTION

 Can use algorithms for Single-Source Shortest Paths  Run BELLMAN-FORD once from each vertex  Time:  If there are no negative-weight edges, could run Dijkstra’s algorithm once from each vertex  Time: My T. Thai 3

TRANSCRIPT

Page 1: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Introduction to Algorithms

All-Pairs Shortest Paths

My T. Thai @ UF

Page 2: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Single-Source Shortest Paths Problem

Input: A weighted, directed graph G = (V, E) Output: An n × n matrix of shortest-path

distances δ. δ(i, j) is the weight of a shortest path from i to j.

My T. [email protected]

2

1 2 3 4 5

1 0 1 -3 2 -4

2 3 0 -4 1 -1

3 7 4 0 5 3

4 2 -1 -5 0 -2

5 8 5 1 6 0

Page 3: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Can use algorithms for Single-Source Shortest Paths

Run BELLMAN-FORD once from each vertex Time:

If there are no negative-weight edges, could run Dijkstra’s algorithm once from each vertex Time:

My T. [email protected]

3

Page 4: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Outline Shortest paths and matrix multiplication

Floyd-Warshall algorithm

Johnson’s algorithm

My T. [email protected]

4

Page 5: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Recursive solution

Optimal substructure: subpaths of shortest paths are shortest paths

Recursive solution: Let = weight of shortest path from i to j that contains ≤ m edges.

Where wij:My T. Thai

[email protected]

Page 6: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Computing the shortest-path weights bottom up

All simple shortest paths contain ≤ n − 1 edges

Compute from bottom up: L(1), L(2), . . . , L(n-1). Compute L(i+1) from L(i) by extending one more edgeTime:

My T. [email protected]

6

Page 7: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Time:

My T. [email protected]

7

Page 8: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Shortest paths and matrix multiplication Extending shortest paths by one more edge

likes matrix product: L(i+1)= L(i).W Compute L(1), L(2), L(4) . . . , L(r) with

My T. [email protected]

8

Time:

Page 9: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Example

My T. [email protected]

9

Page 10: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Floyd-Warshall algorithm For path p = <v1, v2, . . . , vl> , v2 … vl-1 are

intermediate vertices from v1 to vl Define = shortest-path weight of any path from i

to j with all intermediate vertices in {1, 2, . . . , k} Consider a shortest path with all intermediate

vertices in {1, 2, . . . , k}: If k is not an intermediate vertex, all intermediate vertices

in {1, 2, . . . , k -1} If k is an intermediate vertex:

[email protected]

Page 11: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Floyd-Warshall algorithm Recursive formula:

Time:

My T. [email protected]

11

Note: since we have at most n vertices, return

Page 12: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Constructing a shortest path is the predecessor of vertex j on a shortest

path from vertex i with all intermediate vertices in the set {1, 2, . . . , k}

My T. [email protected]

12

(Use vertex k)

Page 13: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Example

My T. [email protected]

13

Page 14: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

My T. [email protected]

14

Page 15: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

My T. [email protected]

15

Page 16: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Johnson’s algorithm Reweighting edges to get non-negative weight

edges: For all u, v V∈ , p is a shortest path using

w if and only if p is a shortest path using

For all (u, v) E, ∈ Run Dijkstra’s algorithm once from each vertex

My T. [email protected]

16

Page 17: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Reweighting

My T. [email protected]

17

Page 18: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Proof of lemma 25.1 First, we prove

With cycle ,

My T. [email protected]

18

Page 19: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Producing nonnegative weights Construct

Since no edges enter s, has the same set of cycles as G has a negative-weight cycle if and only if G does

Define: Claim:

Proof: Triangle inequality of shortest pathsMy T. Thai

[email protected]

Page 20: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Johnson’s algorithmTime:

My T. [email protected]

20

Page 21: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Example

My T. [email protected]

21

Page 22: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

My T. [email protected]

22

Page 23: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Summary Dynamic-programming algorithm based on matrix

multiplication Define sub-optimal solutions based on the length of paths Use the technique of “repeated squaring” Time: Floyd-Warshall algorithm Define sub-optimal solutions based on the set of allowed

intermediate vertices Time:

My T. [email protected]

23

Page 24: Introduction to Algorithms All-Pairs Shortest Paths My T. UF

Johnson’s algorithm Reweight edges to non-negative weight edges Run Dijkstra’s algorithm once from each vertex Time: Faster than Floyd-Warshall algorithm when the graph

is dense E = o(V2)

My T. [email protected]

24