Shortest path problems

Similar documents
Shortest Path Problem

Minimum Spanning Trees

Shortest Paths. Nishant Mehta Lectures 10 and 11

Data Structures and Algorithms. Werner Nutt

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Shortest Paths. Nishant Mehta Lectures 10 and 11

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

Lecture 11: Analysis of Algorithms (CS ) 1

Introduction to Algorithms. Lecture 11

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

Single Source Shortest Path

COMP251: Single source shortest paths

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

22 Elementary Graph Algorithms. There are two standard ways to represent a

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

MA 252: Data Structures and Algorithms Lecture 36. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

Minimum Spanning Tree

Outline. (single-source) shortest path. (all-pairs) shortest path. minimum spanning tree. Dijkstra (Section 4.4) Bellman-Ford (Section 4.

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Classical Shortest-Path Algorithms

Dijkstra s Shortest Path Algorithm

22 Elementary Graph Algorithms. There are two standard ways to represent a

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Chapter 25: All-Pairs Shortest-Paths

Artificial Intelligence

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

COT 6405 Introduction to Theory of Algorithms

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

Single Source Shortest Path (SSSP) Problem

Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2007 Simonas Šaltenis

The Shortest Path Problem

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

Algorithms for Data Science

Analysis of Algorithms, I

Minimum Spanning Trees Ch 23 Traversing graphs

More Graph Algorithms: Topological Sort and Shortest Distance

Design and Analysis of Algorithms 演算法設計與分析. Lecture 13 December 18, 2013 洪國寶

1 Dijkstra s Algorithm

Lecture 6 Basic Graph Algorithms

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Graphs II - Shortest paths

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Shortest Path Algorithms

Minimum Spanning Trees My T. UF

Algorithm Design and Analysis

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

1 The Shortest Path Problem

All Shortest Paths. Questions from exercises and exams

Algorithms and Data Structures

Lecture I: Shortest Path Algorithms

CSE 502 Class 23. Jeremy Buhler Steve Cole. April BFS solves unweighted shortest path problem. Every edge traversed adds one to path length

CS521 \ Notes for the Final Exam

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

Introduction to Algorithms

Lecture #7. 1 Introduction. 2 Dijkstra s Correctness. COMPSCI 330: Design and Analysis of Algorithms 9/16/2014

Introduction to Algorithms

Lecture 5: Graph algorithms 1

Algorithm Design and Analysis

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Introduction Single-source shortest paths All-pairs shortest paths. Shortest paths in graphs

Dijkstra s algorithm for shortest paths when no edges have negative weight.

Review of course COMP-251B winter 2010

CHAPTER 13 GRAPH ALGORITHMS

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

Dijkstra s Algorithm

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Week 11: Minimum Spanning trees

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

ECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm

Unit 2: Algorithmic Graph Theory

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

1 Non greedy algorithms (which we should have covered

Graph Algorithms (part 3 of CSC 282),

Minimum Spanning Trees

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Sources for this lecture 2. Shortest paths and minimum spanning trees

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance

Mon Tue Wed Thurs Fri

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Shortest Paths. CSE 373 Data Structures Lecture 21

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

Dijkstra s Algorithm. Dijkstra s algorithm is a natural, greedy approach towards

Graph Algorithms shortest paths, minimum spanning trees, etc.

Week 12: Minimum Spanning trees and Shortest Paths

Solutions to relevant spring 2000 exam problems

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

Transcription:

Next... Shortest path problems Single-source shortest paths in weighted graphs Shortest-Path Problems Properties of Shortest Paths, Relaxation Dijkstra s Algorithm Bellman-Ford Algorithm Shortest-Paths in DAG s 12/04/17 CSE 3101 1

Shortest Path Generalize distance to weighted setting Digraph G = (V,E) with weight function W: E R (assigning real values to edges) Weight of path p = v 1 v 2 v k is k 1 w( p) w( vi, vi 1) i1 Shortest path = a path of the minimum weight Applications static/dynamic network routing robot motion planning map/route generation in traffic 12/04/17 CSE 3101 2

Shortest path problems Shortest-Path problems Unweighted shortest-paths BFS. Single-source, single-destination: Given two vertices, find a shortest path between them. Single-source, all destinations: Find a shortest path from a given source (vertex s) to each of the vertices. The topic of this lecture. [Solution to this problem solves the previous problem efficiently]. Greedy algorithm! All-pairs. Find shortest-paths for every pair of vertices. Dynamic programming algorithm. 12/04/17 CSE 3101 3

Optimal Substructure Theorem: subpaths of shortest paths are shortest paths Proof (cut and paste) if some subpath were not the shortest path, one could substitute the shorter subpath and create a shorter total path Suggests that there may be a greedy algorithm 12/04/17 CSE 3101 4

Triangle Inequality Definition (u,v) weight of a shortest path from u to v Theorem (u,v) (u,x) + (x,v) for any x Proof shortest path u v is no longer than any other path u v in particular, the path concatenating the shortest path u x with the shortest path x v 12/04/17 CSE 3101 5

Negative Weights and Cycles? Negative edges are OK, as long as there are no negative weight cycles (otherwise paths with arbitrary small lengths would be possible) Shortest-paths can have no cycles (otherwise we could improve them by removing cycles) Any shortest-path in graph G can be no longer than n 1 edges, where n is the number of vertices 12/04/17 CSE 3101 6

Relaxation For each vertex in the graph, we maintain d[v], the estimate of the shortest path from s, initialized to at start Relaxing an edge (u,v) means testing whether we can improve the shortest path to v found so far by going through u u u 2 2 v Relax(u,v) v u u 2 2 v Relax(u,v) v Relax (u,v,w) if d[v] > d[u] +w(u,v)then d[v] d[u]+w(u,v) [v] u 12/04/17 CSE 3101 7

Dijkstra's Algorithm Non-negative edge weights Greedy, similar to Prim's algorithm for MST Like breadth-first search (if all weights = 1, one can simply use BFS) Use Q, priority queue keyed by d[v] (BFS used FIFO queue, here we use a PQ, which is re-organized whenever some d decreases) Basic idea maintain a set S of solved vertices at each step select "closest" vertex u, add it to S, and relax all edges from u 12/04/17 CSE 3101 8

Dijkstra's Algorithm: pseudocode Graph G, weight function w, root s relaxing edges 12/04/17 CSE 3101 9

Dijkstra's Algorithm: example 10 u 1 v 10 u 1 v s 5 2 3 9 4 6 7 x 2 y s 5 2 3 9 4 6 7 x 2 y s 10 5 u 2 3 x 1 7 2 9 v 4 6 y s 10 5 u x 1 v 2 3 9 4 6 7 2 y 12/04/17 CSE 3101 10

Dijkstra's Algorithm: example (2) u 10 5 2 3 x v 1 9 7 2 4 6 y 10 5 u 2 3 x 1 9 7 2 v 4 6 y Observe relaxation step (lines 10-11) setting d[v] updates Q (needs Decrease-Key) similar to Prim's MST algorithm 12/04/17 CSE 3101 11

Dijkstra's Algorithm: correctness We will prove that whenever u is added to S, d[u] = d(s,u), i.e., that d is minimum, and that equality is maintained thereafter Proof Note that v, d[v] d(s,v) Let u be the first vertex picked such that there is a shorter path than d[u], i.e., that d[u] > d(s,u) We will show that this assumption leads to a contradiction 12/04/17 CSE 3101 12

Dijkstra's Algorithm: correctness (2) Let y be the first vertex V S on the actual shortest path from s to u, then it must be that d[y] = (s,y) because d[x] is set correctly for y's predecessor x S on the shortest path (by choice of u as the first vertex for which d is set incorrectly) when the algorithm inserted x into S, it relaxed the edge (x,y), assigning d[y] the correct value 12/04/17 CSE 3101 13

Dijkstra's Algorithm: correctness (3) d[ u] ( s, u) (initial assumption) ( s, y) ( y, u) (optimal substructure) d[ y] ( y, u) (correctness of d[ y]) d[ y] (no negative weights) But d[u] > d[y] algorithm would have chosen y (from the PQ) to process next, not u Contradiction Thus d[u] = (s,u) at time of insertion of u into S, and Dijkstra's algorithm is correct 12/04/17 CSE 3101 14

Dijkstra's Algorithm: running time Extract-Min executed V time Decrease-Key executed E time Time = V T Extract-Min + E T Decrease-Key T depends on different Q implementations Q T(Extract- Min) T(Decrease- Key) Total array (V) (1) (V 2 ) binary heap (lg V) (lg V) (E lg V) Fibonacci heap (lg V) (1) (amort.) (V lgv + E) 12/04/17 CSE 3101 15

Bellman-Ford Algorithm Dijkstra s doesn t work when there are negative edges: Intuition: we can not be greedy any more on the assumption that the lengths of paths will only increase in the future Bellman-Ford algorithm detects negative cycles (returns false) or returns the shortest path-tree 12/04/17 CSE 3101 16

Bellman-Ford Algorithm Bellman-Ford(G,w,s) 01 for each v V[G] 02 d[v] 03 d[s] 0 04 [s] NIL 05 for i 1 to V[G] -1 do 06 for each edge (u,v) E[G] do 07 Relax (u,v,w) 08 for each edge (u,v) E[G] do 09 if d[v] > d[u] + w(u,v) then return false 10 return true 12/04/17 CSE 3101 17

12/04/17 CSE 3101 18 Bellman-Ford Algorithm: example 5 s z y 6 7 8-3 7 2 9-2 x t -4 s z y 6 7 8-3 7 2 9-2 x t -4 s z y 6 7 8-3 7 2 9-2 x t -4 s z y 6 7 8-3 7 2 9-2 x t -4 5 5 5

Bellman-Ford Algorithm: example (2) s 6 8 7 t y 5-2 -3 2 9 x -4 7 z Bellman-Ford running time: ( V -1) E + E = ( V E ) 12/04/17 CSE 3101 19

Bellman-Ford Algorithm: correctness Let i (s,u) denote the length of path from s to u, that is shortest among all paths, that contain at most i edges Prove by induction that d[u]= i (s,u) after the i-th iteration of Bellman-Ford Base case (i=0) trivial Inductive step (say d[u] = i-1 (s,u)): Either i (s,u) = i-1 (s,u) Or i (s,u) = i-1 (s,z) + w(z,u) In an iteration we try to relax each edge ((z,u) also), so we will catch both cases, thus d[u] = i (s,u) 12/04/17 CSE 3101 20

Bellman-Ford Algorithm: correctness (2) After n-1 iterations, d[u] = n-1 (s,u), for each vertex u. If there is still some edge to relax in the graph, then there is a vertex u, such that n (s,u) < n-1 (s,u). But there are only n vertices in G we have a cycle, and it must be negative. Otherwise, d[u]= n-1 (s,u) = (s,u), for all u, since any shortest path will have at most n-1 edges 12/04/17 CSE 3101 21

Next... Next: All-pairs shortest paths in weighted graphs Matrix multiplication and shortest-paths Floyd Warshall algorithm Transitive closure 12/04/17 CSE 3101 22

12/04/17 CSE 3101 23 All-pairs shortest paths Suppose that we want to calculate information about shortest paths between all pairs of vertices. We have a matrix W of weights: We want a matrix: 0 2 0 2 1 1 0 1 1 0 a b c d 0 0 0 0 1 1 0 1 1 0

A Recursive Solution (0) l ij = 0 if i=j = otherwise (m) (m-1) (m-1) l ij = min (l ij, min 1k n {l ik +w kj } ) (m-1) = min 1k n {l ik +w kj } (i,j) = l ij (n-1) = l ij (n) = l ij (n+1).. 12/04/17 CSE 3101 24

Matrix multiplication: If A is the adjacency matrix for a graph G, then the ij th entry of A n is exactly the number of ways you can get from vertex i to vertex j in exactly n steps. q m1 A ij k 1 ( A # ways to get from i to k in exactly m steps m ) ik A kj # ways to get from k to j in one step If we replace addition of elements by minimum, and multiplication of elements by addition, then the ij th entry of W n is exactly the shortest path from vertex i to vertex j in at most n steps. q m1 m W ( W ) W ij min k 1 ik kj Shortest path weight for m steps from i to k Weight for a further step from k to j 12/04/17 CSE 3101 25

Matrix Multiplication contd. As in Bellman-Ford, no shortest path has more than V -1 vertices in it. Therefore, all the information that we need can be read from the entries in W V -1. Each matrix multiplication takes O(V 3 ). 12/04/17 CSE 3101 26

Matrix Multiplication - complexity Calculating W V -1 takes: O(V 4 ) if we do naïve exponentiation: A 0 A m+1 = I = A A m Q: How many multiplications are required to compute x n? O(V 3 log V) if we do fast exponentiation: A 0 A 1 = I = A A 2m = (A m ) 2 A 2m+1 = A (A m ) 2 12/04/17 CSE 3101 27

The Floyd-Warshall algorithm Instead of increasing the length of the path allowed at each step, suppose that we increase the number of vertices that can be used in forming such paths. Let D (k) be the matrix whose ij th component is the shortest-path weight for a path from vertex i to vertex j using only vertices 1 though k as intermediates. Note that D (0) = W. How can we calculate D (n+1) in terms of D (n)? 12/04/17 CSE 3101 28

Floyd-Warshall algorithm contd. A shortest path from i to j with intermediate vertices in 1..k is either: A shortest path from i to j with intermediate vertices in 1..(k-1). A shortest path from i to k, and a shortest path from k to j, both with vertices in 1..(k-1). Hence, for k>1, we can define: d (k) ij = min(d(k-1) ij, d(k-1) ik + d(k-1) kj ) i i k j j 12/04/17 CSE 3101 29

The Floyd-Warshall algorithm Let n = V, and calculate all F[k] values using: Time and space complexity are O(V 3 ) 12/04/17 CSE 3101 30

Floyd-Warshall algorithm - improvement In fact, we can do better - we only want D (n) : Store only D (n) Time complexity is O(V 3 ), space complexity is O(V 2 ). 12/04/17 CSE 3101 31

Transitive closure Given a directed graph G = (V,E), construct a new graph G = (V,E ) in which (i,j) E if there is a path From i to j in G. (0) t ij = 0 if ij and (i,j) E = 1 if i=j or (i,j) E And for m>0 t ij (m) = t ij (m-1) (t im (m-1) t mj (m-1) ) Reachability queries 12/04/17 CSE 3101 32

Transitive closure algorithm Very similar to Floyd Warshall: 12/04/17 CSE 3101 33

Transitive closure example 12/04/17 CSE 3101 34

Summary We have seen different algorithms for: computing spanning trees; computing minimum spanning trees; computing single-source shortest paths; computing all-pairs shortest paths. Computing transitive closure. Greedy algorithms and dynamic programming play key roles in these algorithms. 12/04/17 CSE 3101 35