1 Shortest Paths Algorithms and Networks 2014/2015 Hans L. Bodlaender Johan M. M. van Rooij.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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=
Chapter 25: All-Pairs Shortest-Paths
Introduction To Algorithms CS 445 Discussion Session 8 Instructor: Dr Alon Efrat TA : Pooja Vaswani 04/04/2005.
Lectures on Network Flows
CS420 lecture twelve Shortest Paths wim bohm cs csu.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
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.
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Algorithms and Networks
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
Maximum flow Algorithms and Networks. A&N: Maximum flow2 Today Maximum flow problem Variants Applications Briefly: Ford-Fulkerson; min cut max flow theorem.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Dynamic Single-source Shortest Paths Camil Demetrescu University of Rome “La Sapienza”
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
5/27/03CSE Shortest Paths CSE Algorithms Shortest Paths Problems.
Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 7 Greedy Graph.
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.
Lecture 16. Shortest Path Algorithms
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)
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
1 Shortest Paths Algorithms and Networks 2015/2016 Hans L. Bodlaender Johan M. M. van Rooij.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Lecture 13 Algorithm Analysis
15.082J & 6.855J & ESD.78J September 30, 2010 The Label Correcting Algorithm.
Theory of Computing Lecture 12 MAS 714 Hartmut Klauck.
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.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Introduction to Algorithms
Lectures on Network Flows
Algorithms and Networks Hans Bodlaender
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Minimum Spanning Tree Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Algorithms (2IL15) – Lecture 7
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 417: Algorithms and Computational Complexity
CS 3013: DS & Algorithms Shortest Paths.
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

1 Shortest Paths Algorithms and Networks 2014/2015 Hans L. Bodlaender Johan M. M. van Rooij

2 Contents  The shortest path problem:  Statement  Versions  Applications  Algorithms  Reminders: Dijkstra, Bellman-Ford, Floyd-Warshall  Reweighting (Johnson’s algorithm)  Scaling technique (Gabow’s algorithm)  Variant algorithms / optimisations:  A*  Bidirectional search  Bottleneck shortest paths

3 Notation  In the entire course (and most of the literature):  G = (V,E) is an undirected graph with vertices V and edge E.  G = (V,A) is a directed graph with vertices V and arcs A.  n = |V| is the number of vertices.  m = |E| or m = |A| is the number of edges/arcs.

4 DEFINITIONS AND APPLICATIONS Shortest Paths – Algorithms and Networks

5 Shortest path problem  Graph G=(V,E), length for each edge e in E, w(e). (Also for directed graphs)  Distance from u to v: length of shortest path from u to v.  Shortest path problem: find distances or find shortest paths.  Versions:  All pairs  Single pair  Single source  Single destination  Lengths can be  All equal (unit lengths) (BFS)  Non-negative  Negative but no negative cycles  Negative with negative cycles (problematic)

6 Notations  d w (s,t): distance of s to t: length of shortest path from s to t when using edge length function w  d(s,t): the same, but w is clear from context  d(s,s) = 0: we always assume there is a path with 0 edges from a vertex to itself.

7 Applications  Route planning  Subroutine in other graph algorithms  Some other examples we will explore further:  Allocating inspection effort on a production line.  Difference constraints.

8 Application 1: Allocating Inspection Efforts on a Production Line  Production line: ordered sequence of n production stages  Each stage can make an item defect  Items are inspected at some stages  When should we inspect to minimize total costs?

9 Solve By Modelling As a Shortest Paths Problem  Production line: ordered sequence of n production stages.  Items are produced in batches of B > 0 items.  Probability that stage i produces a defect item is a i.  Manufacturing cost per item at stage i: p i.  Cost of inspecting at stage j, when last inspection has been done at stage i:  f ij per batch, plus  g ij per item in the batch Where B(i) denotes the expected number of non-defective items after stage i

10 Idea behind model  w(i,j) is the cost of production and inspection from stage i to stage j, assuming we inspect at stage i, and then again at stage j.  Find the shortest path from 0 to n.

11 Application 2: Difference Constraints  Tasks with precedence constraints and running length.  Each task i has:  Time to complete b i > 0  Some tasks can be started after other tasks have been completed:  Constraint: s j + b j ≤ s i  First task can start at time 0. When can we finish last task?  Longest/shortest paths problem on directed acyclic graphs (see next slides)!

