0% found this document useful (0 votes)
6 views64 pages

Seminar 6 - Dynamic Programming

The document outlines a lecture on Dynamic Programming as part of the FIT2004 course at Monash University, covering key concepts such as the paradigm's core idea, overlapping subproblems, and optimal substructure. It includes examples like the Coins Change Problem and the Unbounded Knapsack Problem, illustrating how to apply dynamic programming techniques to solve these problems efficiently. The lecture emphasizes the importance of practice in mastering dynamic programming strategies.

Uploaded by

ttri0026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views64 pages

Seminar 6 - Dynamic Programming

The document outlines a lecture on Dynamic Programming as part of the FIT2004 course at Monash University, covering key concepts such as the paradigm's core idea, overlapping subproblems, and optimal substructure. It includes examples like the Coins Change Problem and the Unbounded Knapsack Problem, illustrating how to apply dynamic programming techniques to solve these problems efficiently. The lecture emphasizes the importance of practice in mastering dynamic programming strategies.

Uploaded by

ttri0026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

6/04/2025

Faculty of Information Technology,


Monash University

FIT2004: Algorithms and Data


Structures

Week 6: Dynamic Programming

1
6/04/2025

Overview

Divide and Greedy Dynamic Data


Network flow
conquer algorithms programming structures
(W 8-9)
(W 1-3) (W 4-5) (W 6-7) (W 10-11)

 Last Lecture: greedy algorithms


 Dijkstra’s Algorithm
 Prim’s Algorithm
 Kruskal’s Algorithm

 Today’s Lecture
 Introduction to Dynamic Programming
 Coins Change
 Unbounded Knapsack
 0/1 Knapsack
 Edit Distance
 Constructing Optimal Solution

FIT2004: Lecture 3 - Quick Sort and Select

Outline

1. Introduction to Dynamic Programming


2. Coins Change
3. Unbounded Knapsack
4. 0/1 Knapsack
5. Edit Distance
6. Constructing Optimal Solution

FIT2004: Lecture 6 - Dynamic Programming

2
6/04/2025

Dynamic Programming Paradigm

 A powerful optimization technique in computer science.

 Applicable to a wide-variety of problems that exhibit certain


properties.

 Practice is the key to be good at dynamic programming.

FIT2004: Lecture 6 - Dynamic Programming

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

 We also need an optimal substructure: optimal solutions to subproblems help


us find optimal solutions to larger problems.

 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

N-th Fibonacci Number


F(N) Time Complexity
if N == 0 or N == 1 T(1) = b // b and c are constants
return N T(N) = T(N-1) + T(N-2) + c
else = O(2N)
return F(N - 1) + F(N - 2)

F(6)
Recursion tree for N = 6
F(5) F(4)

F(4) F(3) F(3) F(2)

F(1) F(2) F(1) F(1) F(0)


F(3) F(2) F(2)

FIT2004: Lecture 6 - Dynamic Programming

N-th Fibonacci Number


F(N) Time Complexity
if N == 0 or N == 1 T(1) = b // b and c are constants
return N T(N) = T(N-1) + T(N-2) + c
else = O(2N)
return F(N - 1) + F(N - 2)
Can we memoize?
F(6)
Recursion tree for N = 6
F(5) F(4)

F(4) F(3) F(3) F(2)

F(1) F(2) F(1) F(1) F(0)


F(3) F(2) F(2)

FIT2004: Lecture 6 - Dynamic Programming

4
6/04/2025

Fibonacci with Memoization: Version 1


memo[0] = 0 // 0th Fibonacci number
Time Complexity
memo[1] = 1 // 1st Fibonacci number
for i=2 to i=N: calls fibDP() roughly 2*N times
memo[i] = -1 So the complexity is O(N)
fibDP(N)
if memo[N] != -1
return memo[N]
else F(6)
memo[N] = fibDP(N-1) + fibDP(N-2);
return memo[N]
F(5) F(4)
Recursion tree for N = 6

F(4) F(3)

Version 1 is called Top-down because it


