原创 2016年06月02日 12:25:15

Dynamic Programming III

首先复习之前两节课中总结的 5 easy steps of DP:

  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.

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

  1. subproblem:

    optimal evaluation of Ai to Aj-1 [i:j]
    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
    ∴total time= O(n3)

  4. topological order:

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

  5. original problem:

    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

  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

    ∴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?
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

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:]
we care about the height:
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



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


在设置向导的第三个页面,点击选择联系人的时候,进入选择联系人的页面。 /** * 选择联系人的按钮的点击事件 * * @param view */ public ...
  • chenfuduo_loveit
  • chenfuduo_loveit
  • 2014年12月05日 22:36
  • 429

吴恩达 知识点梳理(course 4,week 1)

  • asasasaababab
  • asasasaababab
  • 2017年12月15日 16:47
  • 139

[51nod 1184]第N个质数

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


我们认为2是第一个素数,3是第二个素数,5是第三个素数,依次类推。 现在,给定两个整数n和m,0 注意,是第n个素数到第m个素数之间的所有的素数,并不是n和m之间的所有的素数。 ...
  • dakaszeng
  • dakaszeng
  • 2018年01月14日 21:48
  • 62

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

在一秒内求出第10000个质数的c语言实现 #include #define MAXNUM 10000 void func() { register int i, j; int ...
  • dadaxiongdebaobao
  • dadaxiongdebaobao
  • 2016年04月19日 20:50
  • 1613

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

求第n个斐波那契数,在本篇博客中讲两种方法:一是利用递归调用函数实现,另一种是利用循环求出第n个斐波那契数。        首先要了解什么是斐波那契数列,斐波那契数列的特征是前两个数是1,其后数位前两...
  • ValDC_Morning
  • ValDC_Morning
  • 2016年12月22日 17:40
  • 648


给定 n 和 k,求123..n组成的排列中的第 k 个排列。 样例 对于 n = 3, 所有的排列如下: 123 132 213 231 312 321 如果 k = 4, ...
  • wangyuquanliuli
  • wangyuquanliuli
  • 2015年05月14日 22:39
  • 1166

  • u013783788
  • u013783788
  • 2014年04月04日 23:02
  • 175


  • huangf_3958
  • huangf_3958
  • 2008年07月15日 09:33
  • 173