Lecture 16
Dynamic Programming IV
CSE 421 Autumn 2025
1
Previously…
2
Negative weights shortest path
Input: Directed graph and weights and a vertex
Output: For all vertices , the weight of the shortest path
3
For convenience, we’ll consider shortest paths to an endpoint, rather than from a source
(it’s the same thing up to inverting the direction of each edge)
Observation 1: If a path contains a negative weight cycle, then a shortest path doesn’t exist.
Observation 2: If has no negative cycles then there is a shortest path of length .
Proof: Any path of length has a cycle. That cycle has weight , so removing it only decreases the weight. Repeat until path is of length .
Bellman-Ford DP algorithm
What function should our DP algorithm make recursive calls to?
4
Bellman-Ford DP algorithm
The function: For , let be the length of the shortest path consisting of at most edges
5
Can we define the value at some points recursively based on the value at other points?
Bellman-Ford DP algorithm
The function: For , let be the length of the shortest path consisting of at most edges
6
Recursive structure/definition:
Bellman-Ford DP algorithm
The function: For , let be the length of the shortest path consisting of at most edges
7
Recursive structure/definition:
The solution we are interested in is
Today
8
Bellman-Ford DP algorithm
(Assuming no negative cycles)
Filling the memo table:
Return output: The table contains the shortest path from each vertex to . For the actual path from to , follow from until it reaches .
9
Note: syntactically, this is ok because we first compute everything at the - layer, and then we consider the -th layer.
time and space?
Bellman-Ford DP algorithm
Filling the memo table:
Return output: The table contains the shortest path from each vertex to . For the actual path from to , follow from until it reaches .
(Assuming no negative cycles)
10
time and space?
time since, for each , we consider each edge once
space for the tables
Space saving
How might we save on space?
11
Space saving
Key observation: only depends on entries . Rows can be discarded!
12
So we can simply repeat the exact same algorithm as before, but discarding a row once we have used it. We will only ever keep two rows at once.
There’s an even more space efficient version where we keep a single row!
Bellman-Ford better DP implementation
(Assuming no negative cycles)
Filling the memo table:
Return output: The table contains the shortest path from each vertex to . For the actual path from to , follow from until it reaches .
13
Note: for each , we are repeating the same process.
Bellman-Ford better DP implementation
Filling the memo table:
Return output: The table contains the shortest path from each vertex to . For the actual path from to , follow from until it reaches .
(Assuming no negative cycles)
14
time still
space
Bellman-Ford better DP implementation
Filling the memo table:
Return output: The table contains the shortest path from each vertex to . For the actual path from to , follow from until it reaches .
(Assuming no negative cycles)
15
You may be worried by the fact that during round , we update values in as we go along. So, the later comparisons in that round, may involve already updated values, rather than the ones from round -1.
This is ok: in fact, it will make each converge faster to its true value.
Bellman-Ford correctness
16
Correctness of the first version with the larger table follows from our case analysis (+ induction). This shows that � = weight of shortest path with at most edges.
Correctness of the latest version follows from the fact that at the end of round we ensure (this can be shown similarly by induction):�1. weight of shortest path with at most edges.�2. is the weight of some valid path .
Putting 1 and 2 together with fact that there exists a shortest path of length -, means that, after round -, = weight of shortest path .
Even more savings
Observation: If doesn’t decrease in round , then we don’t need to consider any edges in round (as we’d be doing the same comparison as in the previous round). In other words, the paths through have already been considered.
17
Note what this means:
Even better DP implementation
(Assuming no negative cycles)
18
Bellman-Ford example
19
Bellman-Ford example
20
Bellman-Ford example
21
Bellman-Ford example
22
Bellman-Ford example
23
Bellman-Ford example
24
Bellman-Ford example
25
Bellman-Ford example
26
Bellman-Ford example
27
Bellman-Ford example
28
Bellman-Ford example
29
Bellman-Ford example
30
Bellman-Ford example
31
Bellman-Ford runtime
32
Note that the runtime is still in the worst case. Indeed, there are graphs where this is the runtime, e.g. complete graphs.
But, in practice the runtime can be much faster if you are dealing with graphs that have fewer than edges.
Bellman-Ford correctness
33
Correcteness of the original version with the larger table follows from our case analysis (+ induction). This shows that will contain the shortest path of with at most edges.
Correcteness of the latest version follows from the fact that at each round we are doing the same comparisons, except being clever about skipping comparisons that will for sure not result in an update.
Detecting negative cycles
34
Correctness tells us that after - iterations the value is the weight of the shortest path that uses - edges.
This is indeed the shortest path if there are no negative cycles (since we have argued earlier that, if there are no negative cycles, there is always a shortest path using - edges).
How can we slightly modify the algorithm to additionally detect if a negative cycle exists?
Do one additional round and check if any value gets updated! If so, then there must be a negative cycle. If not, no value will ever get updated again (since the next time we are doing the same comparisons again)