SolutionsHW5.docx

上传人:b****4 文档编号:3780290 上传时间:2022-11-25 格式:DOCX 页数:10 大小:243.49KB
下载 相关 举报
SolutionsHW5.docx_第1页
第1页 / 共10页
SolutionsHW5.docx_第2页
第2页 / 共10页
SolutionsHW5.docx_第3页
第3页 / 共10页
SolutionsHW5.docx_第4页
第4页 / 共10页
SolutionsHW5.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

SolutionsHW5.docx

《SolutionsHW5.docx》由会员分享,可在线阅读,更多相关《SolutionsHW5.docx(10页珍藏版)》请在冰豆网上搜索。

SolutionsHW5.docx

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)

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1