Lecture 15
Dynamic programming III
CSE 421 Autumn 2026
1
Previously…
2
Dynamic Programming takeaways
3
Dynamic Programming algorithms for..
4
Today
5
“Bottom-up” vs “Top-down” DP
6
“Bottom-up” vs “Top-down” DP
7
BottomUpTribonacci():
TopDownTribonacci(, Memo = {}):
“Bottom-up” vs “Top-down” DP
Overall, the runtime of Top-down is asymptotically at least as good as Bottom-up! Each square of the memo is only computed at most once.
8
Graph dynamic programming
9
Currency exchange
10
Goal: Find the best sequence of conversions from currency to currency .
This is equivalent to finding the path with the largest product of weights.
Is there a way to reframe the problem as finding the shortest path?
Yes! Set new edge weights to
original weight
Currency exchange
11
Goal: Find the best sequence of conversions from currency to currency .
This is equivalent to finding the path with the largest product of weights.
Is there a way to reframe the problem as finding the shortest path?
Yes! Set new edge weights to
original weight
Then
Currency exchange
12
New Goal: Find the shortest path from currency to currency .
Note: there are negative weights! Shortest means “smallest number” (taking negative numbers into account)
Currency exchange
13
New Goal: Find the shortest path from currency to currency .
Note: there are negative weights! Shortest means “smallest number” (taking negative numbers into account)
A path of total weight implies a currency conversion from 1 unit of to units of
since
implies
Currency exchange
14
New Goal: Find the shortest path from currency to currency .
Note: there are negative weights! Shortest means “smallest number” (taking negative numbers into account)
A path of total weight implies a currency conversion from 1 unit of to units of
For example, here there is a direct conversion of USD to HKD that yields HKD per USD
Currency exchange
15
New Goal: Find the shortest path from currency to currency .
Note: there are negative weights! Shortest means “smallest number” (taking negative numbers into account)
A path of total weight implies a currency conversion from 1 unit of to units of
For example, here there is a direct conversion of USD to HKD that yields HKD per USD
USDBTCEURHKD yields HKD per USD
Currency exchange
16
New Goal: Find the shortest path from currency to currency .
Note: there are negative weights! Shortest means “smallest number” (taking negative numbers into account)
What happens if we change HKD to INR from to ?
Currency exchange
17
New Goal: Find the shortest path from currency to currency .
Note: there are negative weights! Shortest means “smallest number” (taking negative numbers into account)
What happens if we change HKD to INR from to ?
Negative cycle: Sum of weights is negative
Negative weights shortest path
Input: A directed graph with weights and a vertex
Output: For every vertex , the distance of the shortest directed path where here a path’s length is the sum of its weights (taking negative signs into account)
18
Why not just run Dijkstra’s?
Dijkstra’s may incorrectly calculate distances �when negative weights are involved
Negative weights shortest path
19
The Bellman-Ford algorithm
20
A failed attempt
21
Negative weights shortest path
Input: Directed graph and weights and a vertex
Output: For all vertices , the weight of the shortest path .
22
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 consider?
23
Bellman-Ford DP algorithm
The function: For , let be the length of the shortest path consisting of at most edges
24
By the previous Observation 2, the shortest path has weight .
Bellman-Ford DP algorithm
The function: For , let be the length of the shortest path consisting of at most edges
25
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
26
Recursive structure/definition:
Bellman-Ford DP algorithm
The function: For , let be the length of the shortest path consisting of at most edges
27
Recursive structure/definition: