Homework 1 due
Mini-HW 5 released
Due on 10/25 (Thu) 14:20
Homework 2 released
Due on 11/06 (Tue) 18:00 (3.5 weeks)
A4 hardcopy submitted to a box @R307
Softcopy submitted to NTU COOL before the deadline
Frequently check the website for the updated information!
Dynamic Programming
DP #1: Rod Cutting
DP #2: Stamp Problem
DP #3: Knapsack Problem
0/1 Knapsack
Unbounded Knapsack
Multidimensional Knapsack
Fractional Knapsack
DP #4: Matrix-Chain Multiplication
DP #5: Sequence Alignment Problem
Longest Common Subsequence (LCS) / Edit Distance
有100個死囚,隔天執行死刑,典獄長開恩給他們一個存活的機會。
當隔天執行死刑時,每人頭上戴一頂帽子(黑或白)排成一隊伍,在
死刑執行前,由隊伍中最後的囚犯開始,每個人可以猜測自己頭上 的帽子顏色(只允許說黑或白),猜對則免除死刑,猜錯則執行死刑。
若這些囚犯可以前一天晚上先聚集討論方案,是否有好的方法可以
使總共存活的囚犯數量期望值最高?
囚犯排成一排,每個人可以看到前面所有人的帽子,但看不到自己 及後面囚犯的。
由最後一個囚犯開始猜測,依序往前。
每個囚犯皆可聽到之前所有囚犯的猜測內容。
……
Example: 奇數者猜測內 容為前面一位的帽子顏 色 存活期望值為75人 有沒有更多人可以存活的好策略?
Textbook Chapter 15.2 – Matrix-chain multiplication
8
Input: a sequence of n matrices 𝐴
1, … , 𝐴
𝑛
Output: the product of 𝐴
1𝐴
2… 𝐴
𝑛𝐴1 𝐴2 𝐴3 𝐴4 …… 𝐴𝑛
𝐴1.cols=𝐴2.rows
𝐴1and 𝐴2are compatible.
Each entry takes 𝑞 multiplications
A B C
Overall time is
= =
Overall time is
= =
Input: a sequence of integers 𝑙
0, 𝑙
1, … , 𝑙
𝑛 𝑙𝑖−1 is the number of rows of matrix 𝐴𝑖
𝑙𝑖 is the number of columns of matrix 𝐴𝑖
Output: a order of performing 𝑛 − 1 matrix multiplications in the minimum number of operations to obtain the product of 𝐴
1𝐴
2… 𝐴
𝑛𝐴1 𝐴2 𝐴3 𝐴4 …… 𝐴𝑛
𝐴1.cols=𝐴2.rows
𝐴1and 𝐴2are compatible.
Do not need to compute the result but find the fast way to get the result!
𝑃
𝑛: how many ways for 𝑛 matrices to be multiplied
The solution of 𝑃
𝑛is Catalan numbers, Ω
4𝑛𝑛
3 2
, or is also Ω 2
𝑛 Exercise 15.2-3
Subproblems
M(i, j): the min #operations for obtaining the product of 𝐴𝑖 … 𝐴𝑗
Goal: M(1, n)
Optimal substructure: suppose we know the OPT to M(i, j), there are k cases:
Case k: there is a cut right after Ak in OPT Matrix-Chain Multiplication Problem
Input: a sequence of integers 𝑙0, 𝑙1, … , 𝑙𝑛indicating the dimensionality of 𝐴𝑖
Output: a order of matrix multiplications with the minimum number of operations
左右所花的運算量是M(i, k)及M(k+1, j)的最佳解
𝐴𝑖𝐴𝑖+1… 𝐴𝑘 𝐴𝑘+1𝐴𝑘+2 … 𝐴𝑗 𝑖 ≤ 𝑘 < 𝑗
Suppose we know the optimal solution to M(i, j), there are k cases:
Case k: there is a cut right after Ak in OPT
Recursively define the value
左右所花的運算量是M(i, k)及M(k+1, j)的最佳解
Matrix-Chain Multiplication Problem
Input: a sequence of integers 𝑙0, 𝑙1, … , 𝑙𝑛indicating the dimensionality of 𝐴𝑖
Output: a order of matrix multiplications with the minimum number of operations
𝐴𝑘+1..𝑗 𝐴𝑖.rows
=𝑙𝑖−1
𝐴𝑘.cols=𝑙𝑘
𝐴𝑘+1.rows=𝑙𝑘
𝐴𝑗.cols=𝑙𝑗 𝐴𝑖𝐴𝑖+1… 𝐴𝑘 𝐴𝑘+1𝐴𝑘+2… 𝐴𝑗 =
Bottom-up method: solve smaller subproblems first
How many subproblems to solve
#combination of the values 𝑖 and 𝑗 s.t. 1 ≤ 𝑖 ≤ 𝑗 ≤ 𝑛
Matrix-Chain Multiplication Problem
Input: a sequence of integers 𝑙0, 𝑙1, … , 𝑙𝑛indicating the dimensionality of 𝐴𝑖
Output: a order of matrix multiplications with the minimum number of operations
Matrix-Chain(n, l)
initialize two tables M[1..n][1..n] and B[1..n-1][2..n]
for i = 1 to n
M[i][i] = 0 // boundary case
for p = 2 to n // p is the chain length
for i = 1 to n – p + 1 // all i, j combinations j = i + p – 1
M[i][j] = ∞
for k = i to j – 1 // find the best k
q = M[i][k] + M[k + 1][j] + l[i - 1] * l[k] * l[j]
if q < M[i][j]
M[i][j] = q return M
How to decide the order of the matrix
multiplication?
1 2 3 4 5 6 … n
1 0
2 0
3 0
4 0
5 0
6 0
: 0
n 0
Matrix-Chain(n, l)
initialize two tables M[1..n][1..n] and B[1..n-1][2..n]
for i = 1 to n
M[i][i] = 0 // boundary case
for p = 2 to n // p is the chain length
for i = 1 to n – p + 1 // all i, j combinations j = i + p – 1
M[i][j] = ∞
for k = i to j – 1 // find the best k
q = M[i][k] + M[k + 1][j] + l[i - 1] * l[k] * l[j]
if q < M[i][j]
M[i][j] = q
B[i][j] = k // backtracking return M and B
Print-Optimal-Parens(B, i, j) if i == j
print 𝐴𝑖
Matrix 𝑨𝟏 𝑨𝟐 𝑨𝟑 𝑨𝟒 𝑨𝟓 𝑨𝟔 Dimension 30 x 35 35 x 15 15 x 5 5 x 10 10 x 20 20 x 25
1 2 3 4 5 6
1 0
2 0
3 0
4 0
5 0
6 0
15,750
2,625 750
1,000
5,000 7,875
4,375
2,500
3,500 9,375
7,125
53,75 11,875
10,500 15,125
1 2 3 4 5 6
1 2 3 4 5 6
1
2
3
4
5 1
3
3
5 3
3
3 3
3 3
Textbook Chapter 15.4 – Longest common subsequence Textbook Problem 15-5 – Edit distance
22
猴子們各自講話,經過語音辨識系統後,哪一支猴子發出最接近英 文字”banana”的語音為優勝者
How to evaluate the similarity between two sequences?
aeniqadikjaz
svkbrlvpnzanczyqza
banana
Input: two sequences
Output: longest common subsequence of two sequences
The maximum-length sequence of characters that appear left-to-right (but not necessarily a continuous string) in both sequences
X = banana
Y = svkbrlvpnzanczyqza X → ---ba---n-an---a Y → svkbrlvpnzanczyqza X = banana
Y = aeniqadikjaz X → ba-n--an---a- Y → -aeniqadikjaz
4 5
Input: two sequences
Output: the minimum cost of transformation from X to Y
Quantifier of the dissimilarity of two strings
X = banana
Y = svkbrlvpnzanczyqza X → ---ba---n-an---a Y → svkbrlvpnzanczyqza X = banana
Y = aeniqadikjaz X → ba-n--an---a- Y → -aeniqadikjaz
1 deletion, 7 insertions, 1 substitution 12 insertions, 1 substitution
9 13
Input: two sequences
Output: the minimal cost 𝑀
𝑚,𝑛for aligning two sequences
Cost = #insertions × 𝐶INS + #deletions × 𝐶DEL + #substitutions × 𝐶𝑝,𝑞
Subproblems
SA(i, j): sequence alignment between prefix strings 𝑥1, … , 𝑥𝑖 and 𝑦1, … , 𝑦𝑗
Goal: SA(m, n)
Optimal substructure: suppose OPT is an optimal solution to SA(i, j), there are 3 cases:
Case 1: 𝑥𝑖 and 𝑦𝑗 are aligned in OPT (match or substitution)
OPT/{𝑥𝑖, , 𝑦𝑗} is an optimal solution of SA(i-1, j-1)
Case 2: 𝑥𝑖 is aligned with a gap in OPT (deletion)
OPT is an optimal solution of SA(i-1, j)
Case 3: 𝑦𝑗 is aligned with a gap in OPT (insertion)
OPT is an optimal solution of SA(i, j-1)
Sequence Alignment Problem Input: two sequences
Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences
Suppose OPT is an optimal solution to SA(i, j), there are 3 cases:
Case 1: 𝑥𝑖 and 𝑦𝑗 are aligned in OPT (match or substitution)
OPT/{𝑥𝑖, , 𝑦𝑗} is an optimal solution of SA(i-1, j-1)
Case 2: 𝑥𝑖 is aligned with a gap in OPT (deletion)
OPT is an optimal solution of SA(i-1, j)
Case 3: 𝑦𝑗 is aligned with a gap in OPT (insertion)
OPT is an optimal solution of SA(i, j-1)
Sequence Alignment Problem Input: two sequences
Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences
Bottom-up method: solve smaller subproblems first
X\Y 0 1 2 3 4 5 … n
0 1 : m
Sequence Alignment Problem Input: two sequences
Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences
Bottom-up method: solve smaller subproblems first
X\Y 0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 4 8 12 16 20 24 28 32 36 40 44 48
1 4 7 11 15 19 23 27 31 35 39 43 47 51
2 8 4 8 12 16 20 23 27 31 35 39 43 47
3 12 8 12 8 12 16 20 24 28 32 36 40 44
Sequence Alignment Problem Input: two sequences
Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences
a e n i q a d i k j a z
b a n
Seq-Align(X, Y, CDEL, CINS, Cp,q) for j = 0 to n
M[0][j] = j * CINS // |X|=0, cost=|Y|*penalty for i = 1 to m
M[i][0] = i * CDEL // |Y|=0, cost=|X|*penalty for i = 1 to m
for j = 1 to n
M[i][j] = min(M[i-1][j-1]+Cxi,yi, M[i-1][j]+CDEL, M[i][j-1]+CINS)
Bottom-up method: solve smaller subproblems first Sequence Alignment Problem
Input: two sequences
Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences
Bottom-up method: solve smaller subproblems first Sequence Alignment Problem
Input: two sequences
Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences
X\Y 0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 4 8 12 16 20 24 28 32 36 40 44 48
1 4 7 11 15 19 23 27 31 35 39 43 47 51
2 8 4 8 12 16 20 23 27 31 35 39 43 47
3 12 8 12 8 12 16 20 24 28 32 36 40 44
a e n i q a d i k j a z
b a n
Bottom-up method: solve smaller subproblems first Sequence Alignment Problem
Input: two sequences
Output: the minimal cost 𝑀𝑚,𝑛 for aligning two sequences
Find-Solution(M) if m = 0 or n = 0
return {}
v = min(M[m-1][n-1] + Cxm,yn, M[m-1][n] + CDEL, M[m][n-1] + CINS) if v = M[m-1][n] + CDEL // ↑: deletion
return Find-Solution(m-1, n)
if v = M[m][n-1] + CINS // ←: insertion return Find-Solution(m, n-1)
Find-Solution(M) if m = 0 or n = 0
return {}
v = min(M[m-1][n-1] + Cxm,yn, M[m-1][n] + CDEL, M[m][n-1] + CINS) if v = M[m-1][n] + CDEL // ↑: deletion
Seq-Align(X, Y, CDEL, CINS, Cp,q) for j = 0 to n
M[0][j] = j * CINS // |X|=0, cost=|Y|*penalty for i = 1 to m
M[i][0] = i * CDEL // |Y|=0, cost=|X|*penalty for i = 1 to m
for j = 1 to n
M[i][j] = min(M[i-1][j-1]+Cxi,yi, M[i-1][j]+CDEL, M[i][j-1]+CINS) return M[m][n]
Space complexity
If only keeping the most recent two rows: Space-Seq-Align(X, Y)
X\Y 0 1 2 3 … j … n
i - 1 i
X\Y 0 1 2 3 4 5 … n
0 1 : m
Problem: find the min-cost alignment find the shortest path
Divide-and-Conquer +
Dynamic Programming
a
e p p l
p e
a
X\Y 0 1 2 3
0 0 4 8 12
1 4 7 11 15
2 8 4 8 12
3 12 8 12 8
4 16 12 15 12 5 20 16 19 15 a p e
p p l e a
START
END
𝐹 2,3 = distance of the shortest path
Each edge has a length/cost
𝐹 𝑖, 𝑗 : length of the shortest path from 0,0 to 𝑖, 𝑗 (START 𝑖, 𝑗 )
𝐵 𝑖, 𝑗 : length of the shortest path from 𝑖, 𝑗 to 𝑚, 𝑛 ( 𝑖, 𝑗 END)
𝐹 𝑚, 𝑛 = 𝐵 0,0
i = 0
4 1 2 3
j = 0 1 2 3 4 5 6 7
5
𝐵 2,3 = distance of the shortest path
Each edge has a length/cost
𝐹 𝑖, 𝑗 : length of the shortest path from 0,0 to 𝑖, 𝑗 (START 𝑖, 𝑗 )
𝐵 𝑖, 𝑗 : length of the shortest path from 𝑖, 𝑗 to 𝑚, 𝑛 ( 𝑖, 𝑗 END)
Forward formulation
Backward formulation
i = 0
4 1 2 3
j = 0 1 2 3 4 5 6 7
5
Observation 1: the length of the shortest path from 0,0 to 𝑚, 𝑛 that passes through 𝑖, 𝑗 is 𝐹 𝑖, 𝑗 + 𝐵 𝑖, 𝑗
39
𝐹 𝑖, 𝑗 : length of the shortest path from 0,0 to 𝑖, 𝑗 𝐵 𝑖, 𝑗 : length of the shortest path from 𝑖, 𝑗 to 𝑚, 𝑛
i = 0
4 1 2 3
j = 0 1 2 3 4 5 6 7
𝐹 𝑖, 𝑗
𝐵 𝑖, 𝑗
optimal substructure
Observation 2: for any 𝑣 in {0, … , 𝑛}, there exists a 𝑢 s.t. the shortest path between (0,0) and 𝑚, 𝑛 goes through (𝑢, 𝑣)
𝐹 𝑖, 𝑗 : length of the shortest path from 0,0 to 𝑖, 𝑗 𝐵 𝑖, 𝑗 : length of the shortest path from 𝑖, 𝑗 to 𝑚, 𝑛
i = 0 1 2 3
j = 0 1 2 3 4 5 6 7
the shortest path must go across a vertical cut
Observation 1+2:
𝐹 𝑖, 𝑗 : length of the shortest path from 0,0 to 𝑖, 𝑗 𝐵 𝑖, 𝑗 : length of the shortest path from 𝑖, 𝑗 to 𝑚, 𝑛
i = 0
4 1 2 3
j = 0 1 2 3 4 5 6 7
i = 0
4 1 2 3
j = 0 1 2 3 4 5 6 7
Goal: finds optimal solution
How to find the value of 𝑢∗?
Idea: utilize sequence alignment algo.
Call Space-Seq-Align(X,Y[1:v]) to find 𝐹 0, 𝑣 , 𝐹 1, 𝑣 , … , 𝐹 𝑚, 𝑣
Call Back-Space-Seq-Align(X,Y[v+1:n]) to find 𝐵 0, 𝑣 , 𝐵 1, 𝑣 , … , 𝐵 𝑚, 𝑣
Let 𝑢 be the index minimizing 𝐹 𝑢, 𝑣 + 𝐵 𝑢, 𝑣
Goal: finds optimal solution – DC-Align(X, Y)
1. Divide
2. Conquer
3. Combine
Divide the sequence of size n into 2 subsequences
Find 𝑢 to minimize 𝐹 𝑢, 𝑣 + 𝐵 𝑢, 𝑣
Recursive case (𝑛 > 1)
prefix
= DC-Align(X[1:u], Y[1:v])
suffix
= DC-Align(X[u+1:m], Y[v+1:n])
Base case (𝑛 = 1)
Return Seq-Align(X, Y)
Return prefix + suffix
𝑇 𝑚, 𝑛 = time for running DC-Align(X, Y) with 𝑋 = 𝑚, 𝑌 = 𝑛
Space Complexity:
Theorem
Proof
There exists positive constants 𝑎, 𝑏 s.t. all
Use induction to prove
Inductive
Practice to check the initial condition
Given a graph 𝐺 = 𝑉, 𝐸 , each edge 𝑢, 𝑣 ∈ 𝐸 has an associated non- negative probability 𝑝 𝑢, 𝑣 of traversing the edge 𝑢, 𝑣 and producing the corresponding character. Find the most probable path with the
label 𝑠 = 𝜎
1, 𝜎
2, … , 𝜎
𝑛.
ㄨ ㄅ ㄒ ㄎ ㄕ
START
我 烏 為 問
END
爸 不
想 續 小
考 看 卡
書
試 上
白 鄉
Find the path from START to END with
highest prob
𝜎
1𝜎
2… … 𝜎
𝑛START END
produce 𝜎1
produce 𝜎𝑗
V: vocabulary size
47
Input: 𝑛 job requests with start times 𝑠
𝑖, finish times 𝑓
𝑖
Output: the maximum number of compatible jobs
The interval scheduling problem can be solved using an “early-finish-time- first” greedy algorithm in 𝑂(𝑛) time
“Greedy Algorithm”Next topic!
1 2 3 4
job index
Input: 𝑛 job requests with start times 𝑠
𝑖, finish times 𝑓
𝑖, and values 𝑣
𝑖
Output: the maximum total value obtainable from compatible jobs
time 49
1
3
3 4
3 1 1
2 3 4 5 6
job index
Assume that the requests are sorted in non-decreasing order (𝑓𝑖 ≤ 𝑓𝑗 when 𝑖 < 𝑗) 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible
e.g. 𝑝 1 = 0, 𝑝 2 = 0, 𝑝 3 = 1, 𝑝 4 = 1, 𝑝 5 = 4, 𝑝 6 = 3
Subproblems
WIS(i): weighted interval scheduling for the first 𝑖 jobs
Goal: WIS(n)
Optimal substructure: suppose OPT is an optimal solution to WIS(i), there are 2 cases:
Case 1: job 𝑖 in OPT
OPT\{𝑖} is an optimal solution of WIS(p(i))
Weighted Interval Scheduling Problem
Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible
1 3
3 4 1
2 3 4
job index
4
Optimal substructure: suppose OPT is an optimal solution to WIS(i), there are 2 cases:
Case 1: job 𝑖 in OPT
OPT\{𝑖} is an optimal solution of WIS(p(i))
Case 2: job 𝑖 not in OPT
OPT is an optimal solution of WIS(i-1)
Recursively define the value
Weighted Interval Scheduling Problem
Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible
Bottom-up method: solve smaller subproblems first
i 0 1 2 3 4 5 … n
M[i]
WIS(n, s, f, v, p) M[0] = 0
Weighted Interval Scheduling Problem
Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible
53
Bottom-up method: solve smaller subproblems first Weighted Interval Scheduling Problem
Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible
i 0 1 2 3 4 5 6
M[i] 0 1 3 4 5 6 7
1
3
3 4
3 1 1
2 3 4 5 6 job index
WIS(n, s, f, v, p) M[0] = 0
for i = 1 to n
M[i] = max(v[i] + M[p[i]], M[i - 1]) return M[n]
Weighted Interval Scheduling Problem
Input: 𝑛 jobs with 𝑠𝑖, 𝑓𝑖, 𝑣𝑖 , 𝑝(𝑗) = largest index 𝑖 < 𝑗 s.t. jobs 𝑖 and 𝑗 are compatible Output: the maximum total value obtainable from compatible
Find-Solution(M, n) if n = 0
return {}
“Dynamic Programming”: solve many subproblems in polynomial time for which a naïve approach would take exponential time
When to use DP
Whether subproblem solutions can combine into the original solution
When subproblems are overlapping
Whether the problem has optimal substructure
Common for optimization problem
Two ways to avoid recomputation
Top-down with memoization
Bottom-up method
Complexity analysis
Space for tabular filling
Course Website: http://ada.miulab.tw Email: ada-ta@csie.ntu.edu.tw
56
Important announcement will be sent to @ntu.edu.tw mailbox
& post to the course website