Shortest Paths II

Download Report

Transcript Shortest Paths II

Algorithms
LECTURE 14
Shortest Paths II
• Bellman-Ford algorithm
• Floyd-Warshal algorithm
4
November 16, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.1
Negative-weight cycles
Recall: If a graph G = (V, E) contains a negativeweight cycle, then some shortest paths may not exist.
…
Example:
<0
uu
November 16, 2005
vv
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.2
Negative-weight cycles
Recall: If a graph G = (V, E) contains a negativeweight cycle, then some shortest paths may not exist.
…
Example:
<0
uu
vv
Bellman-Ford algorithm: Finds all shortest-path
lengths from a source s ∈ V to all v ∈ V or
determines that a negative-weight cycle exists.
November 16, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.3
Bellman-Ford algorithm
d[s] ← 0
for each v ∈ V – {s}
do d[v] ← ∞
initialization
for i ← 1 to | V | – 1
do for each edge (u, v) ∈ E
do if d[v] > d[u] + w(u, v)
relaxation
then d[v] ← d[u] + w(u, v)
step
for each edge (u, v) ∈ E
do if d[v] > d[u] + w(u, v)
then report that a negative-weight cycle exists
At the end, d[v] = δ(s, v), if no negative-weight cycles.
Time = O(VE).
November 16, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.4
Example of Bellman-Ford
BB
–1
3
AA
4
November 16, 2005
1
2
2
EE
–3
CC
5
D
D
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.5
Example of Bellman-Ford
∞
BB
–1
0
3
AA
4
1
2
∞
2
EE
–3
CC
∞
5
D
D
∞
Initialization.
November 16, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.6
Example of Bellman-Ford
∞
BB
–1
0
AA
4
7
3
5
4
1
2
2
1
8
2
CC
∞
∞
3
6
5
EE
–3
D
D
∞
Order of edge relaxation.
November 16, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.7
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
∞
BB
1
2
2
1
8
2
CC
∞
∞
3
6
5
EE
–3
D
D
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.8
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
∞
BB
1
2
2
1
8
2
CC
∞
∞
3
6
5
EE
–3
D
D
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.9
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
∞
BB
1
2
2
1
8
2
CC
∞
∞
3
6
5
EE
–3
D
D
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.10
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
∞
BB
1
2
2
1
8
2
CC
∞
∞
3
6
5
EE
–3
D
D
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.11
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
2
2
1
8
2
CC
4
∞
∞
3
6
5
EE
–3
D
D
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.12
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
2
2
1
8
2
CC
4
∞
3
6
5
EE
–3
D
D
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.13
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
2
2
1
8
2
CC
2
4
∞
3
6
5
EE
–3
D
D
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.14
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
2
2
1
8
2
CC
2
∞
3
6
5
EE
–3
D
D
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.15
Example of Bellman-Ford
−1
BB
–1
0
AA
4
7
3
5
4
1
2
2
1
8
2
CC
2
∞
3
6
5
EE
–3
D
D
∞
End of pass 1.
November 16, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.16
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
2
2
1
8
2
CC
2
1
∞
3
6
5
EE
–3
D
D
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.17
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
2
2
1
8
2
CC
2
1
3
6
5
EE
–3
D
D
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.18
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
2
2
1
8
2
CC
2
1
3
6
5
EE
–3
D
D
1
∞
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.19
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
1
3
2
1
8
2
CC
2
2
6
5
EE
–3
D
D
1
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.20
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
1
3
2
1
8
2
CC
2
2
6
5
EE
–3
D
D
1
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.21
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
1
3
2
1
8
2
CC
2
2
6
5
EE
–3
D
D
1
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.22
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
1
3
2
1
8
2
CC
2
2
6
5
EE
–3
D
D
1
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.23
Example of Bellman-Ford
–1
0
AA
4
7
3
5
4
November 16, 2005
−1
BB
1
2
2
1
8
2
CC
2
1
3
6
5
EE
–3
D
D
−2
1
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.24
Example of Bellman-Ford
−1
BB
–1
0
AA
4
7
3
5
4
1
2
2
1
8
2
CC
2
1
3
6
5
EE
–3
D
D
−2
End of pass 2 (and 3 and 4).
November 16, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.25
Correctness
Theorem. If G = (V, E) contains no negativeweight cycles, then after the Bellman-Ford
algorithm executes, d[v] = δ(s, v) for all v ∈ V.
November 16, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L18.26
Shortest paths
Single-source shortest paths
• Nonnegative edge weights
◆Dijkstra’s algorithm: O(E + V lg V)
• General
◆Bellman-Ford algorithm: O(VE)
• DAG
◆One pass of Bellman-Ford: O(V + E)
November 21, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L19.27
Shortest paths
Single-source shortest paths
• Nonnegative edge weights
◆Dijkstra’s algorithm: O(E + V lg V)
• General
◆Bellman-Ford: O(VE)
• DAG
◆One pass of Bellman-Ford: O(V + E)
All-pairs shortest paths
• Nonnegative edge weights
◆Dijkstra’s algorithm |V| times: O(VE + V 2 lg V)
• General
◆Three algorithms today.
November 21, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L19.28
All-pairs shortest paths
Input: Digraph G = (V, E), where V = {1, 2,
…, n}, with edge-weight function w : E → R.
Output: n × n matrix of shortest-path lengths
δ(i, j) for all i, j ∈ V.
November 21, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L19.29
All-pairs shortest paths
Input: Digraph G = (V, E), where V = {1, 2,
…, n}, with edge-weight function w : E → R.
Output: n × n matrix of shortest-path lengths
δ(i, j) for all i, j ∈ V.
IDEA:
• Run Bellman-Ford once from each vertex.
• Time = O(V 2E).
• Dense graph (n2 edges) ⇒ Θ(n 4) time in the
worst case.
Good first try!
November 21, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L19.30
Pseudocode for Floyd- Warshall
for k ← 1 to n
do for i ← 1 to n
do for j ← 1 to n
do if cij > cik + ckj
then cij ← cik + ckj
relaxation
Notes:
• Okay to omit superscripts, since extra relaxations
can’t hurt.
• Runs in Θ(n3) time.
• Simple to code.
• Efficient in practice.
November 21, 2005
Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson
L19.31