All Pairs Shortest Paths Eric Price UT Austin CS 331, Spring 2020 - - PowerPoint PPT Presentation

all pairs shortest paths
SMART_READER_LITE
LIVE PREVIEW

All Pairs Shortest Paths Eric Price UT Austin CS 331, Spring 2020 - - PowerPoint PPT Presentation

All Pairs Shortest Paths Eric Price UT Austin CS 331, Spring 2020 Coronavirus Edition CS 331, Spring Eric Price (UT Austin) All Pairs Shortest Paths / 10 Talk Outline APSP 1 Problems 2 CS 331, Spring Eric Price (UT Austin) All Pairs


slide-1
SLIDE 1

All Pairs Shortest Paths

Eric Price

UT Austin

CS 331, Spring 2020 Coronavirus Edition

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-2
SLIDE 2

Talk Outline

1

APSP

2

Problems

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-3
SLIDE 3

All Pairs Shortest Paths

Given a graph G, find shortest s t path distance for all s, t ∈ V .

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-4
SLIDE 4

All Pairs Shortest Paths

Given a graph G, find shortest s t path distance for all s, t ∈ V . Approaches:

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-5
SLIDE 5

All Pairs Shortest Paths

Given a graph G, find shortest s t path distance for all s, t ∈ V . Approaches:

◮ Bellman-Ford for all s: O(V 2E) Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-6
SLIDE 6

All Pairs Shortest Paths

Given a graph G, find shortest s t path distance for all s, t ∈ V . Approaches:

◮ Bellman-Ford for all s: O(V 2E) ◮ Dijkstra for all s: O(VE + V 2 log V ) if nonnegative weights Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-7
SLIDE 7

All Pairs Shortest Paths

Given a graph G, find shortest s t path distance for all s, t ∈ V . Approaches:

◮ Bellman-Ford for all s: O(V 2E) ◮ Dijkstra for all s: O(VE + V 2 log V ) if nonnegative weights ◮ Floyd-Warshall: O(V 3) Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-8
SLIDE 8

All Pairs Shortest Paths

Given a graph G, find shortest s t path distance for all s, t ∈ V . Approaches:

◮ Bellman-Ford for all s: O(V 2E) ◮ Dijkstra for all s: O(VE + V 2 log V ) if nonnegative weights ◮ Floyd-Warshall: O(V 3) ◮ Johnson: O(VE + V 2 log V ) in general Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-9
SLIDE 9

Floyd-Warshall

1: function FloydWarshall(A)

⊲ A[u → v] is cost of u → v edge

2:

D ← A ⊲ (or ∞ if no edge)

3:

for w ∈ V do

4:

for u ∈ V do

5:

for v ∈ V do

6:

D[u, v] ← min(D[u, v], D[u, w] + D[w, v])

7:

return D

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-10
SLIDE 10

Floyd-Warshall

1: function FloydWarshall(A)

⊲ A[u → v] is cost of u → v edge

2:

D ← A ⊲ (or ∞ if no edge)

3:

for w ∈ V do

4:

for u ∈ V do

5:

for v ∈ V do

6:

D[u, v] ← min(D[u, v], D[u, w] + D[w, v])

7:

return D Takes an adjacency matrix, returns a distance matrix

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-11
SLIDE 11

Floyd-Warshall

1: function FloydWarshall(A)

⊲ A[u → v] is cost of u → v edge

2:

D ← A ⊲ (or ∞ if no edge)

3:

for w ∈ V do

4:

for u ∈ V do

5:

for v ∈ V do

6:

D[u, v] ← min(D[u, v], D[u, w] + D[w, v])

7:

return D Takes an adjacency matrix, returns a distance matrix O(n3) time

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-12
SLIDE 12

Floyd-Warshall

1: function FloydWarshall(A)

⊲ A[u → v] is cost of u → v edge

2:

D ← A ⊲ (or ∞ if no edge)

3:

for w ∈ V do

