+ All Categories
Home > Documents > Lecture 15: All-Pairs Shortest Paths

Lecture 15: All-Pairs Shortest Paths

Date post: 05-Apr-2022
Category:
Upload: others
View: 15 times
Download: 0 times
Share this document with a friend
47
Lecture 15: All-Pairs Shortest Paths Michael Dinitz October 19, 2021 601.433/633 Introduction to Algorithms Michael Dinitz Lecture 15: APSP October 19, 2021 1 / 13
Transcript
Page 1: Lecture 15: All-Pairs Shortest Paths

Lecture 15: All-Pairs Shortest Paths

Michael Dinitz

October 19, 2021601.433/633 Introduction to Algorithms

Michael Dinitz Lecture 15: APSP October 19, 2021 1 / 13

Page 2: Lecture 15: All-Pairs Shortest Paths

Announcements

▸ HW5 due now

▸ HW6 due next Thursday

▸ Mid-Semester feedback on Campuswire!

Michael Dinitz Lecture 15: APSP October 19, 2021 2 / 13

Page 3: Lecture 15: All-Pairs Shortest Paths

Introduction

Setup:

▸ Directed graph G = (V,E)▸ Length `(x,y) on each edge (x,y) ∈ E▸ Length of path P is `(P) = ∑(x,y)∈P `(x,y)▸ d(x,y) =minx→y paths P `(P)

Last time: All distances from source node v ∈ V.

Today: Distances between all pairs of nodes!

Obvious solution: single-source from each v ∈ V▸ No negative weights: n runs of Dijkstra, time O(n(m + n log n))▸ Negative weights: n runs of Bellman-Ford, time O(nmn) = O(mn2)

Can we do better? Particularly for negative edge weights?

▸ Main goal today: Negative weights as fast as possible.

Michael Dinitz Lecture 15: APSP October 19, 2021 3 / 13

Page 4: Lecture 15: All-Pairs Shortest Paths

Introduction

Setup:

▸ Directed graph G = (V,E)▸ Length `(x,y) on each edge (x,y) ∈ E▸ Length of path P is `(P) = ∑(x,y)∈P `(x,y)▸ d(x,y) =minx→y paths P `(P)

Last time: All distances from source node v ∈ V.

Today: Distances between all pairs of nodes!

Obvious solution:

single-source from each v ∈ V▸ No negative weights: n runs of Dijkstra, time O(n(m + n log n))▸ Negative weights: n runs of Bellman-Ford, time O(nmn) = O(mn2)

Can we do better? Particularly for negative edge weights?

▸ Main goal today: Negative weights as fast as possible.

Michael Dinitz Lecture 15: APSP October 19, 2021 3 / 13

Page 5: Lecture 15: All-Pairs Shortest Paths

Introduction

Setup:

▸ Directed graph G = (V,E)▸ Length `(x,y) on each edge (x,y) ∈ E▸ Length of path P is `(P) = ∑(x,y)∈P `(x,y)▸ d(x,y) =minx→y paths P `(P)

Last time: All distances from source node v ∈ V.

Today: Distances between all pairs of nodes!

Obvious solution: single-source from each v ∈ V

▸ No negative weights: n runs of Dijkstra, time O(n(m + n log n))▸ Negative weights: n runs of Bellman-Ford, time O(nmn) = O(mn2)

Can we do better? Particularly for negative edge weights?

▸ Main goal today: Negative weights as fast as possible.

Michael Dinitz Lecture 15: APSP October 19, 2021 3 / 13

Page 6: Lecture 15: All-Pairs Shortest Paths

Introduction

Setup:

▸ Directed graph G = (V,E)▸ Length `(x,y) on each edge (x,y) ∈ E▸ Length of path P is `(P) = ∑(x,y)∈P `(x,y)▸ d(x,y) =minx→y paths P `(P)

Last time: All distances from source node v ∈ V.

Today: Distances between all pairs of nodes!

Obvious solution: single-source from each v ∈ V▸ No negative weights: n runs of Dijkstra, time O(n(m + n log n))▸ Negative weights: n runs of Bellman-Ford, time O(nmn) = O(mn2)

Can we do better? Particularly for negative edge weights?

▸ Main goal today: Negative weights as fast as possible.

Michael Dinitz Lecture 15: APSP October 19, 2021 3 / 13

Page 7: Lecture 15: All-Pairs Shortest Paths

Introduction

Setup:

▸ Directed graph G = (V,E)▸ Length `(x,y) on each edge (x,y) ∈ E▸ Length of path P is `(P) = ∑(x,y)∈P `(x,y)▸ d(x,y) =minx→y paths P `(P)

Last time: All distances from source node v ∈ V.

Today: Distances between all pairs of nodes!

Obvious solution: single-source from each v ∈ V▸ No negative weights: n runs of Dijkstra, time O(n(m + n log n))▸ Negative weights: n runs of Bellman-Ford, time O(nmn) = O(mn2)