12 Model  Take vertex for each task.  Take special vertex v 0.  Vertex v 0 models time 0.  Arc (v 0, i) for each task vertex i, with length 0.  For each precedence constraint s j + b j s i an arc (j, i) with length b j.  Note: we have a Directed Acyclic Graph (DAG).  If there is a cycle, then we set is infeasible.

13 Long paths give time lower bounds  If there is a path from v 0 to vertex i with length x, then task i cannot start before time x.  Proof with induction...  Optimal: start each task i at time equal to the length of longest path from v 0 to i.  This gives a valid scheme, and it is optimal by the solution.  The longest path problem can be solved in O(n+m) time, as we have a directed acyclic graph.  Transforming to shortest paths problem: multiply all lengths and times by –1.

14 REMINDER: ALGORITHMS FOR SHORTEST PATHS Shortest Paths – Algorithms and Networks

15 Basics of single source algorithms  Source vertex s.  Each vertex v has variable D[v]  Invariant: d(s,v)  D[v] for all v  Initially: D[s]=0; vs: D[v] =   Relaxation/update step over edge (u,v):  D[v] = min{ D[v], D[u]+ w(u,v) }  To compute a shortest path (not only the length), one could maintain pointers to the `previous vertex on the current shortest path’ (sometimes NULL): p(v).  Initially: p(v) = NULL for each v.  Relaxation step becomes:If D[v] > D[u]+ w(u,v) then D[v] = D[u] + w(u,v); p(v) = u; End if; p-values build paths of length D(v). Shortest paths tree! p-values build paths of length D(v). Shortest paths tree!

16 Dijkstra’s Algorithm 1. Initialize 2. Take priority queue Q, initially containing all vertices 3. While Q is not empty,  Select vertex v from Q of minimum value D[v]  Relax across all outgoing edges from v  Note: each relaxation can cause a change of a D-value and thus a change in the priority queue.  This happens at most |E| times.

17 On Dijkstra’s Algorithm  Assumes all lengths are non-negative  Correctness proof (done in `Algoritmiek’)  Running time depends on selection algorithm for vertex v, that it, the implementation of the priority queue:  O(n 2 ): array (select v in O(n) time)  O((m + n) log n): red-black trees, heaps  O(m + n log n): Fibonacci heaps  …

18 Negative lengths  What if w(u,v) < 0?  What if there are negative cycles reachable from s?  No shortest paths!  Bellman-Ford algorithm:  For instances without negative cycles.  In O(nm) time: single source shortest path problem when there are no negative cycles reachable from s.  Also: detects whether a negative cycle exists.

19 Bellman-Ford Algorithm 1. Initialize 2. Repeat |V|-1 times:  For every edge (u,v) in E do: Relax(u,v,w) 3. For every edge (u,v) in E  If D[v] > D[u] + w(u,v) then there exists a negative cycle.  Invariant: If there is no negative cycle reachable from s, then after i runs of main loop, we have:  If there is a shortest path from s to u with at most i edges, then D[u]=d(s,u), for all u.  If there is no negative cycle reachable from s, then every vertex has a shortest path with at most n – 1 edges.  If there is a negative cycle reachable from s, then there will always be an edge with a relaxation/update possible. Clearly: O(nm) time Clearly: O(nm) time

20 Finding a negative cycle in a graph  Reachable from s:  Apply Bellman-Ford, and look back with pointers.  Or: add a vertex s with edges to each vertex in G. G s s

21 All Pairs Shortest Paths: Floyd-Warshall  Dynamic programming: O(n 3 ) (Floyd-Warshall, 1962) 1. Initialised(u,v) = w(u,v) for all (u, v) in E d(u,v) = otherwise 2. For all u in V - For all v in V -For all x in V -d(v,x) = min{ d(v,u) + d(u,x), d(v,x) }

22 All Pairs Shortest Paths: Johnson’s Algorithm  Johnson: improvement for sparse graphs with reweighting technique:  O(n 2 log n + nm) time.  Works if no negative cycles.  Observation: if all weights are non-negative we can run Dijkstra with each vertex as starting vertex.  This gives O(n 2 log n + nm) time using a Fibonacci heap.  What if we have negative lengths: reweighting…

23 Reweighting with a potential function  Let h: V → R be any function to the reals.  Write w h (u,v) = w(u,v) + h(u) – h(v).  Lemmas (for the height/potential function h):  Let P be a path from x to y. Then: w h (P) = w(P) + h(x) – h(y).  d h (x,y) = d(x,y) + h(x) – h(y).  P is a shortest path from x to y with lengths w, if and only if, it is so with lengths w h.  G has a negative-length circuit with lengths w, if and only if, it has a negative-length circuit with lengths w h.

24 What height function h is good?  Look for height function h with  w h (u,v)  0, for all edges (u,v).  If so, we can:  Compute w h (u,v) for all edges.  Run Dijkstra but now with w h (u,v).  Special method to make h with a SSSP problem and Bellman-Ford.

25 Choosing h 1. Add a new vertex s to the graph. 2. Set h(v) = d(s,v) 3. Solving SSSP problem with negative edge lengths; use Bellman-Ford.  If negative cycle detected: stop.  Note: for all edges (u,v):  w h (u,v)= w(u,v) + h(u) – h(v) = w(u,v) + d(s,u) – d(s,v)  0 because: d(s,u) + w(u,v)  d(s,v) G 0 s s

26 Johnson’s algorithm 1. Build graph G’ (as shown) 2. Compute with Bellman-Ford d(s,v) for all v 3. Set w h (u,v) = w(u,v) + d G’ (s,u) – d G’ (s,v) for all (u,v) in A. 4. For all u do:  Use Dijkstra’s algorithm to compute d h (u,v) for all v.  Set d(u,v) = d h (u,v) + d G’ (s,v) – d G’ (s,u). O(n 2 log n + nm) time

27 USING THE NUMBERS Shortest Paths - Algorithms and Networks

28 Using the numbers  Back to the single source shortest paths problem with non- negative distances  Suppose  is an upper bound on the maximum distance from s to a vertex v.  Let L be the largest length of an edge.  Single source shortest path problem is solvable in O(m + ) time.

29 In O(m+) time  Keep array of doubly linked lists: L[0], …, L[],  Maintain that for v with D[v]  ,  v in L[D[v]].  Keep a current minimum .  Invariant: all L[k] with k <  are empty  Run ‘Dijkstra’ while:  Update D[v] from x to y: take v from L[x] (with pointer), and add it to L[y]: O(1) time each.  Extract min: while L[] empty, ++; then take the first element from list L[].  Total time: O(m+)

30 Corollary and extension  SSSP: in O(m+nL) time. (Take =nL).  Gabow (1985): SSSP problem can be solved in O(m log R L) time, where  R = max{2, m/n}  L : maximum length of edge  Gabow’s algorithm uses scaling technique!

31 Gabow’s Algorithm: Main Idea  First, build a scaled instance:  For each edge e set w’(e) =  w(e) / R .  Recursively, solve the scaled instance and switch to using Dijkstra ‘using the numbers’ if weights are small enough.  R * d w’ (s,v) is when we scale back our scaled instance.  We want d(s,v).  What error did we make while rounding?  Another shortest paths instance can be used to compute the error correction terms!

32 Computing the correction terms through another shortest path problem  Set for each edge (x,y) in E:  Z(x,y) = w(x,y) + R* d w’ (s,x) - R * d w’ (s,y)  Works like a height function, so the same shortest paths!  Height of x is - R * d w’ (s,x)  Z compares the differences in rounding errors from the shortest paths from s to x and y to the weight w(x,y).  Claim: For all vertices v in V:  d(s,v) = d Z (s,v) + R * d w’ (s,v)  Proof by property of the height function:  d(s,v)= d Z (s,v) + h(s) – h(v) = d Z (s,v) – R*d w’ (s,s) + R*d w’ (s,v) (d w’ (s,s) = 0) = d Z (s,v) + R * d w’ (s,v)  Thus, we can compute distances for w by computing distances for Z and for w’

