首页 > 其他 > 详细

Dynamic programming

时间:2014-09-28 23:34:37      阅读:379      评论:0      收藏:0      [点我收藏+]

Hallmarks of dynamic programming

#1

Optimal substructure: an optimal solution to a problem (instance) contains optimal solutions to subproblems.

e.g. if z is a longest common sequence (lcs) of x and y, then any prefix of z is an LCS of a prefix of x and a prefix of y. 

LCS(x, y, i, j)

if x[i] == y[j]

    then c[i,j] <- lcs(x,y,i-1, j-1) +1

  else c[i,j] <- max{lcs(x,y,i-1,j), lcs(x,y,i,j-1)}

return c[i,j];

Worst case: x[i] != y[j] for all i, j

the height of the recursion tree would be m+n in the worst case.

Exponential time complexity

 

# 2

Overlapping subproblems: A recursive solution contains a "small" number of distinct subproblems repeated many times.

 

LCS: subproblems space contains mn distinct subproblems.

Memoization algorithm (not memorization, memo: keep a memo on what‘s done already)

O(mn) time and space complexity

 

Dynamic programming:

Idea: compute a table bottom up

Reconstruct LCS by tracing backwards

 

 

Notes of MIT OCW: Introduction to Algorithms

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-algorithms-sma-5503-fall-2005/

 

Dynamic programming

原文:http://www.cnblogs.com/shalijiang/p/3999105.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!