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

L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Embed Size (px)

Citation preview

Page 1: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

6.006IntroductiontoAlgorithms

Lecture15:ShortestPathsIIProf.ErikDemaine

Page 2: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Today• Bellman‐Fordalgorithmforsingle‐sourceshortestpaths

• Runningtime• Correctness• Handlingnegative‐weightcycles• Directedacyclicgraphs

Page 3: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Recall: ShortestPaths•• ifthere’snopathfrom to• ifthere’sapathfrom tothatvisitsanegative‐weightcycle

BA

C

Example:1

2

3

−7

Ds t2

5

−2

3

10

Page 4: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

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

Page 5: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Recall: RelaxationAlgorithm

for in :

whilesomeedge has :picksuchanedgerelax :

if :u

v

s

Page 6: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

RelaxationAlgorithmIssues

• Neverstoprelaxinginagraphwithnegative‐weightcycles:infiniteloop!

• Apoorchoiceofrelaxationordercanleadtoexponentiallymanyrelaxations:

v1 v2 v3 v4 v5 v7v6

8

88

4

4 4 2 2

2 1

1…

0 ∞ ∞ ∞ ∞ ∞ ∞

Page 7: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

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

Page 8: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Bellman‐FordAlgorithm• Relaxationalgorithm• “Smart”orderofedgerelaxations• Labeledges• Relaxinthisorder:

repetitions

Page 9: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

for in :

for from to :for in :relax :if :

Bellman‐FordAlgorithm

u

v

s ,

Page 10: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Bellman‐FordExample

v1 v2 v3 v4 v5 v7v6

8

88

4

4 4 2 2

2 1

1…

0 ∞ ∞ ∞ ∞ ∞ ∞

edgesorderedrighttoleft

Page 11: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Bellman‐FordExample

v1 v2 v3 v4 v5 v7v6

8

88

4

4 4 2 2

2 1

1…

0 ∞ ∞ ∞ ∞ ∞ ∞

edgesorderedlefttoright

Page 12: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Bellman‐FordExample

BA

C

1

2

4

−1

Ds t4

5

−2

3

1∞

0

∞ ∞

edgesorderedtopdown,lefttoright

Page 13: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Bellman‐FordinPractice• Distance‐vectorroutingprotocol– Repeatedlyrelaxedgesuntilconvergence

– Relaxationislocal!• OntheInternet:

– RoutingInformationProtocol(RIP)

– InteriorGatewayRoutingProtocol(IGRP) photobyRossImlach,2011

http://www.flickr.com/photos/rossimlach/5446205998/

Page 14: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

for in :

for from to :for in :relax

for in :if :reportthatanegative‐weightcycleexists

Bellman‐FordAlgorithmwithNegative‐WeightCycleDetection

u

v

s ,

Page 15: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

for in :

for from to :for in :relax

for in :if :reportthatanegative‐weightcycleexists

Bellman‐FordAnalysis

Page 16: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Recall: RelaxingIsSafe• Lemma: Therelaxationalgorithmmaintainstheinvariantthat forall .

• Proof: Byinductiononthenumberofsteps.– Considerrelax– Byinduction,– Bytriangleinequality,

– Sosetting is“safe”

u

v

s

Page 17: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Bellman‐FordCorrectness• Claim: Afteriteration ofBellman‐Ford,

isatmosttheweightofeverypathfromto usingatmost edges,forall .

• Proof: Byinductionon .– Beforeiteration ,– Relaxationonlydecreases ’s remainstrue– Iteration considersallpathswith edgeswhenrelaxing ’sincomingedges

vs

edges

Page 18: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Bellman‐FordCorrectness• Theorem: If hasnonegative‐weightcycles,thenattheendofBellman‐Ford, forall .

• Proof:– Withoutnegative‐weightcycles,shortestpathsarealwayssimple

– Everysimplepathhas vertices,so edges

– Claim iterationsmake– Safety

Page 19: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Bellman‐FordCorrectness• Theorem: Bellman‐Fordcorrectlyreportsnegative‐weightcyclesreachablefrom .

• Proof:– Ifnonegative‐weightcycle,thenprevioustheoremimplies ,andbytriangleinequality,

,soBellman‐Fordwon’tincorrectlyreportanegative‐weightcycle.

– Ifthere’sanegative‐weightcycle,thenoneofitsedgescanalwaysberelaxed(onceoneofitsvaluesbecomesfinite),soBellman‐Fordreports.

Page 20: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

for in :

for from to :for in :relax

for from to :for in :if :

Computing

u

v

s ,

Page 21: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

Correctnessof• Theorem: Afterthealgorithm,forall .

• Proof:– Asarguedbefore,after loop,everynegative‐weightcyclehasarelaxable edge

– Setting takeslimitofrelaxation– Allreachablenodesalsohave– Pathfromoriginal toanyvertex (including )with hasatmost edges

– (Sorelaxationisimpossibleafter loop.)

Page 22: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

WhyDidThisWorkSoWell?

• It’saDAG (directedacyclicgraph)• Wefollowedatopologicalsortedorder

v1 v2 v3 v4 v5 v7v6

8

88

4

4 4 2 2

2 1

1…

0 ∞ ∞ ∞ ∞ ∞ ∞

edgesorderedlefttoright

Page 23: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

ShortestPathsinaDAG• SimplifiedBellman‐Ford:noiteration,nocycles

for in :

topologicallysortthevertices#now infor in :(inorder)

for in :relax

Page 24: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

CorrectnessinDAG• Theorem: InaDAG,thisalgorithmsets

forall .• Proof: Byinductiononrank

– Claimbyinductionthatwhenwehit inouterloop

– Basecase: correct(nocycles)– Whenwehit ,we’vealreadyhitallpreviousvertices,includingallverticeswithedgesinto

– Byinduction,theseverticeshadcorrectvalueswhenwerelaxedtheedgesinto

Page 25: L15 - Shortest Paths II€¢ Bellman‐Ford algorithm for single‐source shortest paths ... Relaxation Algorithm for in : while some edge has : ... By induction on rank

NextUp• Dijkstra’salgorithm

– Relaxedgesinagrowingballaround– Fast:nearlylineartime– Onlyonepassthroughedges,butneedlogarithmictimetopicknextedgetorelax

– Doesn’tworkwithnegativeedgeweights

s