l15 - shortest paths iicourses.csail.mit.edu/6.006/spring11/lectures/lec15.pdf · shortest paths...
TRANSCRIPT
Today• Bellman‐Fordalgorithmforsingle‐sourceshortestpaths
• Runningtime• Correctness• Handlingnegative‐weightcycles• Directedacyclicgraphs
Recall: ShortestPaths•• ifthere’snopathfrom to• ifthere’sapathfrom tothatvisitsanegative‐weightcycle
BA
C
Example:1
2
3
−7
Ds t2
5
−2
3
10
Recall: Single‐SourceShortestPaths
• Problem: Givenadirectedgraphwithedge‐weightfunction ,andasource vertex ,compute forall– Alsowantshortest‐pathtreerepresentedby
Example:BA
C
1
2
3
7
Ds t2
5
−2
3
10
0 1
−2 8
40
RelaxationAlgorithmIssues
• Neverstoprelaxinginagraphwithnegative‐weightcycles:infiniteloop!
• Apoorchoiceofrelaxationordercanleadtoexponentiallymanyrelaxations:
v1 v2 v3 v4 v5 v7v6
8
88
4
4 4 2 2
2 1
1…
0 ∞ ∞ ∞ ∞ ∞ ∞
Bellman&Ford
RichardE.Bellman(1920–1984)
IEEEMedalofHonor,1979
LesterR.Ford,Jr.(1927–)
presidentofMAA,1947–48http://www.amazon.com/Bellman‐Continuum‐Collection‐Works‐Richard/dp/9971500906 http://www.maa.org/aboutmaa/maaapresidents.html
Bellman‐FordAlgorithm• Relaxationalgorithm• “Smart”orderofedgerelaxations• Labeledges• Relaxinthisorder:
repetitions
Bellman‐FordinPractice• Distance‐vectorroutingprotocol– Repeatedlyrelaxedgesuntilconvergence
– Relaxationislocal!• OntheInternet:
– RoutingInformationProtocol(RIP)
– InteriorGatewayRoutingProtocol(IGRP) photobyRossImlach,2011
http://www.flickr.com/photos/rossimlach/5446205998/
for in :
for from to :for in :relax
for in :if :reportthatanegative‐weightcycleexists
Bellman‐FordAlgorithmwithNegative‐WeightCycleDetection
u
v
s ,
for in :
for from to :for in :relax
for in :if :reportthatanegative‐weightcycleexists
Bellman‐FordAnalysis
Recall: RelaxingIsSafe• Lemma: Therelaxationalgorithmmaintainstheinvariantthat forall .
• Proof: Byinductiononthenumberofsteps.– Considerrelax– Byinduction,– Bytriangleinequality,
– Sosetting is“safe”
u
v
s
Bellman‐FordCorrectness• Claim: Afteriteration ofBellman‐Ford,
isatmosttheweightofeverypathfromto usingatmost edges,forall .
• Proof: Byinductionon .– Beforeiteration ,– Relaxationonlydecreases ’s remainstrue– Iteration considersallpathswith edgeswhenrelaxing ’sincomingedges
vs
edges
Bellman‐FordCorrectness• Theorem: If hasnonegative‐weightcycles,thenattheendofBellman‐Ford, forall .
• Proof:– Withoutnegative‐weightcycles,shortestpathsarealwayssimple
– Everysimplepathhas vertices,so edges
– Claim iterationsmake– Safety
Bellman‐FordCorrectness• Theorem: Bellman‐Fordcorrectlyreportsnegative‐weightcyclesreachablefrom .
• Proof:– Ifnonegative‐weightcycle,thenprevioustheoremimplies ,andbytriangleinequality,
,soBellman‐Fordwon’tincorrectlyreportanegative‐weightcycle.
– Ifthere’sanegative‐weightcycle,thenoneofitsedgescanalwaysberelaxed(onceoneofitsvaluesbecomesfinite),soBellman‐Fordreports.
Correctnessof• Theorem: Afterthealgorithm,forall .
• Proof:– Asarguedbefore,after loop,everynegative‐weightcyclehasarelaxable edge
– Setting takeslimitofrelaxation– Allreachablenodesalsohave– Pathfromoriginal toanyvertex (including )with hasatmost edges
– (Sorelaxationisimpossibleafter loop.)
WhyDidThisWorkSoWell?
• It’saDAG (directedacyclicgraph)• Wefollowedatopologicalsortedorder
v1 v2 v3 v4 v5 v7v6
8
88
4
4 4 2 2
2 1
1…
0 ∞ ∞ ∞ ∞ ∞ ∞
edgesorderedlefttoright
ShortestPathsinaDAG• SimplifiedBellman‐Ford:noiteration,nocycles
for in :
topologicallysortthevertices#now infor in :(inorder)
for in :relax
CorrectnessinDAG• Theorem: InaDAG,thisalgorithmsets
forall .• Proof: Byinductiononrank
– Claimbyinductionthatwhenwehit inouterloop
– Basecase: correct(nocycles)– Whenwehit ,we’vealreadyhitallpreviousvertices,includingallverticeswithedgesinto
– Byinduction,theseverticeshadcorrectvalueswhenwerelaxedtheedgesinto