Link

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. True or false: Adding a constant to every edge weight does not change the solution to the single-source shortest paths problem.
  2. True or false: Multiplying a positive constant to every edge weight does not change the solution to the single-source shortest paths problem.
  3. [Adapted from Textbook 4.4.25] 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. Your algorithm should run in time proportional to E log V in the worst case.
  4. Describe a family of graphs with V vertices and E edges for which the worst-case running time of Dijkstra’s algorithm is achieved.
  5. [Adapted from Textbook 4.4.34] Give an algorithm to solve the following problem: Given a weighted digraph, find 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).
  6. [Adapted from Textbook 4.4.37] Develop an algorithm for finding an edge whose removal causes maximal increase in the shortest-paths length from one given vertex to another given vertex in a given edge-weighted digraph.
  7. Q1 from CS 61B 16sp Final
  8. Q5 from CS 61B 16sp Final
  9. Q1a from CS 61BL 16su Final
  10. Q2a from CS 61BL 17su Final
  11. Q7b from CS 61BL 17su Final
  12. Q6 from CS 61B 18sp Final
  13. Q9 from CS 61B 18sp Final
  14. Q4g from CS 61B 18sp Final
  15. Q7 from CS 61B 19sp MT2