Lecture 21 Summary

This lecture was given to studio audience - not to a real class.

Once again, there are no activities in this lecture. There is, however, quite a bit of verbal interaction; please stop the video and allow students to respond in these places.


Today's lecture about shortest paths will tie together what we've learned about dynamic programming with standard graph algorithms.


This slide is a review of what was done earlier in the course with shortest path algorithms.

The instructor makes the point that Dijkstra's algorithm only works in a graph with no negative-cost edges, while the algorithm we will see today will work on a graph with negative-cost edges. However, if all edges have positive cost, Dijkstra's algorithm is more efficient.

Note that if the graph has a negative-cost cycle, the shortest path is undefined; thus no algorithm could operate correctly on such a graph.


At 3:30, the instructor asks a question ( interaction 1 from the interaction summary). He asks, "If there are no negative cycles, the shortest paths are simple paths, that is we repeat no vertices. Why is this?" A student responds, "If all cycles have positive cost, going around a cycle only increases the cost."

The point here is that shortest paths must have no more than n-1 edges. We will use this bound later.


We're going to start with a slightly different problem: Shortest paths with exactly k edges.


Here the instructor gives the recurrence for the shortest paths with exactly k edges problem. For each vertex x adjacent to w in the graph, we consider the costs of paths of length k-1 from v to x plus the cost of the edge from x to w.


Here we just turn the previous slide's recurrence into pseudocode. The outer loop iterates over all possible path lengths; here we use the fact that we know that the path length cannot be more than n-1 (from slide 3).


This is a refinement of the algorithm from the previous slide.

At 10:44 the instructor asks (interaction 2 from the interaction summary), "What is the impact of adding the additional term into the computation? Now what does M[k, w] mean?" A student responds, "Instead of finding the shortest path with _exactly_ k edges, we find the shortest path with _at most_ k edges."


Another refinement of the algorithm; here we've dropped the subscript k.

At 13:10, the instructor asks (interaction 3 from the interaction summary) "How does this change the computation?" A student responds, "Now we are just keeping the latest value, where the previous one was storing all of the iterations." The instructor asks, "Does it have any other impact?" There is no response, so the instructor does an example, first using algorithm 1, then algorithm 3.

At 16:18, he asks, "What does this table look like for version 3?" At 16:58, a student responds, "b gets updated to 3 in the first step, not the second step."

At 17:41, he asks, "Why did you assume the update order was a, b?" A student responds, "Because that's the order you wrote them in the table." The instructor makes the point that since it's a foreach statement, we have no way of knowing the actual update order of the vertices; and different update orders do actually result in different behavior.

The instructor says he wants to make two observations; the update order does make a difference, since it is possible for algorithm 3 to find the answer faster than algorithm 2. It also doesn't store the whole array of all iterations, so it uses less space, but the risk is that maybe it isn't doing the right thing. Thus on the next slide we prove that it is doing the right thing.


We'd prove that there is a path of length M[w] using induction. If we have a path of length M[x] to x, and we update from x to w, then we've got a path of length M[w] to w.

We'd prove that at the end of each iteration, M[w] <= M[i,w] using induction. The idea is that if we're ahead (that is, if the inequality holds, which is true by the induction hypothesis), the updates will keep us ahead (that is, updates maintain the inequality).

P[w] is the vertex that precedes w in the path from v to w.


This is the Bellman-Ford algorithm. At 24:00, the instructor asks, "What is the runtime of this algorithm?" A student responds, "O(nm)".

The instructor makes the point that we haven't made any assumptions about the weights of the edges, so the algorithm holds for negative-cost edges.


But what about negative-weight cycles? We can use the pointer graph to find these. The "pointer graph" is the graph we get by looking at the values in the array P.

At 27:10, the instructor asks, "Can the pointer graph have a cycle?" A student responds, "Yes, if the graph has a negative-weight cycle."


This is the outline of the proof.

The instructor makes the point that if M[x] decreases, the first inequality still holds. And if we change M[w], we change P[w].

There is some confusion as to whether the indices are switched in the first inequality below "Just before the update." The slide is in fact correct; the indices are not switched.


This slide has a typo (and in fact the instructor misspeaks several times). If the graph has no negative cycles, the pointer graph has no cycles (not "no negative cycles"). The edges of the pointer graph are not weighted, so it doesn't make sense to talk about a negative cycle in the pointer graph.


If we want to detect negative-cost cycles in the graph, we can look for a cycle in the pointer graph. Thus we can use the Bellman-Ford algorithm to find negative-cost cycles.

At 35:44, the instructor asks, "Would you ever want to find negative-cost cycles?" there is a long pause, then a student responds, "If we consider an application, a negative cycle would correspond to a situation that is not well-defined." The instructor makes the point that if the costs were distances, looking for negative cycles would correspond to doing a validity check of the graph.


Foreign exchanges are set up so that someone can't make money just by changing their money to a different currency and then back (for example, dollars to Euros back to dollars).

On this slide, the instructor does an example that requires quite a bit of computation; he asks the students to supply the computed values. If you're short on time you need not stop the video here for student responses.

At 41:12, a situation is found where 1 euro becomes 1.024 euros by going around a cycle. Thus someone could just keep going around this cycle as many times as they wanted, and make money.

At 41:55, the instructor asks, "What's the connection here to shortest paths?" A student responds, "In both cases we have undesired behavior associated with a cycle." The instructor elaborates by saying that the differences are that we have a max, not a min, and correspondingly we are looking for positive-cost cycles, not negative-cost cycles. So this problem is equivalent to the shortest paths problem. Having multiplication instead of addition is not a problem, since if we considered the logarithms of the values, we could add instead of multiplying. Thus you can use the Bellman-Ford algorithm to detect the cases where you could make money by exchanging between different currencies.

The instructor briefly returns to two earlier slides and summarizes: We started with a recurrence, and used dynamic programming to find a solution, and then looked at how this connects to negative-cost cycles, and showed some applications of this.