DynamicProgramming技术学习教程.pptx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《DynamicProgramming技术学习教程.pptx》由会员分享,可在线阅读,更多相关《DynamicProgramming技术学习教程.pptx(158页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、14.1 IntroductionF(n)=1if n=0 or 1F(n-1)+F(n-2)if n 1n012345678910F(n)1123581321345589Pseudo code for the recursive algorithm:F(n)1if n=0 or n=1 then return 12elsereturn F(n-1)+F(n-2)Fibonacci number F(n)第1页/共158页2The execution of F(7)F7F6F5F5F4F3F3F2F1F0F2F1F0F1F2F1F0F2F1F0F2F1F0F4F4F3F3F3F2F1F0F2F
2、1F0F2F1F0F1F1F1F1第2页/共158页3The execution of F(7)Computation of F(2)is repeated 8 times!F7F6F5F5F4F3F3F2F1F0F2F1F0F1F2F1F0F2F1F0F2F1F0F4F4F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1F1第3页/共158页4The execution of F(7)Computation of F(3)is also repeated 5 times!F7F6F5F5F4F3F3F2F1F0F2F1F0F1F2F1F0F2F1F0F2F1F0F4F4F3F3F3
3、F2F1F0F2F1F0F2F1F0F1F1F1F1第4页/共158页5The execution of F(7)Many computations are repeated!How to avoid this?F7F6F5F5F4F3F3F2F1F0F2F1F0F1F2F1F0F2F1F0F2F1F0F4F4F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1F1第5页/共158页6Idea for improvementMemorization Store F1(i)somewhere after we have computed its value Afterward,we do
4、nt need to re-compute F1(i);we can retrieve its value from our memory.F1(n)1 if vn 0 then2 vn F1(n-1)+F1(n-2)3 return vnMain()1 v0=v1 12 for i 2 to n do3 vi=-14 output F1(n)第6页/共158页7Look at the execution of F(7)11-1-1-1-1-1-1v0v1v2v3v4v5v6v7F7F6F5F5F4F3F3F2F1F0F2F1F0F1F2F1F0F2F1F0F2F1F0F4F4F3F3F3F2
5、F1F0F2F1F0F2F1F0F1F1F1F1F(i)=Fi第7页/共158页8Look at the execution of F(7)11-1-1-1-1-1-1v0v1v2v3v4v5v6v7F7F6F5F5F4F3F3F2F1F0F2F1F0F1F2F1F0F2F1F0F2F1F0F4F4F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1F1第8页/共158页9Look at the execution of F(7)112-1-1-1-1-1v0v1v2v3v4v5v6v7F7F6F5F5F4F3F3F2F1F0F2F1F0F1F2F1F0F2F1F0F2F1F0F4F4
6、F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1F1第9页/共158页10Look at the execution of F(7)112-1-1-1-1-1v0v1v2v3v4v5v6v7F7F6F5F5F4F3F3F2F1F0F2F1F0F1F2F1F0F2F1F0F2F1F0F4F4F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1F1第10页/共158页11Look at the execution of F(7)1123-1-1-1-1v0v1v2v3v4v5v6v7F7F6F5F5F4F3F3F2F1F0F2F1F0F1F2F1F0F2F1F0F2F1F0F4
7、F4F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1F1第11页/共158页12Look at the execution of F(7)1123-1-1-1-1v0v1v2v3v4v5v6v7F7F6F5F5F4F3F3F2F1F0F2F1F0F1F2F1F0F2F1F0F2F1F0F4F4F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1F1第12页/共158页13F1F0Look at the execution of F(7)11235-1-1-1v0v1v2v3v4v5v6v7F7F6F5F5F4F3F3F2F1F0F2F1F2F1F0F2F1F0F2F1F0F
8、4F4F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1F1第13页/共158页14F1F0F2F1F0F1Look at the execution of F(7)11235-1-1-1F7F6F5F5F4F3F3F2F1F0F2F1F2F1F0F2F1F0F4F4F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1v0v1v2v3v4v5v6v7第14页/共158页15F1F0F2F1F0F1Look at the execution of F(7)112358-1-1F7F6F5F5F4F3F3F2F1F0F2F1F2F1F0F2F1F0F4F4F3F3F3F2F1F0
9、F2F1F0F2F1F0F1F1F1v0v1v2v3v4v5v6v7第15页/共158页16F1F0F2F1F0F2F1F0F2F1F0F3F1F1Look at the execution of F(7)112358-1-1F7F6F5F5F4F3F3F2F1F0F2F1F4F4F3F3F2F1F0F2F1F0F2F1F0F1F1v0v1v2v3v4v5v6v7第16页/共158页17F1F0F2F1F0F2F1F0F2F1F0F3F1F1Look at the execution of F(7)11235813-1F7F6F5F5F4F3F3F2F1F0F2F1F4F4F3F3F2F1F0
10、F2F1F0F2F1F0F1F1v0v1v2v3v4v5v6v7第17页/共158页18F1F0F2F1F0F2F1F0F2F1F0F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1F1Look at the execution of F(7)11235813-1F7F6F5F5F4F3F3F2F1F0F2F1F4F4v0v1v2v3v4v5v6v7第18页/共158页19F1F2F1F0F2F1F0F2F1F0F4F3F3F3F2F1F0F2F1F0F2F1F0F1F1F1F1Look at the execution of F(7)1123581321F7F6F5F5F4F3F3
11、F2F1F0F2F0F1F4v0v1v2v3v4v5v6v7第19页/共158页20This new implementation saves lots of overhead.Can we do even better?ObservationThe 2nd version still make many function calls,and each wastes times in parameters passing,dynamic linking,.In general,to compute F(i),we need F(i-1)&F(i-2)onlyIdea to further im
12、proveCompute the values in bottom-up fashion.That is,compute F(2)(we already know F(0)=F(1)=1),then F(3),then F(4)F2(n)1 F0 12 F1 13 for i 2 to n do4Fi Fi-1+Fi-25 return Fn第20页/共158页21Recursive vs Dynamic programmingRecursive version:F(n)1if n=0 or n=1 then return 12elsereturn F(n-1)+F(n-2)Dynamic P
13、rogramming version:F2(n)1 F0 12 F1 13 for i 2 to n do4Fi Fi-1+Fi-25 return FnTooSlow!Efficient!Time complexity is O(n)第21页/共158页22Summary of the methodologyWrite down a formula that relates a solution of a problem with those of sub-problems.E.g.F(n)=F(n-1)+F(n-2).Index the sub-problems so that they
14、can be stored and retrieved easily in a table(i.e.,array)Fill the table in some bottom-up manner;start filling the solution of the smallest problem.This ensures that when we solve a particular sub-problem,the solutions of all the smaller sub-problems that it depends are available.For historical reas
15、ons,we call such methodologyDynamic Programming.In the late 40s(when computers were rare),programming refers to the tabular method.第22页/共158页23 Dynamic programming VS Divide-and-conquerDivide-and-conquer method 1.Subproblem is independent.2.Subproblem is solved repeatedly.Dynamic programming(DP)1.Su
16、bproblem is not independent.2.Subproblem is just solved once.Common:Problem is partitioned into one or more subproblem,then the solution of subproblem is combined.DP reduces computation by Solving subproblems in a bottom-up fashion.Storing solution to a subproblem the first time it is solved.Looking
17、 up the solution when subproblem is met again.第23页/共158页24第24页/共158页254.2 Assembly-line schedulingEndSij:The jth station on line i(i=1 or 2).aij:The assembly time required at station Sij.ei:Entry time.xi:Exit time.e1e2a11a12a13a21a22a23a1n-1a1na2n-1a2nx1x2t1n-1t2n-1BeginLine 1Line 2t11t21t12t22S11S1
18、2S13S1n-1S1nS21S22S23S2n-1S2n第25页/共158页26One SolutionBrute forceEnumerate all possibilities of selecting stationsCompute how long it takes in each case and choose the best oneProblem:There are 2n possible ways to choose stationsInfeasible when n is large100111 if choosing line 1 at step j(=n)1234n0
19、if choosing line 2 at step j(=3)第26页/共158页27fij=the fastest time to get from the starting point through station SijLet f*denote fastest time to get a chassis all the way through the factory.j=1(getting through station 1)f11=e1+a11 f21=e2+a21f*=min(f1n+x1,f2n+x2)e1e2a11a12a13a21a22a23a1n-1a1na2n-1a2n
20、x1x2t1n-1t2n-1BeginLine 1Line 2t11t21t12t22第27页/共158页282.A Recursive Solution(cont.)Compute fij for j=2,3,n,and i=1,2Fastest way through S1j is either:fastest way through S1j-1then directly through S1j,or f1j=f1j-1+a1jfastest way through S2j-1,transfer from line 2 to line 1,then through S1j f1j=f2j-
21、1+t2j-1+a1jf1j=min(f1j-1+a1j,f2j-1+t2j-1+a1j)a1ja1j-1a2j-1t2j-1S1jS1j-1S2j-1第28页/共158页29The recursive equationFor example,n=5:Solving top-down would result in exponential running timef1jf2j12345f15f25f14f24f13f232 times4 timesf12f22f11f21第29页/共158页303.Computing the Optimal SolutionFor j 2,each value
22、 fij depends only on the values of f1j 1 and f2j-1Compute the values of fijin increasing order of jBottom-up approachFirst find optimal solutions to subproblemsFind an optimal solution to the problem from the subproblemsf1jf2j12345increasing j第30页/共158页31Additional InformationTo construct the optima
23、l solution we need the sequence of what line has been used at each station:lij the line number(1,2)whose station(j-1)has been used to get in fastest time through Sij,j=2,3,nl*-the line whose station n is used to get in the fastest way through the entire factoryl1jl2j2345increasing j第31页/共158页32Step
24、3:Computing the fastest wayDPFastestWay(a,t,e,x,n)1 f11 e1+a11;f21 e2+a212 for j 2 to n do3 if f1j-1+a1j f2 j-1+t2j-1+a1j then4 f1j f1j-1+a1j5 l1j 16 else f1j f2 j-1+t2j-1+a1j 7 l1j 28 if f2j-1+a2j f1j-1+t1j-1+a2j then9 f2j f2j-1+a2j 10 l2j 211 else f2 j f1j-1+t1j-1+a2j then 12 l2j 113 if f1n+x1f2n+
25、x2 then14 f*f1n+x115 l*116 else f*f2n+x2 17 l*2Running time:(n)第32页/共158页33For example24327944885475233421213612BeginEnd123456jf1jf2jf*=389121816202224253230353723456jl1jl2jl*=12111112222第33页/共158页344.Construct an Optimal SolutionPrintStations(l,n)1 i l*2 print“line”i“,station”n3 for j n downto 2 do
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DynamicProgramming 技术 学习 教程
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内