4. All-pairs shortest paths using Dynamic Programming
Suppose we wanted to compute the shortest paths between all pairs of vertices. Dijkstra's algorithm focuses on a single source, so a natural question is whether we can do better than simply running Dijkstra v times. Let's consider a dynamic programming approach.
Suppose that we label the vertices from 0 to 1, and let APSP(i, j) be the weight of the shortest path between vertices i and j such that only vertices 0, 1, ..., K are allowed to be used. Then the cost of the shortest path between vertices i and j is then given by APSP(i, j, n-1).
a) Consider the following graph with 3 vertices.
Compute APSP(j) for all
put in answers.md
b) Do you see a relationship between APSP(i, j, 1) and APSP(i, j, 2)? Can you write APSP(i, 2) in terms of APSP(i, 0) and APSP(i, 1) only?
put in answers.md
c) Suppose that an oracle (i.e., an all-powerful source of information) makes available to us all possible values for APSP(i, j, k: 1) for all i and some particular value of k: 1 c. . Then what is the shortest path APSP(i, j, k)? Well, it is either APSP(i, j, k-1), or some other path from i to j that has length k. Generalize your observation from b) above to give an optimal substructure property for APSP(i, j, k).
put in answers.md
for all, will be inefficient. Suppose we perform top-down memoization so that we only ever
compute each subproblem from scratch once. How many distinct subproblems will be computed from scratch, and what is the resulting work of this dynamic programming algorithm?
put in answers.md
e) Compare the work of this algorithm against that of just running Dijkstra n times. Is our dynamic
put in answers.md