F(3) F(2) starts from the top – attempting the
largest problem first.

F(2) F(1)
FIT2004: Lecture 6 - Dynamic Programming

Fibonacci with Memoization: Version 2


memo[0] = 0 // 0th Fibonacci number
memo[1] = 1 // 1st Fibonacci number Time Complexity
for i=2 to i=N:
memo[i] = memo[i-1] + memo[i-2]
O(N)

memo 0 1 1 2 3 5 8 13 21 34

Version 2 is called Bottom-up because it starts from


the bottom – solving the smallest problem first.

FIT2004: Lecture 6 - Dynamic Programming

5
6/04/2025

Dynamic Programming Strategy


1. Assume you already know the solutions of all sub-problems and
have memoized these solutions (overlapping subproblems).
 E.g., Assume you know Fib(i) for every i < n.

2. Observe how you can solve the original problem using


memoized solutions (optimal substructure) .
 E.g., Fib(n) = Fib(n-1) + Fib(n-2).

3. Solve the original problem by building upon solutions to the


sub-problems.
 E.g., Fib(0), Fib(1), Fib(2), …, Fib(n).

FIT2004: Lecture 6 - Dynamic Programming

Outline

1. Introduction to Dynamic Programming


2. Coins Change
3. Unbounded Knapsack
4. 0/1 Knapsack
5. Edit Distance
6. Constructing Optimal Solution

FIT2004: Lecture 6 - Dynamic Programming

6
6/04/2025

Coins Change Problem


Problem: A country uses N coins with denominations {a1, a2, …, aN}.
Given a value V, find the minimum number of coins that add up to V.

 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).

 Similarly, what is the minimum number of coins to make 13?


 3 (i.e., two 6 coins and one 1 coin)- optimal vs 5- greedy.

FIT2004: Lecture 6 - Dynamic Programming

Coins Change Problem


 Problem: A country uses N coins with denominations {a1, a2, …, aN}. Given a
value V, find the minimum number of coins that add up to V.

 Overlapping subproblems: What shall we store in the memo array?

 We want to know the minimum number of coins which add up to V.


 So lets try

MinCoins[v] = {The fewest coins required to make exactly $v}

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

FIT2004: Lecture 6 - Dynamic Programming

7
6/04/2025

Coins Change Problem


 Problem: A country uses N coins with denominations {a1, a2, …, aN}. Given a
value V, find the minimum number of coins that add up to V.

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

 Assume we have optimal solutions for all v < V (stored in MinCoins[0..V-1]).


How could we determine MinCoins[V]?

FIT2004: Lecture 6 - Dynamic Programming

Coins Change Problem - Example


Coins: [9, 6, 5, 1]
V: 12
MinCoins: 0 1 2 3 4 1 1 2 3 1 2 2
0 1 2 3 4 5 6 7 8 9 10 11 12

 What options do we have to try and make V = $12?


 We have to use any available coin! Lets try using the 9…
 After choosing coin 9, what would be the optimal thing to do?
 Look at MinCoins[12-9] since now we need to make the other $3,
and we already know the best way to do that.
 Repeat this idea for the other coins and see which is best.

FIT2004: Lecture 6 - Dynamic Programming

8
6/04/2025

Coins Change Problem - Example


Coins: [9, 6, 5, 1]
V: 12
MinCoins: 0 1 2 3 4 1 1 2 3 1 2 2
0 1 2 3 4 5 6 7 8 9 10 11 12
 MinCoins[12] =
1 + min(MinCoins[12-9], MinCoins[12-6], MinCoins[12-5], MinCoins[12-1])
= 1 + min(MinCoins[3], MinCoins[6], MinCoins[7], MinCoins[11])
= 1 + min(3, 1, 2, 2)
=2
 In general, MinCoins[v] = 1 + min(MinCoins[v-c] for all c in coins,
where c <= v)
 Note also that if the value is less than every coin, then it cannot
be made. This can be ignored if there is a $1 coin.
FIT2004: Lecture 6 - Dynamic Programming

Coins Change Problem - Example


