28
6.006 Introduction to Algorithms Lecture 14: Shortest Paths I Prof. Erik Demaine

Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

6.006IntroductiontoAlgorithms

Lecture14:ShortestPathsIProf.ErikDemaine

Page 2: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

Today• Shortestpaths• Negative‐weightcycles• Triangleinequality• Relaxationalgorithm• Optimalsubstructure

Page 3: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

ShortestPaths

Page 4: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

ShortestPaths

Page 5: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

HowLongIsYourPath?• Directedgraph• Edge‐weightfunction• Path• Weight of ,denoted ,is

v1v2

v3v4

v54 −2 −5 1Example:

Page 6: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

MyPathIsShorterThanYours

• Ashortestpathfrom to isapath ofminimumpossibleweight from to

• Theshortest‐pathweight from toistheweightofanysuchshortestpath:

BA

DC

Example:1

4

3

72BA

DC

1

4

3

72 ,6

Page 7: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

YouCan’tGetThereFromHere

Page 8: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

YouCan’tGetThereFromHere

• Ifthereisnopathfrom to ,thenneitheristhereashortest pathfrom to

• Define inthiscase

BA

C

Example: 1

4 2

3

7

D

Page 9: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

TheMoreIWalk,TheLessItTakes

• Ashortestpathfrom to mightnotexist,eventhoughthereisapathfrom to

• Negative‐weightcycle

has

BA

C

Example: 1

2

3

−7

D

Page 10: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

TheMoreIWalk,TheLessItTakes

• Define ifthere’sapathfromto thatvisitsanegative‐weightcycle

BA

C

Example:1

2

3

−7

Ds t2

5

−2

3

10

Page 11: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

Single‐SourceShortestPaths• Problem: Givenadirectedgraphwithedge‐weightfunction ,andasource vertex ,compute forall

BA

C

Example:1

2

3

7

Ds t2

5

−2

3

10

0 1

−2 8

40

Page 12: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

Shortest‐PathTree• Ideallyalsocomputeashortest‐pathtreecontainingashortestpathfromsource toevery (assumingshortestpathsexist)– Representbystoringparent foreach

BA

C

Example:1

2

3

7

Ds t2

5

−2

3

10

0 1

−2 8

40

Page 13: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

Shortest‐PathTrees

• Ideallyalsocomputetheunionofoneshortestpathfromsource toevery ,whichformstheshortest‐pathtree

bicycletravelinSanFranciscoarea

bicycletravelinSeattlearea

BrandonMartin‐Anderson&NinoWalker

http://graphserver.sourceforge.net/gallery.html

Page 14: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

Single‐SourceShortest‐PathAlgorithms

• Relaxationalgorithm (TODAY)– Frameworkformostshortest‐pathalgorithms– Notnecessarilyefficient

• Bellman‐Fordalgorithm (LECTURE15)– Dealswithnegativeweights– Slowbutpolynomial

• Dijkstra’s algorithm (LECTURE16)– Fast(nearlylineartime)– Requiresnonnegativeweights

Page 15: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

Brute‐ForceAlgorithm

• Numberofpathscanbeinfinite:

return encountered with smallest

BA

C

1

2

3

−7

Ds t2

5

−2

3

10

Page 16: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

Brute‐ForceAlgorithm

• Numberofpathscanbeexponential:

##assumenonegative‐weightcycles

return encountered with smallest

s vnv1 v2 …=t

pathsfrom to ; verticesandedges

Page 17: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

Relaxation• Ingeneral,referstolettingasolution(temporarily)violateaconstraint,andtryingtofixtheseviolations

Magic Geekhttp://www.youtube.com/

watch?v=Y12daEZTUYo

Page 18: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

TriangleInequality• Theorem: Forall ,wehave

• Proof: Shortestpathfrom to isatmostanyparticularpath,e.g.,thebluechain.

u

x

v

Page 19: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

RelaxationApproach• Maintaindistanceestimateforeach

• Goal: forall• Invariant:• Initialization:

• Repeatedlyimproveestimatestowardgoal,byaimingtoachievetriangleinequality

in :

Page 20: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

EdgeRelaxation• Consideranedge

• [triangleineq.][candidatepath]

u

v

s

:

Page 21: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

RelaxationAlgorithmfor in :

whilesomeedge has :picksuchanedgerelax :

if :u

v

s

Page 22: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

RelaxationAlgorithmwithShortest‐PathTree

for in :

whilesomeedge has :picksuchanedgerelax :

if :u

v

s

Page 23: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

RelaxingIsSafe• Lemma: Therelaxationalgorithmmaintainstheinvariantthat forall .

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

– Sosetting is“safe”

u

v

s

Page 24: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

InfiniteRelaxation• Ifanegative‐weightcycleisreachablefrom ,thenrelaxationcanneverterminate

BA

C

1

2

3

−7

Ds t2

5

−2

3

10

0

∞ ∞

∞ ∞

Page 25: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

LongRelaxation

v1 v2 v3 v4 v5 v7v6

8

88

4

4 4 2 2

2 1

1…

0 ∞ ∞ ∞ ∞ ∞ ∞

Page 26: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

LongRelaxation

• Analysis:– relax– relax– recurseon– relax– recurseon

v1 v2 v3 v4 v5 v7v6

8

88

4

4 4 2 2

2 1

1…

0 ∞ ∞ ∞ ∞ ∞ ∞

/

Page 27: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

AreYouSureThisIsaGoodIdea?

• Bellman‐Fordalgorithm: (LECTURE15)– Relaxalloftheedges– Repeat times– Polynomialtime!

• Dijkstra’s algorithm: (LECTURE16)– Relaxedgesinagrowingballaround– Nearlylineartime!– (butdoesn’tworkwithnegativeedgeweights)

Page 28: Prof. Erik DemaineMy Path Is Shorter Than Yours • A shortest path from to is a path of minimum possible weight from to • The shortest‐path weight from to is the weight of any

OptimalSubstructure• Lemma: Asubpath ofashortestpathisashortestpath(betweenitsendpoints).

• Proof: Bycontradiction.– Iftherewereashorterpathfrom to ,thenwecouldshortcut thepathfrom to ,contradictingthatwehadashortestpath.

u x y v