# 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

• 本文已收录于以下专栏：

## 求第N个质数算法

def calc_prime(prime,num): i,gab=7,2 while num>3: flag=True for x in prime: ...
• lisulong1
• 2016年09月08日 16:27
• 195

## 21读取联系人的结构

• chenfuduo_loveit
• 2014年12月05日 22:36
• 429

## 吴恩达Deeplearning.ai 知识点梳理（course 4，week 1）

CNN基础知识
• asasasaababab
• 2017年12月15日 16:47
• 139

## [51nod 1184]第N个质数

• WerKeyTom_FTD
• 2017年01月12日 20:52
• 1330

## 计算第n个素数到第m个素数之间所有的素数的和，包括第n个素数和第m个素数

• dakaszeng
• 2018年01月14日 21:48
• 62

## 在1秒内求出第10000个质数的算法c 实现

• 2016年04月19日 20:50
• 1613

## 求第n个斐波那契数 递归和迭代两种方法

• ValDC_Morning
• 2016年12月22日 17:40
• 648

## LintCode-第k个排列

• wangyuquanliuli
• 2015年05月14日 22:39
• 1166

## 决定了睡觉了就是大家

http://my.fengniao.com/8462932
• u013783788
• 2014年04月04日 23:02
• 175

## 大家好,我在CSDN开博了,主要是和大家探讨技术方面的问题,请大家以后多多光顾!

具体哪个方面的技术还没想到，想到什么就写什么吧，希望大家以后多多光顾，共同探讨、一起进步!...
• huangf_3958
• 2008年07月15日 09:33
• 173

举报原因： 您举报文章：MITOPENCOURSE6.006的第21个和第22个lecture学习笔记 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)