Can we do better? Particularly for negative edge weights?

▸ Main goal today: Negative weights as fast as possible.

Michael Dinitz Lecture 15: APSP October 19, 2021 3 / 13

Page 8: Lecture 15: All-Pairs Shortest Paths

Floyd-Warshall Algorithm

Michael Dinitz Lecture 15: APSP October 19, 2021 4 / 13

Page 9: Lecture 15: All-Pairs Shortest Paths

Floyd-Warshall: A Different Dynamic Programming Approach

To simplify notation, let V = {1,2, . . . ,n} and `(i, j) =∞ if (i, j) /∈ EBellman-Ford subproblems: length of shortest path with at most some number of edges

New subproblems:▸ Intuition: “shortest path from u to v either goes through node n, or it doesn’t”

▸ If it doesn’t: shortest uses only first nodes in {1,2, . . . ,n − 1}.▸ If it does: consists of a path P1 from u to n and a path P2 from n to v, neither of which

uses n (internally).

▸ Subproblems: shortest path from u to v that only uses nodes in {1,2, . . .k} for all u,v,k.

Michael Dinitz Lecture 15: APSP October 19, 2021 5 / 13

Page 10: Lecture 15: All-Pairs Shortest Paths

Floyd-Warshall: A Different Dynamic Programming Approach

To simplify notation, let V = {1,2, . . . ,n} and `(i, j) =∞ if (i, j) /∈ EBellman-Ford subproblems: length of shortest path with at most some number of edges

New subproblems:▸ Intuition: “shortest path from u to v either goes through node n, or it doesn’t”

▸ If it doesn’t: shortest uses only first nodes in {1,2, . . . ,n − 1}.▸ If it does: consists of a path P1 from u to n and a path P2 from n to v, neither of which

uses n (internally).

▸ Subproblems: shortest path from u to v that only uses nodes in {1,2, . . .k} for all u,v,k.

Michael Dinitz Lecture 15: APSP October 19, 2021 5 / 13

Page 11: Lecture 15: All-Pairs Shortest Paths

Floyd-Warshall: A Different Dynamic Programming Approach

To simplify notation, let V = {1,2, . . . ,n} and `(i, j) =∞ if (i, j) /∈ EBellman-Ford subproblems: length of shortest path with at most some number of edges

New subproblems:▸ Intuition: “shortest path from u to v either goes through node n, or it doesn’t”

▸ If it doesn’t: shortest uses only first nodes in {1,2, . . . ,n − 1}.▸ If it does: consists of a path P1 from u to n and a path P2 from n to v, neither of which

uses n (internally).

▸ Subproblems: shortest path from u to v that only uses nodes in {1,2, . . .k} for all u,v,k.

Michael Dinitz Lecture 15: APSP October 19, 2021 5 / 13

Page 12: Lecture 15: All-Pairs Shortest Paths

Formalizing Subproblems

u→ v path P: “intermediate nodes” are all nodes in P other than u,v.

dkij: distance from i to j using only i→ j paths with intermediate vertices in {1,2, . . . ,k}.

▸ Goal: compute dkij

for all i, j,k ∈ [n].▸ Return dn

ijfor all i, j ∈ V.

dkij =⎧⎪⎪⎨⎪⎪⎩

`(i, j)

if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj)

if k ≥ 1

Michael Dinitz Lecture 15: APSP October 19, 2021 6 / 13

Page 13: Lecture 15: All-Pairs Shortest Paths

Formalizing Subproblems

u→ v path P: “intermediate nodes” are all nodes in P other than u,v.

dkij: distance from i to j using only i→ j paths with intermediate vertices in {1,2, . . . ,k}.

▸ Goal: compute dkij

for all i, j,k ∈ [n].▸ Return dn

ijfor all i, j ∈ V.

dkij =⎧⎪⎪⎨⎪⎪⎩

`(i, j)

if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj)

if k ≥ 1

Michael Dinitz Lecture 15: APSP October 19, 2021 6 / 13

Page 14: Lecture 15: All-Pairs Shortest Paths

Formalizing Subproblems

u→ v path P: “intermediate nodes” are all nodes in P other than u,v.

dkij: distance from i to j using only i→ j paths with intermediate vertices in {1,2, . . . ,k}.

▸ Goal: compute dkij

for all i, j,k ∈ [n].▸ Return dn

ijfor all i, j ∈ V.

dkij =⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj)

if k ≥ 1

Michael Dinitz Lecture 15: APSP October 19, 2021 6 / 13

Page 15: Lecture 15: All-Pairs Shortest Paths

Formalizing Subproblems

u→ v path P: “intermediate nodes” are all nodes in P other than u,v.

dkij: distance from i to j using only i→ j paths with intermediate vertices in {1,2, . . . ,k}.

▸ Goal: compute dkij

for all i, j,k ∈ [n].▸ Return dn

ijfor all i, j ∈ V.

dkij =⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj) if k ≥ 1

Michael Dinitz Lecture 15: APSP October 19, 2021 6 / 13

Page 16: Lecture 15: All-Pairs Shortest Paths

Optimal Substructure

Theorem

For all i, j,k ∈ [n]:dkij =

⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj) if k ≥ 1

If k = 0: ✓If k ≥ 1: prove ≤ and ≥≤: Two feasible solutions≥: Let P be shortest i→ j path with all intermediate nodes in [k]

▸ If k not an intermediate node of P: P has all intermediate nodes in [k − 1] Ô⇒min(dk−1

ij,dk−1

ik+ dk−1

kj) ≤ dk−1

ij≤ `(P) = dk

