BBM 202 - ALGORITHMS
SHORTEST PATH
- DEPT. OF COMPUTER ENGINEERING
Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University.
T ODAY Shortest Paths Edge-weighted digraph API Shortest-paths - - PowerPoint PPT Presentation
BBM 202 - ALGORITHMS D EPT . OF C OMPUTER E NGINEERING S HORTEST P ATH Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University. T ODAY Shortest Paths
Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University.
4
4->5 0.35 5->4 0.35 4->7 0.37 5->7 0.28 7->5 0.28 5->1 0.32 0->4 0.38 0->2 0.26 7->3 0.39 1->3 0.29 2->7 0.34 6->2 0.40 3->6 0.52 6->0 0.58 6->4 0.93 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52 edge-weighted digraph shortest path from 0 to 6
5
6
VLSI chip.
, RIP).
7
Reference: Network Flows: Theory, Algorithms, and Applications, R. K. Ahuja, T. L. Magnanti, and J. B. Orlin, Prentice Hall, 1993.
http://en.wikipedia.org/wiki/Seam_carving
8
10
v weight w public class DirectedEdge DirectedEdge(int v, int w, double weight) weighted edge v→w int from() vertex v int to() vertex w double weight() weight of this edge String toString() string representation
11
public class DirectedEdge { private final int v, w; private final double weight; public DirectedEdge(int v, int w, double weight) { this.v = v; this.w = w; this.weight = weight; } public int from() { return v; } public int to() { return w; } public int weight() { return weight; } }
from() and to() replace either() and other()
12
public class EdgeWeightedDigraph EdgeWeightedDigraph(int V) edge-weighted digraph with V vertices EdgeWeightedDigraph(In in) edge-weighted digraph from input stream void addEdge(DirectedEdge e) add weighted directed edge e Iterable<DirectedEdge> adj(int v) edges pointing from v int V() number of vertices int E() number of edges Iterable<DirectedEdge> edges() all edges String toString() string representation
13
adj 1 2 3 4 5 6 7
2 .26 4 .38
Bag objects
reference to a
DirectedEdge
8 15 4 5 0.35 5 4 0.35 4 7 0.37 5 7 0.28 7 5 0.28 5 1 0.32 0 4 0.38 0 2 0.26 7 3 0.39 1 3 0.29 2 7 0.34 6 2 0.40 3 6 0.52 6 0 0.58 6 4 0.93
1 3 .29 2 7 .34 3 6 .52 4 7 .37 4 5 .35 5 1 .32 5 7 .28 5 4 .35 6 4 .93 6 0 .58 6 2 .40 7 3 .39 7 5 .28
tinyEWD.txt
V E
14
public class EdgeWeightedDigraph { private final int V; private final Bag<Edge>[] adj; public EdgeWeightedDigraph(int V) { this.V = V; adj = (Bag<DirectedEdge>[]) new Bag[V]; for (int v = 0; v < V; v++) adj[v] = new Bag<DirectedEdge>(); } public void addEdge(DirectedEdge e) { int v = e.from(); adj[v].add(e); } public Iterable<DirectedEdge> adj(int v) { return adj[v]; } }
add edge e = v→w only to v's adjacency list
15
SP sp = new SP(G, s); for (int v = 0; v < G.V(); v++) { StdOut.printf("%d to %d (%.2f): ", s, v, sp.distTo(v)); for (DirectedEdge e : sp.pathTo(v)) StdOut.print(e + " "); StdOut.println(); } public class SP SP(EdgeWeightedDigraph G, int s) shortest paths from s in graph G double distTo(int v) length of shortest path from s to v Iterable <DirectedEdge> pathTo(int v) shortest path from s to v boolean hasPathTo(int v) is there a path from s to v?
16
% java SP tinyEWD.txt 0 0 to 0 (0.00): 0 to 1 (1.05): 0->4 0.38 4->5 0.35 5->1 0.32 0 to 2 (0.26): 0->2 0.26 0 to 3 (0.99): 0->2 0.26 2->7 0.34 7->3 0.39 0 to 4 (0.38): 0->4 0.38 0 to 5 (0.73): 0->4 0.38 4->5 0.35 0 to 6 (1.51): 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52 0 to 7 (0.60): 0->2 0.26 2->7 0.34 public class SP SP(EdgeWeightedDigraph G, int s) shortest paths from s in graph G double distTo(int v) length of shortest path from s to v Iterable <DirectedEdge> pathTo(int v) shortest path from s to v boolean hasPathTo(int v) is there a path from s to v?
18
shortest-paths tree from 0
edgeTo[] distTo[] 0 null 0 1 5->1 0.32 1.05 2 0->2 0.26 0.26 3 7->3 0.37 0.97 4 0->4 0.38 0.38 5 4->5 0.35 0.73 6 3->6 0.52 1.49 7 2->7 0.34 0.60
19
public double distTo(int v) { return distTo[v]; } public Iterable<DirectedEdge> pathTo(int v) { Stack<DirectedEdge> path = new Stack<DirectedEdge>(); for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()]) path.push(e); return path; }
update distTo[w] and edgeTo[w].
20
black edges are in edgeTo[] s 3.1 7.2 4.4 v→w successfully relaxes 1.3
v w
21
update distTo[w] and edgeTo[w].
private void relax(DirectedEdge e) { int v = e.from(), w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; } }
22
s w v
3.1 7.2 distTo[w] distTo[v]
weight of v->w is 1.3
23
distTo[w] = distTo[vk] ≤ ek.weight() + ek-1.weight() + … + e1.weight()
distTo[vk]
≤
distTo[vk-1] + ek.weight() distTo[vk-1 ]
≤
distTo[vk-2] + ek-1.weight() ... distTo[v1]
≤
distTo[v0] + e1.weight()
weight of shortest path from s to w weight of some path from s to w ei = ith edge on shortest path from s to w
to v (and edgeTo[v] is last edge on path).
24
Initialize distTo[s] = 0 and distTo[v] = ∞ for all other vertices. Repeat until optimality conditions are satisfied:
Generic algorithm (to compute SPT from s)
25
Initialize distTo[s] = 0 and distTo[v] = ∞ for all other vertices. Repeat until optimality conditions are satisfied:
Generic algorithm (to compute SPT from s)
27
Edsger W. Dijkstra Turing award 1972
“ Do only what only you can do. ” “ In their capacity as a tool, computers will be but a ripple on the surface of our culture. In their capacity as intellectual challenge, they are without precedent in the cultural history of mankind. ” “ The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence. ” “ It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of
“ APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques
www.cs.utexas.edu/users/EWD
28
(non-tree vertex with the lowest distTo[] value).
29
4 7 1 3 5 2 6 s 6 9 8 4 5 7 1 5 4 15 3 12 20 13 11 9 an edge-weighted digraph 0→1 5.0 0→4 9.0 0→7 8.0 1→2 12.0 1→3 15.0 1→7 4.0 2→3 3.0 2→6 11.0 3→6 9.0 4→5 4.0 4→6 20.0 4→7 5.0 5→2 1.0 5→6 13.0 7→5 6.0 7→2 7.0
(non-tree vertex with the lowest distTo[] value).
30
4 7 1 3 5 2 6 choose source vertex 0 v distTo[] edgeTo[] 0 0.0 - 1 2 3 4 5 6 7
(non-tree vertex with the lowest distTo[] value).
31
4 7 1 3 5 2 6 relax all edges incident from 0 9 8 5 v distTo[] edgeTo[] 0 0.0 - 1 2 3 4 5 6 7
∞ ∞ ∞
(non-tree vertex with the lowest distTo[] value).
∞
32
4 7 1 3 5 2 6 relax all edges incident from 0 9 8 5 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7
∞
5
∞
8 9
(non-tree vertex with the lowest distTo[] value).
33
4 7 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7 1
(non-tree vertex with the lowest distTo[] value).
34
4 7 1 3 5 2 6 choose vertex 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7
(non-tree vertex with the lowest distTo[] value).
35
4 7 1 3 5 2 6 relax all edges incident from 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7 4 15 12 5
∞ ∞
8
(non-tree vertex with the lowest distTo[] value).
36
4 7 1 3 5 2 6 relax all edges incident from 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 4 15 12 ✔
∞ ∞
5 17 20 8
(non-tree vertex with the lowest distTo[] value).
37
4 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 7
(non-tree vertex with the lowest distTo[] value).
38
4 7 1 3 5 2 6 choose vertex 7 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7
(non-tree vertex with the lowest distTo[] value).
∞
39
4 7 1 3 5 2 6 relax all edges incident from 7 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 6 7 8 17
(non-tree vertex with the lowest distTo[] value).
40
4 7 1 3 5 2 6 relax all edges incident from 7 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 14.0 7→5 6 7 8.0 0→7 6 7 8 17
∞
14 15
(non-tree vertex with the lowest distTo[] value).
41
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 14.0 7→5 6 7 8.0 0→7
(non-tree vertex with the lowest distTo[] value).
42
4 7 1 3 5 2 6 select vertex 4 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 14.0 7→5 6 7 8.0 0→7
(non-tree vertex with the lowest distTo[] value).
43
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 14.0 7→5 6 7 8.0 0→7 relax all edges incident from 4 4 5 20 8 14 9
∞
(non-tree vertex with the lowest distTo[] value).
44
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 relax all edges incident from 4 4 5 20 ✔
∞
29 8 14 9 13
(non-tree vertex with the lowest distTo[] value).
45
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7
(non-tree vertex with the lowest distTo[] value).
46
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 select vertex 5
(non-tree vertex with the lowest distTo[] value).
47
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 relax all edges incident from 5 1 13 29 13 15
(non-tree vertex with the lowest distTo[] value).
48
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 relax all edges incident from 5 1 13 29 13 15 14 26
(non-tree vertex with the lowest distTo[] value).
49
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7
(non-tree vertex with the lowest distTo[] value).
50
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 select vertex 2
(non-tree vertex with the lowest distTo[] value).
51
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 relax all edges incident from 2 3 11 26 14 20
(non-tree vertex with the lowest distTo[] value).
52
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 relax all edges incident from 2 3 11 26 14 20 17 25
(non-tree vertex with the lowest distTo[] value).
53
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7
(non-tree vertex with the lowest distTo[] value).
54
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 select vertex 3
(non-tree vertex with the lowest distTo[] value).
55
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 relax all edges incident from 3 9 3 25 20
(non-tree vertex with the lowest distTo[] value).
56
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 relax all edges incident from 3 9 ✔ 3 25 20
(non-tree vertex with the lowest distTo[] value).
57
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3
(non-tree vertex with the lowest distTo[] value).
58
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 select vertex 6
(non-tree vertex with the lowest distTo[] value).
59
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 relax all edges incident from 6
(non-tree vertex with the lowest distTo[] value).
60
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3
(non-tree vertex with the lowest distTo[] value).
61
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 shortest-paths tree from vertex s s
62
63
leaving distTo[w] ≤ distTo[v] + e.weight().
64
distTo[] values are monotone decreasing
edge weights are nonnegative and we choose lowest distTo[] value at each step
65
public class DijkstraSP { private DirectedEdge[] edgeTo; private double[] distTo; private IndexMinPQ<Double> pq; public DijkstraSP(EdgeWeightedDigraph G, int s) { edgeTo = new DirectedEdge[G.V()]; distTo = new double[G.V()]; pq = new IndexMinPQ<Double>(G.V()); for (int v = 0; v < G.V(); v++) distTo[v] = Double.POSITIVE_INFINITY; distTo[s] = 0.0; pq.insert(s, 0.0); while (!pq.isEmpty()) { int v = pq.delMin(); for (DirectedEdge e : G.adj(v)) relax(e); } } }
relax vertices in order
66
private void relax(DirectedEdge e) { int v = e.from(), w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.decreaseKey(w, distTo[w]); else pq.insert (w, distTo[w]); } }
update PQ
67
† amortized
PQ implementation insert delete-min decrease-key total array 1 V 1 V 2 binary heap log V log V log V E log V d-way heap (Johnson 1975) d logd V d logd V logd V E log E/V V Fibonacci heap (Fredman-Tarjan 1984) 1 † log V † 1 † E + V log V
68
S e
s v w
70
71
4 7 1 3 5 2 6 s 6 9 8 4 5 7 1 5 4 15 3 12 20 13 11 9 an edge-weighted DAG 0→1 5.0 0→4 9.0 0→7 8.0 1→2 12.0 1→3 15.0 1→7 4.0 2→3 3.0 2→6 11.0 3→6 9.0 4→5 4.0 4→6 20.0 4→7 5.0 5→2 1.0 5→6 13.0 7→5 6.0 7→2 7.0
72
4 7 1 3 5 2 6 topological order: 0 1 4 7 5 2 3 6
73
4 7 1 3 5 2 6 choose vertex 0 v distTo[] edgeTo[] 0 0.0 - 1 2 3 4 5 6 7 0 1 4 7 5 2 3 6
74
4 7 1 3 5 2 6 relax all edges incident from 0 9 8 5 v distTo[] edgeTo[] 0 0.0 - 1 2 3 4 5 6 7
∞ ∞ ∞
0 1 4 7 5 2 3 6
∞
75
4 7 1 3 5 2 6 relax all edges incident from 0 9 8 5 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7
∞
5
∞
8 9 0 1 4 7 5 2 3 6
76
4 7 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7 1 0 1 4 7 5 2 3 6
77
4 7 1 3 5 2 6 choose vertex 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7 0 1 4 7 5 2 3 6
78
4 7 1 3 5 2 6 relax all edges incident from 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7 4 15 12 5
∞ ∞
8 0 1 4 7 5 2 3 6
79
4 7 1 3 5 2 6 relax all edges incident from 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 4 15 12 ✔
∞ ∞
5 8 17 20 0 1 4 7 5 2 3 6
80
4 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 7 0 1 4 7 5 2 3 6
81
4 1 3 5 2 6 select vertex 4 (Dijkstra would have selected vertex 7) v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 7 0 1 4 7 5 2 3 6
82
4 1 3 5 2 6 relax all edges incident from 4 4 5 20 8 9
∞
v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 7
∞
0 1 4 7 5 2 3 6
83
4 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 relax all edges incident from 4 4 5 20 ✔
∞
29 8 9 13 7
∞
0 1 4 7 5 2 3 6
84
4 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 7 0 1 4 7 5 2 3 6
85
7 1 3 5 2 6 choose vertex 7 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 4 0 1 4 7 5 2 3 6
86
7 1 3 5 2 6 relax all edges incident from 7 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 6 7 8 17 13 4 0 1 4 7 5 2 3 6
87
7 1 3 5 2 6 relax all edges incident from 7 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 6 7 8 17 15 13 4 0 1 4 7 5 2 3 6
88
7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 4 0 1 4 7 5 2 3 6
89
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 select vertex 5 0 1 4 7 5 2 3 6
90
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 15.0 7→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 29.0 4→6 7 8.0 0→7 relax all edges incident from 5 1 13 13 15 29 0 1 4 7 5 2 3 6
91
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 relax all edges incident from 5 1 13 29 13 15 14 26 0 1 4 7 5 2 3 6
92
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 0 1 4 7 5 2 3 6
93
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 select vertex 2 0 1 4 7 5 2 3 6
94
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 relax all edges incident from 2 3 11 26 14 20 0 1 4 7 5 2 3 6
95
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 relax all edges incident from 2 3 11 26 14 20 17 25 0 1 4 7 5 2 3 6
96
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 0 1 4 7 5 2 3 6
97
4 7 1 3 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 select vertex 3 0 1 4 7 5 2 3 6
98
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 relax all edges incident from 3 9 3 25 20 0 1 4 7 5 2 3 6
99
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 relax all edges incident from 3 9 ✔ 3 25 20 0 1 4 7 5 2 3 6
100
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 0 1 4 7 5 2 3 6
101
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 select vertex 6 0 1 4 7 5 2 3 6
102
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 relax all edges incident from 6 0 1 4 7 5 2 3 6
103
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 0 1 4 7 5 2 3 6
104
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 shortest-paths tree from vertex s s 0 1 4 7 5 2 3 6
leaving distTo[w] ≤ distTo[v] + e.weight().
105
distTo[] values are monotone decreasing
because of topological order, no edge pointing to v will be relaxed after v is relaxed edge weights can be negative!
106
public class AcyclicSP { private DirectedEdge[] edgeTo; private double[] distTo; public AcyclicSP(EdgeWeightedDigraph G, int s) { edgeTo = new DirectedEdge[G.V()]; distTo = new double[G.V()]; for (int v = 0; v < G.V(); v++) distTo[v] = Double.POSITIVE_INFINITY; distTo[s] = 0.0; Topological topological = new Topological(G); for (int v : topological.order()) for (DirectedEdge e : G.adj(v)) relax(e); } }
topological order
107
108
109
110
seam
111
seam
112
113
equivalent: reverse sense of equality in relax() 5->4 0.35 4->7 0.37 5->7 0.28 5->1 0.32 4->0 0.38 0->2 0.26 3->7 0.39 1->3 0.29 7->2 0.34 6->2 0.40 3->6 0.52 6->0 0.58 6->4 0.93 5->4 -0.35 4->7 -0.37 5->7 -0.28 5->1 -0.32 4->0 -0.38 0->2 -0.26 3->7 -0.39 1->3 -0.29 7->2 -0.34 6->2 -0.40 3->6 -0.52 6->0 -0.58 6->4 -0.93 longest paths input shortest paths input
114
Parallel job scheduling solution
4 3 5 9 7 6 8 2 1 41 70 91 123 173
0 41.0 1 7 9
1 51.0 2 2 50.0 3 36.0 4 38.0 5 45.0 6 21.0 3 8 7 32.0 3 8 8 32.0 2 9 29.0 4 6 job duration must complete before
115 41 51
1 1
50
2 2
36
3 3
38
4 4
45
5 5
21
6 6
32
7 7
32
8 8
29
9 9
precedence constraint (zero weight) job start job finish duration zero-weight edge to each job start zero-weight edge from each job finish
0 41.0 1 7 9
1 51.0 2 2 50.0 3 36.0 4 38.0 5 45.0 6 21.0 3 8 7 32.0 3 8 8 32.0 2 9 29.0 4 6 job duration must complete before
116
41 51
1 1
50
2 2
36
3 3
38
4 4
45
5 5
21
6 6
32
7 7
32
8 8
29
9 9
critical path duration Parallel job scheduling solution
4 3 5 9 7 6 8 2 1 41 70 91 123 173
118
3 1 2 4 2
6 3 1 11 13 2 15
Dijkstra selects vertex 3 immediately after 0. But shortest path from 0 to 3 is 0→1→2→3. Adding 9 to each edge weight changes the shortest path from 0→1→2→3 to 0→3.
119
4->5 0.35 5->4 -0.66 4->7 0.37 5->7 0.28 7->5 0.28 5->1 0.32 0->4 0.38 0->2 0.26 7->3 0.39 1->3 0.29 2->7 0.34 6->2 0.40 3->6 0.52 6->0 0.58 6->4 0.93 digraph 5->4->7->5 negative cycle (-0.66 + 0.37 + 0.28) 0->4->7->5->4->7->5...->1->3->6 shortest path from 0 to 6
assuming all vertices reachable from s
for (int i = 0; i < G.V(); i++) for (int v = 0; v < G.V(); v++) for (DirectedEdge e : G.adj(v)) relax(e);
120
pass i (relax each edge)
Initialize distTo[s] = 0 and distTo[v] = ∞ for all other vertices. Repeat V times:
Bellman-Ford algorithm
121
0→1 5.0 0→4 9.0 0→7 8.0 1→2 12.0 1→3 15.0 1→7 4.0 2→3 3.0 2→6 11.0 3→6 9.0 4→5 4.0 4→6 20.0 4→7 5.0 5→2 1.0 5→6 13.0 7→5 6.0 7→2 7.0 4 7 1 3 5 2 6 s 6 9 8 4 5 7 1 5 4 15 3 12 20 13 11 9 an edge-weighted digraph
122
4 7 1 3 5 2 6 initialize v distTo[] edgeTo[] 0 0.0 - 1 2 3 4 5 6 7
123
4 7 1 3 5 2 6 pass 0 5
∞
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 2 3 4 5 6 7
distTo[1] distTo[0]
124
4 7 1 3 5 2 6 pass 0 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 5 6 7 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 5
∞
5
125
4 7 1 3 5 2 6 pass 0 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 5 6 7 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
∞
9
126
4 7 1 3 5 2 6 pass 0 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
∞
9 9
127
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7
∞
8
128
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7
∞
8 8
129
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 3 4 9.0 0→4 5 6 7 8.0 0→7
∞
5 12
130
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 4 9.0 0→4 5 6 7 8.0 0→7
∞
5 12 17
131
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
∞
v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 4 9.0 0→4 5 6 7 8.0 0→7 5 15
132
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
∞
v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 5 15 20
133
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 5 8 4
134
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 17 20 3
135
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 7 8.0 0→7 17
∞
11
136
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 28.0 2→6 7 8.0 0→7 17
∞
28 11
137
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 28.0 2→6 7 8.0 0→7 20 28 9
138
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 6 28.0 2→6 7 8.0 0→7 9
∞
4
139
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 28.0 2→6 7 8.0 0→7 9
∞
13 4
140
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 28.0 2→6 7 8.0 0→7 9 28 20
141
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 28.0 2→6 7 8.0 0→7 9 8 5
142
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 17.0 1→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 28.0 2→6 7 8.0 0→7 17 13 1
143
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 28.0 2→6 7 8.0 0→7 17 13 1 14
144
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 28.0 2→6 7 8.0 0→7 28 13 13
145
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 28 13 13 26
146
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 8 13 6
147
4 7 1 3 5 2 6 pass 0 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 8 14 7
148
4 7 1 3 5 2 6 pass 1 5 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 5
149
4 7 1 3 5 2 6 pass 1 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 9 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 9
150
4 7 1 3 5 2 6 pass 1 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 8 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 8
151
4 7 1 3 5 2 6 pass 1 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 5 12 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 14
152
4 7 1 3 5 2 6 pass 1 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 5 15 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 20
153
4 7 1 3 5 2 6 pass 1 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 5 8 4 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7
154
4 7 1 3 5 2 6 pass 1 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 14 20 3 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 20.0 1→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7
155
4 7 1 3 5 2 6 pass 1 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 14 20 3 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 17
2-3 successfully relaxed in pass 1, but not pass 0
156
4 7 1 3 5 2 6 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 14 11 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 26.0 5→6 7 8.0 0→7 26 pass 1
26
157
4 7 1 3 5 2 6 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 14 25 11 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7
2-6 successfully relaxed in pass 0 and pass 1
pass 1
158
4 7 1 3 5 2 6 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 17 9 25 pass 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7
159
4 7 1 3 5 2 6 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 9 4 pass 1 13 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7
160
4 7 1 3 5 2 6 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 9 25 20 pass 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7
161
4 7 1 3 5 2 6 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 9 8 5 pass 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7
162
4 7 1 3 5 2 6 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 14 13 1 pass 1 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7
163
4 7 1 3 5 2 6 pass 1 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 25 13 13 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7
164
4 7 1 3 5 2 6 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 8 13 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 pass 1
165
4 7 1 3 5 2 6 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 8 14 7 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 pass 1
166
4 7 1 3 5 2 6 pass 2, 3, 4, … (no further changes) 0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7
167
4 7 1 5 2 6 v distTo[] edgeTo[] 0 0.0 - 1 5.0 0→1 2 14.0 5→2 3 17.0 2→3 4 9.0 0→4 5 13.0 4→5 6 25.0 2→6 7 8.0 0→7 3 shortest-paths tree from vertex s s
168
4 7 10 13 SPT passes
169
Initialize distTo[s] = 0 and distTo[v] = ∞ for all other vertices. Repeat V times:
Bellman-Ford algorithm
170
be careful to keep at most one copy
171
public class BellmanFordSP { private double[] distTo; private DirectedEdge[] edgeTo; private boolean[] onQ; private Queue<Integer> queue; public BellmanFordSPT(EdgeWeightedDigraph G, int s) { distTo = new double[G.V()]; edgeTo = new DirectedEdge[G.V()];
queue = new Queue<Integer>(); for (int v = 0; v < V; v++) distTo[v] = Double.POSITIVE_INFINITY; distTo[s] = 0.0; queue.enqueue(s); while (!queue.isEmpty()) { int v = queue.dequeue();
for (DirectedEdge e : G.adj(v)) relax(e); } } } queue of vertices whose
distTo[] value changes
private void relax(DirectedEdge e) { int v = e.from(), w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (!onQ[w]) { queue.enqueue(w);
} } }
172
algorithm restriction typical case worst case extra space topological sort no directed cycles E + V E + V V Dijkstra (binary heap) no negative weights E log V E log V V Bellman-Ford no negative cycles E V E V V Bellman-Ford (queue-based) E + V E V V
173
boolean hasNegativeCycle() is there a negative cycle? Iterable <DirectedEdge> negativeCycle() negative cycle reachable from s
4->5 0.35 5->4 -0.66 4->7 0.37 5->7 0.28 7->5 0.28 5->1 0.32 0->4 0.38 0->2 0.26 7->3 0.39 1->3 0.29 2->7 0.34 6->2 0.40 3->6 0.52 6->0 0.58 6->4 0.93 digraph 5->4->7->5 negative cycle (-0.66 + 0.37 + 0.28)
174
edgeTo[v]
s 3 v 2 6 1 4 5
175
1000 × 0.741 × 1.366 × 0.995 = 1007.14497
USD EUR GBP CHF CAD USD 1 0.741 0.657 1.061 1.011 EUR 1.35 1 0.888 1.433 1.366 GBP 1.521 1.126 1 1.614 1.538 CHF 0.943 0.698 0.62 1 0.953 CAD 0.995 0.732 0.65 1.049 1
176
USD
0.741 1.350 0.888 1.126 0.620 1.614 1.049 0.953 1.011 0.995 0.650 1.538 0.732 1.366 0.657 1.521 1.061 0.943 1.433 0.698
EUR GBP CHF CAD
0.741 * 1.366 * .995 = 1.00714497
USD
.2998
.1188
. 4 7 8
4 7 8 7
.0481
1 9 . 5 .4308
. 3 1 2
3 1 1 9 .4201
.0587
3 5 9 8 . 3 5 9 5
EUR GBP CHF CAD
replace each weight w with ln(w)
.2998 - .3119 + .0050 = -.0071
177
178