Coins: [9, 5, 6, 1]
V: 12
MinCoins: 0 1 2 3 4 1 1 2 3 1 2 2 2
0 1 2 3 4 5 6 7 8 9 10 11 12

Overlapping subproblems:
MinCoins[v] = {The fewest coins required to make exactly $v}.

Optimal substructure:

FIT2004: Lecture 6 - Dynamic Programming

9
6/04/2025

Coins Change Problem – Implementation


(Bottom up)
With DP, you can generally A
implement straight from the B
C
recurrence to code.

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]

FIT2004: Lecture 6 - Dynamic Programming

Coins Change – Implementation


(top down)
//Assume that the coin_change function has appropriately initialised memo to an
//array of nulls, and called our auxiliary function
Coin_change_aux(c[1..n], v) This recursive call just
if v = 0, return 0 returns memo[v-c[i]]
if memo[v] = null instantly if we have already
calculated it (because of
min_coins = infinity this “if”)
for i in 1 to n
if c[i] <= v
min_coins = min(min_coins, 1 + coin_change_aux(c, v-c[i]))
memo[v] = min_coins
return memo[v]

FIT2004: Lecture 6 - Dynamic Programming

10
6/04/2025

Outline

1. Introduction to Dynamic Programming


2. Coins Change
3. Unbounded Knapsack
4. 0/1 Knapsack
5. Edit Distance
6. Constructing Optimal Solution

FIT2004: Lecture 6 - Dynamic Programming

Unbounded Knapsack Problem


Problem: Given a capacity C and a set of items with their weights and values, you
need to pick items such that their total weight is at most C and their total value is
maximized. What is the maximum value you can take? In unbounded knapsack,
you can pick an item as many times as you want.
Item A B C D

Weight 9kg 5kg 6kg 1kg


Value $550 $350 $180 $40

Example: What is the maximum value for the above example given the capacity is
12 kg?

Answer: $780 (take two Bs and two Ds).


Greedy solution does not always work.
This is one of the most popular algorithmic problems globally!

FIT2004: Lecture 6 - Dynamic Programming

11
6/04/2025

DP Solution for Unbounded Knapsack


Problem: Given a capacity C and a set of items with their weights and values, you need
to pick items such that their total weight is at most C and their total value is
maximized. What is the maximum value you can take? In unbounded knapsack, you
can pick an item as many times as you want.

 We want the LARGEST value under a given weight.

 Overlapping subproblems: What shall we store in the memo array?


 Memo[i] = LARGEST value with capacity at most i.

 Optimal substructure: If we know optimal solutions to all subproblems, how can we


build an optimal solution to a larger problem?
 Similar logic to coin change:
 We need to choose an item…
 For each possible item choice, find out how much value we could get (using
subproblems) and then take the best one.

FIT2004: Lecture 6 - Dynamic Programming

DP Solution for Unbounded Knapsack


 What is the maximum value given capacity is 12 kg?

 If we take item 1, then we have 3kg left.

 The best we can do with 3kg is memo[3] = $120.

 So one option for memo[12] would be value[1] + memo[12-weight[item_1]].

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

DP Solution for Unbounded Knapsack


Memo[12] could be:
 value[1] + memo[12-weight[item_1]] = 550 + 120 = 670
 value[2] + memo[12-weight[item_2]] = 350 + 430 = 780
 value[3] + memo[12-weight[item_3]] = 390 + 180 = 570
 value[4] + memo[12-weight[item_4]] = 740 + 40 = 780

 Choose the best!

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

DP Solution for Unbounded Knapsack


Memo[12] could be:
 value[1] + memo[12-weight[1]] = 550 + 120 = 670
 value[2] + memo[12-weight[2]] = 350 + 430 = 780
 value[3] + memo[12-weight[3]] = 390 + 180 = 570
 value[4] + memo[12-weight[4]] = 740 + 40 = 780

 Choose the best!

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

DP Solution for Unbounded Knapsack


Lets write our recurrence:
 What is our base case?
 With no capacity, we cannot take any items. Also note, as before, that if an item is
