Lecture 13
Dynamic programming
CSE 421 Autumn 2025
1
Previously…
2
Divide and conquer
3
Today
4
Dynamic Programming
5
The Dynamic Programming (DP) paradigm
6
Dynamic programming is the natural evolution of Divide and Conquer
They share a common core:
Where they differ:
The Dynamic Programming (DP) paradigm
7
Dynamic programming is the natural evolution of Divide and Conquer
They share a common core:
Where they differ:
A first example: Tribonacci numbers
Input: Integer
Output: Tribonacci number defined recursively: and�.
8
There is a somewhat natural Divide and Conquer algorithm:
DCTribonacci():
A first example: Tribonacci numbers
Input: Integer
Output: Tribonacci number defined recursively: and�.
9
There is a somewhat natural Divide and Conquer algorithm:
DCTribonacci():
Runtime: ?
A first example: Tribonacci numbers
Easy to believe that runtime is exponential in by just looking at the number of nodes in the picture (each node is one call made by the algorithm)
A first example: Tribonacci numbers
To find the runtime formally, we need to solve:� and
Dropping the term (which makes the runtime even faster), we can “guess” the form of the solution to be for some . This is a solution iff:
(or )
There is a positive root:
So the runtime is approximately
Can we do better?
A first example: Tribonacci numbers
The key to a much faster runtime is to realize that the subproblems are greatly overlapping (they share the same subsubproblems, etc.). In the previous algorithm, we were solving the same problems over and over again.
Add “memoization” (and we get DP!)
13
Memo-Tribonacci(, Memo = {}):
It’s basically the same algorithm as before!
Except we’ve added “memo” table. We populate it as we go along, and we call previously stored results from it (instead of computing them again)
DCTribonacci():
Memo-Tribonacci runtime analysis
What is the largest could be?
14
Memo-Tribonacci runtime analysis
Theorem: .
Proof: By induction. Base cases are For induction��.
Corollary: Each can be expressed using bits.
15
Memo-Tribonacci runtime analysis
16
Memo-Tribonacci runtime analysis
17
Memo-Tribonacci runtime analysis
18
Memo-Tribonacci runtime analysis
19
Memo-Tribonacci runtime analysis
20
Memo-Tribonacci runtime analysis
21
Edit distance
22
How many edits are needed here?
and may not have the same length
Edit distance
23
How many edits are needed here?
and may not have the same length
Edit distance
24
How many edits are needed here?
and may not have the same length
A useful recursive definition of edit distance
25
Note that we can define/compute “edit distance” recursively!
Definitions:
Can we define in terms for or ?
A useful recursive definition of edit distance
26
If , what is ?
A useful recursive definition of edit distance
27
A useful recursive definition of edit distance
28
What if ?
A useful recursive definition of edit distance
29
A useful recursive definition of edit distance
30
Note: it doesn’t matter when gets substituted with . We still need to transform into .
A useful recursive definition of edit distance
31
A useful recursive definition of edit distance
32
A useful recursive definition of edit distance
33
A useful recursive definition of edit distance
Recursive definition :
34
The edit distance of the original problem is
A useful recursive definition of edit distance
Recursive algorithm :
35
The edit distance of the original problem is
This recursive algorithm is asking to solve the same problems many times over. ��It’s a perfect candidate for Dynamic Programming!
Memoization
36
Recall that the key feature of dynamic programming, other than recursion, is that we keep a “memo” table with results of subproblems we have solved
Memoization
37
Recall that the key feature of dynamic programming, other than recursion, is that we keep a “memo” table with results of subproblems we have solved
Note that the value only depends on:
DP algorithm overview: fill table column by column, left to right, bottom to top. �Output .
)asad
-
--
-
Edit distance DP algorithm
38
DP-EditDistance:
time
iterations
time per loop
Total time: