Lecture 14
Dynamic programming II
CSE 421 Autumn 2026
1
Some midterm logistics
2
Monday November 9, at 5.30pm in JHN 102 or 075 (depending on the last digit of your UW ID), or scheduled with DRS.
Previously…
3
The Dynamic Programming (DP) paradigm
4
Dynamic programming is the natural evolution of Divide and Conquer
They share a common core:
Where they differ:
The Dynamic Programming (DP) paradigm
5
Dynamic programming is the natural evolution of Divide and Conquer
They share a common core:
Where they differ:
Today
6
Edit distance
7
and may not have the same length
A useful recursive definition of edit distance
8
Note that we can define/compute “edit distance” recursively!
Definitions:
A useful recursive definition of edit distance
Recursive definition :
9
The edit distance of the original problem is
Memoization
10
Note that the value only depends on:
DP algorithm: fill table column by column, left to right, bottom to top. �Output .
)asad
-
--
-
Finding the set of edits
11
Finding the set of edits
12
Recall: if , then --
Being in one case tells us that the associated transformation is the optimal way to solve the problem!
If , then there are three ways in which relates to the previous subproblems
For example, being in case 3 means that the optimal way to get from to�is to insert at the end, and do the optimal edits to get from to
Finding the set of edits
13
-
--
-
Finding the set of edits
14
-
--
-
. . .
Finding the set of edits
15
means optimal last move is�“delete ”
means optimal last move is�“insert ”
means optimal last move is
-
--
-
Finding the set of edits
16
The path from the top right corner to the bottom left tells us all the optimal edits.
means optimal last move is�“delete ”
means optimal last move is�“insert ”
means optimal last move is
Keeping track of all the edges just introduces an factor overhead in time and space.
Takeaways
17
The Knapsack problem
18
Maximize the value of the items grabbed subject to the weight constraint of the bag
(We encountered a similar problem about �beans in HW 2: what’s the difference here?)
The Knapsack problem
Input: Items with positive integer weights and values ,�and a max weight for the bag.
Output: Subset such that and maximizes .
19
Let =
The Knapsack problem
Input: Items with positive integer weights and values ,�and a max weight for the bag.
Output: Subset such that and maximizes .
20
Brute-force algorithm: check all possible subsets , and choose the optimal one amongst those satisfying the weight constraint.
Runtime:
Let =
The Knapsack problem
Input: Items with positive integer weights and values ,�and a max weight for the bag.
Output: Subset such that and maximizes .
21
Brute-force algorithm: check all possible subsets , and choose the optimal one amongst those satisfying the weight constraint.
Runtime:
complexity of adding numbers and �(for each we have to do such additions to compute the value, and check the constraint)
Let =
A dynamic programming algorithm
22
For a DP algorithm, we need to find a recursive structure.
Can we find a recursive definition for the optimal value?
Concretely, we need to specify a function(s) (what does it take as input and what does it output?), that allows us to define the optimal value in terms of “optimal values” for smaller problems.
The functions:
A dynamic programming algorithm
23
The functions:
Base cases:
How can we express in terms of Val() at smaller inputs?
A dynamic programming algorithm
24
Observation: item is either in the optimal subset or not.
(best we can do with subject to weight constraint )
(best we can do with subject to weight constraint )
So,
Remaining available weight
Remaining available weight
A dynamic programming algorithm
25
So,
Correspondingly, depending on the maximization above, either or .
A dynamic programming algorithm
26
So,
By the way, why not consider a different function where , �which outputs the best value attainable with weight using elements from ?
The latter would later give a memo table later of size instead of !
Memoization for Knapsack
27
Table of
Memoization for Knapsack
28
Table of
Memoization for Knapsack
29
Path from top right corner to the bottom row tells you which items to include.
Table of
Knapsack DP algorithm
Filling the memo table:
30
Returning output: Return as the max value. Walk from in to find which items to include.
Knapsack DP algorithm
Runtime
31
Knapsack DP algorithm
32
Runtime