shortest paths - university of texas at san...

8
Shortest Paths Single-Source Shortest Paths Dijkstra’s Algorithm Bellman-Ford Algorithm Difference Constraints All-Pairs Shortest Paths Floyd-Warshall Algorithm Johnson’s Algorithm Shortest-Paths............................................. 2 Example Weighted Graphs..................................... 3 Example Bad Graph ......................................... 4 Key Properties ............................................ 5 Key Properties Continued ..................................... 6 Basic Procedures ........................................... 7 Basic Procedures Continued ................................... 8 Dijkstra’s Algorithm ......................................... 9 Dijkstra Analysis .......................................... 10 Bellman-Ford Algorithm ..................................... 11 Bellman-Ford Algorithm ..................................... 12 Difference Constraints ...................................... 13 Difference Constraints Continued ............................... 14 Difference Constraints Example ................................ 15 All-Pairs Shortest Paths ..................................... 16 All-Pairs Recursion ......................................... 17 All-Pairs Algorithm ........................................ 18 All-Pairs Example ......................................... 19 Floyd-Warshall Algorithm .................................... 20 Floyd-Warshall Analysis ..................................... 21 Floyd-Warshall Example ..................................... 22 Johnson’s Algorithm for Sparse Graphs ........................... 23 1 Johnson’s Algorithm........................................ 24 Johnson’s Algorithm Analysis .................................. 25 Johnson Example .......................................... 26 2

Upload: others

Post on 04-Aug-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shortest Paths - University of Texas at San Antoniobylander/cs5633/notes/shortest-pathshandout.pdf · Johnson’s Algorithm for Sparse Graphs Idea of Johnson’s Algorithm: Run Dijkstra’s

Shortest Paths

Single-Source Shortest Paths

Dijkstra’s Algorithm

Bellman-Ford Algorithm

Difference Constraints

All-Pairs Shortest Paths

Floyd-Warshall Algorithm

Johnson’s Algorithm

Shortest-Paths. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Example Weighted Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Example Bad Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Key Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Key Properties Continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Basic Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Basic Procedures Continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Dijkstra’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Dijkstra Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Bellman-Ford Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Bellman-Ford Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Difference Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Difference Constraints Continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Difference Constraints Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15All-Pairs Shortest Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16All-Pairs Recursion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17All-Pairs Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18All-Pairs Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Floyd-Warshall Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Floyd-Warshall Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Floyd-Warshall Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Johnson’s Algorithm for Sparse Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

1

Johnson’s Algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Johnson’s Algorithm Analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Johnson Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2

Page 2: Shortest Paths - University of Texas at San Antoniobylander/cs5633/notes/shortest-pathshandout.pdf · Johnson’s Algorithm for Sparse Graphs Idea of Johnson’s Algorithm: Run Dijkstra’s

Shortest-Paths

Shortest path problems on weighted graphs (directed or undirected) have twomain types:

Single-Source Shortest-Path: find the shortest paths from source vertex s to allother vertices.

All-Pairs Shortest-Path: find the shortest paths between all pairs of vertices.

Some notation:w(u, v) = weight of edge (u, v)w(p) = sum of weights on path p

Let δ(u, v) = shortest distance from u to v.

Let sopt❀ v stand for shortest path from s to v.

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 2

Example Weighted Graphs

1

23

7

85

54 7

9

6

4

8

a

b c d

ei

h g f

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 3

3

Example Bad Graph

Graphs with negative-weight cycles can’t be solved.

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 4

Key Properties

Triangle Inequality: If (u, v) is an edge, then δ(s, v) ≤ δ(s, u) + w(u, v). If p isa path from u to v, then δ(s, v) ≤ δ(s, u) + w(p).

Proof: δ(s, v) > δ(s, u) + w(u, v) contradicts δ’s definition. So doesδ(s, v) > δ(s, u) + w(p).

Optimal Subpath Property: If u is on sopt❀ v, then δ(s, v) = δ(s, u) + δ(u, v).

Proof: Let p be sopt❀ v, and let p1 and p2 be the subpaths s ❀ u and u ❀ v.

By definition, w(p) = w(p1) + w(p2) ≥ δ(s, u) + δ(u, v). A lower δ(s, u) orδ(s, v) would contradict p’s optimality.

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 5

4

Page 3: Shortest Paths - University of Texas at San Antoniobylander/cs5633/notes/shortest-pathshandout.pdf · Johnson’s Algorithm for Sparse Graphs Idea of Johnson’s Algorithm: Run Dijkstra’s

Key Properties Continued

Convergence Property: If s ❀ u→ v is a shortest path, thenδ(s, v) = δ(s, u) + w(u, v).

Proof: Follows from the optimal subpath property.

Let v.d ≥ δ(s, v) for all v with s.d = 0 = δ(s, s).

Path-Relaxation Property: If v.d > δ(s, v), then some edge (x, y) satisfiesv.d = δ(s, x) and y.d > x.d+ w(x, y).

Proof: Some edge (x, y) on sopt❀ v must be the first edge with x.d = δ(s, x)

and y.d > x.d+ w(x, y) = δ(s, y).

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 6

Basic Procedures

These properties justify the following procedures for single-sourceshortest-paths.v.d = distance from s to v.v.π = previous vertex on s ❀ v path.

Initialize-Single-Source(G, s)for each vertex v in G

v.d ← ∞v.π ← nil

s.d ← 0

Initial ∞ ensures that d.v ≥ δ(s, v) for all v.s.d = 0 ensures that s.d = δ(s, s).

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 7

5

Basic Procedures Continued

Relax(u, v, w)if v.d > u.d+ w(u, v)v.d ← u.d+ w(u, v)v.π ← u

If u.d ≥ δ(s, u) and v.d ≥ δ(s, v) before Relax(u, v, w) then they remain trueafterwards.

Also, if v.d > δ(s, v), then the Path Relaxation Property implies that some callto Relax will improve d somewhere.

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 8

Dijkstra’s Algorithm

Dijkstra’s Algorithm assumes all weights are nonnegative.

Dijkstra(G,w, s)Initialize-Single-Source(G, s)S ← ∅Q ← G.V using d field for priority queuewhile Q 6= ∅

u ← Extract-Min(Q)S ← S ∪ {u}for each v ∈ G.Adj[u]

Relax(u, v, w)

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 9

6

Page 4: Shortest Paths - University of Texas at San Antoniobylander/cs5633/notes/shortest-pathshandout.pdf · Johnson’s Algorithm for Sparse Graphs Idea of Johnson’s Algorithm: Run Dijkstra’s

Dijkstra Analysis

Proof of Correctness:

Basis: s is assigned to u, s.d = 0 = δ(s, s)Assume: For all x ∈ S, x.d = δ(s, x)Show: u.d = δ(s, u) when u is extracted.Induction: Proof by contradiction.

Suppose δ(s, u) < u.d.

Some edge (x, y) in sopt❀ u goes from S to Q.

y.d = δ(s, y) because (x, y) has been relaxed,but u.d ≤ y.d = δ(s, y) ≤ δ(s, u) < u.d

is a contradiction.

Dijkstra is O(E lg V ) with binary heaps.

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 10

Bellman-Ford Algorithm

This works with negative weights. false is returned if a negative cycle isdetected.

Bellman-Ford(G,w, s)Initialize-Single-Source(G, s)for i ← 1 to |V | − 1

for each edge (u, v) in G

Relax(u, v, w)for each edge (u, v) in G

if v.d > u.d+ w(u, v)return false

return true

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 11

7

Bellman-Ford Algorithm

Proof of Correctness:

All finite shortest paths have ≤ V − 1 edges.

The ith iteration relaxes ith edge in the optimal path.

If shortest path is finite, then after V − 1 iterations, v.d = δ(s, v).

If there is an infinite shortest path,then v.d > u.d+ w(u, v) for some edge (u, v).

Bellman-Ford is O(V E).

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 12

Difference Constraints

A difference constraint is xj − xi ≤ bk, where xi and xj are variables and bk is aconstant.

Difference constraints can represent time relationships between events.

If x2 must occur within 2 hours after x1, then x2 − x1 ≤ 2.

If x2 must occur at least 2 hours after x1, then x2 − x1 ≥ 2, which isequivalent to x1 − x2 ≤ −2.

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 13

8

Page 5: Shortest Paths - University of Texas at San Antoniobylander/cs5633/notes/shortest-pathshandout.pdf · Johnson’s Algorithm for Sparse Graphs Idea of Johnson’s Algorithm: Run Dijkstra’s

Difference Constraints Continued

The triangle inequality for shortest paths is a difference constraint.δ(s, v) ≤ δ(s, u) + w(u, v) is equivalent to δ(s, v)− δ(s, u) ≤ w(u, v).

A set of difference constraints xj − xi ≤ bk can be reduced to a weighted graphby w(vi, vj) = bk and w(s, vj) = w(s, vi) = 0.

There is a solution for single-source shortest paths if and only if there is asolution to the difference constraints.

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 14

Difference Constraints Example

x1 − x2 ≤ 0x1 − x5 ≤ −1x2 − x5 ≤ 1x3 − x1 ≤ 5x4 − x1 ≤ 4x4 − x3 ≤ −1x5 − x3 ≤ −3x5 − x4 ≤ −3

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 15

9

All-Pairs Shortest Paths

The all-pairs shortest-paths problem is finding the shortest path for each pair ofvertices. Algorithms for this problem can be adapted for transitive closure.

Let δ(i, j,m) be the shortest length from i to j using ≤ m edges.

δ(i, j, 1) =

0 if i = j

w(i, j) if (i, j) is an edge∞ otherwise

δ(i, j, 2m) =n

mink=1

(δ(i, k,m) + δ(k, j,m))

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 16

All-Pairs Recursion

This recursive definition is correct.

Basis: Equation for δ(i, j, 1) is correct.Assume: δ(i, j,m) is correct.Show: Equation for δ(i, j, 2m) is correct.Induction: Optimal path of ≤ 2m edges consists

of two optimal paths of ≤ m edges.

This leads to a O(V 3 lg V ) algorithm.

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 17

10

Page 6: Shortest Paths - University of Texas at San Antoniobylander/cs5633/notes/shortest-pathshandout.pdf · Johnson’s Algorithm for Sparse Graphs Idea of Johnson’s Algorithm: Run Dijkstra’s

All-Pairs Algorithm

All-Pairs(G,w)n ← |G.V |D ← an n× n matrix initialized to δ(i, j, 1)m ← 1while m < n− 1

Extend(D, n)m ← 2m

return D

Extend(D, n)for i ← 1 to n

for j ← 1 to n

for k ← 1 to n

D[i, j] ← min(D[i, j], D[i, k] +D[k, j])

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 18

All-Pairs Example

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 19

11

Floyd-Warshall Algorithm

Floyd-Warshall(G,w)n ← |G.V |D ← an n× n matrix initialized to δ(i, j, 1)for k ← 1 to n

for i ← 1 to n

for j ← 1 to n

D[i, j] ← min(D[i, j], D[i, k] +D[k, j])return D

Floyd-Warshall is O(V 3).

For transitive closure, use a 0/1 matrix and:D[i, j] ← D[i, j] ∨ (D[i, k] ∧D[k, j])

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 20

Floyd-Warshall Analysis

Let λ(i, j, k) be the shortest distance from i to j with all intermediate vertices≤ k.

Basis: D[i, j] ≤ λ(i, j, 0) due to initialization.Assume: D[i, j] ≤ λ(i, j, k−1) before kth iteration.Show: D[i, j] ≤ λ(i, j, k) after kth iteration.Induction: Either λ(i, j, k) = λ(i, j, k − 1) or

λ(i, j, k) = λ(i, k, k−1) + λ(k, j, k−1)

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 21

12

Page 7: Shortest Paths - University of Texas at San Antoniobylander/cs5633/notes/shortest-pathshandout.pdf · Johnson’s Algorithm for Sparse Graphs Idea of Johnson’s Algorithm: Run Dijkstra’s

Floyd-Warshall Example

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 22

Johnson’s Algorithm for Sparse Graphs

Idea of Johnson’s Algorithm:Run Dijkstra’s algorithm n times.Obtain O(V E lg V ) time using binary heaps.Better than O(V 3) if E is o(V 2)

Problem: Negative weights.

Solution: Clever way to transform the graph so that all weights are positive

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 23

13

Johnson’s Algorithm

Johnson(G,w, s)(G′, w′) ← (G,w) with new vertex s, w′(s, v) = 0if Bellman-Ford(G′, w′, s) = false

error negative-weight cyclefor (u, v) ∈ G

w(u, v) ← w(u, v) + δ′(s, u)− δ′(s, v)for each vertex u in G

Dijkstra(G, w, u)for each vertex v in G

D[u, v] ← δ(u, v)− δ′(s, u) + δ′(s, v)return D

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 24

Johnson’s Algorithm Analysis

Triangle inequality, δ′(s, v) ≤ δ′(s, u) + w(u, v), implies0 ≤ δ′(s, u) + w(u, v)− δ′(s, v) = w(u, v).Thus, w(u, v) ≥ 0 for all edges (u, v).

For any path p from u to v, w(p) = w(p) + δ′(s, u)− δ′(s, v) because of thetelescoping sum.

Johnson is O(V E lg V ) if binary heaps are used for Dijkstra.

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 25

14

Page 8: Shortest Paths - University of Texas at San Antoniobylander/cs5633/notes/shortest-pathshandout.pdf · Johnson’s Algorithm for Sparse Graphs Idea of Johnson’s Algorithm: Run Dijkstra’s

Johnson Example

CS 5633 Analysis of Algorithms Chapter 24 and 25: Slide – 26

15