4:

for u ∈ V do

5:

for v ∈ V do

6:

D[u, v] ← min(D[u, v], D[u, w] + D[w, v])

7:

return D Takes an adjacency matrix, returns a distance matrix O(n3) time

Lemma

Let P = (s, u1, . . . , uk, t) be a shortest s → t path. After visiting w / ∈ {s, t}, P \ {w} is also a shortest s t path in D.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-13
SLIDE 13

Floyd-Warshall

1: function FloydWarshall(A)

⊲ A[u → v] is cost of u → v edge

2:

D ← A ⊲ (or ∞ if no edge)

3:

for w ∈ V do

4:

for u ∈ V do

5:

for v ∈ V do

6:

D[u, v] ← min(D[u, v], D[u, w] + D[w, v])

7:

return D Takes an adjacency matrix, returns a distance matrix O(n3) time

Lemma

Let P = (s, u1, . . . , uk, t) be a shortest s → t path. After visiting w / ∈ {s, t}, P \ {w} is also a shortest s t path in D. Q: negative edges?

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-14
SLIDE 14

Floyd-Warshall

1: function FloydWarshall(A)

⊲ A[u → v] is cost of u → v edge

2:

D ← A ⊲ (or ∞ if no edge)

3:

for w ∈ V do

4:

for u ∈ V do

5:

for v ∈ V do

6:

D[u, v] ← min(D[u, v], D[u, w] + D[w, v])

7:

return D Takes an adjacency matrix, returns a distance matrix O(n3) time

Lemma

Let P = (s, u1, . . . , uk, t) be a shortest s → t path. After visiting w / ∈ {s, t}, P \ {w} is also a shortest s t path in D. Q: negative edges? OK

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-15
SLIDE 15

Floyd-Warshall

1: function FloydWarshall(A)

⊲ A[u → v] is cost of u → v edge

2:

D ← A ⊲ (or ∞ if no edge)

3:

for w ∈ V do

4:

for u ∈ V do

5:

for v ∈ V do

6:

D[u, v] ← min(D[u, v], D[u, w] + D[w, v])

7:

return D Takes an adjacency matrix, returns a distance matrix O(n3) time

Lemma

Let P = (s, u1, . . . , uk, t) be a shortest s → t path. After visiting w / ∈ {s, t}, P \ {w} is also a shortest s t path in D. Q: negative edges? OK Negative cycles?

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-16
SLIDE 16

Floyd-Warshall

1: function FloydWarshall(A)

⊲ A[u → v] is cost of u → v edge

2:

D ← A ⊲ (or ∞ if no edge)

3:

for w ∈ V do

4:

for u ∈ V do

5:

for v ∈ V do

6:

D[u, v] ← min(D[u, v], D[u, w] + D[w, v])

7:

return D Takes an adjacency matrix, returns a distance matrix O(n3) time

Lemma

Let P = (s, u1, . . . , uk, t) be a shortest s → t path. After visiting w / ∈ {s, t}, P \ {w} is also a shortest s t path in D. Q: negative edges? OK Negative cycles? Check if diagonal < 0

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-17
SLIDE 17

Floyd-Warshall

1: function FloydWarshall(A)

⊲ A[u → v] is cost of u → v edge

2:

D ← A ⊲ (or ∞ if no edge)

3:

for w ∈ V do

4:

for u ∈ V do

5:

for v ∈ V do

6:

D[u, v] ← min(D[u, v], D[u, w] + D[w, v])

7:

return D Takes an adjacency matrix, returns a distance matrix O(n3) time

Lemma

Let P = (s, u1, . . . , uk, t) be a shortest s → t path. After visiting w / ∈ {s, t}, P \ {w} is also a shortest s t path in D. Q: negative edges? OK Negative cycles? Check if diagonal < 0

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-18
SLIDE 18

Johnson’s algorithm

Recall:

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-19
SLIDE 19

Johnson’s algorithm

