Shortest Paths Definitions Single Source Algorithms

Slides:



Advertisements
Similar presentations
Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China
Advertisements

Single Source Shortest Paths
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
CS138A Single Source Shortest Paths Peter Schröder.
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
Chapter 25: All-Pairs Shortest-Paths
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
CS420 lecture twelve Shortest Paths wim bohm cs csu.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Path Algorithms
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
All-Pairs Shortest Paths
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Topological Sorting and Least-cost Path Algorithms.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Minimum weight spanning trees
Introduction to Algorithms Jiafen Liu Sept
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Lecture 13 Algorithm Analysis
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
All-Pairs Shortest Paths (26.0/25)
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Announcement 2: A 2 hour midterm (open book) will be given on March (Tuesday) during the lecture time. 2018/12/4.
Page 620 Single-Source Shortest Paths
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Chapter 24: Single-Source Shortest Paths
Graph Algorithms: Shortest Path
Chapter 24: Single-Source Shortest Paths
CS 3013: DS & Algorithms Shortest Paths.
Chapter 24: Single-Source Shortest-Path
Topological Sorting Minimum Spanning Trees Shortest Path
Presentation transcript:

Shortest Paths Definitions Single Source Algorithms Bellman Ford DAG shortest path algorithm Dijkstra All Pairs Algorithms Using Single Source Algorithms Matrix multiplication Floyd-Warshall Both of above use adjacency matrix representation and dynamic programming Johnson’s algorithm Uses adjacency list representation

Single Source Definition Input Weighted, connected directed graph G=(V,E) Weight (length) function w on each edge e in E Source node s in V Task Compute a shortest path from s to all nodes in V

All Pairs Definition Input Task Weighted, connected directed graph G=(V,E) Weight (length) function w on each edge e in E We will typically assume w is represented as a matrix Task Compute a shortest path from all nodes in V to all nodes in V

Comments If edges are not weighted, then BFS works for single source problem Optimal substructure A shortest path between s and t contains other shortest paths within it No known algorithm is better at finding a shortest path from s to a specific destination node t in G than finding the shortest path from s to all nodes in V

Negative weight edges Negative weight edges can be allowed as long as there are no negative weight cycles If there are negative weight cycles, then there cannot be a shortest path from s to any node t (why?) If we disallow negative weight cycles, then there always is a shortest path that contains no cycles

Relaxation technique For each vertex v, we maintain an upper bound d[v] on the weight of shortest path from s to v d[v] initialized to infinity Relaxing an edge (u,v) Can we improve the shortest path to v by going through u? If d[v] > d[u] + w(u,v), d[v] = d[u] + w(u,v) This can be done in O(1) time

Bellman-Ford Algorithm 3 5 7 -5 2 1 s Bellman-Ford (G, w, s) Initialize-Single-Source(G,s) for (i=1 to V-1) for each edge (u,v) in E relax(u,v); if d[v] > d[u] + w(u,v) return NEGATIVE WEIGHT CYCLE 3 5 -1 -5 2 1 s

Running Time for (i=1 to V-1) The above takes (V-1)O(E) = O(VE) time for each edge (u,v) in E relax(u,v); The above takes (V-1)O(E) = O(VE) time if d[v] > d[u] + w(u,v) return NEGATIVE WEIGHT CYCLE The above takes O(E) time

Proof of Correctness If there is a shortest path from s to any node v, then d[v] will have this weight at end Let p = (e1, e2, …, ek) = (v1, v2, v3, …, v,+1) be a shortest path from s to v s = v1, v = vk+1, ei = (vi, vi+1) At beginning of ith iteration, during which we relax edge ei, d[vi] must be correct, so at end of ith iteration, d[vi+1] will e correct Iteration is the full sweep of relaxing all edges Proof by induction

Negative weight cycle for each edge (u,v) in E if d[v] > d[u] + w(u,v) return NEGATIVE WEIGHT CYCLE If no neg weight cycle, d[v] ≤ d[u] + w(u,v) for all (u,v) If there is a negative weight cycle C, for some edge (u,v) on C, it must be the case that d[v] > d[u] + w(u,v). Suppose this is not true for some neg. weight cycle C sum these (d[v] ≤ d[u] + w(u,v)) all the way around C We end up with sum(d[v]) ≤ sum(d[u]) + weight(C) This is impossible unless weight(C) = 0 But weight(C) is negative, so this cannot happen Thus for some (u,v) on C, d[v] > d[u] + w(u,v)

DAG shortest path algorithm 5 DAG-SP (G, w, s) Initialize-Single-Source(G,s) Topologically sort vertices in G for each vertex u, taken in sorted order for each edge (u,v) in E relax(u,v); 3 4 -4 s 1 5 3 8

Running time Improvement O(V+E) for the topological sorting We only do 1 relaxation for each edge: O(E) time for each vertex u, taken in sorted order for each edge (u,v) in E relax(u,v); Overall running time: O(V+E)

Proof of Correctness If there is a shortest path from s to any node v, then d[v] will have this weight at end Let p = (e1, e2, …, ek) = (v1, v2, v3, …, v,+1) be a shortest path from s to v s = v1, v = vk+1, ei = (vi, vi+1) Since we sort edges in topological order, we will process node vi (and edge ei) before processing later edges in the path.