ij▸ If k is an intermediate node of P: divide P into P1 (subpath from i to k) and P2

(subpath from k to j)

min(dk−1ij ,dk−1ik + dk−1kj ) ≤ dk−1ik + dk−1kj ≤ `(P1) + `(P2) = `(P) = dkij

Michael Dinitz Lecture 15: APSP October 19, 2021 7 / 13

Page 17: Lecture 15: All-Pairs Shortest Paths

Optimal Substructure

Theorem

For all i, j,k ∈ [n]:dkij =

⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj) if k ≥ 1

If k = 0: ✓

If k ≥ 1: prove ≤ and ≥≤: Two feasible solutions≥: Let P be shortest i→ j path with all intermediate nodes in [k]▸ If k not an intermediate node of P: P has all intermediate nodes in [k − 1] Ô⇒min(dk−1

ij,dk−1

ik+ dk−1

kj) ≤ dk−1

ij≤ `(P) = dk

ij▸ If k is an intermediate node of P: divide P into P1 (subpath from i to k) and P2

(subpath from k to j)

min(dk−1ij ,dk−1ik + dk−1kj ) ≤ dk−1ik + dk−1kj ≤ `(P1) + `(P2) = `(P) = dkij

Michael Dinitz Lecture 15: APSP October 19, 2021 7 / 13

Page 18: Lecture 15: All-Pairs Shortest Paths

Optimal Substructure

Theorem

For all i, j,k ∈ [n]:dkij =

⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj) if k ≥ 1

If k = 0: ✓If k ≥ 1: prove ≤ and ≥

≤: Two feasible solutions≥: Let P be shortest i→ j path with all intermediate nodes in [k]▸ If k not an intermediate node of P: P has all intermediate nodes in [k − 1] Ô⇒min(dk−1

ij,dk−1

ik+ dk−1

kj) ≤ dk−1

ij≤ `(P) = dk

ij▸ If k is an intermediate node of P: divide P into P1 (subpath from i to k) and P2

(subpath from k to j)

min(dk−1ij ,dk−1ik + dk−1kj ) ≤ dk−1ik + dk−1kj ≤ `(P1) + `(P2) = `(P) = dkij

Michael Dinitz Lecture 15: APSP October 19, 2021 7 / 13

Page 19: Lecture 15: All-Pairs Shortest Paths

Optimal Substructure

Theorem

For all i, j,k ∈ [n]:dkij =

⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj) if k ≥ 1

If k = 0: ✓If k ≥ 1: prove ≤ and ≥≤:

Two feasible solutions≥: Let P be shortest i→ j path with all intermediate nodes in [k]▸ If k not an intermediate node of P: P has all intermediate nodes in [k − 1] Ô⇒min(dk−1

ij,dk−1

ik+ dk−1

kj) ≤ dk−1

ij≤ `(P) = dk

ij▸ If k is an intermediate node of P: divide P into P1 (subpath from i to k) and P2

(subpath from k to j)

min(dk−1ij ,dk−1ik + dk−1kj ) ≤ dk−1ik + dk−1kj ≤ `(P1) + `(P2) = `(P) = dkij

Michael Dinitz Lecture 15: APSP October 19, 2021 7 / 13

Page 20: Lecture 15: All-Pairs Shortest Paths

Optimal Substructure

Theorem

For all i, j,k ∈ [n]:dkij =

⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj) if k ≥ 1

If k = 0: ✓If k ≥ 1: prove ≤ and ≥≤: Two feasible solutions

≥: Let P be shortest i→ j path with all intermediate nodes in [k]▸ If k not an intermediate node of P: P has all intermediate nodes in [k − 1] Ô⇒min(dk−1

ij,dk−1

ik+ dk−1

kj) ≤ dk−1

ij≤ `(P) = dk

ij▸ If k is an intermediate node of P: divide P into P1 (subpath from i to k) and P2

(subpath from k to j)

