原创 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



MIT 6.00 1X Lecture 7 Debugging 学习笔记

l MIT6.00 1x (麻省理工:计算机科学和Python编程导论) Lecture 7 Debugging 调试 7.1 TESTING AND DEBUGGING 测试和调试 Testi...

MIT算法导论学习笔记-Lecture4 分治法(续)

第四讲:分治法(续) 4.1 快速排序(Quicksort)

MIT 6.00 1x Lecture 3 - Simple Algorithms 简单算法 学习笔记

l MIT6.00 1x (麻省理工:计算机科学和Python编程导论) Lecture 3 - Simple Algorithms 简单算法 3.1 iteration 迭代 Iteration 迭...

MIT6.00 1x Lecture 2 - Core elements of programs 学习笔记

Lecture 2 - Core elements of programs 编程基础 2.1 Type of programming languages 编程语言的类别 • Goal: 目标(将我们想...

Machine Learning Foundation Lecture 03 Type Of Learning 学习笔记


斯坦福机器学习笔记-Lecture 1,2



第一讲:算法分析 1

Machine Learning Foundation Lecture 04 Feasuibility of learning 学习笔记

笔记4,机器学习的可行性。 知识点1:有时候机器学习是做不到的。

MIT 6.00 1x Lecture 5 Recursion 递归 学习笔记

l MIT6.00 1x (麻省理工:计算机科学和Python编程导论) Lecture5 Recursion 递归 5.1 ITERATIVE ALGORITHMS 迭代算法 Iterativ...


牛顿方法() 指数fenbu
  • BiggerQ
  • BiggerQ
  • 2014年08月03日 14:33
  • 453