Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
6.006IntroductiontoAlgorithms
Lecture14:ShortestPathsIProf.ErikDemaine
Today• Shortestpaths• Negative‐weightcycles• Triangleinequality• Relaxationalgorithm• Optimalsubstructure
ShortestPaths
ShortestPaths
HowLongIsYourPath?• Directedgraph• Edge‐weightfunction• Path• Weight of ,denoted ,is
v1v2
v3v4
v54 −2 −5 1Example:
MyPathIsShorterThanYours
• Ashortestpathfrom to isapath ofminimumpossibleweight from to
• Theshortest‐pathweight from toistheweightofanysuchshortestpath:
BA
DC
Example:1
4
3
72BA
DC
1
4
3
72 ,6
YouCan’tGetThereFromHere
YouCan’tGetThereFromHere
• Ifthereisnopathfrom to ,thenneitheristhereashortest pathfrom to
• Define inthiscase
BA
C
Example: 1
4 2
3
7
D
TheMoreIWalk,TheLessItTakes
• Ashortestpathfrom to mightnotexist,eventhoughthereisapathfrom to
• Negative‐weightcycle
has
BA
C
Example: 1
2
3
−7
D
TheMoreIWalk,TheLessItTakes
• Define ifthere’sapathfromto thatvisitsanegative‐weightcycle
•
BA
C
Example:1
2
3
−7
Ds t2
5
−2
3
10
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
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
Shortest‐PathTrees
• Ideallyalsocomputetheunionofoneshortestpathfromsource toevery ,whichformstheshortest‐pathtree
bicycletravelinSanFranciscoarea
bicycletravelinSeattlearea
BrandonMartin‐Anderson&NinoWalker
http://graphserver.sourceforge.net/gallery.html
Single‐SourceShortest‐PathAlgorithms
• Relaxationalgorithm (TODAY)– Frameworkformostshortest‐pathalgorithms– Notnecessarilyefficient
• Bellman‐Fordalgorithm (LECTURE15)– Dealswithnegativeweights– Slowbutpolynomial
• Dijkstra’s algorithm (LECTURE16)– Fast(nearlylineartime)– Requiresnonnegativeweights
Brute‐ForceAlgorithm
• Numberofpathscanbeinfinite:
return encountered with smallest
BA
C
1
2
3
−7
Ds t2
5
−2
3
10
Brute‐ForceAlgorithm
• Numberofpathscanbeexponential:
##assumenonegative‐weightcycles
return encountered with smallest
s vnv1 v2 …=t
pathsfrom to ; verticesandedges
Relaxation• Ingeneral,referstolettingasolution(temporarily)violateaconstraint,andtryingtofixtheseviolations
Magic Geekhttp://www.youtube.com/
watch?v=Y12daEZTUYo
TriangleInequality• Theorem: Forall ,wehave
• Proof: Shortestpathfrom to isatmostanyparticularpath,e.g.,thebluechain.
u
x
v
RelaxationApproach• Maintaindistanceestimateforeach
• Goal: forall• Invariant:• Initialization:
• Repeatedlyimproveestimatestowardgoal,byaimingtoachievetriangleinequality
in :
EdgeRelaxation• Consideranedge
• [triangleineq.][candidatepath]
u
v
s
:
RelaxationAlgorithmfor in :
whilesomeedge has :picksuchanedgerelax :
if :u
v
s
RelaxationAlgorithmwithShortest‐PathTree
for in :
whilesomeedge has :picksuchanedgerelax :
if :u
v
s
RelaxingIsSafe• Lemma: Therelaxationalgorithmmaintainstheinvariantthat forall .
• Proof: Byinductiononthenumberofsteps.– Considerrelax– Byinduction,– Bytriangleinequality,
– Sosetting is“safe”
u
v
s
InfiniteRelaxation• Ifanegative‐weightcycleisreachablefrom ,thenrelaxationcanneverterminate
BA
C
1
2
3
−7
Ds t2
5
−2
3
10
0
∞ ∞
∞ ∞
∞
LongRelaxation
v1 v2 v3 v4 v5 v7v6
8
88
4
4 4 2 2
2 1
1…
0 ∞ ∞ ∞ ∞ ∞ ∞
LongRelaxation
• Analysis:– relax– relax– recurseon– relax– recurseon
v1 v2 v3 v4 v5 v7v6
8
88
4
4 4 2 2
2 1
1…
0 ∞ ∞ ∞ ∞ ∞ ∞
/
AreYouSureThisIsaGoodIdea?
• Bellman‐Fordalgorithm: (LECTURE15)– Relaxalloftheedges– Repeat times– Polynomialtime!
• Dijkstra’s algorithm: (LECTURE16)– Relaxedgesinagrowingballaround– Nearlylineartime!– (butdoesn’tworkwithnegativeedgeweights)
OptimalSubstructure• Lemma: Asubpath ofashortestpathisashortestpath(betweenitsendpoints).
• Proof: Bycontradiction.– Iftherewereashorterpathfrom to ,thenwecouldshortcut thepathfrom to ,contradictingthatwehadashortestpath.
u x y v