heavier than the capacity we have left, we cannot take it.

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

 Complete the recurrence:

FIT2004: Lecture 6 - Dynamic Programming

DP Solution for Unbounded Knapsack


Overlapping subproblems: memo[i] = most value with capacity at most i.

Optimal substructure:

FIT2004: Lecture 6 - Dynamic Programming

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

E.g., Fill Memo[13]


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 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] ]

FIT2004: Lecture 6 - Dynamic Programming

15
6/04/2025

Top Down vs Bottom Up

 Top-down may save some


computations (E.g., some smaller
subproblems may not need to be
solved).
 Space saving trick may be
applied for bottom-up to reduce
space complexity.
 You may find one easier to think
about.
 In some cases, the solution
cannot be written bottom-up
without some silly contortions.
FIT2004: Lecture 6 - Dynamic Programming

Outline

1. Introduction to Dynamic Programming


2. Coins Change
3. Unbounded Knapsack
4. 0/1 Knapsack
5. Edit Distance
6. Constructing Optimal Solution

FIT2004: Lecture 6 - Dynamic Programming

16
6/04/2025

0/1 Knapsack Problem


Same as unbounded knapsack except that each item can only be picked at most
once.

Example: What is the maximum value for the example given below given capacity
is 11kg?

Answer: $590 (B and D) for capacity of 10kg.


Greedy solution may not always work.

(Note that A & C together can produce 11kg, but value is only $580)

Item A B C D

Weight 6kg 1kg 5kg 9kg


Value $230 $40 $350 $550

FIT2004: Lecture 6 - Dynamic Programming

0/1 Knapsack Problem


Same as unbounded knapsack except that each item can only be picked at most
once.

Difference from unbounded: If we pick an item X, giving us a remaining capacity R,


we have to somehow make sure that X is not part of the optimal solution to our
new subproblem of size R.

Idea: Lets have two axes on which we think about subproblems:


 Capacity
 Which items are part of the subproblem

FIT2004: Lecture 6 - Dynamic Programming

17
6/04/2025

0/1 Knapsack Problem


Problem: What is the solution for 0/1 knapsack for items {A,B,C,D} where capacity = 11kg.
Assume that we have computed solutions for every capacity<=11 considering the items {A,B,C} (see
table below).
What is the solution for capacity=11 and set {A,B,C,D} ?
 Case 1: the knapsack must NOT contain D
 Solution for 0/1 knapsack with set {A,B,C} and capacity 11 = 580

 Case 2: the knapsack must contain D


 The value of item D + solution for 0/1 knapsack with set {A,B,C} and capacity 11-9=2

 This gives a value of 550+40 = 590


Item A B C D
 Solution = max(Case1, Case2)
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550

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

0/1 Knapsack Problem


Assume we know the optimal solutions for every subproblem and results are stored in Memo[ ][ ]
Memo[ i ][ c ] contains the solution of knapsack for Set[1 … i] and capacity c.

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

0/1 Knapsack Problem


Assume we know the optimal solutions for every subproblem and results are stored in Memo[ ][ ]
Memo[ i ][ c ] contains the solution of knapsack for Set[1 … i] and capacity c.

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

0/1 Knapsack Problem


Assume we know the optimal solutions for every subproblem and results are stored in Memo[ ][ ]
Memo[ i ][ c ] contains the solution of knapsack for Set[1 … i] and capacity c.

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

0/1 Knapsack Problem


Assume we know the optimal solutions for every subproblem and results are stored in Memo[ ][ ]
Memo[ i ][ c ] contains the solution of knapsack for Set[1 … i] and capacity c.

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

0/1 Knapsack Problem


Assume we know the optimal solutions for every subproblem and results are stored in Memo[ ][ ]
Memo[ i ][ c ] contains the solution of knapsack for Set[1 … i] and capacity c.

Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550

max(580, 550 + 40)

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

0/1 Knapsack Problem


