Link
Shortest Paths
Analyzing three variations of Dijkstraā€™s algorithm for solving two different shortest paths problems.
Kevin Lin, with thanks to many others.
1
Ask questions anonymously on Piazza. Look for the pinned Lecture Questions thread.

Feedback from the Reading Quiz
A graph where every vertex is connected to every other vertex is called a complete graph.
2

3
BreadthFirstPaths
Instance variables store algorithm data.
marked[v] is true iff v connected to s.edgeTo[v] is vertex visited to get to v.
BreadthFirstPaths constructor computes the result of the algorithm with the bfs iterative method.

Cost model given undirected graph?
Each vertex is visited at most once.Each edge is checked at most twice.
private boolean[] marked;
private int[] edgeTo;
private void bfs(Graph G, int s) {
  Queue<Integer> fringe = ...;
  fringe.add(s);
  marked[s] = true;
  while (!fringe.isEmpty()) {
    int v = fringe.remove();
    for (int w : G.adj(v)) {
      if (!marked[w]) {
        fringe.add(w);
        marked[w] = true;
        edgeTo[w] = v;
      }
    }
  }
}
Demo

Single-Source Shortest Paths
Goal: Find the shortest paths from source vertex s to every other vertex.
Identify: Solution will always be a tree.
4
1
2
3
4
5
6
0
s
5
2
1
15
3
2
11
5
1
1
4
1
v
distTo[]
edgeTo[]
0
0.0
- 
1
2.0
0 -> 1
2
1.0
0 -> 2
3
11.0
6 -> 3
4
5.0
1 -> 4
5
9.0
4 -> 5
6
10.0
4 -> 6

Shortest Paths Tree
If G is a connected edge-weighted graph with V vertices and E edges, how many edges are in the Shortest Paths Tree (SPT) of G? Assume every vertex is reachable.
5
1
2
3
4
5
6
0
s
v
distTo[]
edgeTo[]
0
0.0
- 
1
2.0
0 -> 1
2
1.0
0 -> 2
3
11.0
6 -> 3
4
5.0
1 -> 4
5
9.0
4 -> 5
6
10.0
4 -> 6
Q

If G is a connected edge-weighted graph with V vertices and E edges, how many edges are in the Shortest Paths Tree (SPT) of G? Assume every vertex is reachable.
6

Finding a Shortest Paths Tree
What is the shortest paths tree for the graph below starting from the source vertex A?
7
Q
B
C
A
s
5
5
D
1
2
2
1

Finding a Shortest Paths Tree
What is the shortest paths tree for the graph below starting from the source vertex A?
Hypothesis: BFS queue considers A-B and A-C (marking immediate neighbors) first.
Identify: Explore A-C-B before A-B.
8
A
B
C
A
s
5
5
D
1
2
2
1
0
1
2
4

Algorithm for Finding a Shortest Paths Tree
Identify: Explore A-C-B before A-B.
Plan: Visit vertices in order of distance from source, not necessarily immediate neighbors.Maintain a priority queue ordered on the shortest available paths.
9
B
C
A
s
5
5
D
1
2
2
1
0
1
2
4

Dijkstraā€™s Algorithm
Insert all vertices into fringe PQ, storing vertices in order of distance from source.
While fringe is not empty: Remove (closest) vertex v and relax all edges pointing from v.
10
Demo
1
2
3
4
5
6
0
s
5
2
1
15
3
2
11
5
1
1
4
1
Edge Relaxation (v, w).
For each edge (v, w), add edge to the SPT only if the edge is closer than our best-so-far.

Dijkstraā€™s Pseudocode
PQ.add(s, 0)
For all other vertices v, PQ.add(v, infinity)
While PQ is not empty:
p = PQ.removeSmallest()Relax all edges from p

Relaxing an edge (v, w) with weight:
If distTo[w] > distTo[v] + weight:
distTo[w] = distTo[v] + weightedgeTo[w] = vPQ.changePriority(w, distTo[w])
Invariants
edgeTo[v]: best known predecessor of v.
distTo[v]: best known distance of s to v.
PQ maintains vertices based on distTo.

Important properties
Always visits vertices in order of total distance from source. Relaxation always fails on edges to visited (white) vertices.
11

