25
6.006 Introduction to Algorithms Lecture 15: Shortest Paths II Prof. Erik Demaine

L15 - Shortest Paths IIcourses.csail.mit.edu/6.006/spring11/lectures/lec15.pdf · Shortest Paths • Problem:Given a directed graph with edge‐weight function , and a sourcevertex

Embed Size (px)

Citation preview

6.006IntroductiontoAlgorithms

Lecture15:ShortestPathsIIProf.ErikDemaine

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

Recall: RelaxationAlgorithm

for in :

whilesomeedge has :picksuchanedgerelax :

if :u

v

s

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

for in :

for from to :for in :relax :if :

Bellman‐FordAlgorithm

u

v

s ,

Bellman‐FordExample

v1 v2 v3 v4 v5 v7v6

8

88

4

4 4 2 2

2 1

1…

0 ∞ ∞ ∞ ∞ ∞ ∞

edgesorderedrighttoleft

Bellman‐FordExample

v1 v2 v3 v4 v5 v7v6

8

88

4

4 4 2 2

2 1

1…

0 ∞ ∞ ∞ ∞ ∞ ∞

edgesorderedlefttoright

Bellman‐FordExample

BA

C

1

2

4

−1

Ds t4

5

−2

3

1∞

0

∞ ∞

edgesorderedtopdown,lefttoright

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.

for in :

for from to :for in :relax

for from to :for in :if :

Computing

u

v

s ,

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

NextUp• Dijkstra’salgorithm

– Relaxedgesinagrowingballaround– Fast:nearlylineartime– Onlyonepassthroughedges,butneedlogarithmictimetopicknextedgetorelax

– Doesn’tworkwithnegativeedgeweights

s