Assume we know the optimal solutions for every subproblem and results are stored in Memo[ ][ ]
Memo[ i ][ c ] contains the solution of knapsack for Set[1 … i] and capacity c.

Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550

max(580, 550 + 40)

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

0/1 Knapsack Problem


Assume we know the optimal solutions for every subproblem and results are stored in Memo[ ][ ]
Memo[ i ][ c ] contains the solution of knapsack for Set[1 … i] and capacity c.

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

0/1 Knapsack Problem


Assume we know the optimal solutions for every subproblem and results are stored in Memo[ ][ ]
Memo[ i ][ c ] contains the solution of knapsack for Set[1 … i] and capacity c.

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

0/1 Knapsack Problem


Assume we know the optimal solutions for every subproblem and results are stored in Memo[ ][ ]
Memo[ i ][ c ] contains the solution of knapsack for Set[1 … i] and capacity c.

Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550

max(620, 550 + 40)

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

0/1 Knapsack Problem


Assume we know the optimal solutions for every subproblem and results are stored in Memo[ ][ ]
Memo[ i ][ c ] contains the solution of knapsack for Set[1 … i] and capacity c.

Item A B C D
Weight 6kg 1kg 5kg 9kg
Value $230 $40 $350 $550

max(620, 550 + 40)

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

0/1 Knapsack Problem


Complexity:
 We need to fill this two-dimensional grid.

 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

0/1 Knapsack Problem


Complexity:
 We need to fill this two-dimensional grid.

 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

0/1 Knapsack Problem


Complexity:
 Knapsack complexity is not polynomial, it is actually exponential
 The knapsack algorithm runs in O(CN), where
 N is the # of items (array)
 C is the max capacity (single value)

 What is the definition of time complexity?


 The amount of time taken by an algorithm to run, as a function of the length of the input (in bits) (not the
value of the input).

 Think about how many bits it takes to specify input.


 Instead of C, lets talk about B, the number of bits to specify C.
 C is the capacity. C can be described with log 2 𝐶 + 1 bits.
 For 𝐶 = 4kg, # of bits = 3 (C = 100 in binary)
 For 𝐶 = 8kg, # of bits = 4 (C = 1000 in binary)
 For 𝐶 = 16kg, # of bits = 5 (C = 10000 in binary)

 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).

FIT2004: Lecture 6 - Dynamic Programming

24
6/04/2025

Reducing Space Complexity


 While generating each row, we only need to look at values from the
previous row.
 So all values from the earlier rows may be discarded.
 Reduces space complexity to O(C) (or O(2B) as we saw).

 Note: Space saving not possible for top-down dynamic programming


(since we don’t know the order we solve subproblems).
 Note: This simple space saving trick cannot be used when we want to
reconstruct the solution. We need a more advanced idea for that case!

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

FIT2004: Lecture 6 - Dynamic Programming

Outline

1. Introduction to Dynamic Programming


2. Coins Change
3. Unbounded Knapsack
4. 0/1 Knapsack
5. Edit Distance
6. Constructing Optimal Solution

FIT2004: Lecture 6 - Dynamic Programming

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.

FIT2004: Lecture 6 - Dynamic Programming

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 ?

FIT2004: Lecture 6 - Dynamic Programming

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

Some Applications of Edit Distance


 Natural Language Processing
 Auto-correction
 Query suggestions
 Bioinformatics
 DNA/Protein sequence alignment

FIT2004: Lecture 6 - Dynamic Programming

27
6/04/2025

Computing Edit Distance


We want to convert s1 to s2 containing n and m letters, respectively.
To gain an intuition for this problem, lets look at some situations we might run into.
This is a good technique in general, try playing around with the problem and see
what happens.
n

How much does it cost to turn s1 into


s1: s2 if the last characters are the same?
? ? ? . . . ? ? ? x
We can leave the last character, and
s2: ? ? ? . . . ? x just convert the front part of one
string into the front part of the other
m
edit(s1[1..n], s2[1..m])
=edit(s1[1..n-1], s2[1..m-1])

FIT2004: Lecture 6 - Dynamic Programming

Computing Edit Distance


We want to convert s1 to s2 containing n and m letters, respectively.
To gain an intuition for this problem, lets look at some situations we might run into.
This is a good technique in general, try playing around with the problem and see
what happens.
n

How much does it cost to turn s1 into


s1: s2 if the last characters are different?
? ? ? . . . ? ? ? x
We have some options.
s2: ? ? ? . . . ? ?

FIT2004: Lecture 6 - Dynamic Programming

28
6/04/2025

Computing Edit Distance


 Remember! We can assume that we have solved ALL subproblems already. In
other words, we know:
 Edit(s1[1..i], s2[1..j]) for all i≤n, j≤m BUT NOT when i = n AND j = m (since this is the exact problem we are
trying to solve).
 Alternatively, we could think about it visually.
 In this table, cell[i][j] is the cost of turning s1[1..i] into s2[1..j]

1 2 3 . . . m Known:
Unknown:
1
2
3
.
.
.
n

FIT2004: Lecture 6 - Dynamic Programming

Computing Edit Distance


We know Edit(s1[1..i], s2[1..j]) for all i≤n, From the bottom right corner, there are
j≤m BUT NOT i = n AND j = m. three things we can do:

• Go up
Equivalently, we know all the blue cells. • Go left
• Go left and up
1 2 3 . . . m
1
2
3
.
.
.
n

FIT2004: Lecture 6 - Dynamic Programming

29
6/04/2025

Computing Edit Distance


We know Edit(s1[1..i], s2[1..j]) for all i≤n, From the bottom right corner, there are
j≤m BUT NOT i = n AND j = m. three things we can do:

• 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]).

FIT2004: Lecture 6 - Dynamic Programming

Computing Edit Distance


We know Edit(s1[1..i], s2[1..j]) for all i≤n, From the bottom right corner, there are
j≤m BUT NOT i = n AND j = m. three things we can do:

• 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).

FIT2004: Lecture 6 - Dynamic Programming

30
6/04/2025

Computing Edit Distance


We know Edit(s1[1..i], s2[1..j]) for all i≤n, From the bottom right corner, there are
j≤m BUT NOT i = n AND j = m. three things we can do:

• 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).

FIT2004: Lecture 6 - Dynamic Programming

Computing Edit Distance


Base cases?
 When one string is empty, the cost is just the length of the other string.
 If s1 is empty, we would have to insert each character in the other string,
