SHORTEST PATHS STUDY GUIDE
Graph Representation
 Use DirectedEdge instead of Edge.
 Adjacency list tracks only edges pointing from vertex.
Shortest path representation
 Can represent shortest path as a tree.
 In Java, we represent the shortest path tree with two arrays: distTo[] and edgeTo[].
Edge relaxation
 What does it mean to relax an edge? Update shortest path tree (edgeTo[] and distTo[]) to reflect the information contained in that edge. What does it mean to relax a vertex?
 The code for relax should be second nature.
 What are the shortest paths optimality conditions? Why is it true?
 What is the generic shortest path algorithm? Why does it always eventually get the right answer?
Manually tracing Bellmanâ€“Ford and Dijkstra's algorithm.
This should be easy.
BellmanFord algorithm
 Edgeweighted digraph must have no negative cycles.
 Relax all the edges. Then relax them all again. And again.
Repeat this V times. You are done.
 Running time is obviously E V.
 Proof: After ith relaxation of all E edges, the algorithm has found a path that is at least as short
as the shortest path from s to v containing <= i edges.
There are at most V1 edges between any two vertices in the shortest path tree (otherwise it is not a tree).
Dijkstra's algorithm
 Edge weights must be nonnegative.
 Relax the vertices in increasing order of distance from s.
 Important invariant: Once a vertex belongs to the shortest path tree,
its distTo[] and edgeTo[] entries never change. Why is this invariant true?
How does it prove correctness?
 Why is the running time (V + E) log V if we use a binary heap to implement the priority queue?
Why do the book and slides say E log V? If we change our priority queue implementation,
how does the running time change
 How is Dijkstra's algorithm similar to Prim's algorithm? How is it dffferent?
Recommended Problems
C level

Simulate Dijkstra's algorithm on the edgeweighted digraph below, starting from vertex 0.
 Give all the distTo[] and edgeTo[] values that result.
 What is the maximum number of items in the priority queue?
 What is the last vertex popped from the priority queue?
 What letter is spelled out by the edges of the shortestpath tree (SPT) computed by Dijksta's algorithm?
Answers
(a)

distTo[ ] 
edgeTo[ ] 
0 
0 
 
1 
24 
0 
2  5  0 
3  6  2 
4  34  5 
5  14  3 
(b) 4, (c) 4, (d) S
 Textbook 4.3.1 and 4.4.1
B level
 Spring 2012 Final, #5
 Fall 2012 Final, #5
 Fall 2011 Final, #4
 Think about how to solve the undirected version of the shortest paths problem. Why is it easy for graphs with positive edge weights? Why is it hard if we include negative edge weights?
 Textbook 4.4.25
 Since Prim's algorithm and Dijkstra's algorithm are so similar, why do we need to check for cycles in Prim's,
but not in Dijkstra's?
 Textbook 4.4.42 and 4.4.44
A level
 Fall 2018 Final, #15 (good followup after Fall 20 week 9 precept)
 Textbook 4.4.34
 Textbook 4.4.37