




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 7 Dynamic ProgrammingThe design and Analysis of Computer AlgorithmsOutlineThe dynamic programming strategy is a very useful technique to solve many combinatorial optimization problems. Dynamic Programming; The shortest path;The resource allocation problem;The longest common subsequence (LCS)
2、 problem; 0/1 knapsack problem;Optimal binary search trees; Matrix-chain multiplication; Single step graph edge searching; Fibonacci sequenceFibonacci sequence: 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , Fi = i if i 1 Fi = Fi-1 + Fi-2 if i 2 Solved by a recursive program: Much replicated computation is d
3、one. It should be solved by a simple loop. Dynamic ProgrammingDynamic Programming is an algorithm design method that can be used when the solution to a problem may be viewed as the result of a sequence of decisionsThe shortest pathTo find a shortest path in a multi-stage graphApply the greedy method
4、 : the shortest path from S to T : 1 + 2 + 5 = 8 The shortest path in multistage graphse.g. The greedy method can not be applied to this case: (S, A, D, T) 1+4+18 = 23.The real shortest path is: (S, C, F, T) 5+2+2 = 9. Dynamic programming approach Dynamic programming approach (forward approach): d(S
5、, T) = min1+d(A, T), 2+d(B, T), 5+d(C, T) d(A,T) = min4+d(D,T), 11+d(E,T) = min4+18, 11+13 = 22.d(B, T) = min9+d(D, T), 5+d(E, T), 16+d(F, T) = min9+18, 5+13, 16+2 = 18.d(C, T) = min 2+d(F, T) = 2+2 = 4d(S, T) = min1+d(A, T), 2+d(B, T), 5+d(C, T) = min1+22, 2+18, 5+4 = 9. The above way of reasoning
6、is called backward reasoning.Backward approach (forward reasoning) d(S, A) = 1d(S, B) = 2d(S, C) = 5d(S,D)=mind(S,A)+d(A,D), d(S,B)+d(B,D) = min 1+4, 2+9 = 5 d(S,E)=mind(S,A)+d(A,E), d(S,B)+d(B,E) = min 1+11, 2+5 = 7 d(S,F)=mind(S,B)+d(B,F), d(S,C)+d(C,F) = min 2+16, 5+2 = 7d(S,T) = mind(S, D)+d(D,
7、T), d(S,E)+d(E,T), d(S, F)+d(F, T) = min 5+18, 7+13, 7+2 = 9 Principle of optimalityPrinciple of optimality: Suppose that in solving a problem, we have to make a sequence of decisions D1, D2, , Dn. If this sequence is optimal, then the last k decisions, 1 k n must be optimal. e.g. the shortest path
8、problem If i, i1, i2, , j is a shortest path from i to j, then i1, i2, , j must be a shortest path from i1 to jIn summary, if a problem can be described by a multistage graph, then it can be solved by dynamic programming.Forward approach and backward approach:Note that if the recurrence relations ar
9、e formulated using the forward approach then the relations are solved backwards . i.e., beginning with the last decisionOn the other hand if the relations are formulated using the backward approach, they are solved forwards.To solve a problem by using dynamic programming, we should :Find out the rec
10、urrence relations.Represent the problem by a multistage graph.Dynamic programmingThe resource allocation problem m resources, n projects profit Pi, j : j resources are allocated to project i. maximize the total profit. The multistage graph solution The resource allocation problem can be described as
11、 a multistage graph.(i, j) : i resources allocated to projects 1, 2, , je.g. node H=(3, 2) : 3 resources allocated to projects 1, 2.Find the longest path from S to T : (S, C, H, L, T), 8+5+0+0=132 resources allocated to project 1.1 resource allocated to project 2.0 resource allocated to projects 3,
12、4.The longest common subsequence (LCS) problem A string : A = b a c a dA subsequence of A: deleting 0 or more symbols from A (not necessarily consecutive).e.g. ad, ac, bac, acad, bacad, bcd.Common subsequences of A = b a c a d and and B = a c c b a d c b : ad, ac, bac, acad.The longest common subseq
13、uence (LCS) of A and B: acad.The LCS algorithmLet A = a1 a2 am and B = b1 b2 bn Let Li,j denote the length of the longest common subsequence of a1 a2 ai from A and b1 b2 bj from B.L0,0 = L0,j = Li,0 = 0 for 1im, 1jn. We have to calculate for the LCS problem: Lm,nThe dynamic programming approach for
14、solving the LCS problem:Time complexity: O(mn).Tracing back in the LCS algorithme.g. A = b a c a d, B = a c c b a d c bAfter all Li,js have been found, we can trace back to find the longest common subsequence of A and B.LCS of Nonebbabacbac, acaacada1 and Ba2 and Ba3 and Ba4 and Ba5 and Ba0 and BTo
15、calculate the LCS of A and BA = c b a c a d, B = a c c b a d c bLCS of Nonea1-a1 and Ba1-a2 and Ba1-a3 and Ba1-a4 and Ba1-a5 and Ba0 and Ba1-a6 and B0/1 knapsack problemn objects , weight W1, W2, ,Wn profit P1, P2, , Pn capacity M maximize subject to M here xi = 0 or 1, 1in e. g. The multistage grap
16、h solutionThe 0/1 knapsack problem can be described by a multistage graph.The dynamic programming approachThe longest path represents the optimal solution: x1=0, x2=1, x3=1 = 20+30 = 50 Let fi(Q) be the value of an optimal solution to objects 1,2,3,i with capacity Q.fi(Q) = max fi-1(Q), fi-1(Q-Wi)+P
17、i The optimal solution is fn(M). fi(Q) = max fi-1(Q), fi-1(Q-Wi)+Pi The optimal solution is f3(10). f3(10) = max f2(10), f2(10-5)+30 f2(10)= max f1(10), f1(10-3)+20 f2(5)= max f1(5), f1(5-3)+20 f1(10)=40, f1(7)=0f1(5)=0 , f1(2)=0f2(10)= max 40, 0+20 =40f2(5)= max 0, 0+20 =200/1 knapsack problem 4 8
18、50 5Optimal binary search trees e.g. binary search trees for 3, 7, 9, 12; Optimal binary search treesn identifiers : a1 a2 a3 an Pi, 1in : the probability that ai is searched. Qi, 0in : the probability that x is searched, where ai x ai+1 (a0=-, an+1=).Identifiers : 4, 5, 8, 10, 11, 12, 14Internal no
19、de : successful search, Pi External node : unsuccessful search, QiThe expected cost of a binary tree:The level of the root : 1The dynamic programming approachLet C(i, j) denote the cost of an optimal binary search tree containing ai,aj .The cost of the optimal binary search tree with ak as its root
20、:General formulaComputation relationships of sub-treese.g. n=4Time complexity : O(n3) (n-m) C(i, j)s are computed when j-i=m. Each C(i, j) with j-i=m can be computed in O(m) time. Matrix-chain multiplicationn matrices A1, A2, , An with size p0 p1, p1 p2, p2 p3, , pn-1 pn To determine the multiplicat
21、ion order such that # of scalar multiplications is minimized.To compute Ai Ai+1, we need pi-1pipi+1 scalar multiplications.e.g. n=4, A1: 3 5, A2: 5 4, A3: 4 2, A4: 2 5(A1 A2) A3) A4, # of scalar multiplications: 3 * 5 * 4 + 3 * 4 * 2 + 3 * 2 * 5 = 114(A1 (A2 A3) A4, # of scalar multiplications: 3 *
22、5 * 2 + 5 * 4 * 2 + 3 * 2 * 5 = 100(A1 A2) (A3 A4), # of scalar multiplications: 3 * 5 * 4 + 3 * 4 * 5 + 4 * 2 * 5 = 160Let m(i, j) denote the minimum cost for computing Ai Ai+1 AjComputation sequence :Time complexity : O(n3)For following example: Their number of row and column are: p1=10, p2=20, p3
23、=50, p4=1, p5=100, according above dynamic algorithm, we can construct the following table: The calculation processes1.2. m(1,2) =P1*p2*p3=10*20*50=10000 M12 m(2,3) =p2*p3*p4=20*50*1=1000 M23 m(3,4) =p3*p4*p5=50*1*100=5000 M343. m(1,3)= = = M12 M3M1 M23 =M12*M3=10*50*1=500The size of M12 is 1050, M3
24、: 501 M12 1050Total number is 10000+500=10500 for =M1*M23=10*20*1=200 M23 201M11020, M23201 =10*20*1=200Total number is 1000+200=1200 forM13=min(10500,1200)=12004. m(2,4)= = =M23:201, M34: 50100 M4: 1100, M2:2050 M23 M4 = 20*1*100=2000Total number =1000+2000=3000 forM2 M34 =20*50*100=100000Total num
25、ber =5000+100000=105000 forM24=min(3000,105000)=3000M23 M4M2 M345. m(1,4)= =Min =Min M13: 10 1 M24: 20 100M4: 1 100 M1: 10 20*M13M4=10*1*100=1000total number =1200+1000=2200 for*M1 M24=10*20*100=20000total number =3000+20000=23000 forM14 =Min(2200,23000)=2200 forM13 M4M1 M24Please finish the followi
26、ng calculationfor SIEPlease finish the following calculationSingle step graph edge searchingfugitive: can move in any speed and is hidden in some edge of an undirected graph G=(V,E)edge searcher(guard): search an edge (u, v) from u to v, or stay at vertex u to prevent the fugitive passing through uG
27、oal: to capture the fugitive in one step. no extra guards needed extra guards needed cost of a searcher from u to v: wt(u)a guard staying at u: wt(u)Cost of the following: 2wt(a)+wt(b)+wt(b) (one extra guard stays at b)Problem definition: To arrange the searchers with the minimal cost to capture the fugitive in one step. NP-hard for general graphs; P for trees.The weighted single step graph edge searching problem on treesT(vi): the tree includes vi , vj (parent of vi) and all descendant nodes of vi.C(T(vi), vi , vj ):cost of an optimal searching plan with sear
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 江西省南昌市2025届高三下学期4月模拟检测(二模)语文试卷及参考答案
- 北京临川学校2025届高三4月教学质量检测试题(二模)(文+理)数学试题
- 《人民科学家的精神风采》课件
- 2025年朝阳下载货运从业资格证模拟考试题
- 减肥行业现象研究报告
- 幼儿园各类预案
- 举办2025年社区八一建军节活动主题方案
- 基于tms320f280049设计的简单电路
- 二零二五版授予虚拟股合同
- 二零二五版房屋租赁主体变更三方合同
- 2024年重庆出版集团招聘笔试参考题库含答案解析
- 中国特色社会主义理论与实践复习资料-研究生
- 【高中历史】辽夏金元的统治课件-2024届高三历史统编版一轮复习
- 幼儿行为观察与分析案例教程 课件 第5、6章 幼儿情绪表现的观察分析与指导、幼儿认知发展的观察分析与指导
- 《强化学习理论与应用》深度强化学习概述
- 23CG60 预制桩桩顶机械连接(螺丝紧固式)
- -发育性髋关节脱位课件
- 婴幼儿的心肺复苏-课件
- 小说叙述视角与叙述人称公开课课件
- sat数学考试试题
- 泰国介绍英文
评论
0/150
提交评论