min(dk−1ij ,dk−1ik + dk−1kj ) ≤ dk−1ik + dk−1kj ≤ `(P1) + `(P2) = `(P) = dkij

Michael Dinitz Lecture 15: APSP October 19, 2021 7 / 13

Page 21: Lecture 15: All-Pairs Shortest Paths

Optimal Substructure

Theorem

For all i, j,k ∈ [n]:dkij =

⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj) if k ≥ 1

If k = 0: ✓If k ≥ 1: prove ≤ and ≥≤: Two feasible solutions≥: Let P be shortest i→ j path with all intermediate nodes in [k]

▸ If k not an intermediate node of P:

P has all intermediate nodes in [k − 1] Ô⇒min(dk−1

ij,dk−1

ik+ dk−1

kj) ≤ dk−1

ij≤ `(P) = dk

ij▸ If k is an intermediate node of P: divide P into P1 (subpath from i to k) and P2

(subpath from k to j)

min(dk−1ij ,dk−1ik + dk−1kj ) ≤ dk−1ik + dk−1kj ≤ `(P1) + `(P2) = `(P) = dkij

Michael Dinitz Lecture 15: APSP October 19, 2021 7 / 13

Page 22: Lecture 15: All-Pairs Shortest Paths

Optimal Substructure

Theorem

For all i, j,k ∈ [n]:dkij =

⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj) if k ≥ 1

If k = 0: ✓If k ≥ 1: prove ≤ and ≥≤: Two feasible solutions≥: Let P be shortest i→ j path with all intermediate nodes in [k]

▸ If k not an intermediate node of P: P has all intermediate nodes in [k − 1] Ô⇒min(dk−1

ij,dk−1

ik+ dk−1

kj) ≤ dk−1

ij≤ `(P) = dk

ij

▸ If k is an intermediate node of P: divide P into P1 (subpath from i to k) and P2

(subpath from k to j)

min(dk−1ij ,dk−1ik + dk−1kj ) ≤ dk−1ik + dk−1kj ≤ `(P1) + `(P2) = `(P) = dkij

Michael Dinitz Lecture 15: APSP October 19, 2021 7 / 13

Page 23: Lecture 15: All-Pairs Shortest Paths

Optimal Substructure

Theorem

For all i, j,k ∈ [n]:dkij =

⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj) if k ≥ 1

If k = 0: ✓If k ≥ 1: prove ≤ and ≥≤: Two feasible solutions≥: Let P be shortest i→ j path with all intermediate nodes in [k]

▸ If k not an intermediate node of P: P has all intermediate nodes in [k − 1] Ô⇒min(dk−1

ij,dk−1

ik+ dk−1

kj) ≤ dk−1

ij≤ `(P) = dk

ij▸ If k is an intermediate node of P:

divide P into P1 (subpath from i to k) and P2

(subpath from k to j)

min(dk−1ij ,dk−1ik + dk−1kj ) ≤ dk−1ik + dk−1kj ≤ `(P1) + `(P2) = `(P) = dkij

Michael Dinitz Lecture 15: APSP October 19, 2021 7 / 13

Page 24: Lecture 15: All-Pairs Shortest Paths

Optimal Substructure

Theorem

For all i, j,k ∈ [n]:dkij =

⎧⎪⎪⎨⎪⎪⎩`(i, j) if k = 0

min(dk−1ij,dk−1

ik+ dk−1

kj) if k ≥ 1

If k = 0: ✓If k ≥ 1: prove ≤ and ≥≤: Two feasible solutions≥: Let P be shortest i→ j path with all intermediate nodes in [k]

▸ If k not an intermediate node of P: P has all intermediate nodes in [k − 1] Ô⇒min(dk−1

ij,dk−1

ik+ dk−1

kj) ≤ dk−1

ij≤ `(P) = dk

ij▸ If k is an intermediate node of P: divide P into P1 (subpath from i to k) and P2

(subpath from k to j)

min(dk−1ij ,dk−1ik + dk−1kj ) ≤ dk−1ik + dk−1kj ≤ `(P1) + `(P2) = `(P) = dkijMichael Dinitz Lecture 15: APSP October 19, 2021 7 / 13

Page 25: Lecture 15: All-Pairs Shortest Paths

Floyd-Warshall Algorithm

Usually bottom-up, since so simple:

M[i, j,0] = `(i, j) for all i, j ∈ [n]for(k = 1 to n)

for(i = 1 to n)for(j = 1 to n)

M[i, j,k] =min(M[i, j,k − 1],M[i,k,k − 1] +M[k, j,k − 1])

Correctness: obvious for k = 0. For k ≥ 1:

M[i, j,k] =min(M[i, j,k − 1],M[i,k,k − 1] +M[k, j,k − 1]) (def of algorithm)

=min(dk−1ij ,dk−1ik + dk−1kj ) (induction)

= dkij (optimal substructure)

Running Time: O(n3)

Michael Dinitz Lecture 15: APSP October 19, 2021 8 / 13

Page 26: Lecture 15: All-Pairs Shortest Paths

Floyd-Warshall Algorithm

Usually bottom-up, since so simple:

M[i, j,0] = `(i, j) for all i, j ∈ [n]for(k = 1 to n)

