all-pairs shortest paths

39
All-Pairs Shortest Paths Find the distance between every pair of vertices in a weighted graph G.

Upload: tashya-stokes

Post on 02-Jan-2016

62 views

Category:

Documents


4 download

DESCRIPTION

All-Pairs Shortest Paths. Find the distance between every pair of vertices in a weighted graph G. From A one may reach C faster by reaching by traveling from A to B and then from B to C. All-Pairs Shortest Paths. We can make n calls to Dijkstra’s algorithm O(n(n+m)log n) time. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: All-Pairs Shortest Paths

All-Pairs Shortest Paths• Find the distance between every pair of vertices in

a weighted graph G.

Page 2: All-Pairs Shortest Paths

All-Pairs Shortest Paths

• We can make n calls to Dijkstra’s algorithm – O(n(n+m)log n) time

From A one may reach C faster by reaching by traveling from A to B and then from B to C.

Alternatively ... Use dynamic programming: The Floyd’s Algorithm

O(n3) time.

Page 3: All-Pairs Shortest Paths

Floyd’s Shortest Path Algorithm

• Idea #1: Number the vertices 1, 2, …, n.• Idea #2: Shortest path between vertex i

and vertex j without passing through any other vertex is weight(edge(i,j)).– Let it be D0(i,j).

• Idea #3: If Dk(i,j) is the shortest path between vertex i and vertex j using vertices numbered 1, 2, …, k, as intermediate vertices, then Dn(i,j) is the solution to the shortest path problem between vertex i and vertex j.

Page 4: All-Pairs Shortest Paths

k

j

i

Uses only verticesnumbered 1,…,k-1 Uses only vertices

numbered 1,…,k-1

Uses only vertices numbered 1,…,k(compute weight of this edge)

Assume that we have Dk-1(i,j) for all i, and j.We wish to compute Dk(i,j).What are the possibilities? Choose between(1) a path through vertex k.

Path Length: Dk(i,j) = Dk-1(i,k) + Dk-1(k,j). (2) Skip vertex k altogether.

Path Length: Dk(i,j) = Dk-1(i,j).

Floyd’s Shortest Path Algorithm

Page 5: All-Pairs Shortest Paths

k

j

i

Uses only verticesnumbered 1,…,k-1 Uses only vertices

numbered 1,…,k-1

Uses only vertices numbered 1,…,k(compute weight of this edge)

otherwise

