1 of 27

Lecture 15

Dynamic programming III

CSE 421 Autumn 2026

1

2 of 27

Previously…

2

3 of 27

Dynamic Programming takeaways

  • Identify the recursive structure of the problem you are trying to solve
  • Iterate through subproblems starting from the “smallest” and building up to the “largest”. For each one:
    • Find the optimal value (and record it), using the previously-computed optimal values to smaller subproblems.
    • If useful, also record the choices made to obtain each optimal value.
  • Runtime given by: (size of the memo table) x (time it takes to solve each subproblem given the previous)

3

4 of 27

Dynamic Programming algorithms for..

4

  • Tribonacci numbers
  • Edit distance
  • Knapsack

5 of 27

Today

5

  • DP on graphs: Detecting negative cycles & shortest path in a graph with negative weights (Bellman-Ford)

6 of 27

“Bottom-up” vs “Top-down” DP

  • So far we have seen that the recursive subproblems in DP algorithms are always smaller.
  • This yields a natural “bottom-up” ordering for filling the memo table (start from the small base cases, and slowly move upwards).
  • Instead, we could also fill up the table with a “top-down” approach. We are still filling the memo table starting from the smaller cases, but the way we implement the recursion is different.

6

7 of 27

“Bottom-up” vs “Top-down” DP

7

BottomUpTribonacci():

  • Initialize Memo[].
  • For , set Memo[] = 1.
  • For to :
    • Memo[] = Memo[-] + Memo[-] + Memo[-]

TopDownTribonacci(, Memo = {}):

  • If is in Memo, output Memo[].
  • If , output 1.
  • Memo[] = TopDownTribonacci(-, Memo) + TopDownTribonacci(-, Memo) + �TopDownTribonacci(-, Memo)

8 of 27

“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.

  • Advantage of Top-down:
    • Not all squares of the table may be needed! Top-down only computes what’s needed, which in some cases could yield significant savings (not just constant factors).
  • Disadvantage of Top-down:
    • The depth of the recursion stack may be large, e.g. in TopDownTribonacci, the depth is (i.e. there are nested calls!)
    • The recursion stack is often in the computer’s memory (RAM), while the memo table is in hard drive. RAM is much more limited, so you can get memory overflow errors if not careful.�

8

9 of 27

Graph dynamic programming

9

10 of 27

Currency exchange

  • USD to BTC: 0.00001
  • BTC to EUR: 70,240
  • INR to USD: 0.0127
  • EUR to INR: 97.01
  • EUR to HKD: 9.85
  • HKD to INR: 11.31
  • USD to HKD: 6.96

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

11 of 27

Currency exchange

  • USD to BTC: 0.00001
  • BTC to EUR: 70,240
  • INR to USD: 0.0127
  • EUR to INR: 97.01
  • EUR to HKD: 9.85
  • HKD to INR: 11.31
  • USD to HKD: 6.96

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

12 of 27

Currency exchange

  • USD to BTC: 0.00001
  • BTC to EUR: 70,240
  • INR to USD: 0.0127
  • EUR to INR: 97.01
  • EUR to HKD: 9.85
  • HKD to INR: 11.31
  • USD to HKD: 6.96

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)

13 of 27

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

14 of 27

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

15 of 27

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

16 of 27

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 ?

17 of 27

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 ?

  • Consider the highlighted path from USD to USD: it converts 1 USD to USD
  • Constitutes a negative cycle in the graph
  • In the currency exchange problem, negative cycles represent arbitrage
  • Since there is a negative cycle, 1 USD can be converted into >1 USD. (A shortest path does not exist)

Negative cycle: Sum of weights is negative

18 of 27

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

19 of 27

Negative weights shortest path

  • Dijkstra’s property: Once a vertex comes off the priority queue, its distance never gets updated again.
  • In this example,
    • Dijkstra’s would start by setting distance of as and of as when considering ’s neighbours.
    • would then be the next to come off the queue (since ), and this would be the final distance assigned to . However, there’s a shorter path that goes through .

19

20 of 27

The Bellman-Ford algorithm

  • Dijkstra’s is a greedy algorithm and suffices to calculate shortest paths when all weights are non-negative: distances never need to be recalculated once set.
  • Bellman-Ford is a dynamic programming algorithm for computing shortest path in directed graphs with negative weights:
    • Will run slower than Dijkstra’s: time versus time
    • Will involve “resetting” distances as the algorithm goes along
    • Bellman-Ford will detect negative cycles since shortest paths are “undefined” if there are negative cycles.

20

21 of 27

A failed attempt

  • If a graph has negative weights, let
  • What if we adjusted every edge weight to . Then, can we just run standard Dijkstra’s on the adjusted graph?
  • No. This does not simply “translate” every path weight by !

​

21

22 of 27

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 .

23 of 27

Bellman-Ford DP algorithm

What function should our DP algorithm consider?

23

24 of 27

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 .

25 of 27

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?

26 of 27

Bellman-Ford DP algorithm

The function: For , let be the length of the shortest path consisting of at most edges

26

Recursive structure/definition:

    • Case 1: The shortest path uses edges. Then��
    • Case 2: The shortest path uses exactly edges. Let be the first vertex on the path. Then��

27 of 27

Bellman-Ford DP algorithm

The function: For , let be the length of the shortest path consisting of at most edges

27

Recursive structure/definition: