6.006Introduction to AlgorithmsLecture 14: Shortest Paths IProf. Erik DemaineToday• Shortest paths• Negative‐weight cycles• Triangle inequality• Relaxation algorithm• Optimal substructureShortest PathsShortest PathsHow Long Is Your Path?• Directed graph • Edge‐weight function • Path • Weight of , denoted , is v1v2v3v4v54−2 −51Example:My 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 such shortest path:BADCExample:14372BADC14372 , 6You Can’t Get There From HereYou Can’t Get There From Here• If there is no path from to , then neither is there a shortest path from to • Define in this caseBACExample:14237DThe More I Walk,The Less It Takes• A shortest path from to might not exist, even though there is a path from to • Negative‐weight cycle hasBACExample:123−7DThe More I Walk,The Less It Takes• Define if there’s a path from to that visits a negative‐weight cycle •BACExample:123−7Dst25−2310Single‐Source Shortest Paths• Problem: Given a directed graph with edge‐weight function , and a source vertex , compute for all BACExample:1237Dst25−231001−2840Shortest‐Path Tree• Ideally also compute a shortest‐path treecontaining a shortest path from source to every (assuming shortest paths exist)– Represent by storing parent for each BACExample:1237Dst25−231001−2840Shortest‐Path Trees• Ideally also compute the union of one shortest path from source to every , which forms the shortest‐path treebicycle travel inSan Francisco areabicycle travel inSeattle areaBrandon Martin‐Anderson& Nino Walkerhttp://graphserver.sourceforge.net/gallery.htmlSingle‐Source Shortest ‐Path Algorithms• Relaxation algorithm (TODAY)– Framework for most shortest‐path algorithms– Not necessarily efficient• Bellman‐Ford algorithm (LECTURE 15)– Deals with negative weights– Slow but polynomial• Dijkstra’s algorithm (LECTURE 16)– Fast (nearly linear time)– Requires nonnegative weightsBrute‐Force Algorithm• Number of paths can be infinite:return encountered with smallest BAC123−7Dst25−2310Brute‐Force Algorithm• Number of paths can be exponential:## assume no negative‐weight cyclesreturn encountered with smallest svnv1v2…= tpaths from to ; vertices and edgesRelaxation• In general, refers to letting a solution (temporarily) violate a constraint, and trying to fix these violationsMagic Geekhttp://www.youtube.com/ watch?v=Y12daEZTUYoTriangle Inequality• Theorem: For all , we have• Proof: Shortest path from to is at most any particular path, e.g., the blue chain. uxvRelaxation Approach• Maintain distance estimatefor each • Goal: for all • Invariant:• Initialization:• Repeatedly improve estimates toward goal, by aiming to achieve triangle inequalityin :Edge Relaxation• Consider an edge • [triangle ineq.][candidate path]uvs:Relaxation Algorithmfor in :while some edge has :pick such an edge relax :if :uvsRelaxation Algorithmwith Shortest‐Path Treefor in :while some edge has :pick such an edge relax :if :uvsRelaxing Is Safe• Lemma: The relaxation algorithm maintains the invariant that for all .• Proof: By induction on the number of steps.– Consider relax– By induction, – By triangle inequality,– So setting is “safe” uvsInfinite Relaxation• If a negative‐weight cycle is reachable from , then relaxation can never terminateBAC123−7Dst25−23100∞∞∞∞∞Long Relaxationv1v2v3v4v5v7v688844422211…0 ∞∞∞∞∞∞Long Relaxation• Analysis:– relax – relax – recurse on – relax – recurse on v1v2v3v4v5v7v688844422211…0 ∞∞∞∞∞∞/Are You SureThis Is a Good Idea?• Bellman‐Ford algorithm: (LECTURE 15)– Relax all of the edges– Repeat times– Polynomial time!• Dijkstra’s algorithm: (LECTURE 16)– Relax edges in a growing ball around – Nearly linear time!– (but doesn’t work with negative edge weights)Optimal Substructure• Lemma: A subpath of a shortest path is a shortest path (between its endpoints).• Proof: By contradiction.– If there were a shorter path from to ,then we could shortcut the path from to , contradicting that we had a shortest path.
View Full Document