Dijkstra’s Algorithm Dijkstra (G, w, s) /* Assumption: all edge weights non-negative */ Initialize-Single-Source(G,s) Completed = {}; ToBeCompleted = V; While ToBeCompleted is not empty u =EXTRACT-MIN(ToBeCompleted); Completed += {u}; for each edge (u,v) relax(u,v); 15 5 7 4 3 2 1 s

Running Time Analysis While ToBeCompleted is not empty u =EXTRACT-MIN(ToBeCompleted); Completed += {u}; for each edge (u,v) relax(u,v); Each edge relaxed at most once: O(E) Need to decrease-key potentially once per edge Need to extract-min once per node

Running Time Analysis cont’d O(E) edge relaxations Priority Queue operations O(E) decrease key operations O(V) extract-min operations Three implementations of priority queues Array: O(V2) time decrease-key is O(1) and extract-min is O(V) Binary heap: O(E log V) time assuming E >= V decrease-key and extract-min are O(log V) Fibonacci heap: O(V log V + E) time decrease-key is O(1) amortized time and extract-min is O(log V)

Compare Dijkstra’s algorithm to Prim’s algorithm for MST Dijsktra Priority Queue operations O(E) decrease key operations O(V) extract-min operations Prim O(E) decrease-key operations Is this a coincidence or is there something more here?

Proof of Correctness s u v Only nodes in S Assume that Dijkstra’s algorithm fails to compute length of all shortest paths from s Let v be the first node whose shortest path length is computed incorrectly Let S be the set of nodes whose shortest paths were computed correctly by Dijkstra prior to adding v to the processed set of nodes. Dijkstra’s algorithm has used the shortest path from s to v using only nodes in S when it added v to S. The shortest path to v must include one node not in S Let u be the first such node. s u v Only nodes in S

Proof of Correctness The length of the shortest path to u must be at least that of the length of the path computed to v. Why? The length of the path from u to v must be < 0. No path can have negative length since all edge weights are non-negative, and thus we have a contradiction. s u v Only nodes in S

Computing paths (not just distance) Maintain for each node v a predecessor node p(v) p(v) is initialized to be null Whenever an edge (u,v) is relaxed such that d(v) improves, then p(v) can be set to be u Paths can be generated from this data structure

All pairs algorithms using single source algorithms Call a single source algorithm from each vertex s in V O(V X) where X is the running time of the given algorithm Dijkstra linear array: O(V3) Dijkstra binary heap: O(VE log V) Dijkstra Fibonacci heap: O(V2 log V + VE) Bellman-Ford: O(V2 E) (negative weight edges)

Two adjacency matrix based algorithms Matrix-multiplication based algorithm Let Lm(i,j) denote the length of the shortest path from node i to node j using at most m edges What is our desired result in terms of Lm(i,j)? What is a recurrence relation for Lm(i,j)? Floyd-Warshall algorithm Let Lk(i,j) denote the length of the shortest path from node i to node j using only nodes within {1, …, k} as internal nodes. What is our desired result in terms of Lk(i,j)? What is a recurrence relation for Lk(i,j)?

Conceptual pictures i k j i k j OR Shortest path using at most 2m edges Try all possible nodes k i k j Shortest path using at most m edges Shortest path using at most m edges Shortest path using nodes 1 through k Shortest path using nodes 1 through k-1 i k j Shortest path using nodes 1 through k-1 OR Shortest path using nodes 1 through k-1

Running Times Matrix-multiplication based algorithm O(V3 log V) log V executions of “matrix-matrix” multiplication Not quite matrix-matrix multiplication but same running time Floyd-Warshall algorithm O(V3) V iterations of an O(V2) update loop The constant is very small, so this is a “fast” O(V3)

Johnson’s Algorithm Key ideas Reweight edge weights to eliminate negative weight edges AND preserve shortest paths Use Bellman-Ford and Dijkstra’s algorithms as subroutines Running time: O(V2 log V + VE) Better than earlier algorithms for sparse graphs

Reweighting Original edge weight is w(u,v) New edge weight: w’(u,v) = w(u,v) + h(u) – h(v) h(v) is a function mapping vertices to real numbers Key observation: Let p be any path from node u to node v w’(p) = w(p) + h(u) – h(v)

Computing vertex weights h(v) Create a new graph G’ = (V’, E’) by adding a new vertex s to V adding edges (s,v) for all v in V with w(s,v) = 0 Set h(v) to be the length of the shortest path from this new node s to node v This is well-defined if G’ does not contain negative weight cycles Note that h(v) ≤ h(u) + w(u,v) for all (u,v) in E’ Thus, w’(u,v) = w(u,v) + h(u) – h(v) ≥ 0 3 4 7 -5 -7 1

Algorithm implementation Run Bellman-Ford on G’ from new node s If no negative weight cycle, then use h(v) values from Bellman-Ford Now compute w’(u,v) for each edge (u,v) in E Now run Dijkstra’s algorithm using w’ Use each node as source node Modify d[u,v] at end by adding h(v) and subtracting h(u) to get true path weight Running time: O(VE) [from one run of Bellman-Ford] + O(V2 log V + VE) [from V runs of Dijkstra]