edge an is ),( if),(

if00, jijiji vvvvedgeweight

ji

D

1,

1,

1,, ,min k

jkkki

kji

kji DDDD

Floyd’s Shortest Path Algorithm

Page 6: All-Pairs Shortest Paths

Example:

4

1

3

5

2

1

1 1

8

2

43

8

0 8 3 1

8 0 4 2

3 4 0 1 1

1 1 0 8

2 1 8 0

D0

Floyd’s Shortest Path Algorithm

Page 7: All-Pairs Shortest Paths

Example:

4

1

3

5

2

1

1 1

8

2

43

8

0 8 3 1

8 0 4 9 2

3 4 0 1 1

1 9 1 0 8

2 1 8 0

D1

Floyd’s Shortest Path Algorithm

Page 8: All-Pairs Shortest Paths

Example:

4

1

3

5

2

1

1 1

8

2

43

8

0 8 3 1 10

8 0 4 9 2

3 4 0 1 1

1 9 1 0 8

10 2 1 8 0

D2

Floyd’s Shortest Path Algorithm

Page 9: All-Pairs Shortest Paths

Example:

4

1

3

5

2

1

1 1

8

2

43

8

0 7 3 1 4

7 0 4 5 2

3 4 0 1 1

1 5 1 0 2

4 2 1 2 0

D3

Floyd’s Shortest Path Algorithm

Page 10: All-Pairs Shortest Paths

Example:

4

1

3

5

2

1

1 1

8

2

43

8

0 6 2 1 3

6 0 4 5 2

2 4 0 1 1

1 5 1 0 2

3 2 1 2 0

D4

Floyd’s Shortest Path Algorithm

Page 11: All-Pairs Shortest Paths

Example:

4

1

3

5

2

1

1 1

8

2

43

8

0 5 2 1 3

5 0 3 4 2

2 3 0 1 1

1 4 1 0 2

3 2 1 2 0

D5

Floyd’s Shortest Path Algorithm

Page 12: All-Pairs Shortest Paths

Algorithm AllPair(G) {assumes vertices 1,…,n} for all vertex pairs (i,j)

if i jd0[i,i] 0

else if (i,j) is an edge in Gd0[i,j] weight of edge (i,j)

else d0[i,j] + for k 1 to n do

for i 1 to n do for j 1 to n do

dk[i,j] min(dk-1[i,j], dk-1[i,k]+dk-1[k,j])

Floyd’s Shortest Path Algorithm

O(n+m)or

O(n2)

O(n3)

Page 13: All-Pairs Shortest Paths

Floyd’s Shortest Path Algorithm

Observation: dk [i,k] = dk-1[i,k] dk[k,j] = dk-1[k,j]

Page 14: All-Pairs Shortest Paths

Algorithm AllPair(G) {assumes vertices 1,…,n} for all vertex pairs (i,j)

if i jd[i,i] 0

else if (i,j) is an edge in Gd[i,j] weight of edge (i,j)

else d[i,j] + for k 1 to n do

for i 1 to n do for j 1 to n do

d[i,j] min(d[i,j], d[i,k]+d[k,j])

Floyd’s Shortest Path Algorithm

Page 15: All-Pairs Shortest Paths

Algorithm AllPair(G) {assumes vertices 1,…,n} for all vertex pairs (i,j) path[i,j] null

if i jd[i,i] 0

else if (i,j) is an edge in Gd[i,j] weight of edge (i,j)

else d[i,j] + for k 1 to n do

for i 1 to n do for j 1 to n do

if (d[i,k]+d[k,j] < d[i,j]) path[i,j] vertex k d[i,j] d[i,k]+d[k,j]

Floyd’s Shortest Path Algorithm

Page 16: All-Pairs Shortest Paths

• Can be applied to Directed Graphs

• Can accept –ve edges as long as there are no –ve cycles.

Floyd’s Shortest Path Algorithm

Page 17: All-Pairs Shortest Paths

Transitive Closure• Transitive Relation:

A relation R on a set A is called transitive if and only if for any a, b, and c in A, whenever <a, b> R , and <b, c> R , <a, c> R

Page 18: All-Pairs Shortest Paths

Transitive Closure• Given a graph G, the

transitive closure of G is the G* such that– G* has the same

vertices as G– if G has a path from u

to v (u v), G* has a edge from u to v

B

A

D

C

E

B

A

D

C

E

G

G*The transitive closure providesreachability information about a graph.

Page 19: All-Pairs Shortest Paths

Computing the Transitive Closure

• We can perform BFS starting at each vertex– O(n(n+m))

If there's a way to get from A to B and from B to C, then there's a way to get from A to C.

Alternatively ... Use dynamic programming: Warshall’s Algorithm

Page 20: All-Pairs Shortest Paths

Floyd-Warshall Transitive Closure

• Idea #1: Number the vertices 1, 2, …, n.• Idea #2: Consider paths that use only

vertices numbered 1, 2, …, k, as intermediate vertices:

k

j

i

Uses only verticesnumbered 1,…,k-1 Uses only vertices

numbered 1,…,k-1

Uses only vertices numbered 1,…,k(add this edge if it’s not already in)

Page 21: All-Pairs Shortest Paths

Example:

4

1

3

20 1 0 1

1 0 1 0

0 1 0 1

1 0 1 0

A0

Warshall’s Transitive Closure

Page 22: All-Pairs Shortest Paths

Example:

4

1

3

20 1 0 1

1 0 1 1

0 1 0 1

1 1 1 0

A1

Warshall’s Transitive Closure

Page 23: All-Pairs Shortest Paths

Example:

4

1

3

20 1 1 1

1 0 1 1

1 1 0 1

1 1 1 0

A2

Warshall’s Transitive Closure

Page 24: All-Pairs Shortest Paths

Example:

4

1

3

20 1 1 1

1 0 1 1

1 1 0 1

1 1 1 0

A3

Warshall’s Transitive Closure

Page 25: All-Pairs Shortest Paths

Example:

4

1

3

20 1 1 1

1 0 1 1

1 1 0 1

1 1 1 0

A4

Warshall’s Transitive Closure

Page 26: All-Pairs Shortest Paths

Warshall’s Algorithm• Warshall’s algorithm numbers

the vertices of G as v1 , …, vn and computes a series of graphs G0, …, Gn

– G0=G

– Gk has a edge (vi, vj) if G has a path from vi to vj with intermediate vertices in the set {v1 , …, vk}

• We have that Gn = G*

• In phase k, graph Gk is computed from Gk 1

• Running time: O(n3), assuming areAdjacent is O(1) (e.g., adjacency matrix)

Algorithm FloydWarshall(G)Input graph GOutput transitive closure G* of Gi 1for all v G.vertices()

denote v as vi

i i + 1G0 Gfor k 1 to n do

Gk Gk 1

for i 1 to n (i k) dofor j 1 to n (j i, k) do

if Gk 1.areAdjacent(vi, vk) Gk 1.areAdjacent(vk, vj)

if Gk.areAdjacent(vi, vj) Gk.insertEdge(vi, vj , k)

return Gn

Page 27: All-Pairs Shortest Paths

Shortest Path Algorithm for DAGs

• Use Dijkstra’s algorithm– Time: O((n+m) log n)

• Use Topological sort based algorithm– Time: O(n+m).

Page 28: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORDPVD

MCODFW

SFO

LAX

LGA

HNL

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

Page 29: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORD4

PVD3

MCO1

DFW5

SFO8

LAX6

LGA2

HNL7

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

Page 30: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORD4

PVD3

MCO1

DFW5

SFO8

LAX6

LGA2

HNL7

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

$210

$224

$224

Page 31: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORD4

PVD3

MCO1

DFW5

SFO8

LAX6

LGA2

HNL7

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

$210

$224

$261

Page 32: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORD4

PVD3

MCO1

DFW5

SFO8

LAX6

LGA2

HNL7

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

$210

$224

$238

Page 33: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORD4

PVD3

MCO1

DFW5

SFO8

LAX6

LGA2

HNL7

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

$210

$224

$238$408

Page 34: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORD4

PVD3

MCO1

DFW5

SFO8

LAX6

LGA2

HNL7

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

$210

$224

$238$408

$208

$308

Page 35: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORD4

PVD3

MCO1

DFW5

SFO8

LAX6

LGA2

HNL7

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

$210

$224

$238$408

$208

$308

Page 36: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORD4

PVD3

MCO1

DFW5

SFO8

LAX6

LGA2

HNL7

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

$210

$224

$238$408

$208

$268

$719

Page 37: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORD4

PVD3

MCO1

DFW5

SFO8

LAX6

LGA2

HNL7

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

$210

$224

$238$408

$208

$268

$719

Page 38: All-Pairs Shortest Paths

Directed Graph With –ve weight

ORD4

PVD3

MCO1

DFW5

SFO8

LAX6

LGA2

HNL7

$170

-$100

$277-$200

-$100

$210

$224

$246

$68

$511

$28

$2

61

0

$210

$224

$238$408

$208

$268

$719

Page 39: All-Pairs Shortest Paths

DAG-based Algorithm

• Works even with negative-weight edges

• Uses topological order• Doesn’t use any fancy

data structures• Is much faster than

Dijkstra’s algorithm• Running time: O(n+m).

Algorithm DagDistances(G, s)for all v G.vertices()

v.parent nullif v s

v.distance 0else

v.distance Perform a topological sort of the verticesfor u 1 to n do {in topological order}

for each e G.outEdges(u)w G.opposite(u,e)r getDistance(u) weight(e)if r getDistance(w)

w.distance r w.parent u