SolutionsHW5.docx
《SolutionsHW5.docx》由会员分享,可在线阅读,更多相关《SolutionsHW5.docx(10页珍藏版)》请在冰豆网上搜索。
![SolutionsHW5.docx](https://file1.bdocx.com/fileroot1/2022-11/25/c1a68903-e13b-4ca9-b1da-4d990153f0f8/c1a68903-e13b-4ca9-b1da-4d990153f0f81.gif)
SolutionsHW5
参考答案
HomeworkFive
DesignandAnalysisofAlgorithms
2010,北京
1LetG=(V,E)beaconnectedweightedgraph,where|V|=n,|E|=m.Everyedgehasaweightwhichisanintegerbetween1andW,whereWisaconstant.DesignanO(m)algorithmtocomputetheminimumspanningtree(MST).
Solution:
ThekeyideaishowcanweefficientlyimplementthepriorityQtosupporttheExtract-Min(Q)andupdatethevaluekey[v]intheQ.
Sincethekey[v]isin[0,W]wedefineasetforeachvalue:
L[k]={u|key[u]=k},0kW,and
L[W+1]={u|key[u]=}.
Weusealinkedlisttochainallverticesineachset.Obviously,uL[key[u]].
WeinitializeL[0]={s},L[W+1]=V–{s},andL[k]=fork{1,2,…,W}.Theseindexedsets,L[k]0kW+1,formthepriorityqueueQ.
EachtimeweneedtoExtract-Min(Q),wesearchthelistsfromL[0]untilanon-emptylistisfound.Avertexinthefirstnon-emptylististhevertexweneed.
Whenavertexisupdated,itwillbemovedtoacorrespondingset.Usinglinkedlists,deletingandinsertinganelementinalistcanbedoneinO
(1)time.
Now,wearereadytopresentthealgorithm.
ModifiedPrim’salgorithm(G,W,r)
1foreachvV[G]
2do{key[v]
3[v]nil
4}
5key[r]0
6S
7fork=1toW
8doL[k].
9L[0]{s}
10L[W+1]=V–{s}
11whileSV
12do{i0
13whileL[i]=
14doii+1
15uExtractanelementfromL[i]
16SS{u}
17foreachvAdj[u]
18doifvSandw(u,v)19then{[v]u
20L[key[v]]L[key[v]]-{v}
21key[v]w(u,v)
22L[key[v]]L[key[v]]{v}
23}
24}
25End
ThetimecomplexityisO(m)becausetheExtractanelementoperationneedsatmostO(W)=O
(1)timeandtherearensuchoperations.Moreover,thereatotalofO(m)updatesonthevalueofkey(v)andeachneedsO
(1)time.
2UseDijkstra’salgorithmtofindtheshortestpathtreeforthefollowinggraph.Pleasetakethevertexsasthesource.Youmustshowdetailedsteps,onefigureforeachstep.(Initializationcouldbeomitted.)
Solution:
Weusecircletoindicatethatcorrespondingvertexismarked.
Step0.Initialization
Step1
Step2
Step3
Step4
Step5
Step6
Step7
Step8
Step9
Step10
Theshortestpathtreeisasfollows.
3(a)LetTandT’betwospanningtreesofaconnectedgraphG.SupposethatanedgexisinTbutnotinT’.ProvethatthereisanedgeyinT’butnotinTsuchthat(T-{x}){y}and(T’-{y}){x}arespanningtreesofG.Note,TandT’maynotbetheminimumspanningtrees.
Proof.
Supposex=(u,v).RemovingxfromTwillpartitionTintotwosubtreesT1andT2asshownbythefollowingfigure(a).
LetV1bethesetofverticesinT1andV2bethesetofverticesinT2.IfwefollowtheuniquepathfromutovinT’,wewillmeetanedgey=(a,b)suchthataV1andbV2.(Notethattheremaybemorethanonesuchedge.Edgeyisthefirstonewemeet.)
Now,(T-{x}){y}isobviouslyaspanningtree.
WeobservethatremovingyfromT’partitionsT’intotwosubtreessuchthatverticesaandbbelongtodifferentsubtrees.Obviously,verticesuandabelongtothesamesubtreeandverticesvandbbelongtotheother.Therefore,(T’-{y}){x}isaspanningtreealso.
3(b)SupposealledgesinaconnectedgraphGhavedistinctweights.ProvethatGhasauniqueminimumspanningtree.Thatis,youcannotfindtwodifferentminimumspanningtrees.
Proof.
Weprovethisbycontradiction.Suppose,forthesakeofcontradiction,therearetwodifferentminimumspanningtrees,TandT’.BecauseTT’,thereisanedgexTbutxT’.Fromproblem1(a),thereisanedgeyT’butyTsuchthat(T-{x}){y}and(T’-{y}){x}arespanningtreesofG.
Becausetheweightsofxandyaredifferent,wemayassumew(x)>w(y).Then,theweightofspanningtree(T-{x}){y}isW(T)-w(x)+w(y),andW(T)-w(x)+w(y)