33 Gabow’s algorithm 1. If L ≤ R, then  solve the problem using the O(m+nR) algorithm (Base case) 2. Else  For each edge e: set w’(e) = w(e) / R.  Recursively, compute the distances but with the new length function w’.  Set for each edge (u,v): Z(u,v) = w(u,v) + R* d w’ (s,u) – R * d w’ (s,v).  Compute d Z (s,v) for all v (how? See next slide!)  Compute d(s,v) using: d(s,v) = d Z (s,v) + R * d w’ (s,v)

34 A property of Z  For each edge (u,v)  E we have:  Z(u,v) = w(u,v) + R* d w’ (s,u) – R * d w’ (s,v)  0, because  w(u,v)  R * w’(u,v)  R * (d w’ (s,v) – d w’ (s,u)).  So, a variant of Dijkstra can be used to compute distances for Z.

35 Computing distances for Z  For each vertex v we have: d Z (s,v) ≤ nR for all v reachable from s  Consider a shortest path P for distance function w’ from s to v  For each of the less than n edges e on P, w(e) ≤ R + R*w’(e)  So, d(s,v) ≤ w(P) ≤ nR + R* w’(P) = nR + R* d w’ (s,v)  Use that d(s,v) = d Z (s,v) + R * d w’ (s,v)  So, we can use the O(m+nR) algorithm (Dijkstra with doubly-linked lists) to compute all values d Z (v).

36 Gabow’s algorithm (analysis)  Recursive step: costs O( m log R L’) with L’=  L/R   SSSP for Z costs O(m + nR) = O(m).  R = max{2, m/n}  Note: log R L’  (log R L) – 1.  So, Gabow’s algorithm uses O(m log R L) time.

37 Example a a b b t t s s

38 VARIANTS: A* AND BIDIRECTIONAL SEARCH Shortest Paths - Algorithms and Networks

39 A*  Practical heuristic for shortest paths  Consider shortest paths in geometric setting (e.g., routeplanning for cars system)  Standard Dijkstra would explore many paths that are clearly in the wrong direction  Utrecht to Groningen would look at roads near Den Bosch or even Maastricht

40 A heuristic for the distance to the target  Suppose we have a heuristic h that approximates the distance to target t.  We call h admissible if for each vertex v: h(v) ≤ d(v,t)  We call h is consistent if for each (u,v) in E: h(u) ≤ h(v) + w(u,v)  Example: Euclidean distance on the plane of the points  Somewhat smaller than the real distance, but looks reasonable

41 A* algorithm as shortest paths with height function  Use h as a height function, i.e., set for each edge (u,v): w h (u,v) = w(u,v) – h(u) + h(v)  Consistent: all new lengths are non-negative  Admissible: no fear of stopping too early  Note: arcs in wrong direction are less frequently used  Faster algorithm; still correct

42 Bidirectional search  For a single pair shortest path problem:  Start a Dijkstra-search from both sides simultaneously  Analysis needed for stopping criterion  Faster in practice  Combines nicely with A* st st

43 BOTTLENECK SHORTEST PATH Shortest Paths – Algorithms and Networks

44 Bottleneck shortest path  Given: weighted graph G, weight w(e) for each arc, vertices s, t.  Problem: find a path from s to t such that the maximum weight of an arc on the path is as small as possible.  Or, reverse: such that the minimum weight is as large as possible: maximum capacity path

45 Algorithms  On directed graphs: O((m+n) log m),  Or: O((m+n) log L) with L the maximum absolute value of the weights.  Binary search and DFS.  On undirected graphs: O(m+n) with divide and conquer strategy.

46 Bottleneck shortest paths on undirected graphs  Find the median weight of all weights of edges, say r.  Look to graph G r formed by edges with weight at most r.  If s and t in same connected component of G r, then the bottleneck is at most r: now remove all edges with weight more than r, and repeat (recursion).  If s and t in different connected components of G r : the bottleneck is larger than r. Now, contract all edges with weight at most r, and recurse.  T(m) = O(m) + T(m/2)

47 CONCLUSION Shortest Paths – Algorithms and Networks

48 Summary on Shortest Paths We have treated:  Some examples of applications  Several algorithms for shortest paths  Variants of the problemDijkstra / Floyd-Warshall  Detection of negative cyclesBellman-Ford Algorithm  Reweighting techniqueJohnson’s Algorithm  Scaling techniqueGabow’s Algorithm  A*, bidirectional search  Bottleneck shortest paths  Any questions?