for(i = 1 to n)for(j = 1 to n)

M[i, j,k] =min(M[i, j,k − 1],M[i,k,k − 1] +M[k, j,k − 1])Correctness: obvious for k = 0. For k ≥ 1:

M[i, j,k] =min(M[i, j,k − 1],M[i,k,k − 1] +M[k, j,k − 1]) (def of algorithm)

=min(dk−1ij ,dk−1ik + dk−1kj ) (induction)

= dkij (optimal substructure)

Running Time: O(n3)

Michael Dinitz Lecture 15: APSP October 19, 2021 8 / 13

Page 27: Lecture 15: All-Pairs Shortest Paths

Floyd-Warshall Algorithm

Usually bottom-up, since so simple:

M[i, j,0] = `(i, j) for all i, j ∈ [n]for(k = 1 to n)

for(i = 1 to n)for(j = 1 to n)

M[i, j,k] =min(M[i, j,k − 1],M[i,k,k − 1] +M[k, j,k − 1])Correctness: obvious for k = 0. For k ≥ 1:

M[i, j,k] =min(M[i, j,k − 1],M[i,k,k − 1] +M[k, j,k − 1]) (def of algorithm)

=min(dk−1ij ,dk−1ik + dk−1kj ) (induction)

= dkij (optimal substructure)

Running Time: O(n3)Michael Dinitz Lecture 15: APSP October 19, 2021 8 / 13

Page 28: Lecture 15: All-Pairs Shortest Paths

Johnson’s Algorithm

Michael Dinitz Lecture 15: APSP October 19, 2021 9 / 13

Page 29: Lecture 15: All-Pairs Shortest Paths

Reweighting

Different Approach: Can we “fix” negative weights so Dijkstra from every node works?

▸ Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −α be smallest length (most negative). Add α to every edge.

▸ Does this work? No!

▸ New length of path P is `(P) +α∣P∣, so original shortestpath might no longer be shortest path if it has manyedges.

Reweighting

Di↵erent Approach: Can we “fix” negative weights so Dijkstra from every node works?

� Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −↵ be smallest length (most negative). Add ↵ to every edge.

� Does this work?

No!

� New length of path P is `(P) +↵�P�, so original shortest path might no longer beshortest path if it has many edges.

Some other kind of reweighting? Need new lengths ˆ̀ such that:

� Path P a shortest path under lengths ` if and only i↵ P a shortest path underlengths ˆ̀

� ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Zoo

i

Some other kind of reweighting? Need new lengths ˆ̀ such that:

▸ Path P a shortest path under lengths ` if and only P a shortest path under lengths ˆ̀

▸ ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Michael Dinitz Lecture 15: APSP October 19, 2021 10 / 13

Page 30: Lecture 15: All-Pairs Shortest Paths

Reweighting

Different Approach: Can we “fix” negative weights so Dijkstra from every node works?

▸ Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −α be smallest length (most negative). Add α to every edge.

▸ Does this work?

No!

▸ New length of path P is `(P) +α∣P∣, so original shortestpath might no longer be shortest path if it has manyedges.

Reweighting

Di↵erent Approach: Can we “fix” negative weights so Dijkstra from every node works?

� Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −↵ be smallest length (most negative). Add ↵ to every edge.

� Does this work?

No!

� New length of path P is `(P) +↵�P�, so original shortest path might no longer beshortest path if it has many edges.

Some other kind of reweighting? Need new lengths ˆ̀ such that:

� Path P a shortest path under lengths ` if and only i↵ P a shortest path underlengths ˆ̀

� ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Zoo

i

Some other kind of reweighting? Need new lengths ˆ̀ such that:

▸ Path P a shortest path under lengths ` if and only P a shortest path under lengths ˆ̀

▸ ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Michael Dinitz Lecture 15: APSP October 19, 2021 10 / 13

Page 31: Lecture 15: All-Pairs Shortest Paths

Reweighting

Different Approach: Can we “fix” negative weights so Dijkstra from every node works?

▸ Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −α be smallest length (most negative). Add α to every edge.

▸ Does this work? No!

▸ New length of path P is `(P) +α∣P∣, so original shortestpath might no longer be shortest path if it has manyedges.

Reweighting

Di↵erent Approach: Can we “fix” negative weights so Dijkstra from every node works?

� Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −↵ be smallest length (most negative). Add ↵ to every edge.

� Does this work?

No!

� New length of path P is `(P) +↵�P�, so original shortest path might no longer beshortest path if it has many edges.

Some other kind of reweighting? Need new lengths ˆ̀ such that:

� Path P a shortest path under lengths ` if and only i↵ P a shortest path underlengths ˆ̀

� ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Zoo

i

Some other kind of reweighting? Need new lengths ˆ̀ such that:

▸ Path P a shortest path under lengths ` if and only P a shortest path under lengths ˆ̀

▸ ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Michael Dinitz Lecture 15: APSP October 19, 2021 10 / 13

Page 32: Lecture 15: All-Pairs Shortest Paths

Reweighting

Different Approach: Can we “fix” negative weights so Dijkstra from every node works?

▸ Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −α be smallest length (most negative). Add α to every edge.

▸ Does this work? No!

▸ New length of path P is `(P) +α∣P∣, so original shortestpath might no longer be shortest path if it has manyedges.

Reweighting

Di↵erent Approach: Can we “fix” negative weights so Dijkstra from every node works?

� Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −↵ be smallest length (most negative). Add ↵ to every edge.

� Does this work?

No!

� New length of path P is `(P) +↵�P�, so original shortest path might no longer beshortest path if it has many edges.

Some other kind of reweighting? Need new lengths ˆ̀ such that:

� Path P a shortest path under lengths ` if and only i↵ P a shortest path underlengths ˆ̀

� ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Zoo

i

Some other kind of reweighting? Need new lengths ˆ̀ such that:

▸ Path P a shortest path under lengths ` if and only P a shortest path under lengths ˆ̀

▸ ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Michael Dinitz Lecture 15: APSP October 19, 2021 10 / 13

Page 33: Lecture 15: All-Pairs Shortest Paths

Reweighting

Different Approach: Can we “fix” negative weights so Dijkstra from every node works?

▸ Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −α be smallest length (most negative). Add α to every edge.

▸ Does this work? No!

▸ New length of path P is `(P) +α∣P∣, so original shortestpath might no longer be shortest path if it has manyedges.

Reweighting

Di↵erent Approach: Can we “fix” negative weights so Dijkstra from every node works?

� Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −↵ be smallest length (most negative). Add ↵ to every edge.

� Does this work?

No!

� New length of path P is `(P) +↵�P�, so original shortest path might no longer beshortest path if it has many edges.

Some other kind of reweighting? Need new lengths ˆ̀ such that:

� Path P a shortest path under lengths ` if and only i↵ P a shortest path underlengths ˆ̀

� ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Zoo

i

Some other kind of reweighting? Need new lengths ˆ̀ such that:

▸ Path P a shortest path under lengths ` if and only P a shortest path under lengths ˆ̀

▸ ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Michael Dinitz Lecture 15: APSP October 19, 2021 10 / 13

Page 34: Lecture 15: All-Pairs Shortest Paths

Reweighting

Different Approach: Can we “fix” negative weights so Dijkstra from every node works?

▸ Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −α be smallest length (most negative). Add α to every edge.

▸ Does this work? No!

▸ New length of path P is `(P) +α∣P∣, so original shortestpath might no longer be shortest path if it has manyedges.

Reweighting

Di↵erent Approach: Can we “fix” negative weights so Dijkstra from every node works?

� Time would be O(n(m + n log n)) = O(mn + n2 log n), better than Floyd-Warshall

First attempt: Let −↵ be smallest length (most negative). Add ↵ to every edge.

� Does this work?

No!

� New length of path P is `(P) +↵�P�, so original shortest path might no longer beshortest path if it has many edges.

Some other kind of reweighting? Need new lengths ˆ̀ such that:

� Path P a shortest path under lengths ` if and only i↵ P a shortest path underlengths ˆ̀

� ˆ̀(u,v) ≥ 0 for all (u,v) ∈ E

Zoo

i

Some other kind of reweighting? Need new lengths ˆ̀ such that:

▸ Path P a shortest path under lengths ` if and only P a shortest path under lengths ˆ̀

▸ ˆ̀(u,v) ≥ 0 for all (u,v) ∈ EMichael Dinitz Lecture 15: APSP October 19, 2021 10 / 13

Page 35: Lecture 15: All-Pairs Shortest Paths

Vertex Reweighting

Neat observation: put weights at vertices!

▸ Let h ∶ V → R be node weights.

▸ Let `h(u,v) = `(u,v) + h(u) − h(v)

Let P = ⟨v0,v1, . . . ,vk⟩ be arbitrary (not necessarily shortest) path.

`h(P) = k−1∑i=0 `h(vi,vi+1) =

k−1∑i=0 (`(vi,vi+1) + h(vi) − h(vi+1))

= h(v0) − h(vk) + k−1∑i=0 `(vi,vi+1) (telescoping)

= `(P) + h(v0) − h(vk)h(v0) − h(vk) added to every v0 → vk path, so shortest path from v0 to vk still shortest path!

Michael Dinitz Lecture 15: APSP October 19, 2021 11 / 13

