44 Shortest Paths
44 Shortest Paths
edge-weighted digraph
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 shortest path from 0 to 6
1->3 0.29
0->2 0.26
2->7 0.34
2->7 0.34
6->2 0.40
7->3 0.39
3->6 0.52
3->6 0.52
6->0 0.58
6->4 0.93
2
Google maps
3
Shortest path applications
・PERT/CPM.
・Map routing.
・Seam carving.
・Texture mapping.
・Robot navigation.
・Typesetting in TeX. http://en.wikipedia.org/wiki/Seam_carving
4
Shortest path variants
Which vertices?
・Single source: from one vertex s to every other vertex.
・Single sink: from every vertex to one vertex t.
・Source-sink: from one vertex s to another t.
・All pairs: between all pairs of vertices.
Restrictions on edge weights?
・Nonnegative weights.
・Euclidean weights.
・Arbitrary weights.
Cycles?
・No directed cycles.
・No "negative cycles." which variant?
weight
v w
7
Weighted directed edge: implementation in Java
8
Edge-weighted digraph API
tinyEWD.txt
V 0 2 .26 0 4 .38
8 E
15
4 5 0.35 1 3 .29
adj
5 4 0.35
0
4 7 0.37 2 7 .34 Bag objects
5 7 0.28 1
7 5 0.28 2
5 1 0.32 3 6 .52 reference to a
3 DirectedEdge
0 4 0.38
0 2 0.26 4 object
4 7 .37 4 5 .35
7 3 0.39 5
1 3 0.29
6
2 7 0.34 5 1 .32 5 7 .28 5 4 .35
6 2 0.40 7
3 6 0.52
6 4 .93 6 0 .58 6 2 .40
6 0 0.58
6 4 0.93
7 3 .39 7 5 .28
10
Edge-weighted digraph: adjacency-lists implementation in Java
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 class SP
12
Single-source shortest paths API
public class SP
% 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
13
4.4 S HORTEST P ATHS
‣ APIs
‣ shortest-paths properties
‣ Dijkstra's algorithm
Algorithms
‣ edge-weighted DAGs
15
Data structures for single-source shortest paths
16
Edge relaxation
v 3.1
1.3
s
w 7.2 4.4
black edges
are in edgeTo[]
17
Edge relaxation
18
Shortest-paths optimality conditions
1.3
s
w 7.2 distTo[w]
19
Shortest-paths optimality conditions
21
Generic shortest-paths algorithm
22
4.4 S HORTEST P ATHS
‣ APIs
‣ shortest-paths properties
‣ Dijkstra's algorithm
Algorithms
‣ edge-weighted DAGs
25
Dijkstra's algorithm demo
4 6
27
Dijkstra's algorithm visualization
28
Dijkstra's algorithm visualization
29
Dijkstra's algorithm: correctness proof
Pf.
・Each edge e = v→w is relaxed exactly once (when vertex v is relaxed),
leaving distTo[w] ≤ distTo[v] + e.weight().
・Inequality holds until algorithm terminates because:
– distTo[w] cannot increase distTo[] values are monotone decreasing
– distTo[v] will not change we choose lowest distTo[] value at each step
(and edge weights are nonnegative)
30
Dijkstra's algorithm: Java implementation
32
Dijkstra's algorithm: which priority queue?
unordered array 1 V 1 V2
† amortized
Bottom line.
・Array implementation optimal for dense graphs.
・Binary heap much faster for sparse graphs.
・4-way heap worth the trouble in performance-critical situations.
・Fibonacci heap best in theory, but not worth implementing.
33
Computing a spanning tree in a graph
1 15 3
5
4
12
s 0 3
8
7 2 9
7
9 6 1
11
5
5
4 13
4 20 6
A. Yes!
36
Acyclic shortest paths demo
0→1 5.0
1 15 3
0→4 9.0
5 0→7 8.0
4
12 1→2 12.0
s 0 3
8 1→3 15.0
7 2 9 1→7 4.0
7
2→3 3.0
9 6 1 2→6 11.0
11
5 3→6 9.0
5
4→5 4.0
4 13
4→6 20.0
4 20 6 4→7 5.0
5→2 1.0
5→6 13.0
an edge-weighted DAG 7→5 6.0
7→2 7.0
37
Acyclic shortest paths demo
1 3
v distTo[] edgeTo[]
0 0.0 -
s 0 1 5.0 0→1
2 14.0 5→2
7 2 3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
5 6 25.0 2→6
7 8.0 0→7
4 6
38
Shortest paths in edge-weighted DAGs
Pf.
・Each edge e = v→w is relaxed exactly once (when vertex v is relaxed),
leaving distTo[w] ≤ distTo[v] + e.weight().
・Inequality holds until algorithm terminates because:
– distTo[w] cannot increase distTo[] values are monotone decreasing
39
Shortest paths in edge-weighted DAGs
40
Content-aware resizing
Seam carving. [Avidan and Shamir] Resize an image without distortion for
display on cell phones and web browsers.
http://www.youtube.com/watch?v=vIFCV2spKtg
41
Content-aware resizing
Seam carving. [Avidan and Shamir] Resize an image without distortion for
display on cell phones and web browsers.
43
Content-aware resizing
44
Content-aware resizing
seam
45
Content-aware resizing
46
Longest paths in edge-weighted DAGs
Key point. Topological sort algorithm works even with negative weights.
47
Longest paths in edge-weighted DAGs: application
Parallel job scheduling. Given a set of jobs with durations and precedence
constraints, schedule the jobs (by finding a start time for each) so as to
achieve the minimum completion time, while respecting the constraints.
48
Critical path method
45
5 5
CPM. Use longest path from the source to schedule each job.
7 3
0 9 6 8 2
5 4
0 41 70 91 123 173
51
41 1 1
0 0
50
32 32
2 2
duration 7 7 8 8
critical path
21 36
6 6 3 3
29
9 9
38
4 4
45
5 5
0 6 1
3 3 2
0 14 1
3 11 2
Bellman-Ford algorithm
Repeat V times:
- Relax each edge.
54
Bellman-Ford algorithm demo
0→1 5.0
0→4 9.0
0→7 8.0
1 15 3 1→2 12.0
5 1→3 15.0
4
12 1→7 4.0
s 0 3
2→3 3.0
8
7 2 9 2→6 11.0
7
3→6 9.0
9 6 1
11 4→5 4.0
5
5 4→6 20.0
4 13 4→7 5.0
5→2 1.0
4 20 6
5→6 13.0
7→5 6.0
55
Bellman-Ford algorithm demo
1 3
v distTo[] edgeTo[]
0 0.0 -
s 0 1 5.0 0→1
2 14.0 5→2
7 2 3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
5 6 25.0 2→6
7 8.0 0→7
4 6
56
Bellman-Ford algorithm: visualization
passes
4 7 10
13 SPT
Bellman-Ford algorithm
Repeat V times:
- Relax each edge.
Pf idea. After pass i, found path that is at least as short as any shortest
path containing i (or fewer) edges.
58
Bellman-Ford algorithm: practical improvement
Overall effect.
・The running time is still proportional to E × V in worst case.
・But much faster than that in practice.
59
Single source shortest-paths implementation: cost summary
no directed
topological sort E+V E+V V
cycles
Dijkstra no negative
E log V E log V V
(binary heap) weights
Bellman-Ford EV EV V
no negative
cycles
Bellman-Ford
E+V EV V
(queue-based)
60
Finding a negative cycle
digraph
4->5digraph
0.35
5->4 -0.66
4->5 0.35 s
4->7 0.37
5->4 -0.66
5->7 0.28
4->7 0.37
7->5 0.28
5->7 0.28
5->1 0.32
7->5 0.28
0->4 0.38
5->1 0.32
0->2 0.26
0->4 0.38
7->3 0.39
0->2 0.26
1->3 0.29 negative cycle (-0.66 + 0.37 + 0.28)
7->3 0.39
2->7 0.34
1->3 0.29 negative cycle (-0.66 + 0.37 + 0.28)
5->4->7->5
6->2 0.40
2->7 0.34 5->4->7->5
3->6 0.52
6->2 0.40
6->0 0.58 shortest path from 0 to 6
3->6 0.52
6->4 0.93
6->0 0.58 shortest path from 0 to 6
0->4->7->5->4->7->5...->1->3->6
6->4 0.93 0->4->7->5->4->7->5...->1->3->6
An edge-weighted digraph with a negative cycle
An edge-weighted digraph with a negative cycle 61
Finding a negative cycle
s 2 6 3 4
edgeTo[v]
1 5 v
62
Negative cycle application: arbitrage detection
63
Negative cycle application: arbitrage detection
1.4
66
41 1
7
1.3
0. 1.
33
12
6
0.657
USD GBP
1. 1.521
0 61
3 8
1.0
.5
14
1
11
1.6
32
20
0.9
0.6
0.7
0.6
65
95
98
0. 0.
94
3
1.049
CAD CHF
0.953
An arbitrage opportunity
EUR
.2998 - .3119 + .0050 = -.0071 .1
18
0 01 8
3
-.
-.3
98
119
9
.2 -.
598
-.3
11
87
.4201
USD GBP
-. -.4914
05 replace each
92 05
weight w 43
-.0
787
with !ln(w) -.
109
-.4
20
80
.00
.35
.31
.47
30
50
95
.4 .0
58
7
-.0478
CAD CHF
.0481
Nonnegative weights.
・Arises in many application.
・Dijkstra's algorithm is nearly linear-time.
Acyclic edge-weighted digraphs.
・Arise in some applications.
・Topological sort algorithm is linear time.
・Edge weights can be negative.
Negative weights and negative cycles.
・Arise in some applications.
・If no negative cycles, can find shortest paths via Bellman-Ford.
・If negative cycles, can find one via Bellman-Ford.
Shortest-paths is a broadly useful problem-solving model.
66