Dijkstraā€™s Runtime Analysis
PQ.add(s, 0)
For all other vertices v, PQ.add(v, infinity)
While PQ is not empty:
p = PQ.removeSmallest()Relax all edges from p

Relaxing an edge (v, w) with weight:
If distTo[w] > distTo[v] + weight:
distTo[w] = distTo[v] + weightedgeTo[w] = vPQ.changePriority(w, distTo[w])
ArrayHeapMinPQ implementation.
V adds, each O(log V) time.
V removals, each O(log V) time.
E changePriority, each O(log V) time.

Overall:	O(V log V + V log V + E log V).
Simple:	O(V log V + E log V).
Assuming E > V, this is just O(E log V) for connected graphs.
12

Dijkstraā€™s Algorithm Correctness
Dijkstraā€™s algorithm. Visit vertices in order of distance from source.
On visit, relax every edge from the visited vertex.

Dijkstraā€™s can fail if the graph has negative weight edges. Give an example graph.
13
Q

Dijkstraā€™s Algorithm Correctness
Dijkstraā€™s algorithm. Visit vertices in order of distance from source.
On visit, relax every edge from the visited vertex.

Dijkstraā€™s can fail if the graph has negative weight edges. Give an example graph.
Hide the real shortest path behind a later-explored path.
14
A
33
34
-99
1
9
1
9

Dijkstraā€™s Algorithm Correctness
Dijkstraā€™s algorithm. Visit vertices in order of distance from source.
On visit, relax every edge from the visited vertex.

Dijkstraā€™s can fail if the graph has negative weight edges. Give an example graph.
Hide the real shortest path behind a later-explored path.
15
A
33
34
-99
-90
9
1
9
Violates distTo invariant!

16
Single-Pair Shortest Path Problem
Ā© Mapbox; Ā© OpenStreetMap; Improve this map.

17
Single-Pair Shortest Path: Dijkstraā€™s Algorithm
Ā© Mapbox; Ā© OpenStreetMap; Improve this map.

18
Single-Pair Shortest Path: A* Search
Ā© Mapbox; Ā© OpenStreetMap; Improve this map.

A* Search Algorithm
Dijkstraā€™s algorithm with one modification.
Dijkstraā€™s algorithm:	Priority is defined by distTo[v] only.
A* search:		Priority is defined by distTo[v] + h(v, goal).
Where h(v, goal) is a heuristic: an estimate of the distance from v to the goal.
19
distTo[v]
h(v, goal)
Demo

Computing a Heuristic
Where h(v, goal) is a heuristic: an estimate of the distance from v to the goal.
For maps, we can use Euclidean distance (right triangle hypotenuse length).
20
h(v, goal)

Computing a Heuristic
Where h(v, goal) is a heuristic: an estimate of the distance from v to the goal.
For maps, we can use Euclidean distance (right triangle hypotenuse length).
Will A* search return the correct shortest path if h(v, goal) = 10 for every v in the graph?
21
h(v, goal)
Q

Will A* search return the correct shortest path if h(v, goal) = 10 for every v in the graph?
22

23
A bad heuristic: overestimating distance for a single point
Ā© Mapbox; Ā© OpenStreetMap; Improve this map.
h(Montlake Bridge, goal) = 100 miles

Single-Pair Shortest Path Algorithms
Dijkstraā€™s algorithm:	Priority defined by distTo[v] only.
A* search:		Priority defined by distTo[v] + h(v, goal). Correct if h is good.
Best-first (greedy):	Priority defined by h(v, goal) only. Can be wrong even with good h!
For this course: Know that the choice of h matters. If h is bad, A* can be incorrect.
24
distTo[v]
h(v, goal)
Demo

Summary
Single-Source Shortest Paths: Shortest path from s to every reachable vertex.
Shortest path from s to every vertex is a shortest paths tree with V - 1 edges.
Compute the SPT on non-negative, weighted graphs using Dijkstraā€™s algorithm.

Single-Pair Shortest Path: Shortest path from s to a specific vertex v.
Dijkstraā€™s algorithm is inefficient because it solves for the SPT to every vertex.
Modify Dijkstraā€™s algorithm by adding a heuristic to capture the potential path length.
A* search algorithm can be much faster than Dijkstraā€™s.
25