欢迎来到天天文库
浏览记录
ID:40384302
大小:693.88 KB
页数:17页
时间:2019-08-01
《Ch9.3-4 Shortest-Path Algorithm, Network Flow Problem 》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、§3ShortestPathAlgorithmsGivenadigraphG=(V,E),andacostfunctionc(e)foreE(G).ThelengthofapathPfromsourcetodestinationis(alsocalledc(ei)weightedpathlength).eiP1.Single-SourceShortest-PathProblemGivenasinputaweightedgraph,G=(V,E),andadistinguishedvertex,s,findtheshortestweightedpathfromstoever
2、yothervertexinG.Negative-cost22cyclev1v2v1v2Note:Ifthereisno4131041–103negative-costcycle,2222v3v4v5v3v4v5theshortestpath84845656fromstosis11definedtobezero.v6v7v6v71/17§3ShortestPathAlgorithmsUnweightedShortestPathsSketchoftheidea1vv20:v31221:v1andv6Breadth-first0vvv33452:vandvsearch24
3、1v6v733:vandv57ImplementationTable[i].Dist::=distancefromstov/*initializedtobeexceptifors*/Table[i].Known::=1ifvischecked;or0ifnotiTable[i].Path::=fortrackingthepath/*initializedtobe0*/2/17§3ShortestPathAlgorithmsvoidUnweighted(TableT){intCurrDist;VertexV,W;for(CurrDist=0;CurrDist4、ex;CurrDist++){for(eachvertexV)if(!T[V].Known&&T[V].Dist==CurrDist){T[V].Known=true;for(eachWadjacenttoV)IfVisunknownif(T[W].Dist==Infinity){yethasDist5、end-forCurrDist*/}T=O(6、V7、2)Theworstcase:v9v8v7v6v5v4v3v2v13/17§3ShortestPathAlgorithmsImprovement12voidUnweighted(TableT)v1v2{/*TisinitializedwiththesourcevertexSgiven*/023QueueQ;v3v4v5VertexV,W;v6v7Q=CreateQueue(NumVertex);MakeEmpty(Q);13Enqueue(S,Q);/*Enqueuethesourcevertex*/while(!IsEmp8、ty(Q)){V=Dequeue(Q);DistPathT[V].Known=true;/*notreallynecessary*/v11v03v7for(eachWadjacenttoV)v22v01v5if(T[W].Dist==Infinity){v300T[W].Dist=T[V].Dist+1;v4v42v01T[W].Path=V;vEnqueue(W,Q);v53v022}/*end-ifDist==Infinity*/v61v03v6}/*end-while*/v73v04v31DisposeQueue(Q);/*freememory*/}T=O(9、10、V11、+12、E13、)4/17§3ShortestPathAlgorithmsDijkstra’sAlgorithm(forweightedshortestpaths)LetS={sandv’swhoseshortestpathshavebeenfound}iForanyuS,definedistance[u]=minimallengthofpath{s(viS)u}.Ifthepathsaregeneratedinnon-decreasingorder,thentheshortestp
4、ex;CurrDist++){for(eachvertexV)if(!T[V].Known&&T[V].Dist==CurrDist){T[V].Known=true;for(eachWadjacenttoV)IfVisunknownif(T[W].Dist==Infinity){yethasDist5、end-forCurrDist*/}T=O(6、V7、2)Theworstcase:v9v8v7v6v5v4v3v2v13/17§3ShortestPathAlgorithmsImprovement12voidUnweighted(TableT)v1v2{/*TisinitializedwiththesourcevertexSgiven*/023QueueQ;v3v4v5VertexV,W;v6v7Q=CreateQueue(NumVertex);MakeEmpty(Q);13Enqueue(S,Q);/*Enqueuethesourcevertex*/while(!IsEmp8、ty(Q)){V=Dequeue(Q);DistPathT[V].Known=true;/*notreallynecessary*/v11v03v7for(eachWadjacenttoV)v22v01v5if(T[W].Dist==Infinity){v300T[W].Dist=T[V].Dist+1;v4v42v01T[W].Path=V;vEnqueue(W,Q);v53v022}/*end-ifDist==Infinity*/v61v03v6}/*end-while*/v73v04v31DisposeQueue(Q);/*freememory*/}T=O(9、10、V11、+12、E13、)4/17§3ShortestPathAlgorithmsDijkstra’sAlgorithm(forweightedshortestpaths)LetS={sandv’swhoseshortestpathshavebeenfound}iForanyuS,definedistance[u]=minimallengthofpath{s(viS)u}.Ifthepathsaregeneratedinnon-decreasingorder,thentheshortestp
5、end-forCurrDist*/}T=O(
6、V
7、2)Theworstcase:v9v8v7v6v5v4v3v2v13/17§3ShortestPathAlgorithmsImprovement12voidUnweighted(TableT)v1v2{/*TisinitializedwiththesourcevertexSgiven*/023QueueQ;v3v4v5VertexV,W;v6v7Q=CreateQueue(NumVertex);MakeEmpty(Q);13Enqueue(S,Q);/*Enqueuethesourcevertex*/while(!IsEmp
8、ty(Q)){V=Dequeue(Q);DistPathT[V].Known=true;/*notreallynecessary*/v11v03v7for(eachWadjacenttoV)v22v01v5if(T[W].Dist==Infinity){v300T[W].Dist=T[V].Dist+1;v4v42v01T[W].Path=V;vEnqueue(W,Q);v53v022}/*end-ifDist==Infinity*/v61v03v6}/*end-while*/v73v04v31DisposeQueue(Q);/*freememory*/}T=O(
9、
10、V
11、+
12、E
13、)4/17§3ShortestPathAlgorithmsDijkstra’sAlgorithm(forweightedshortestpaths)LetS={sandv’swhoseshortestpathshavebeenfound}iForanyuS,definedistance[u]=minimallengthofpath{s(viS)u}.Ifthepathsaregeneratedinnon-decreasingorder,thentheshortestp
此文档下载收益归作者所有