版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 煉焦煤制備工崗前基礎(chǔ)在崗考核試卷含答案
- 區(qū)塊鏈應(yīng)用操作員操作規(guī)程強化考核試卷含答案
- 三月三掃墓請假條
- 2025年半柔半剛射頻同軸電纜項目合作計劃書
- 2026年智能門窗光伏供電片項目可行性研究報告
- 2025年江蘇省鎮(zhèn)江市中考物理真題卷含答案解析
- 2025年四川省資陽市中考物理真題卷含答案解析
- 2025年臨床核心制度培訓(xùn)考核試卷(含答案)
- 2025年地質(zhì)勘探員安全生產(chǎn)知識定期考核題目及答案
- 選礦工技能鞏固考核試卷及答案
- 地坪漆施工方案范本
- 學(xué)習(xí)方法總結(jié)高效學(xué)習(xí)的技巧與方法
- 綜合醫(yī)院心身疾病診治
- 港口安全生產(chǎn)管理模版
- 產(chǎn)房與兒科交接登記表
- 2022-2023學(xué)年四川省宜賓市高一(下)期末數(shù)學(xué)試卷(含解析)
- 教你填《廣東省普通高中學(xué)生檔案》精編版
- 韓國語topik單詞-初級+中級
- 克林頓1993年就職演講+(中英文)
- 商業(yè)倫理與會計職業(yè)道德(第四版)第五章企業(yè)對外經(jīng)營道德規(guī)范
- DB13 5161-2020 鍋爐大氣污染物排放標(biāo)準(zhǔn)
評論
0/150
提交評論