6 006 Introduction to Algorithms Lecture 14 Shortest Paths I Prof Erik Demaine Today Shortest paths Negative weight cycles Triangle inequality Relaxation algorithm Optimal substructure Shortest Paths Shortest Paths How Long Is Your Path Directed graph Edge weight function Path Weight of denoted Example v1 4 is 2 v2 v3 5 1 v4 v5 My Path Is Shorter Than Yours A shortest path from to is a path of from to minimum possible weight The shortest path weight from to is the weight of any such shortest path A Example 1 B 2 4 C A 7 3 D 1 B 2 4 C 7 3 D 6 You Can t Get There From Here You 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 case Example A 1 B 2 4 C 7 3 D The 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 has Example A 1 B 7 2 C 3 D The More I Walk The Less It Takes Define if there s a path from to that visits a negative weight cycle Example A 5 s B 2 2 2 1 C 3 7 3 D t 10 Single Source Shortest Paths Problem Given a directed graph with edge weight function and a for all source vertex compute Example 0 A 5 0 s B 2 2 2 1 1 C 2 3 7 3 D 8 t 10 4 Shortest Path Tree Ideally also compute a shortest path tree containing a shortest path from source to assuming shortest paths exist every Represent by storing parent Example 0 A 5 0 s 1 B 2 2 2 1 C 2 for each 3 7 3 D 8 t 10 4 Shortest Path Trees bicycle travel in Seattle area Ideally also compute the union of one shortest path from source to every which forms the shortest path tree bicycle travel in San Francisco area Brandon Martin Anderson Nino Walker http graphserver sourceforge net gallery html Single 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 Fast nearly linear time Requires nonnegative weights LECTURE 16 Brute Force Algorithm return encountered with smallest Number of paths can be infinite A 5 s B 2 2 2 1 C 3 7 3 D t 10 Brute Force Algorithm assume no negative weight cycles return encountered with smallest Number of paths can be exponential s v2 v1 vn t paths from to vertices and edges Relaxation In general refers to letting a solution temporarily violate a constraint and trying to fix these violations Magic Geek http www youtube com watch v Y12daEZTUYo Triangle Inequality Theorem For all we have u v x Proof Shortest path from to is at most any particular path e g the blue chain Relaxation Approach Maintain distance estimate for each Goal for all Invariant Initialization in Repeatedly improve estimates toward goal by aiming to achieve triangle inequality Edge Relaxation Consider an edge u s v triangle ineq candidate path Relaxation Algorithm for in while some edge has pick such an edge relax if u s v Relaxation Algorithm with Shortest Path Tree for in while some edge has pick such an edge relax if u s v Relaxing 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 u s v So setting is safe Infinite Relaxation If a negative weight cycle is reachable from then relaxation can never terminate A 5 0 s B 2 2 2 1 C 3 7 3 D t 10 Long Relaxation 4 8 v1 0 8 v2 8 v3 4 v4 2 4 v5 2 v6 1 2 v7 1 Long Relaxation 4 8 v1 0 8 v2 8 v3 4 v4 2 4 v5 2 v6 1 2 Analysis relax relax recurse on relax recurse on v7 1 Are You Sure This Is a Good Idea Bellman Ford algorithm LECTURE 15 Relax all of the edges times Repeat 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 u x y v 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
Unlocking...