Seminar 6 - Dynamic Programming
Seminar 6 - Dynamic Programming
1
6/04/2025
Overview
Today’s Lecture
Introduction to Dynamic Programming
Coins Change
Unbounded Knapsack
0/1 Knapsack
Edit Distance
Constructing Optimal Solution
Outline
2
6/04/2025
Core Idea
Divide a complicated problem by breaking it down into simpler subproblems
in a recursive manner and solve these.
Question: But how does this differ from `Divide and Conquer' approach?
Overlapping subproblems: the same subproblem needs to (potentially) be used multiple
times (in contrast to independent subproblems in Divide and Conquer).
So how do we do this?
Identify the overlapping subproblems
Solve the smaller subproblems and memoize their solutions
use those memoized solutions to gradually build solution for the original problem
**Memoize means storing the result so that they can be used next time instead
of calculating the same thing again and again.
FIT2004: Lecture 6 - Dynamic Programming
3
6/04/2025
F(6)
Recursion tree for N = 6
F(5) F(4)
4
6/04/2025
F(4) F(3)
F(2) F(1)
FIT2004: Lecture 6 - Dynamic Programming
memo 0 1 1 2 3 5 8 13 21 34
5
6/04/2025
Outline
6
6/04/2025
Example: Suppose the coins are {1, 5, 10, 50} and the value V is
110. The minimum number of coins required to make 110 is 3
(two 50 coins, and one 10 coin).
Greedy solution does not always work. E.g., If Coins = {1, 5, 6, 9}.
What would be the minimum number of coins to make 12?
It would be 4 ( i.e. coins of 9, 1, 1, 1)- a greedy solution.
However, the optimal is 2 (i.e., two 6 coins).
Note: your first guess to include in the memo array may not be right, so try
the most obvious thing and then play around if you can’t make it work.
7
6/04/2025
Overlapping subproblems:
MinCoins[v] = {The fewest coins required to make exactly $v}
Optimal substructure: To find the optimal substructure, we first deal with the
base case(s). In this case, to make $0 requires 0 coins, so MinCoins[0] = 0.
8
6/04/2025
Overlapping subproblems:
MinCoins[v] = {The fewest coins required to make exactly $v}.
Optimal substructure:
9
6/04/2025
Coin_change(c[1..n], V)
Mincoins[0..V] = ∞ (note we start from 0 index here)
Mincoins[0] = 0 (from recurrence)
for each i from 1 to V A
options = [ ]
for k from 1 to n
if i < c[k] (from recurrence) B
do nothing
else
C
options.append(…) = [ MinCoins[i-c[k]], provided i >= c[k] ] (from recurrence)
Mincoins[i] = min(options)+1
return Mincoins[V]
10
6/04/2025
Outline
Example: What is the maximum value for the above example given the capacity is
12 kg?
11
6/04/2025
Item 1 2 3 4
Weight 9kg 5kg 6kg 1kg
Value $550 $350 $180 $40
Memo 40 80 120 160 350 390 430 470 550 700 740
1 2 3 4 5 6 7 8 9 10 11 12
FIT2004: Lecture 6 - Dynamic Programming
12
6/04/2025
Item 1 2 3 4
Weight 9kg 5kg 6kg 1kg
Value $550 $350 $180 $40
Memo 40 80 120 160 350 390 430 470 550 700 740
1 2 3 4 5 6 7 8 9 10 11 12
FIT2004: Lecture 6 - Dynamic Programming
Item 1 2 3 4
Weight 9kg 5kg 6kg 1kg
Value $550 $350 $180 $40
Memo 40 80 120 160 350 390 430 470 550 700 740 780
1 2 3 4 5 6 7 8 9 10 11 12
FIT2004: Lecture 6 - Dynamic Programming
13
6/04/2025
Otherwise, we want the maximum over all values (1 ≤ i ≤ n, vi) of items that
we could take (wi ≤ c).
But also taking into account the optimal value we could fit into the rest of
our knapsack, once we took that item.
Optimal substructure:
14
6/04/2025
Bottom-up Solution
// Construct Memo[ ] starting from 1 until C in a way similar to previous slide.
Initialize Memo[ ] to contain 0 for all indices
for c = 1 to C Time Complexity:
maxValue = 0
O(NC)
for i=1 to N
if Weight[ i ] <= c Space Complexity:
thisValue = Value[i] + Memo[c - Weight[ i ] ] O(C + N)
if thisValue > maxValue
maxValue = thisValue
Memo[c] = maxValue
Memo 40 80 120 160 350 390 430 470 550 700 740 780
1 2 3 4 5 6 7 8 9 10 11 12 13
FIT2004: Lecture 6 - Dynamic Programming
Top-down Solution
Initialize Memo[ ] to contain -1 for all indices // -1 indicates solution for this index has not
yet been computed
Memo[0] = 0
function knapsack(Capacity)
if Memo[Capacity] != -1:
return Memo[Capacity]
else:
maxValue = 0
for i=1 to N
if Weight[ i ] <= Capacity
thisValue = Value[i] + knapsack(Capacity - Weight[ i ] )
if thisValue > maxValue
maxValue = thisValue
Memo[Capacity] = maxValue
return Memo[Capacity]
Bottom up solution:
Values[i] + Memo[ Capacity – Weights[i] ]
15
6/04/2025
Outline
16
6/04/2025
Example: What is the maximum value for the example given below given capacity
is 11kg?
(Note that A & C together can produce 11kg, but value is only $580)
Item A B C D
17
6/04/2025
1 2 3 4 5 6 7 8 9 10 11
{A,B,C} 40 40 40 40 350 390 390 390 390 390 580
1 2 3 4 5 6 7 8 9 10 11
{A,B,C,D} 40 40 40 40 350 390 390 390 550 590 590
FIT2004: Lecture 6 - Dynamic Programming
Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550
max(40,0)
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B ?
3 C
i 4 D
FIT2004: Lecture 6 - Dynamic Programming
18
6/04/2025
Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590
FIT2004: Lecture 6 - Dynamic Programming
Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550
max(
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590
FIT2004: Lecture 6 - Dynamic Programming
19
6/04/2025
Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550
max(580
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590
FIT2004: Lecture 6 - Dynamic Programming
Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590
FIT2004: Lecture 6 - Dynamic Programming
20
6/04/2025
Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590 590
FIT2004: Lecture 6 - Dynamic Programming
Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550
max(
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590 590
FIT2004: Lecture 6 - Dynamic Programming
21
6/04/2025
Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550
max(620
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590 590
FIT2004: Lecture 6 - Dynamic Programming
Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590 590
FIT2004: Lecture 6 - Dynamic Programming
22
6/04/2025
Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590 590 620
FIT2004: Lecture 6 - Dynamic Programming
Filling each cell is O(1) since it is the max of 2 numbers, each of which can be
computed in a constant number of lookups.
Therefore, the time and space complexity are both O(NC) where N is the
number of items and C is the capacity of the knapsack.
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590 590 620
FIT2004: Lecture 6 - Dynamic Programming
23
6/04/2025
Filling each cell is O(1) since it is the max of 2 numbers, each of which can be
computer in a constant number of lookups.
Therefore, the time and space complexity are both O(NC) where N is the
number of items and C is the capacity of the knapsack.
1 2 3 4 5 6 7 8 9 10 11 12
0 Φ 0 0 0 0 0 0 0 0 0 0 0 0
1 A 0 0 0 0 0 230 230 230 230 230 230 230
2 B 40 40 40 40 40 230 270 270 270 270 270 270
3 C 40 40 40 40 350 390 390 390 390 390 580 620
i 4 D 40 40 40 40 350 390 390 390 550 590 590 620
FIT2004: Lecture 6 - Dynamic Programming
log2 𝐶 + 1 = 𝐵 ֜ 𝐶 = 2𝐵
Now we can say our algorithm runs in O(CN) = O(2𝐵 𝑁), which is not polynomial in the size of the input (as
expected for an NP-complete problem).
24
6/04/2025
1 2 3 4 5 6 7 8 9 10 11 12
3 C 40 40 40 40 350 390 390 390 390 390 580 620
4 D 40 40 40 40 350 390 390 390 550 590
Outline
25
6/04/2025
Edit Distance
The words computer and commuter are very similar, and a change of
just one letter, p → m, will change the first word into the second.
The word sport can be changed into sort by the deletion of p, or
equivalently, sort can be changed into sport by the insertion of p.
Notion of editing provides a simple and handy formalisation to
compare two strings.
The goal is to convert the first string (i.e., sequence) into the second
through a series of edit operations.
The permitted edit operations are:
1. insertion of a symbol into a sequence.
2. deletion of a symbol from a sequence.
3. substitution or replacement of one symbol with another in a sequence.
Edit Distance
Edit distance between two sequences
Edit distance is the minimum number of edit operations required to convert
one sequence into another (assuming that all operations have equal costs).
For example:
Edit distance between computer and commuter is 1.
Edit distance between sport and sort is 1.
Edit distance between shine and sings is ?
Edit distance between dnasgivethis and dentsgnawstrims is ?
26
6/04/2025
Examples
shine → sings dnasgivethis → dentsgnawstrims
s = s (no change) d = d (no change)
h → i (substitute 'h' → 'i’) Insert ‘e’
i = n (substitute 'i' → 'n’) n = n (no change)
n → g (substitute 'n' → 'g’) a → t (substitute ‘a' → ‘t’)
e → s (substitute 'e' → 's’) s = s (no change)
4 edits (substitutes) in total g = g (no change)
Insert ‘n’
i = a (substitute 'i' → ‘a’)
shine → sings
v → w (substitute ‘v' → ‘w’)
s = s (no change)
e → s (substitute 'e' → 's’)
Delete ‘h’
t = t (no change)
i = i (no change)
h → r (substitute 'h' → ‘r’)
n = n (no change)
i = i (no change)
e → g (substitute 'e' → ‘g’)
Insert ‘m’
Insert ‘s’
s = s (no change)
3 edits (1 delete, 1 substitute, 1
3 inserts + 5 substitutes = 4 edits in total
insert) in total
FIT2004: Lecture 6 - Dynamic Programming
27
6/04/2025
28
6/04/2025
1 2 3 . . . m Known:
Unknown:
1
2
3
.
.
.
n
• Go up
Equivalently, we know all the blue cells. • Go left
• Go left and up
1 2 3 . . . m
1
2
3
.
.
.
n
29
6/04/2025
• Go up
Equivalently, we know all the blue cells. • Go left
• Go left and up
1 2 3 . . . m
1 Going up:
2
Subproblem: edit(s1[1..n-1], s2[1..m]).
3
• First delete s1[n].
.
• Then turn s1[1..n-1] into s2[1..m].
.
.
Total cost:
n
cost(delete) + edit(s1[1..n-1], s2[1..m]).
• Go up
Equivalently, we know all the blue cells. • Go left
• Go left and up
1 2 3 . . . m
1 Going left:
2
Subproblem: edit(s1[1..n], s2[1..m-1]).
3
• First turn s1[1..n] into s2[1..m-1].
.
• Then insert s2[m] at the end of s1.
.
.
n
Total cost:
edit(s1[1..n], s2[1..m-1]) + cost(insert).
30
6/04/2025
• Go up
Equivalently, we know all the blue cells. • Go left
• Go left and up
1 2 3 . . . m
1 Going left:
2
Subproblem: edit(s1[1..n-1], s2[1..m-1]).
3
• Replace s1[n] with s2[m].
.
• Turn s1[1..n-1] into s2[1..m-1].
.
.
n
Total cost:
edit(s1[1..n-1], s2[1..m-1]) + cost(replace).
31
6/04/2025
We want the minimum cost, so if all costs are 1, our optimal substructure is
Optimal substructure:
32
6/04/2025
Example
Example
33
6/04/2025
Example
Example
34
6/04/2025
Example
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0
I 2
N 3
G 4
S 5
Example
35
6/04/2025
Example
Example
36
6/04/2025
Example
Example
37
6/04/2025
Example
Example
38
6/04/2025
Example
Example
39
6/04/2025
Example
Outline
40
6/04/2025
Decision Array
Make a second array of the same size.
Each time you fill in a cell of the memo array, record your decision in the
decision array.
Remember, going right (or coming from the left) is insert.
Going down (or coming from up) is delete.
Going down and right (or coming from up and left) is replace OR do nothing.
Φ S H I N E
Φ 0
S
I
N
G
S
41
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S
S
I
N
G
S
Φ S H I N E
Φ 0 1
S
I
N
G
S
Decision Array
Φ S H I N E
Φ null Insert S Insert H
S
I
N
G
S
Φ S H I N E
Φ 0 1 2
S
I
N
G
S
42
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I
S
I
N
G
S
Φ S H I N E
Φ 0 1 2 3
S
I
N
G
S
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N
S
I
N
G
S
Φ S H I N E
Φ 0 1 2 3 4
S
I
N
G
S
43
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S
I
N
G
S
Φ S H I N E
Φ 0 1 2 3 4 5
S
I
N
G
S
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S
I Delete I
N Delete N
G Delete G
S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1
I 2
N 3
G 4
S 5
44
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing
I Delete I
N Delete N
G Delete G
S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0
I 2
N 3
G 4
S 5
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H
I Delete I
N Delete N
G Delete G
S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1
I 2
N 3
G 4
S 5
45
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I
I Delete I
N Delete N
G Delete G
S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2
I 2
N 3
G 4
S 5
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N
I Delete I
N Delete N
G Delete G
S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3
I 2
N 3
G 4
S 5
46
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I
N Delete N
G Delete G
S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2
N 3
G 4
S 5
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I Delete I
N Delete N
G Delete G
S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1
N 3
G 4
S 5
47
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I Delete I Replace I,H Do nothing Insert N Insert E
N Delete N Delete N Replace N,H Delete N Do nothing Insert E
G Delete G Delete G Delete G Delete G Delete G Replace G, E
S Delete S Delete S Delete S Delete S Delete S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I Delete I Replace I,H Do nothing Insert N Insert E
N Delete N Delete N Replace N,H Delete N Do nothing Insert E
G Delete G Delete G Delete G Delete G Delete G Replace G, E
S Delete S Delete S Delete S Delete S Delete S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
48
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I Delete I Replace I,H Do nothing Insert N Insert E
N Delete N Delete N Replace N,H Delete N Do nothing Insert E
G Delete G Delete G Delete G Delete G Delete G Replace G, E
S Delete S Delete S Delete S Delete S Delete S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I Delete I Replace I,H Do nothing Insert N Insert E
N Delete N Delete N Replace N,H Delete N Do nothing Insert E
G Delete G Delete G Delete G Delete G Delete G Replace G, E
S Delete S Delete S Delete S Delete S Delete S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
49
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I Delete I Replace I,H Do nothing Insert N Insert E
N Delete N Delete N Replace N,H Delete N Do nothing Insert E
G Delete G Delete G Delete G Delete G Delete G Replace G, E
S Delete S Delete S Delete S Delete S Delete S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I Delete I Replace I,H Do nothing Insert N Insert E
N Delete N Delete N Replace N,H Delete N Do nothing Insert E
G Delete G Delete G Delete G Delete G Delete G Replace G, E
S Delete S Delete S Delete S Delete S Delete S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
50
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I Delete I Replace I,H Do nothing Insert N Insert E
N Delete N Delete N Replace N,H Delete N Do nothing Insert E
G Delete G Delete G Delete G Delete G Delete G Replace G, E
S Delete S Delete S Delete S Delete S Delete S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I Delete I Replace I,H Do nothing Insert N Insert E
N Delete N Delete N Replace N,H Delete N Do nothing Insert E
G Delete G Delete G Delete G Delete G Delete G Replace G, E
S Delete S Delete S Delete S Delete S Delete S Delete S
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
51
6/04/2025
Decision Array
Φ S H I N E
Φ null Insert S Insert H Insert I Insert N Insert E
S Delete S Do nothing Insert H Insert I Insert N Insert E
I Delete I Delete I Replace I,H Do nothing Insert N Insert E
N Delete N Delete N Replace N,H Delete N Do nothing Insert E
G Delete G Delete G Delete G Delete G Delete G Replace G, E
S Delete S Delete S Delete S Delete S Delete S Delete S
Sequence of operations:
Delete S (from position 5)
Replace G with E (at position 4)
Insert H (at position 2)
• SINGS
• SING
• SINE
• SHINE
Backtracking
• Start in bottom right.
• Are the letters the same?
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
52
6/04/2025
Backtracking
• Start in bottom right.
• Are the letters the same?
• No
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Backtracking
• Start in bottom right.
• Are the letters the same?
• No
• From recurrence…
• We know that our current value (3) was obtained from any of the three previous cells
by adding 1.
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
53
6/04/2025
Backtracking
• Start in bottom right.
• Are the letters the same?
• No
• From recurrence…
• We know that our current value (3) was obtained from any of the three previous cells
by adding 1.
• So our options are up or up-and-left.
• Choose one arbitrarily.
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Backtracking
• Continue from our new cell (but remember the path).
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
54
6/04/2025
Backtracking
• Continue from our new cell (but remember the path).
• Letters are different.
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Backtracking
• Continue from our new cell (but remember the path).
• Letters are different.
• Must have come from the cell above.
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
55
6/04/2025
Backtracking
• Letters are the same.
• From our recurrence, we know:
• If our value is the same as the up-and-left cell, then we could have came from
there.
• If our value is one more than the up cell or the left cell, then we could have come
from there.
• In this case, we came from up-and-left.
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Backtracking
• Letter are the same.
• From our recurrence, we know:
• If our value is the same as the up-and-left cell, then we could have came from
there.
• If our value is one more than the up cell or the left cell, then we could have come
from there.
• In this case, we came from up-and-left.
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
56
6/04/2025
Backtracking
• Continue in this way.
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Backtracking
• Continue in this way.
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
57
6/04/2025
Backtracking
• Continue in this way.
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Backtracking
• Continue in this way.
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
58
6/04/2025
Backtracking
• Continue in this way.
• When you reach the top left cell, you are done.
• So the sequence of operations is:
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Backtracking
• Continue in this way.
• When you reach the top left cell, you are done.
• So the sequence of operations is:
• Replace(5, E)
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
59
6/04/2025
Backtracking
• Continue in this way.
• When you reach the top left cell, you are done.
• So the sequence of operations is:
• Replace(5, E), delete[4]
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Backtracking
• Continue in this way.
• When you reach the top left cell, you are done.
• So the sequence of operations is:
• Replace(5, E), delete[4], nothing
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
60
6/04/2025
Backtracking
• Continue in this way.
• When you reach the top left cell, you are done.
• So the sequence of operations is:
• Replace(5, E), delete[4], nothing, nothing
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Backtracking
• Continue in this way.
• When you reach the top left cell, you are done.
• So the sequence of operations is:
• Replace(5, E), delete[4], nothing, nothing, insert(2, H)
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
61
6/04/2025
Backtracking
• Continue in this way.
• When you reach the top left cell, you are done.
• So the sequence of operations is:
• Replace(5, E), delete[4], nothing, nothing, insert(2, H), nothing
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
Backtracking
• Continue in this way.
• When you reach the top left cell, you are done.
• So the sequence of operations is:
• Replace(5, E), delete[4], nothing, nothing, insert(2, H), nothing
• SINGS
• SINGE (replace position 5 with E)
• SINE (delete G in position 4)
• SHINE (insert H at position 2)
Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1 1 2 3
N 3 2 2 2 1 2
G 4 3 3 3 2 2
S 5 4 4 4 3 3
62
6/04/2025
Efficiency:
Backtracking requires to determine what decision was made which costs additional
computation.
However, time complexity is the same.
Note the simple space saving trick discussed can only be used when the
solution does not need to be constructed.
63
6/04/2025
Concluding Remarks
Dynamic Programming Strategy
Assume you already know the optimal solutions for all subproblems and have
memoized these solutions.
Observe how you can solve the original problem using this memoization.
Iteratively solve the sub-problems and memoize.
Coming Up Next
Dynamic Programming Graph Algorithms.
Reading
Course Notes: Chapter 7
You can also check algorithms’ textbooks for contents related to this
lecture, e.g.:
CLRS: Chapter 15
KT: Chapter 6
Rou: Chapter 16
64