# MITOPENCOURSE6.006的第21个和第22个lecture学习笔记

## Dynamic Programming III

1. define subproblems 定义子问题
2. guess(part of solution) 猜测部分解
3. recurrence relating the solution to the subproblem then figure out time/subproblem 递归定义
4. recurse+memorize/bottom up check acyclic/topological order 计算最优值
total time=subproblems * time/subproblem
5. solve original problem 得出原始问题的解

genneral tips for step1:

how to choose subproblems for strings and sequences？

• suffixes x[i:] ∀i Θ(n)
• prefixes x[:j] ∀j Θ(n)
• substrings x[i:j] ∀i<< j Θ(n2)

Parenthesization 矩阵连乘问题的最优加括号方式

now we have a sequence of matirces，what should guess?what feature of the solution of the optimal solution would you like to guess?
whole solution:exponentially.what can we do to make progress?
the last operation?the last multiplication.

(A0……Ak-1)•(Ak……An-1)
(a prefix)the last operation(a suffix)
the recurse of (A0……Ak-1):
(A0……Ak’-1)•(Ak’……Ak-1)
(a prefix)the operation(a substring)
so we choose the substring.

1. subproblem:

optimal evaluation of Ai to Aj-1 [i:j]
(Ai……Ak-1)•(Ak……Aj-1)
the choices of k is O(j-i+1)=O(n)

2. guess:

the outer most/last multiplication

3. recurrence:

DP[i:j]=min(DP(i,k)+DP(k,j)+cost of A[i:k]•A[k:j],for k in
range(i+1,j)) time/subproblem=O(n) neglecting recursion
subproblems=n2
∴total time= O(n3)

4. topological order:

so in what order should I evaluate this recurrence——increasing
substring size

5. original problem:

DP(0,n)
DP(i,i+1)=0(base case)
DP≠shortest paths in DAG

Edit Distance 编辑距离

given two strings x&y，what’s the cheapest way to convert x->y?
character edit has 3 ways:insert,delete,replace
define cost of insert/delete =1,cost of replace={0,∞}
we look at thesuffixes in x&y

1. subproblem:

edit distance on x[i:]&y[j:] ∀i,j
subproblems=Θ(|x|•|y|)

2. guess:

the first character 3 possibilities: replace x[i]->y[j] insert y[j]
delete x[i]

3. recurrence:

DP(i,j) a suffix of x & a suffix of y = min{
cost of replacex[i]->y[j]+DP(i+1,j+1)
cost of insert y[j]+DP(i,j+1)
cost of delete x[i] +DP(i+1,j)}

4. topo order suffixes increasing

for i =|x| down to 0
for j = |y| down to 0
bottom-up towards origin

5. orignal question

DP(0,0)
time/subproblem=O(1)
subproblems=O(|x|•|y|)
∴total time=O(|x|•|y|)

knapsack 背包问题

list of items each has size Si & value Vi
knapsack of size:S
max of values for subset of items ∑Si≤S

subproblem:suffix [i:] of items & remaining capacity X <=S
guess:is item i in subset or not?
recurrence:DP(i,X)=max(DP(i+1,X),DP(i+1,X-Si)+Vi)
subproblems=Θ(n•s)
nlogS is pseudopolynomial time between exponential and polynomial

### Dynamic Programming IV

2 kinds of guessing

• in step2&3 which subproblem to use to solve bigger subproblem
• in step1 add more subproblems to guess/remembering more feature of solutions

Piano and guitar figuring 钢琴和吉他的指法问题

given misical pieces:a sequence of n notes ,find fingering for each single note
fingers 1……F(5)
difficulty measure d(p,f,q,g),p,q for notes f,g for fingers

subproblem: how to play note[i:]suffix
guess:which finger to use for notes[i]
recurrence:DP(i)=min(DP[i+1]+d(i,f,i+1,?)) for fingers in 1……F(5)

BUT what to do next?we don’t know.So we guess this in the second way——in step1 add more subproblems to guess/remembering more feature of solutions

1. subproblem:

how to play notes[i:] when use f for notes[i]

2. guess:

finger g for notes[i+1]

3. recurrence

DP(i,f)=min(DP(i+1,g)+d(i,f,i+1,g)), for g in 1……F

4. topo order

for i in reversed (range(n)): for f in 1……F

5. orignal problem:

min(DP(o,f)for f in 1……F complete biparite graph,then do single
shortest paths in DAGS subproblems=Θ(nF) time/subproblem=Θ(F) total
time=Θ(nF2)=Θ(n)

Guitar “fingers”= finger+string,timeΘ(nF2S2),mapping Θ(n(F+1)2F)

Tetris 俄罗斯方块

• given seq of n pieces
• must drop from top
• full rows don’t clear
• can you survive?
• the width is small
• empty board initially

without these constrictions,it will be NP-complete

subproblem:suffix pieces[i:]
given board skyline how many choices?n(h+1)w
then guess how to place pieces[i]
total time= Θ(nw(h+1)w)

Super Mario Brothers

• given level n
• small w&h screen
• mario’s velocity
• information storation cwh
• score S,time T

we have pseudopolynomial Θ(STcwh)
then where is the screen& level?Θ(wSTcwh)
now you can draw DAG graphs then find shortest paths or doing DP subproblem according to max score or min time

#### 高级算法日记2：第1次课笔记

2017-06-05 15:37:52

#### 753. Cracking the Safe

2017-12-25 18:53:33

#### 大数据算法作业笔记1

2017-10-08 20:24:36

#### DP5 编辑距离 Edit Distance @geeksforgeeks

2013-12-24 01:03:49

#### LeetCode 446. Arithmetic Slices II - Subsequence

2016-11-06 10:30:49

#### MIT6.006 笔记

2016-04-22 16:10:28

#### MIT机电工程与计算机科学系【本科生课程】6.006.算法导论.Introduction.to.Algorithms

2015年01月23日 435KB 下载

#### MIT机电工程与计算机科学系【本科生课程】6.006.算法导论.

2015年01月23日 435KB 下载

#### MIT6_006F11_lec01.pdf

2015年07月25日 1.89MB 下载

#### MIT、CMU 美国计算机专业最牛20名学校大点评

2006-08-19 23:04:00