Recall:

◮ Dijkstra would be great if we had nonnegative edges Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-20
SLIDE 20

Johnson’s algorithm

Recall:

◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R, the graph with weights

w ′(u → v) := w(u → v) − h(u) + h(v). has shortest path distances D′[s, t] = D[s, t] + h(t) − h(s).

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-21
SLIDE 21

Johnson’s algorithm

Recall:

◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R, the graph with weights

w ′(u → v) := w(u → v) − h(u) + h(v). has shortest path distances D′[s, t] = D[s, t] + h(t) − h(s).

Idea: in O(VE + V 2 log V ),

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-22
SLIDE 22

Johnson’s algorithm

Recall:

◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R, the graph with weights

w ′(u → v) := w(u → v) − h(u) + h(v). has shortest path distances D′[s, t] = D[s, t] + h(t) − h(s).

Idea: in O(VE + V 2 log V ),

1

Compute a single h so w ′(u → v) ≥ 0 for all h.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-23
SLIDE 23

Johnson’s algorithm

Recall:

◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R, the graph with weights

w ′(u → v) := w(u → v) − h(u) + h(v). has shortest path distances D′[s, t] = D[s, t] + h(t) − h(s).

Idea: in O(VE + V 2 log V ),

1

Compute a single h so w ′(u → v) ≥ 0 for all h. (h consistent)

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-24
SLIDE 24

Johnson’s algorithm

Recall:

◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R, the graph with weights

w ′(u → v) := w(u → v) − h(u) + h(v). has shortest path distances D′[s, t] = D[s, t] + h(t) − h(s).

Idea: in O(VE + V 2 log V ),

1

Compute a single h so w ′(u → v) ≥ 0 for all h. (h consistent part 2)

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-25
SLIDE 25

Johnson’s algorithm

Recall:

◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R, the graph with weights

w ′(u → v) := w(u → v) − h(u) + h(v). has shortest path distances D′[s, t] = D[s, t] + h(t) − h(s).

Idea: in O(VE + V 2 log V ),

1

Compute a single h so w ′(u → v) ≥ 0 for all h. (h consistent part 2)

2

Compute D′ for every source s using Dijkstra on w ′.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-26
SLIDE 26

Johnson’s algorithm

Recall:

◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R, the graph with weights

w ′(u → v) := w(u → v) − h(u) + h(v). has shortest path distances D′[s, t] = D[s, t] + h(t) − h(s).

Idea: in O(VE + V 2 log V ),

1

Compute a single h so w ′(u → v) ≥ 0 for all h. (h consistent part 2)

2

Compute D′ for every source s using Dijkstra on w ′.

3

Output D.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-27
SLIDE 27

Johnson’s algorithm

Recall:

◮ Dijkstra would be great if we had nonnegative edges ◮ Reweighting: for any h : V → R, the graph with weights

w ′(u → v) := w(u → v) − h(u) + h(v). has shortest path distances D′[s, t] = D[s, t] + h(t) − h(s).

Idea: in O(VE + V 2 log V ),

1

Compute a single h so w ′(u → v) ≥ 0 for all h. (h consistent part 2)

2

Compute D′ for every source s using Dijkstra on w ′.

3

Output D.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-28
SLIDE 28

Finding a good heuristic

Pick an arbitrary source s∗.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-29
SLIDE 29

Finding a good heuristic

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-30
SLIDE 30

Finding a good heuristic

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time. Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-31
SLIDE 31

Finding a good heuristic

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time.

Set h(u) := −D[s∗, u]

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-32
SLIDE 32

Finding a good heuristic

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time.

Set h(u) := −D[s∗, u] Now, for any u, v ∈ V , w′(u → v) := w(u → v) − h(u) + h(v) = w(u → v) + D[s∗, u] − D[s∗, v].

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-33
SLIDE 33

Finding a good heuristic

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time.