starting from nothing.
 So edit(s1[], s2[1..j]) = j (# of insertions)
 edit(s1[1..i], s2[]) = i (# of deletions)

FIT2004: Lecture 6 - Dynamic Programming

31
6/04/2025

Computing Edit Distance


If the last characters are the same:
 edit(s1[1..n-1], s2[1..m-1])

If the last characters are different, three options:


 cost(delete) + edit(s1[1..n-1], s2[1..m]) → go up
 edit(s1[1..n], s2[1..m-1]) + cost(insert) → go left
 edit(s1[1..n-1], s2[1..m-1]) + cost(replace) → got left and up

We want the minimum cost, so if all costs are 1, our optimal substructure is

FIT2004: Lecture 6 - Dynamic Programming

Computing Edit Distance


Overlapping subproblems: Dist[i,j] = cost of operations to turn s1[1...i] into
s2[1…j].

Optimal substructure:

FIT2004: Lecture 6 - Dynamic Programming

32
6/04/2025

Example

i rows and j columns


Φ S H I N E
Φ
S
I
N
G
S

FIT2004: Lecture 6 - Dynamic Programming

Example

i rows and j columns


Φ S H I N E
Φ 0 1 2 3 4 5
S
I
N
G
S

FIT2004: Lecture 6 - Dynamic Programming

33
6/04/2025

Example

i rows and j columns


Φ S H I N E
Φ 0 1 2 3 4 5
S 1
I 2
N 3
G 4
S 5

FIT2004: Lecture 6 - Dynamic Programming

Example

i rows and j columns


Φ S H I N E
Φ 0 1 2 3 4 5
S 1
I 2
N 3
G 4
S 5

FIT2004: Lecture 6 - Dynamic Programming

34
6/04/2025

Example

i rows and j columns

Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0
I 2
N 3
G 4
S 5

FIT2004: Lecture 6 - Dynamic Programming

Example

i rows and j columns


Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0
I 2
N 3
G 4
S 5

FIT2004: Lecture 6 - Dynamic Programming

35
6/04/2025

Example

i rows and j columns


Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1
I 2
N 3
G 4
S 5

FIT2004: Lecture 6 - Dynamic Programming

Example

i rows and j columns


Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1
I 2
N 3
G 4
S 5

FIT2004: Lecture 6 - Dynamic Programming

36
6/04/2025

Example

i rows and j columns


Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2
I 2
N 3
G 4
S 5

FIT2004: Lecture 6 - Dynamic Programming

Example

i rows and j columns


Φ 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

FIT2004: Lecture 6 - Dynamic Programming

37
6/04/2025

Example

i rows and j columns


Φ 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

FIT2004: Lecture 6 - Dynamic Programming

Example

i rows and j columns


Φ 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

FIT2004: Lecture 6 - Dynamic Programming

38
6/04/2025

Example

i rows and j columns


Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1
N 3
G 4
S 5

FIT2004: Lecture 6 - Dynamic Programming

Example

i rows and j columns


Φ S H I N E
Φ 0 1 2 3 4 5
S 1 0 1 2 3 4
I 2 1 1
N 3
G 4 Now you Try!
S 5

FIT2004: Lecture 6 - Dynamic Programming

39
6/04/2025

Example

i rows and j columns


Φ 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

FIT2004: Lecture 6 - Dynamic Programming

Outline

1. Introduction to Dynamic Programming


2. Coins Change
3. Unbounded Knapsack
4. 0/1 Knapsack
5. Edit Distance
6. Constructing Optimal Solution

FIT2004: Lecture 6 - Dynamic Programming

40
6/04/2025

Constructing optimal solutions


 The algorithms we have seen determine optimal values:
 Minimum number of coins
 Maximum value of knapsack
 Edit distance
 How do we construct optimal solution that gives the optimal value:
 The coins to give the change
 The items to put in knapsack
 Converting one string to the other
 There may be multiple optimal solutions and our goal is to return just
one solution!
 Two strategies can be used.
1. Create an additional array recording decision at each step
2. Backtracking

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 6 - Dynamic Programming

62
6/04/2025

Backtracking Vs Decision array?


 Space usage:
 Backtracking requires less space as it does not require creating an additional array.

 However, space complexity is the same.

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

FIT2004: Lecture 6 - Dynamic Programming

Review yourselves: Subset Sum Problem

The Subset Sum Problem is a classic problem in computer science and


combinatorics. Given a set of integers and a target sum, determine
whether there is a subset of the given set whose sum is equal to the
target.
It means, given a set of numbers N = {a1, a2, …, an} and a value V, is their a
subset of N such that the sum of elements is exactly V?
Note: Unlike Coins Change problem, a number can only be used once to
make the value V.
Example: Suppose N = {1, 5, 6, 9} and the value V is 13. The answer is
FALSE because no subset of N adds to 13. For V=15, the answer is TRUE
because 9 + 6 = 15.
It is an NP-complete problem, meaning no known polynomial-time
algorithm exists for all inputs. It is a special case of the Knapsack Problem.
It appears in many real-world problems such as resource allocation,
cryptography, partitioning tasks, etc.
FIT2004: Lec-4: Dynamic Programming

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.

Things to do (this list is not exhaustive)


 Practice, practice, practice:
 http://www.geeksforgeeks.org/tag/dynamic-programming/
 https://www.topcoder.com/community/data-science/data-science-tutorials/dynamic-programming-
from-novice-to-advanced/

Coming Up Next
 Dynamic Programming Graph Algorithms.

FIT2004: Lecture 6 - Dynamic Programming

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

FIT2004: Lecture 8 - Network Flow

64

You might also like