Page 36: Lecture 15: All-Pairs Shortest Paths

Vertex Reweighting

Neat observation: put weights at vertices!

▸ Let h ∶ V → R be node weights.

▸ Let `h(u,v) = `(u,v) + h(u) − h(v)Let P = ⟨v0,v1, . . . ,vk⟩ be arbitrary (not necessarily shortest) path.

`h(P) = k−1∑i=0 `h(vi,vi+1) =

k−1∑i=0 (`(vi,vi+1) + h(vi) − h(vi+1))

= h(v0) − h(vk) + k−1∑i=0 `(vi,vi+1) (telescoping)

= `(P) + h(v0) − h(vk)h(v0) − h(vk) added to every v0 → vk path, so shortest path from v0 to vk still shortest path!

Michael Dinitz Lecture 15: APSP October 19, 2021 11 / 13

Page 37: Lecture 15: All-Pairs Shortest Paths

Vertex Reweighting

Neat observation: put weights at vertices!

▸ Let h ∶ V → R be node weights.

▸ Let `h(u,v) = `(u,v) + h(u) − h(v)Let P = ⟨v0,v1, . . . ,vk⟩ be arbitrary (not necessarily shortest) path.

`h(P) = k−1∑i=0 `h(vi,vi+1) =

k−1∑i=0 (`(vi,vi+1) + h(vi) − h(vi+1))

= h(v0) − h(vk) + k−1∑i=0 `(vi,vi+1) (telescoping)

= `(P) + h(v0) − h(vk)h(v0) − h(vk) added to every v0 → vk path, so shortest path from v0 to vk still shortest path!

Michael Dinitz Lecture 15: APSP October 19, 2021 11 / 13

Page 38: Lecture 15: All-Pairs Shortest Paths

Vertex Reweighting

Neat observation: put weights at vertices!

▸ Let h ∶ V → R be node weights.

▸ Let `h(u,v) = `(u,v) + h(u) − h(v)Let P = ⟨v0,v1, . . . ,vk⟩ be arbitrary (not necessarily shortest) path.

`h(P) = k−1∑i=0 `h(vi,vi+1) =

k−1∑i=0 (`(vi,vi+1) + h(vi) − h(vi+1))

= h(v0) − h(vk) + k−1∑i=0 `(vi,vi+1) (telescoping)

= `(P) + h(v0) − h(vk)h(v0) − h(vk) added to every v0 → vk path, so shortest path from v0 to vk still shortest path!

Michael Dinitz Lecture 15: APSP October 19, 2021 11 / 13

Page 39: Lecture 15: All-Pairs Shortest Paths

Vertex Reweighting

Neat observation: put weights at vertices!

▸ Let h ∶ V → R be node weights.

▸ Let `h(u,v) = `(u,v) + h(u) − h(v)Let P = ⟨v0,v1, . . . ,vk⟩ be arbitrary (not necessarily shortest) path.

`h(P) = k−1∑i=0 `h(vi,vi+1) =

k−1∑i=0 (`(vi,vi+1) + h(vi) − h(vi+1))

= h(v0) − h(vk) + k−1∑i=0 `(vi,vi+1) (telescoping)

= `(P) + h(v0) − h(vk)

h(v0) − h(vk) added to every v0 → vk path, so shortest path from v0 to vk still shortest path!

Michael Dinitz Lecture 15: APSP October 19, 2021 11 / 13

Page 40: Lecture 15: All-Pairs Shortest Paths

Vertex Reweighting

Neat observation: put weights at vertices!

▸ Let h ∶ V → R be node weights.

▸ Let `h(u,v) = `(u,v) + h(u) − h(v)Let P = ⟨v0,v1, . . . ,vk⟩ be arbitrary (not necessarily shortest) path.

`h(P) = k−1∑i=0 `h(vi,vi+1) =

k−1∑i=0 (`(vi,vi+1) + h(vi) − h(vi+1))

= h(v0) − h(vk) + k−1∑i=0 `(vi,vi+1) (telescoping)

= `(P) + h(v0) − h(vk)h(v0) − h(vk) added to every v0 → vk path, so shortest path from v0 to vk still shortest path!

Michael Dinitz Lecture 15: APSP October 19, 2021 11 / 13

Page 41: Lecture 15: All-Pairs Shortest Paths

Making lengths nonnegative

So vertex reweighting preserves shortest paths. Find weights to make lengths nonnegative?

Add new node s to graph, edges (s,v) for all v ∈ V of length 0

▸ Run Bellman-Ford from s, then for all u ∈ V set h(u) to be d(s,u)▸ Note h(u) ≤ 0 for all u ∈ V

Want to show that `h(u,v) ≥ 0 for all edges (u,v).

▸ Triangle inequality: h(v) = d(s,v) ≤ d(s,u) + `(u,v) = h(u) + `(u,v)`h(u,v) = `(u,v) + h(u) − h(v) ≥ `(u,v) + h(u) − (h(u) + `(u,v)) = 0