Set h(u) := −D[s∗, u] Now, for any u, v ∈ V , w′(u → v) := w(u → v) − h(u) + h(v) = w(u → v) + D[s∗, u] − D[s∗, v]. But by the triangle inequality, D[s∗, v] ≤ D[s∗, u] + w(u → v) so w′(u → v) ≥ 0.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-34
SLIDE 34

Johnson’s algorithm

Pick an arbitrary source s∗.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-35
SLIDE 35

Johnson’s algorithm

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-36
SLIDE 36

Johnson’s algorithm

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time. Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-37
SLIDE 37

Johnson’s algorithm

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time.

Set h(u) := −D[s∗, u]

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-38
SLIDE 38

Johnson’s algorithm

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time.

Set h(u) := −D[s∗, u] Define the graph with weights w′(u → v) := w(u → v) − h(u) + h(v).

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-39
SLIDE 39

Johnson’s algorithm

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time.

Set h(u) := −D[s∗, u] Define the graph with weights w′(u → v) := w(u → v) − h(u) + h(v). For every s ∈ V , run Dijkstra from s on w′ to compute distance matrix D′

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-40
SLIDE 40

Johnson’s algorithm

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time.

Set h(u) := −D[s∗, u] Define the graph with weights w′(u → v) := w(u → v) − h(u) + h(v). For every s ∈ V , run Dijkstra from s on w′ to compute distance matrix D′ Output D[s, t] = D′[s, t] − h(t) + h(s).

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-41
SLIDE 41

Johnson’s algorithm

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time.

Set h(u) := −D[s∗, u] Define the graph with weights w′(u → v) := w(u → v) − h(u) + h(v). For every s ∈ V , run Dijkstra from s on w′ to compute distance matrix D′ Output D[s, t] = D′[s, t] − h(t) + h(s). Overall: O(VE + V 2 log V ) time.

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-42
SLIDE 42

Johnson’s algorithm

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time.

Set h(u) := −D[s∗, u] Define the graph with weights w′(u → v) := w(u → v) − h(u) + h(v). For every s ∈ V , run Dijkstra from s on w′ to compute distance matrix D′ Output D[s, t] = D′[s, t] − h(t) + h(s). Overall: O(VE + V 2 log V ) time. Q:

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-43
SLIDE 43

Johnson’s algorithm

Pick an arbitrary source s∗. Compute shortest paths D[s∗, u] from s∗ on original graph.

◮ with Bellman-Ford in O(VE) time.

Set h(u) := −D[s∗, u] Define the graph with weights w′(u → v) := w(u → v) − h(u) + h(v). For every s ∈ V , run Dijkstra from s on w′ to compute distance matrix D′ Output D[s, t] = D′[s, t] − h(t) + h(s). Overall: O(VE + V 2 log V ) time. Q: negative cycles?

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-44
SLIDE 44

All Pairs Shortest Paths

Given a graph G, find shortest s t path distance for all s, t ∈ V . Approaches:

◮ Bellman-Ford for all s: O(V 2E) ◮ Dijkstra for all s: O(VE + V 2 log V ) if nonnegative weights ◮ Floyd-Warshall: O(V 3) ◮ Johnson: O(VE + V 2 log V ) in general Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-45
SLIDE 45

Talk Outline

1

APSP

2

Problems

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-46
SLIDE 46

Shortest Path Problems

http://jeffe.cs.illinois.edu/teaching/algorithms/book/ 08-sssp.pdf Problem 2: Dijkstra with k negative edges. Problem 3: vertices, not edges, have weight. Problem 5: edge reinsertion Problem 4: Replacement paths on directed graphs Problem 12: Smallest shortest path Problem 16, 17: Remember reductions? Problem 1 of https://www.cs.utexas.edu/~ecprice/courses/ 331h/psets/331h-ps6.pdf

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-47
SLIDE 47

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10

slide-48
SLIDE 48

Eric Price (UT Austin) All Pairs Shortest Paths CS 331, Spring / 10