```
Shortest Paths
Variants of Dijkstra’s shortest paths algorithm.
Kevin Lin, with thanks to many others.
1
Dijkstra’s Algorithm
Give the order in which Dijkstra’s algorithm would visit each vertex starting from vertex A, where “visiting a vertex v” means “relaxing all of the edges out of v.”
2
Q
A
B
C
F
E
H
D
G
5
1
6
4
4
4
5
2
2
3
Q1: Give the order in which Dijkstra’s algorithm would visit each vertex starting from vertex A, where “visiting a vertex v” means “relaxing all of the edges out of v.”
Dijkstra’s Algorithm
Give the order in which Dijkstra’s algorithm would visit each vertex starting from vertex A, where “visiting a vertex v” means “relaxing all of the edges out of v.”
3
A
A
B
C
F
E
H
D
G
5
1
6
4
4
4
5
2
2
3
Weight Modification
Change one of the weights in the graph so that the shortest paths tree returned by Dijkstra’s algorithm is not correct.
4
Q
A
B
C
F
E
H
D
G
5
1
6
4
4
4
5
2
2
3
Q1: Change one of the weights in the graph so that the shortest paths tree returned by Dijkstra’s algorithm is not correct.
Weight Modification
Change one of the weights in the graph so that the shortest paths tree returned by Dijkstra’s algorithm is not correct.
5
A
A
B
C
F
E
H
D
G
5
1
6
4
4
4
5
2
2
3
A* Search
Give the path that A* search returns from A to G with the heuristic defined below.
h(A, G) = 2
h(B, G) = 2
h(C, G) = 20
h(D, G) = 2
h(E, G) = 6
h(F, G) = 2
h(G, G) = 0
h(H, G) = 2
6
Q
A
B
C
F
E
H
D
G
5
1
6
4
4
4
5
2
2
3
Q1: Give the path that A* search returns from A to G with the heuristic defined below.
A* Search
Give the path that A* search returns from A to G with the heuristic defined below.
7
A
A
B
C
F
E
H
D
G
5
1
6
4
4
4
5
2
2
3
8
A bad heuristic: overestimating distance for a single point
© Mapbox; © OpenStreetMap; Improve this map.
h(Montlake Bridge, goal) = 100 miles
Content-Aware Image Resizing
Seam carving. Resize an image without distortion for display on phones and internet.
9
Seam carving for content-aware image resizing (Avidan, Shamir/ACM); Broadway Tower (Newton2, Yummifruitbat/Wikimedia)
Original Photo
Horizontally-Scaled
Seam-Carved
10
Seam Carving
To find a vertical seam…
Vertex. Pixel in image.
Edge. Cost to go from a pixel to its 3 downward neighbors.
Weight. Energy function of 8 neighboring pixels.
Seam. Shortest path (sum of weights) from top to bottom.
11
Shortest Paths (Robert Sedgewick, Kevin Wayne/Princeton)
Seam Carving
Given a digraph with positive edge weights, and two distinguished subsets of vertices S and T, find a shortest path from any vertex in S to any vertex in T.
Give a reduction from seam carving to single-pair shortest paths.
12
Shortest Paths (Robert Sedgewick, Kevin Wayne/Princeton)
Q
S
T
Vertex. Pixel in image.
Edge. Cost to go from a pixel to its 3 downward neighbors.
Weight. Energy function of 8 neighboring pixels.
Seam. Shortest path (sum of weights) from top to bottom.
Q1: Give a reduction from seam carving to single-pair shortest paths.
Seam Carving
Given a digraph with positive edge weights, and two distinguished subsets of vertices S and T, find a shortest path from any vertex in S to any vertex in T.
Give a reduction from seam carving to single-pair shortest paths.
13
Shortest Paths (Robert Sedgewick, Kevin Wayne/Princeton)
A
S
T
```