Shortest Paths Study Guide

Single-Source Shortest Paths. Suppose we want to record the shortest paths from some source vertex s to every other vertex in the graph. In an unweighted graph, we can use BFS to solve this problem. But if edges have weights (representing, for example road lengths), we can solve this problem by computing the shortest paths tree. Typically, a shortest paths tree is stored as an array of edgeTo and distTo values. edgeTo[w] contains the vertex v whose edge (v, w) is in the shortest path to w. distTo[w] contains the corresponding distance from s to w.

Dijkstra’s Algorithm. Dijkstra’s algorithm works by visiting each vertex in order of its distance from the source, where visiting a vertex means relaxing every neighboring vertex. Informally, relaxing an edge means using it if it’s better than the best known distance to the target vertex, otherwise ignoring it.

Single-Pair Shortest Paths. If we need only the path to a single target, then Dijkstra’s is inefficient as it explores many many edges that we don’t care about. For example, when routing from Seattle to New York, we’d explore everything within a radius of thousands of miles in all directions before reaching New York.

A* Search. To fix this, we make a very minor change to Dijkstra’s algorithm, where instead of visiting vertices in order of distance from the source, we visit them in order of distance from the source + h(v), where h(v) is some heuristic. Choice of heuristic can affect the runtime for A* search, and some heuristics can even cause A* search to return a path that is longer than the actual shortest path.

  1. [Adapted from Textbook 4.4.34] Given a weighted digraph, describe an algorithm for finding a monotonic shortest path from s to every other vertex. A path is monotonic if the weight of every edge on the path is either strictly increasing or strictly decreasing. The path should be simple (no repeated vertices).
  2. [Adapted from Textbook 4.4.37] Given a weighted digraph and two vertices s and t, describe an algorithm for finding an edge whose removal causes maximal increase in the s–t single-pair shortest path length.
  3. Q7b from CS 61BL 17su Final (Solution)
  4. Q6 from CS 61B 18sp Final (Solution)
  5. Q9 from CS 61B 18sp Final (Solution)
  6. Q7 from CS 61B 19sp MT2 (Solution)