6 DP
6 DP
DYNAMIC PROGRAMMING
Term 2, 2023
Table of Contents 2
1. Introduction
2. Example Problems
3. Applications to graphs
4. Puzzle
What is dynamic programming? 3
any base cases, which are the trivial subproblems - those for
which the recurrence is not required.
Putting it all together 8
1. Introduction
2. Example Problems
3. Applications to graphs
4. Puzzle
Longest Increasing Subsequence 10
Problem
Instance: a sequence of n real numbers A[1..n].
We now look for all indices j < i such that A[j] < A[i].
Solution
Subproblems: for each 1 ≤ i ≤ n, let Q(i) be the problem of
determining opt(i), the maximum length of an increasing
subsequence of A[1..i] which ends with A[i].
i 1 2 3 4 5 6 7 8 9
A[i] 1 5 3 6 2 7 4 8 6
opt(i) 1 2 2 3 2 4 3 5 4
What if the problem asked for not only the length, but the
entire longest increasing subsequence?
i 1 2 3 4 5 6 7 8 9
A[i] 1 5 3 6 2 7 4 8 6
opt(i) 1 2 2 3 2 4 3 5 4
pred(i) 1 1 2 1 4 3 6 7
Problem
Instance: A list of n activities with starting times si and finishing
times fi . No two activities can take place simultaneously.
i −3 i −1
i −4 i −2 i
Solution
Subproblems: for each 1 ≤ i ≤ n, let P(i) be the problem of
determining t(i), the maximal duration of a non-overlapping
subsequence of the first i activities which ends with activity i.
In the i th slot of our table, we should store not only t(i) but
the value previous(i) = j such that the optimal solution of
P(i) extends the optimal solution of subproblem P(j).
Problem
Instance: You are given n types of coin denominations of values
v1 < v2 < . . . < vn (all integers). Assume v1 = 1 (so that you can
always make change for any integer amount) and that you have an
unlimited supply of coins of each denomination.
Attempt 1
Greedily take as many coins of value vm as possible, then vm−1 ,
and so on.
Exercise
Design a counterexample to the above algorithm.
Making Change 33
We will try to find the optimal solution for not only C , but
every amount up to C .
Solution
Subproblems: for each 0 ≤ i ≤ C , let P(i) be the problem of
determining opt(i), the fewest coins needed to make change for an
amount i.
Note
Our algorithm is NOT a polynomial time algorithm in the length of
the input, because C is represented by log C bits, while the
running time is O(nC ). There is no known polynomial time
algorithm for this problem!
Making Change 37
In the i th slot of the table, we would store both opt(i) and the
coin type k = pred(i) which minimises opt(i − vk ).
Notation
We denote the k that minimises opt(i − vk ) by
argmin opt(i − vk ).
1≤k≤n
Integer Knapsack Problem (with duplicates) 39
Problem
Instance: You have n types of items; all items of kind i are
identical and of weight wi and value vi . All weights are integers.
You can take any number of items of each kind. You also have a
knapsack of capacity C .
Assume we have solved the problem for all total weights j < i.
Solution
Subproblems: for each 0 ≤ i ≤ C , let P(i) be the problem of
determining opt(i), the maximum value that can be achieved using
up to i units of weight, and m(i), the type of some item in such a
collection.
Problem
Instance: You have n items, the ith of which has weight wi and
value vi . All weights are integers. You also have a knapsack of
capacity C .
Question
If we know the optimal solution for each total weight j < i, can we
deduce the optimal solution for weight i?
Answer
No! If we begin our solution for weight i with item k, we have
i − wk remaining weight to fill. However, we did not record
whether item k was itself already used in the optimal solution for
that weight.
Integer Knapsack Problem (without duplicates) 45
For each total weight i, we will find the optimal solution using
only the first k items.
Solution
Subproblems: for 0 ≤ i ≤ C and 0 ≤ k ≤ n, let P(i, k) be the
problem of determining opt(i, k), the maximum value that can be
achieved using up to i units of weight and using only the first k
items, and m(i, k), the (largest) index of an item in such a
collection.
0 i − wk i C
0
k −1
k
Problem
Instance: a set of n positive integers xi .
Solution
The best packing of this knapsack produces an optimally balanced
partition, with set S1 given by the items outside the knapsack and
set S2 given by the items in the knapsack.
Matrix chain multiplication 52
(AB)(C )
| {z }
10×5
10 × 100 × 5 10 × 5 × 50
(A)(B)(C ) (ABC )
100 × 5 × 50 10 × 100 × 50
(A) (BC )
| {z }
100×50
Problem
Instance: a compatible sequence of matrices A1 A2 ...An , where Ai
is of dimension si−1 × si .
(Ai+1 . . . Ak ) (Ak+1 . . . Aj ) .
| {z }| {z }
si ×sk sk ×sj
Solution
Subproblems: for all 0 ≤ i < j ≤ n, let P(i, j) be the problem of
determining opt(i, j), the fewest multiplications needed to compute
the product Ai+1 Ai+2 . . . Aj .
Problem
Instance: two sequences S = ha1 , a2 , . . . an i and
S ∗ = hb1 , b2 , . . . , bm i.
If ai and bj are the same symbol (say c), the longest common
subsequence of Si and Sj∗ is formed by appending c to the
∗ .
solution for Si−1 and Sj−1
Solution
Subproblems: for all 0 ≤ i ≤ n and all 0 ≤ j ≤ m let P(i, j) be
the problem of determining opt(i, j), the length of the longest
common subsequence of the truncated sequences
Si = ha1 , a2 , . . . ai i and Sj∗ = hb1 , b2 , . . . , bj i.
! !
!
Longest Common Subsequence 67
Question
Can we do LCS (LCS (S, S ∗ ) , S ∗∗ )?
Answer
Not necessarily!
Longest Common Subsequence 68
However,
Exercise
Confirm that LCS (LCS (S ∗ , S ∗∗ ) , S) and LCS (LCS (S, S ∗∗ ) , S ∗ )
also give wrong answers.
Longest Common Subsequence 69
Problem
Instance: three sequences S = ha1 , a2 , . . . an i,
S ∗ = hb1 , b2 , . . . , bm i and S ∗∗ = hc1 , c2 , . . . , cl i.
Solution
Subproblems: for all 0 ≤ i ≤ n, all 0 ≤ j ≤ m and all 0 ≤ k ≤ l,
let P(i, j, k) be the problem of determining opt(i, j, k), the length
of the longest common subsequence of the truncated sequences
Si = hai , a2 , . . . , ai i, Sj∗ = hb1 , b2 , . . . , bj i and
Sk∗∗ = hc1 , c2 , . . . , ck i.
Problem
Instance: two sequences s = ha1 , a2 , . . . an i and
s ∗ = hb1 , b2 , . . . , bm i.
Solution
Find a longest common subsequence LCS(s, s ∗ ) of s and s ∗ , then
add back the differing elements of the two sequences in the right
places, in any compatible order.
Example
If
s = abacada and s ∗ = xby cazd,
then
LCS(s, s ∗ ) = bcad
and therefore
SCS(s, s ∗ ) = axbyacazda.
Edit Distance 74
Problem
Instance: Given two text strings A of length n and B of length m,
you want to transform A into B. You are allowed to insert a
character, delete a character and to replace a character with
another one. An insertion costs cI , a deletion costs cD and a
replacement costs cR .
Note: if all operations have a unit cost, then you are looking
for the minimal number of such operations required to
transform A into B; this number is called the Levenshtein
distance between A and B.
Solution
Subproblems: for all 0 ≤ i ≤ n and 0 ≤ j ≤ m, let P(i, j) be the
problem of determining opt(i, j), the minimum cost of
transforming the sequence A[1..i] into the sequence B[1..j].
Recurrence: for i, j ≥ 1,
opt(i − 1, j) + cD
opt(i, j − 1) + c
I
opt(i, j) = min (
opt(i − 1, j − 1) if A[i] = B[j]
opt(i − 1, j − 1) + c
R 6 B[j].
if A[i] =
Problem
Instance: a sequence of numbers with operations +, −, × in
between, for example
1 + 2 − 3 × 6 − 1 − 2 × 3 − 5 × 7 + 2 − 8 × 9.
Task: place brackets in a way that the resulting expression has the
largest possible value.
Maximising an expression 80
Exercise
Write a complete solution for this problem. Your solution should
include the subproblem specification, recurrence and base cases.
You should also describe how the overall solution is to be obtained,
and analyse the time complexity of the algorithm.
Turtle Tower 83
Problem
Instance: You are given n turtles, and for each turtle you are
given its weight and its strength. The strength of a turtle is the
maximal weight you can put on it without cracking its shell.
Task: find the largest possible number of turtles which you can
stack one on top of the other, without cracking any turtle.
Hint
Surprisingly difficult! Order turtles in increasing order of the sum
of their weight and their strength, and proceed by recursion.
Problem
Instance: a positive integer n.
Hint
It’s not obvious how to construct a recurrence between the number
of partitions of different values of n. Instead consider restricted
partitions!
1. Introduction
2. Example Problems
3. Applications to graphs
4. Puzzle
Directed acyclic graphs 87
Definition
Recall that a directed acyclic graph (DAG) is exactly that: a
directed graph without (directed) cycles.
b c
d
Topological ordering 88
Definition
Recall that in a directed graph, a topological ordering of the
vertices is one in which all edges point “left to right”.
Problem
Instance: a directed acyclic graph G = (V , E ) in which each edge
e ∈ E has a corresponding weight w (e) (which may be negative),
and a designated vertex s ∈ V .
Notation
Let n = |V | and m = |E |.
Shortest path in a directed acyclic graph 90
If all edge weights are positive, the single source shortest path
problem is solved by Dijkstra’s algorithm in O(m log n).
Later in this lecture, we’ll see how to solve the general single
source shortest path problem in O(nm) using the
Bellman-Ford algorithm.
Solution
Subproblems: for all t ∈ V , let P(t) be the problem of
determining opt(t), the length of a shortest path from s to t.
opt(t) = min{opt(v ) + w (v , t) | (v , t) ∈ E }.
Question
In what order should we solve the subproblems?
Problem
Instance: You are given two assembly lines, each consisting of n
workstations. The k th workstation on each assembly line performs
the k th of n jobs.
Problem (continued)
start finish
s2 t2,k−1 t2,n−1 f2
t2,1
We’ll solve for the shortest path from s to each vertex (i, k).
Solution
Subproblems: for i ∈ {1, 2} and 1 ≤ k ≤ n, let P(i, k) be the
problem of determining opt(i, k), the minimal time taken to
complete the first k jobs, with the k th job performed on assembly
line i.
Remark
This problem is important because it has the same design logic as
the Viterbi algorithm, an extremely important algorithm for many
fields such as speech recognition, decoding convolutional codes in
telecommunications etc. This will be covered in COMP4121
Advanced Algorithms.
Single Source Shortest Paths 105
Problem
Instance: a directed weighted graph G = (V , E ) with edge
weights w (e) which can be negative, but without cycles of
negative total weight, and a designated vertex s ∈ V .
Task: find the weight of the shortest path from vertex s to every
other vertex t.
Notation
Let n = |V | and m = |E |.
Single Source Shortest Paths 106
Observation
For any vertex t, there is a shortest s − t path without cycles.
Proof Outline
Suppose the opposite. Let p be a shortest s − t path, so it must
contain a cycle. Since there are no negative weight cycles,
removing this cycle produces an s − t path of no greater length.
Observation
It follows that every shortest s − t path contains any vertex v at
most once, and therefore has at most n − 1 edges.
Bellman-Ford algorithm 108
. . → v} → t,
p = s| → .{z
p0
Solution
Subproblems: for all 0 ≤ i ≤ n − 1 and all t ∈ V , let P(i, t) be
the problem of determining opt(i, t), the length of a shortest path
from s to t which contains at most i edges.
opt(i, t) = min{opt(i − 1, v ) + w (v , t) | (v , t) ∈ E }.
5
a b
t
−2 s a b c d
6
i
s 0 0 ∞ ∞ ∞ ∞
8 7
−3 −4 1 0 6 ∞ 7 ∞
7
2 0 6 4 7 2
9
c d 3 0 2 4 7 2
2 4 0 2 4 7 −2
opt(i, t) = min{opt(i − 1, v ) + w (v , t) | (v , t) ∈ E }.
Bellman-Ford algorithm 113
pred(i, t) = argmin{opt(i − 1, v ) + w (v , t) | (v , t) ∈ E }.
v ∈V
Bellman-Ford algorithm 114
Problem
Instance: a directed weighted graph G = (V , E ) with edge
weights w (e) which can be negative, but without cycles of
negative total weight.
Task: find the weight of the shortest path from every vertex s to
every other vertex t.
Notation
Let n = |V | and m = |E |.
Floyd-Warshall algorithm 116
Question
When is the shortest path from s to t using the first k vertices as
intermediates actually an improvement on the value from the
previous round?
Answer
When there is a shorter path of the form
s→ ...
|{z} → vk → ...
|{z} → t.
v1 ,...,vk−1 v1 ,...,vk−1
Floyd-Warshall algorithm 118
Solution
Subproblems: for all 1 ≤ i, j ≤ n and 0 ≤ k ≤ n, let P(i, j, k) be
the problem of determining opt(i, j, k), the weight of a shortest
path from vi to vj using only v1 , . . . , vk as intermediate vertices.
Base cases:
0
if i = j
opt(i, j, 0) = w (i, j) if (vi , vj ) ∈ E .
∞ otherwise.
Floyd-Warshall algorithm 119
1. Introduction
2. Example Problems
3. Applications to graphs
4. Puzzle
Puzzle 121
How can you use these two fuses to time a 45 minute interval?
That’s All, Folks!!