版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2025/3/281DynamicProgramming2025/3/282Dynamicprogrammingvs.divide-and-conquer
Divide-and-conqueralgorithmssplitaproblemintoseparatesubproblems,solvethesubproblems,andcombinetheresultsforasolutiontotheoriginalproblem.Divide-and-conqueralgorithmscanbethoughtofastop-downalgorithmsIncontrast,adynamicprogrammingalgorithmproceedsbysolvingsmallproblems,thencombiningthemtofindthesolutiontolargerproblemsDynamicprogrammingcanbethoughtofasbottom-upStoreandnotstoresolutionstosubproblems2025/3/283ThreebasiccomponentsThedevelopmentofadynamicprogrammingalgorithmhasthreebasiccomponents:Arecurrencerelation(fordefiningthevalue/costofanoptimalsolution);Atabularcomputation(forcomputingthevalueofanoptimalsolution);Abacktracingprocedure(fordeliveringanoptimalsolution).2025/3/284ExamplesofDynamicProgrammingAlgorithmsLongestpathinDAGComputingbinomialcoefficientsEditdistanceKnapsackChainmatrixmultiplicationall-pairsshortestpathsTravelingsalesman2025/3/285LongestpathinDAGProblem:GivenaweighteddirectedacyclicgraphG=(V,E),anvertexv,whereeachedgeisassignedanintegerweight,findalongestpathingraphG.2025/3/286LongestpathinDAGdilg(v):thelongestpathendingwithv.dilg(D),dilg(B),dilg(C)dilg(D)=max{dilg(B)+1,dilg(C)+3}Foranyvertexv,dilg(v)=max(u,v)∈E{dilg(u)+w(u,v)}2025/3/287LongestpathinDAGDplongestpath(G)Initializealldilg(.)valuesto∞;LetSbethesetofverticeswithindegree=0;ForeachvertexvinSdodilg(v)=0;3.Foreachv∈V\SinTopologicalSortingorderdo
dilg(v)=max(u,v)∈E{dilg(u)+w(u,v)}4.Returnthedilg(.)withmaximumvalue.2025/3/288ComputingBinomialCoefficients
Abinomialcoefficient,denotedC(n,k),isthenumberofcombinationsofkelementsfromann-elementset(0≤k≤n).RecurrencerelationC(n,k)=C(n-1,k-1)+C(n-1,k)forn>k>0,andC(n,0)=C(n,n)=12025/3/289Dynamicprogrammingsolution:Recordthevaluesofthebinomialcoefficientsinatableofn+1rowsandk+1columns,numberedfrom0tonand0tokrespectively.
0123…k-1k01 111 211 311 … k11 … n-11 n12025/3/2810Dynamicprogrammingsolution:Recordthevaluesofthebinomialcoefficientsinatableofn+1rowsandk+1columns,numberedfrom0tonand0tokrespectively.
0123…k-1k01 111 2121 311 … k11 … n-11 n12025/3/2811Dynamicprogrammingsolution:Recordthevaluesofthebinomialcoefficientsinatableofn+1rowsandk+1columns,numberedfrom0tonand0tokrespectively.
0123…k-1k01 111 2121 31331 … k11 … n-11 n12025/3/2812Dynamicprogrammingsolution:Recordthevaluesofthebinomialcoefficientsinatableofn+1rowsandk+1columns,numberedfrom0tonand0tokrespectively.
0123…k-1k01 111 2121 31331 … k11 … n-11C(n-1,k-1)C(n-1,k) n12025/3/2813Dynamicprogrammingsolution:Recordthevaluesofthebinomialcoefficientsinatableofn+1rowsandk+1columns,numberedfrom0tonand0tokrespectively.
0123…k-1k01 111 2121 31331 … k11 … n-11C(n-1,k-1)C(n-1,k) n1C(n,k)2025/3/2814ComputingBinomialCoefficients
Binomial(n,k)1.for
i=0tondo
C[i,0]=1;2.forj=0tomin(i,k)do
C[j,j]=1;3.for
i=0tondoforj=0tomin(i,k)do
C[i,j]=C[i-1,j-1]+C[i-1,j];4.returnC[n,k].
2025/3/2815EditDistanceThedistancebetweenstrings:alignmentAlignment:awayofwritingthestringsoneabovetheother.
The“-”indicatesa“gap”;anynumberofthesegapcanbeplacedineitherstring.Thecostofanalignmentisthenumberofcolumnsinwhichthelettersdiffer.2025/3/2816EditDistanceCost:3Cost:5The
editdistancebetweentwostringsisthecostoftheirbestpossiblealignment.2025/3/2817EditDistanceSubproblemE[i,j]rightmostcolumnE(i,j)E(i-1,j)E(i,j)=1+E(i-1,j)RelationshipE(i,j)E(i,j-1)E(i,j)=1+E(i,j-1)RelationshipE(i,j)E(i-1,j-1)E(i,j)=1/0+E(i-1,j-1)Relationship2025/3/2818EditDistanceIfx[i]=x[j]thendiff(i,j)=0,otherwisediff(i,j)=1.E(0,j)=j.E(i,0)=i.2025/3/2819EditDistanceTheanswerstoallthesubproblemsE(i,j)formatwo-dimensionaltable.2025/3/2820EditDistanceDPEditDis(x[1..m],y[1..n])Fori=0tomdoE(i,0)=i;2.Forj=1tondoE(0,j)=j;3.Fori=1tomdoforj=1tondoE(i,j)=min{E(i-1,j)+1,E(i,j-1)+1,E(i-1,j-1)+diff(i,j)}4.ReturnE(m,n).Runningtime:O(mn)2025/3/2821ThefindingofsubproblemsisanimportantstepinDynamicprogramming.SubproblemschoosingCommonlyusedmethods:Theinputisx1,x2,…,xn.asubproblemisx1,x2,…,xi.HowmanysubproblemsforthiscaseO(n)2025/3/2822Subproblemschoosing2.Theinputisx1,x2,…,xn,andy1,y2,…,ym.asubproblemisx1,x2,…,xiandy1,…,yj.HowmanysubproblemsforthiscaseO(mn)2025/3/2823Subproblemschoosing3.Theinputisx1,x2,…,xn.asubproblemisxi,xi+1,…,xj.HowmanysubproblemsforthiscaseO(n2)2025/3/2824Subproblemschoosing3.Theinputisarootedtree.asubproblemisarootedsubtree.IfthetreehasnnodesHowmanysubproblemsarethere2025/3/2825KnapsackHisbag(or.knapsack.)willholdatotalweightofatmostWpounds.Therearenitemstopickfrom,ofweightw1,…,wnandvaluev1,…,vn.What'sthemostvaluablecombinationofitemshecanfitintohisbag2025/3/2826KnapsackTwoversionsoftheproblemeachitem:unlimitedquantityeachitem:onlyoneKnapsackwithrepetitionKnapsackwithoutrepetitionW=102025/3/2827KnapsackKnapsackwithoutrepetitionWhatarethesubproblemsK(w)K(w-wi)isthemaximumoneforallitems.smallerknapsackcapacitiesw≤Wfeweritems(forinstance,items1,2,…,j,forj≤n).K(w,j)=maximumvalueachievableusingaknapsackofcapacitywanditems1,…,j.2025/3/2828KnapsackKnapsackwithoutrepetitionWhatarethesubproblemsK(w,j)=maximumvalueachievableusingaknapsackofcapacitywanditems1,…,j.K(W,n)istheanswer.HowtogetsubproblemK(w,j)intermsofsmallersubproblemsK(w,j)=max{K(w-wj,j-1)+vj,K(w,j-1)}w≥wjK(0,j)=0,K(w,0)=0.w<wjK(w,j)=K(w,j-1)2025/3/2829KnapsackDPKnapsacknorepe(item1,…,itemn;w1,…,wn;v1,…,vn,W)Forj=1tondoK(0,j)=0;Forw=0toWdoK(w,0)=0;Forj=1tondoforw=1toWdoifwj>wthenK(w,j)=K(w,j-1);elseK(w,j)=max{K(w,j-1),K(w-wj,j-1)+vj};4.ReturnK(W,n).Runningtime:O(nW)2025/3/2830Knapsack0…w-wj…w…W00…0…0….0::…j-1
0…K[w-wj,j-1]K[w,j-1]j0…K[w,j]n0…goal2025/3/2831Chainmatrixmultiplicationtheorderofmultiplicationsmakesabigdifferenceinthefinalrunningtime!ComputeA1×A2×…×An,A1:m0×m1A2:m1×m2…An:mn-1×mn.Howdowedeterminetheoptimalorder2025/3/2832ChainmatrixmultiplicationMatrixmultiplicationBinarytreeforatreetobeoptimal,itssubtreesmustalsobeoptimal.WhatarethesubproblemscorrespondingtothesubtreesAi×Ai+1×…×AjC(i,j)=minimumcostofmulitiplyingAi×Ai+1×…×Aj.1≤i≤j≤n2025/3/2833ChainmatrixmultiplicationC(i,j)=minimumcostofmulitiplyingAi×Ai+1×…×Aj.1≤i≤j≤nHowtogetsubproblemC(i,j)intermsofsmallersubproblemsforakbetweeniandj,Ai×…×AkandAk+1×…×AjC(i,k)C(k+1,j)C(i,j)C(i,k)C(k+1,j)j≥ij=iC(i,i)=0.2025/3/2834ChainmatrixmultiplicationDpmatrixmul(A1,A2,..,An)Fori=1tonC(i,i)=0;2.Fors=1ton-1dofori=1ton-sdoj=i+s;C(i,j)=min{C(i,k)+C(k+1,j)+mi-1.mk.mj,i≤k<j};3.ReturnC(1,n).Runningtime:O(n3)2025/3/2835AllpairsshortestpathIsthereisagoodsubproblemforcomputingdistancesbetweenallpairsofverticesinagraphShortestpathbetweenu,vu,w1,w2,…,wh,vSupposewedisallowintermediatenodesaltogether.Thenwecansolveall-pairsshortestpathsatonce:theshortestpathfromutovissimplythedirectedge(u,v),ifitexists.2025/3/2836AllpairsshortestpathD(k):allow1,2,…,ktobeintermediatevertices.dij(k)=min{dij(k-1),dik(k-1)+dkj(k-1)}fork
1,dij(0)=wijHowtogetsubproblemdij(k)intermsofsmallersubproblemsijkdij(k-1)dik(k-1)dkj(k-1)2025/3/2837AllpairsshortestpathDpallpairst
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 贾生名谊文言文详解课件
- 2026年电气节能技术的市场竞争力与经济效益评估
- 2026春招:新媒体笔试题及答案
- 2026年电气设备的选型与安全评估
- 货运交通安全
- 医疗人员职业素养与职业规划
- 护理教育与护理人文关怀
- 货梯安全培训考核内容
- 医疗护理礼仪在医患关系中的意义
- 医疗行业品牌推广与营销
- 2025年电子工程师年度工作总结
- 2026年消防设施操作员之消防设备基础知识考试题库500道及完整答案(各地真题)
- 2026年电信运营商物资管理岗位面试题
- 2025年高职会计(成本核算)试题及答案
- 虫鼠害培训课件
- 2025学年上海市七年级语文上册作文题目汇编及解析
- 2026年河南经贸职业学院单招职业技能测试题库及参考答案详解
- ai写作与公文写作培训课件
- 栏杆安装施工方案示例
- JJF 2333-2025 恒温金属浴校准规范
- 2025年水工金属结构行业分析报告及未来发展趋势预测
评论
0/150
提交评论