




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Dynamic ProgrammingCopyright Li Zimao 2007-2008-1 SCUEC第1页,共26页。Expected OutcomesStudents should be able toWrite down the four steps of dynamic programmingCompute a Fibonacci number and the binomial coefficients by dynamic programming Compute the longest common subsequence and the shortest common su
2、persequence of two given sequences by dynamic programmingSolve the invest problem by dynamic programmingCopyright Li Zimao 2007-2008-1 SCUEC第2页,共26页。Dynamic Programming Dynamic Programming is a general algorithm design technique. Invented by American mathematician Richard Bellman in the 1950s to sol
3、ve optimization problemsMain idea: solve several smaller (overlapping) subproblemsrecord solutions in a table so that each subproblem is only solved oncefinal state of the table will be (or contain) solutionDynamic programming vs. divide-and-conquerpartition a problem into overlapping subproblems an
4、d independent onesstore and not store solutions to subproblemsCopyright Li Zimao 2007-2008-1 SCUEC第3页,共26页。Frame of Dynamic ProgrammingProblem solvedSolution can be expressed in a recursive waySub-problems occur repeatedlySubsequence of optimal solution is an optimal solution to the sub-problemFrame
5、Characterize the structure of an optimal solutionRecursively define the value of an optimal solutionCompute the value of an optimal solution in a bottom-up fashionConstruct an optimal solution from computed informationCopyright Li Zimao 2007-2008-1 SCUEC第4页,共26页。Three basic componentsThe development
6、 of a dynamic programming algorithm has three basic components:A recurrence relation (for defining the value/cost of an optimal solution);A tabular computation (for computing the value of an optimal solution);A backtracing procedure (for delivering an optimal solution). Copyright Li Zimao 2007-2008-
7、1 SCUEC第5页,共26页。Example: Fibonacci numbers Recall definition of Fibonacci numbers:f(0) = 0f(1) = 1f(n) = f(n-1) + f(n-2) Computing the nth Fibonacci number recursively (top-down): f(n) f(n-1) + f(n-2)f(n-2) + f(n-3) f(n-3) + f(n-4) .Copyright Li Zimao 2007-2008-1 SCUEC第6页,共26页。Example: Fibonacci num
8、bers Computing the nth fibonacci number using bottom-up iteration: f(0) = 0 f(1) = 1 f(2) = 0+1 = 1 f(3) = 1+1 = 2 f(4) = 1+2 = 3 f(5) = 2+3 = 5 f(n-2) = f(n-1) = f(n) = f(n-1) + f(n-2)ALGORITHM Fib(n)F0 0, F1 1for i2 to n doFi Fi-1 + Fi-2return Fnextra spaceCopyright Li Zimao 2007-2008-1 SCUEC第7页,共
9、26页。Examples of Dynamic ProgrammingComputing binomial coefficientsCompute the longest common subsequenceCompute the shortest common supersquenceWarshalls algorithm for transitive closureFloyds algorithms for all-pairs shortest paths Some instances of difficult discrete optimization problems:knapsack
10、Copyright Li Zimao 2007-2008-1 SCUEC第8页,共26页。Computing Binomial CoefficientsA binomial coefficient, denoted C(n, k), is the number of combinations of k elements from an n-element set (0 k n).Recurrence relation (a problem 2 overlapping subproblems)C(n, k) = C(n-1, k-1) + C(n-1, k), for n k 0, and C(
11、n, 0) = C(n, n) = 1Dynamic programming solution:Record the values of the binomial coefficients in a table of n+1 rows and k+1 columns, numbered from 0 to n and 0 to k respectively.11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1 Copyright Li Zimao 2007-2008-1 SCUEC第9页,共26页。Dynamic Binomial Coefficient Algorit
12、hmfor i = 0 to n dofor j = 0 to minimum( i, k ) doif j = 0 or j = i thenBiCoeff i, j = 1elseBiCoeff i, j = BiCoeff i-1, j-1 + BiCoeff i-1, j end ifend for jend for iCopyright Li Zimao 2007-2008-1 SCUEC第10页,共26页。Longest Common Subsequence (LCS)A subsequence of a sequence S is obtained by deleting zer
13、o or more symbols from S. For example, the following are all subsequences of “president”: pred, sdn, predent.The longest common subsequence problem is to find a maximum length common subsequence between two sequences.Copyright Li Zimao 2007-2008-1 SCUEC第11页,共26页。LCSFor instance, Sequence 1: presiden
14、t Sequence 2: providence Its LCS is priden.presidentprovidenceCopyright Li Zimao 2007-2008-1 SCUEC第12页,共26页。LCSAnother example: Sequence 1: algorithm Sequence 2: alignmentOne of its LCS is algm.a l g o r i t h ma l i g n m e n tCopyright Li Zimao 2007-2008-1 SCUEC第13页,共26页。How to compute LCS?Let A=a
15、1a2am and B=b1b2bn .len(i, j): the length of an LCS between a1a2ai and b1b2bjWith proper initializations, len(i, j) can be computed as follows.Copyright Li Zimao 2007-2008-1 SCUEC第14页,共26页。Copyright Li Zimao 2007-2008-1 SCUEC第15页,共26页。Running time and memory: O(mn) and O(mn).Copyright Li Zimao 2007-
16、2008-1 SCUEC第16页,共26页。The backtracing algorithmCopyright Li Zimao 2007-2008-1 SCUEC第17页,共26页。Copyright Li Zimao 2007-2008-1 SCUEC第18页,共26页。Shortest common super-sequenceDefinition: Let X and Y be two sequences. A sequence Z is a super-sequence of X and Y if both X and Y are subsequence of Z.Shortest
17、 common super-sequence problem:Input: two sequences X and Y.Output: a shortest common super-sequence of X and Y.Example: X=abc and Y=abb. Both abbc and abcb are the shortest common super-sequences for X and Y.Copyright Li Zimao 2007-2008-1 SCUEC第19页,共26页。Recursive Equation:Let leni,j be the length o
18、f an SCS of X1.i and Y1.j.leni,j can be computed as follows: j if i=0, i if j=0,leni,j = leni-1,j-1+1 if i, j0 and xi=yj, minleni,j-1+1, leni-1,j+1 if i, j0 and xiyj. How to compute SCS?Copyright Li Zimao 2007-2008-1 SCUEC第20页,共26页。Solution: ABDCABDABCopyright Li Zimao 2007-2008-1 SCUEC第21页,共26页。Exe
19、rciseConsider the algorithm for LCS as an example, write down the SCS algorithm and analyze it.Copyright Li Zimao 2007-2008-1 SCUEC第22页,共26页。Suppose there are m dollars,and n products. Let fi(x) be the profit of investing x dollars to product i. How to arrange the investment such that the total profit f1(x1) + f2(x2) + + fn(xn) is maximized.Instance:5 thousand dollars,4 products xf1(x)f2(x)f3(x)f4(x)00000111022021251021313103022414153223515204024An interesting example: Investment ProblemCopyright Li Zimao 2007-2008-1 SCUEC第23页,共26页。xF1(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025至2030年中国电信设备装置用通信电缆数据监测研究报告
- 2025至2030年中国甘氨酸钠数据监测研究报告
- 知识产权流程优化从申请到维护的全方位管理
- 科技创新在商业领域的机遇与挑战
- 2025年中储粮储运有限公司校园招聘吉林省岗位(9人)笔试参考题库附带答案详解
- 有关师德师风自查报告
- 2025至2030年中国玉米胚芽粕数据监测研究报告
- 2025至2030年中国滥用药品测试剂数据监测研究报告
- 社会管理中磁性技术的应用与发展趋势分析
- 药店配送合同范本
- 文化差异下的教育国外的小学音乐教育方式探讨
- 2025年无锡科技职业学院高职单招职业技能测试近5年常考版参考题库含答案解析
- 2024年黑龙江建筑职业技术学院高职单招语文历年参考题库含答案解析
- 贵州省贵阳市普通中学2024-2025学年高二上学期期末监测历史试题(含答案)
- 八大员-劳务员模考试题与答案
- 2024危重症患儿管饲喂养护理-中华护理学会团体标准课件
- Python金融数据挖掘与分析实战课程教案教学教案
- 2024年地铁车站照明系统安装与维护劳务分包协议3篇
- 脱硫自动化控制-洞察分析
- 医务人员医德医风培训
- 2024湖北省金口电排站管理处招聘易考易错模拟试题(共500题)试卷后附参考答案
评论
0/150
提交评论