Michael Dinitz Lecture 15: APSP October 19, 2021 12 / 13

Page 42: Lecture 15: All-Pairs Shortest Paths

Making lengths nonnegative

So vertex reweighting preserves shortest paths. Find weights to make lengths nonnegative?

Add new node s to graph, edges (s,v) for all v ∈ V of length 0

▸ Run Bellman-Ford from s, then for all u ∈ V set h(u) to be d(s,u)▸ Note h(u) ≤ 0 for all u ∈ V

Want to show that `h(u,v) ≥ 0 for all edges (u,v).

▸ Triangle inequality: h(v) = d(s,v) ≤ d(s,u) + `(u,v) = h(u) + `(u,v)`h(u,v) = `(u,v) + h(u) − h(v) ≥ `(u,v) + h(u) − (h(u) + `(u,v)) = 0

Michael Dinitz Lecture 15: APSP October 19, 2021 12 / 13

Page 43: Lecture 15: All-Pairs Shortest Paths

Making lengths nonnegative

So vertex reweighting preserves shortest paths. Find weights to make lengths nonnegative?

Add new node s to graph, edges (s,v) for all v ∈ V of length 0

▸ Run Bellman-Ford from s, then for all u ∈ V set h(u) to be d(s,u)▸ Note h(u) ≤ 0 for all u ∈ V

Want to show that `h(u,v) ≥ 0 for all edges (u,v).

▸ Triangle inequality: h(v) = d(s,v) ≤ d(s,u) + `(u,v) = h(u) + `(u,v)

`h(u,v) = `(u,v) + h(u) − h(v) ≥ `(u,v) + h(u) − (h(u) + `(u,v)) = 0

Michael Dinitz Lecture 15: APSP October 19, 2021 12 / 13

Page 44: Lecture 15: All-Pairs Shortest Paths

Making lengths nonnegative

So vertex reweighting preserves shortest paths. Find weights to make lengths nonnegative?

Add new node s to graph, edges (s,v) for all v ∈ V of length 0

▸ Run Bellman-Ford from s, then for all u ∈ V set h(u) to be d(s,u)▸ Note h(u) ≤ 0 for all u ∈ V

Want to show that `h(u,v) ≥ 0 for all edges (u,v).

▸ Triangle inequality: h(v) = d(s,v) ≤ d(s,u) + `(u,v) = h(u) + `(u,v)`h(u,v) = `(u,v) + h(u) − h(v) ≥ `(u,v) + h(u) − (h(u) + `(u,v)) = 0

Michael Dinitz Lecture 15: APSP October 19, 2021 12 / 13

Page 45: Lecture 15: All-Pairs Shortest Paths

Johnson’s Algorithm

▸ Add vertex s to graph, edge (s,u) for all u ∈ V with `(s,u) = 0

▸ Run Bellman-Ford from s, set h(u) = d(s,u)▸ Remove s, run Dijkstra from every node u ∈ V to get dh(u,v) for all u,v ∈ V▸ If want distances, set d(u,v) = dh(u,v) − h(u) + h(v) for all u,v ∈ V

Correctness: From previous discussion.

Running Time: O(n) +O(mn) +O(n(m + n log n)) = O(mn + n2 log n)

Michael Dinitz Lecture 15: APSP October 19, 2021 13 / 13

Page 46: Lecture 15: All-Pairs Shortest Paths

Johnson’s Algorithm

▸ Add vertex s to graph, edge (s,u) for all u ∈ V with `(s,u) = 0

▸ Run Bellman-Ford from s, set h(u) = d(s,u)▸ Remove s, run Dijkstra from every node u ∈ V to get dh(u,v) for all u,v ∈ V▸ If want distances, set d(u,v) = dh(u,v) − h(u) + h(v) for all u,v ∈ V

Correctness: From previous discussion.

Running Time:

O(n) +O(mn) +O(n(m + n log n)) = O(mn + n2 log n)

Michael Dinitz Lecture 15: APSP October 19, 2021 13 / 13

Page 47: Lecture 15: All-Pairs Shortest Paths

Johnson’s Algorithm

▸ Add vertex s to graph, edge (s,u) for all u ∈ V with `(s,u) = 0

▸ Run Bellman-Ford from s, set h(u) = d(s,u)▸ Remove s, run Dijkstra from every node u ∈ V to get dh(u,v) for all u,v ∈ V▸ If want distances, set d(u,v) = dh(u,v) − h(u) + h(v) for all u,v ∈ V

Correctness: From previous discussion.

Running Time: O(n) +O(mn) +O(n(m + n log n)) = O(mn + n2 log n)

Michael Dinitz Lecture 15: APSP October 19, 2021 13 / 13


Recommended