最小耗费生成树Minimum cost spanning tree.docx
- 文档编号:26890487
- 上传时间:2023-06-23
- 格式:DOCX
- 页数:7
- 大小:18.38KB
最小耗费生成树Minimum cost spanning tree.docx
《最小耗费生成树Minimum cost spanning tree.docx》由会员分享,可在线阅读,更多相关《最小耗费生成树Minimum cost spanning tree.docx(7页珍藏版)》请在冰豆网上搜索。
最小耗费生成树Minimumcostspanningtree
最小耗费生成树(Minimumcostspanningtree)
MakeftheedgeoftheloopthatisnotintheT.
SincetheloopisnotincludedintheT,atleastoneedgeoftheloopformedisnotintheT.
FromtheselectionmethodofEandFcanbeseenintheV=U+{e}doublef}isaspanningtree,andTk-hasexactly1edgesdonotappearinV.NowtoprovethatVcostsasmuchasUdoes.Obviously,thecostofVisequaltothecostofU,plusthecostoftheuppere,andthensubtractthecostoftheedgeF.IfthecostofEissmallerthanf,thenthecostofspanningtreeVislessthanthatofU,whichisimpossible.IfthecostofEishigherthanf,intheKruskalalgorithm,fwillbeconsideredbeforeE.SincefisnotinT,theKruskalalgorithmdiscardsFwhenconsideringwhetherfcanjoinT,sofandTconsumelessthanorequaltoftoformaloop.Byselectinge,alloftheseedgesareinU,soUmustcontainloops,butinrealitythisisimpossiblebecauseUisaspanningtree.ThepriceofEishigherthanf'shypothesis,whichwillleadtocontradiction.TheonlyremainingpossibilityisthatEhasthesamecostasF,whichshowsthatVandUconsumethesameamount.
(3)datastructureselectionandcomplexityanalysis
Inordertoselectedgesinanondecreasingorder,aminimumheapcanbebuiltandeachsideisremovedfromthepileasneeded.Whenthereisaeedgeinthegraph,ittakestime(E)toinitializetheheapandO(loGE)toselecteachedge.ThesetToftheedge,togetherwiththeverticesintheG,definesagraphcomposedofatmostNconnectedsubgraphs.Vertexsetsareusedtodescribeeachsubgraph,andthesevertexsetshavenocommonvertex.Todeterminewhethertheedges(U,V)generateloops,simplycheckwhethertheUViscenteredonthesamevertex(thatis,inthesamesubgraph).Ifso,aloopwillbeformed;ifnot,theloopwillnotbegenerated.Thus,twoFindoperationsaresufficientforthevertexset.WhenanedgeiscontainedinT,the2subgraphsaremergedintoasubgraph,i.e.,Un,I,O,N,andoperationsonthesetofthetwosets.CollectionsofF,I,N,D,andUn,I,O,andnoperationscanbeefficientlyexecutedusing8.1,0.2nodetrees(andweightedrulesandpathcompression).ThemaximumnumberofFindoperationsis2E,andthenumberofUn,I,O,andnoperationsisatmostn-1(ifthenetworkisconnected,ithappenstoben-1times).Withtheinitializationtimeofthetree,thecomplexityofthispartofthealgorithmisonlyslightlylargerthanthatofO(n+e).
TheonlyoperationperformedonthecollectionTistoaddanewedgetotheT.Tcanbeimplementedusingarrayt.Addoperationsinarray
Oneendoftheline,becauseatmostn-1edgescanbeaddedtotheT,sothetotaloperationtimefortheTisO(n).
Summinguptheexecutiontimeofeachpartoftheabove,theprogressivecomplexityofFig.13-13isO(n+el,O,GE).
(4)implementation
Usingtheabovedatastructure,figure1-13canbeimplementedwithC++code.First,definetheEdgeNodeclass(seeprocedure13-6),whichistheelementoftheminimumheapandthedatatypeofthespanningtreearrayt.
Program13-6.ThedatatyperequiredbytheKruskalalgorithm
Template
ClassEdgeNode{
P,u,B,l,I,c:
Operator,T(),const,{return,weight,}
P,R,I,V,a,t,e:
Tweight;//edgeheight
Intu,V;//edgeendpoint
};
Inordertomoreeasilyusethesearchandmergestrategy8.1section0.2,thedefinitionoftheUnionFind,itistheconstructorinitializationfunctionprogram8-16,Unionistheweightedfunctionprogramof8-16,Findisthepathprogramof8-17thecompressionofthesearchfunction.
Inordertowritecodethatisnotrelatedtonetworkdescription,anewclassUNetrkWoisalsodefined,whichcontainsallfunctionsappliedtoundirectednetworks.ThedifferencebetweenthisclassandtheUndretedclassisthatthefunctionsintheU,N,D,I,e,C,t,e,D,andRclassesdonotrequireweightededges,whiletheUN,e,t,Wo,R,K,andCrequirethattheedgesmusthaveweights.MembersinUNetrkneedtotakeadvantageofthetraversalfunctionsdefinedintheNetorkclasssuchasB,e,G,I,N,andNextVertexwWo.However,thenewtraversalfunctionneedsnotonlytoreturnthenextadjacentvertex,butalsotoreturntheweighttotheedgeofthevertex.Thesetraversalfunctions,alongwithotherfunctionsofdirectedandundirectedweightednetworks,formtheWNetworkclass(seeprocedure13-7).
Program13-7,classWNetwork
Template
ClassWNetwork:
virtual,public,Network
{
Public:
Virtual,void,First(int,I,int&,J,T&,c)=0;
Virtual,void,Next(int,I,int&,J,T&,c)=0;
};
LikeBeginandNetVetex,A,a,C,e,N,C,y,W,D,I,G,R,a,P,x,h,L,D,I,N,K,,e,D,W,D,I,G,a,P,t,Jclasses,R,I,R,x,Hclasses,functionsF,R,s,e,t,andN.NowA,D,J,a,C,N,C,y,D,I,G,R,P,a,handLink,e,D,W,D,I,G,R,a,P,HclassesneedtobederivedfromW,N,e,t,Wo,R,K,W,e.SinceA,D,a,C,e,C,y,W,R,a,P,handLIKEDWGapHRclass,youneedtoaccessUNetworkmembers,sothesetwoclassesmustalsobederivedfromUNentWoRKJGn.UNetWoRKKRUSK:
alprogramcodetosee13-8,itwillrequireEdges()isdefinedasNetWorkvirtualmembers,andtheUNetWoRKEdgeNisdefinedasode'sfriend).Ifthereisnospanningtree,thefunctionreturnsfalse,otherwisetruEisreturned.Notethatwhenyoureturntrue,theminimumcostspanningtreeisreturnedinthearrayt.
Program13-8,Kr,u,s,K,a,l,Calgorithm++code
Template
BoolUNetwork:
:
Kruskal(EdgeNode,t[])
KRUSK{//usingalalgorithmtofindtheminimumcostspanningtree
//ifdisconnectedreturnsfalse
//ifconnected,int[0:
minimumspanningtree2]-returnsn
Intn=Ve,R,t,I,C,e,s();
Inte=Edges();
/setsthearrayontheedgeofthenetwork
(InitializePos);//graphtraversaldevice
EdgeNode*E=new,EdgeNode,[e+1];
Intk=0;//Ecursor
For(inti=1;I Intj; Tc; First(I,J,C); While(J){//JadjacencyfromI If(I E[++k].weight=c; E[k].u=i; E[k].v=J;} Next(I,J,C); } } Theedgeintheminimumheap// MinHeap>H (1); H.Initialize(E,e,e); UnionFindU(n);//merge/searchstructure Accordingtotheordercost//edgeextraction K=0;//thistimeastheTcursor While(E&&KN-<1){ //spanningtreeisnotcomplete,whilestillremaining EdgeNodex; H.DeleteMin(x);//theminimumcostedge E--; Inta=U.Find(x.u); Intb=U.Find(x.v); If(a! =b){//selectedge T[k++]=x; U.U,N,I,O,n(a,b)} } D,e,a,C,t,I,V,a,t,e,P,O,s(); H.D,e,a,C,t,I,V,a,t,e(); Return(k==n-1); } 2.Primalgorithm SimilartotheKrUSKalalgorithm,thePRImalgorithmcreatestheminimumspanningtreebyselectingmultipleedgesatatime.Thegreedycriterionforchoosingthenextedgeistoselectaleastconsumededgefromtheremainingedges,andthatitsentryshouldmakealltheselectededgesstillatree.Eventually,inallthesteps,theselectededgesformatree.Incontrast,intheKruskalalgorithm,allselectededgesetseventuallyformaforest. ThePRImalgorithmstartswiththetreeTwithasinglevertex,whichcanbeanyvertexintheoriginalimage.ThenaddTtoaminimumcostedge(U,V)toT(U,V){}isatree,thestepsrepeatedlyuntiltheborderedTcontains1edgesn-.Notethatfortheedge(U,V),thereisexactlyonevertexinUandVintheT.ThepseudocodeofthePRImalgorithmisshowninfigure1-14.Thepseudocodealsocontainsthepossibilitythattheinputgraphisnotconnected,andinthiscasethereisnospanningtree.Figure1-15showstheprocessofusingthePRImalgorithmforfigure1-12a.Refinethepseudocodeoffigure1-14intotheC++programandproofofitscorrectness.Leaveitforexercise(exercise31). Assumethatthenetworkhasatleastonevertex SetTtothesetoftheselectededges,andinitializeT= LetTVbeacollectionofverticesalreadyinthetree,andsetTV={1} MakeEacollectionofedgesinanetwork While(E<>&&(T)||<>n-1){ Theorder(U,V)istheminimumcostside,whereu,T,V,V,T,V If(withoutthisedge)B,re,a,K E=E-{(U,V)}//deletesthisedgefromE Addingedges(U,V)inT } If(T||==n-T1)isaminimumspanningtree Theelsenetworkisdisconnectedandhasnominimumspanningtree Figure13-14Primminimumspanningtreealgorithm IfyouchooseavertexneAR(V)basedoneachvertexvnotintheTV, 使得NEAR(V)? 电视且COST(V、NEAR(V))的值是所有这样的NEAR(V)节点中最小的,则实现PRIM算法的时间复杂性为O(N2)。 下一条添加到T中的边是这样的边: 其成本(V(V)最小附近),且VTV。 三.算法Sollin SOLLin算法每步选择若干条边。 在每步开始时,所选择的边及图中的N个顶点形成一个生成树的森林。 在每一步中为森林中的每棵树选择一条边,这条边刚好有一个顶点在树中且边的代价最小。 将所选择的边加入要创建的生成树中。 注意一个森林中的两棵树可选择同一条边,因此必须多次复制同一条边。 当有多条边具有相同的耗费时,两棵树可选择与它们相连的不同的边,在这种情况下,必须丢弃其中的一条边。 开始时,所选择的边的集合为空。 若某一步结束时仅剩下一棵树或没有剩余的边可供选择时算法终止。 图1-6给出了初始状态为图1-12a时,使用SOLLin算法的步骤。 初始入选边数为0时的情形如图13-12a时,森林中的每棵树均是单个顶点。 顶点1,2,,7(1.6),所选择的边分别是(2,7),(3,4),(4)、(5)、(6),(7,2),其中不同的边是(1,6),(2,7),(3,4)和(5,4),将这些边加入入选边的集合后所得到的结果如图1316所示。 下一步具有顶点集{1,6}的树选择边(6,5),剩下的两棵树选择边(2,3),加入这两条边后已形成一棵生成树,构建好的生成树见图136BSOLLin算法的C++程序实现及其正确性证明留作练习
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 最小耗费生成树Minimum cost spanning tree 最小 耗费 生成 Minimum