COMP251: Single source shortest paths

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

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

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

Shortest Path Problem

Single Source Shortest Path

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

Introduction to Algorithms. Lecture 11

Minimum Spanning Trees

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

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

COT 6405 Introduction to Theory of Algorithms

Shortest path problems

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

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

Shortest Paths. Nishant Mehta Lectures 10 and 11

Lecture 11: Analysis of Algorithms (CS ) 1

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

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

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

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

Unit 2: Algorithmic Graph Theory

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

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

Dijkstra s Shortest Path Algorithm

Classical Shortest-Path Algorithms

Single Source Shortest Paths

Shortest Path Algorithms

Data Structures and Algorithms. Werner Nutt

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

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

1 Dijkstra s Algorithm

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

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

Algorithm Design and Analysis

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

Single Source Shortest Path (SSSP) Problem

Minimum Spanning Trees

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

Outline. Computer Science 331. Definitions: Paths and Their Costs. Computation of Minimum Cost Paths

Shortest Path Routing Communications networks as graphs Graph terminology Breadth-first search in a graph Properties of breadth-first search

24 Single-Source Shortest Paths

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

Algorithm Design and Analysis

Minimum Spanning Trees Ch 23 Traversing graphs

Shortest Paths. Shortest Path. Applications. CSE 680 Prof. Roger Crawfis. Given a weighted directed graph, one common problem is finding the shortest

Introductory Computer Programming: Shortest Path Algorithms

Lecture Notes for Chapter 23: Minimum Spanning Trees

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

TIE Graph algorithms

Lecture 18 Graph-Based Algorithms. CSE373: Design and Analysis of Algorithms

Introduction to Algorithms

Algorithm Design, Anal. & Imp., Homework 4 Solution

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Shortest Path Problems

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

Minimum Spanning Tree

Dijkstra s Algorithm. Single source shortest paths for a directed acyclic graph

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

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

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

Outlines: Graphs Part-2

Minimum Spanning Trees My T. UF

Graph Representation

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

Chapter 25: All-Pairs Shortest-Paths

Outline. Computer Science 331. Analysis of Prim's Algorithm

Introduction to Algorithms

6.006 Introduction to Algorithms Spring 2008

Solutions to relevant spring 2000 exam problems

Chapter 22. Elementary Graph Algorithms

Introduction to Algorithms

1 The Shortest Path Problem

Page 1. CSCE 310J Data Structures & Algorithms. CSCE 310J Data Structures & Algorithms. Greedy Algorithms and Graph Optimization Problems

Week 12: Minimum Spanning trees and Shortest Paths

Week 11: Minimum Spanning trees

CS2210 Data Structures and Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Section authors: Hamilton Clower, David Scott, Ian Dundore.

Lecture 10: Analysis of Algorithms (CS ) 1

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

The Shortest Path Problem

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Quiz 2 Practice Problems

CS 561, Lecture 10. Jared Saia University of New Mexico

Minimum Spanning Tree (undirected graph)

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Solution. violating the triangle inequality. - Initialize U = {s} - Add vertex v to U whenever DIST[v] decreases.

Minimum Spanning Trees

Dr. Alexander Souza. Winter term 11/12

Theory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck

Lecture 17: Weighted Graphs, Dijkstra s Algorithm. Courtesy of Goodrich, Tamassia and Olga Veksler

09 B: Graph Algorithms II

Greedy Algorithms CSE 6331

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Transcription:

COMP51: Single source shortest paths Jérôme Waldispühl School of Computer Science McGill University Based on (Cormen et al., 00)

Problem What is the shortest road to go from one city to another? Eample: Which road should I take to go from Montréal to Boston (MA)? Variants: What is the fastest road? What is the cheapest road?

Input: Directed graph G = (V,E) Weight function w: E R Weight of path p = v 0, v 1,, v k = n k=1 w(v k 1, v k ) Modeling as graphs = sum of edges weights on path p Shortest-path weight u to v: " p $ { } min w(p) : u! v If there eists a path u v. δ(u, v) = # $ % Otherwise. Shortest path u to v is any path p such that w(p) = δ(u,v) Generalization of breadth-first search to weighted graphs

Eample s 3 5 t 6 4 1 7 3 y 6 z Shortest path from s?

Eample 3 t 3 6 9 s 0 4 1 7 3 5 5 y 6 11 z Shortest paths are organized as a tree. Vertices store the length of the shortest path from s.

Eample 3 t 3 6 9 s 0 4 1 7 3 5 5 y 6 11 z Shortest paths are not necessarily unique!

Variants Single-source: Find shortest paths from a given source verte s V to every verte v V. Single-destination: Find shortest paths to a given destination verte. Single-pair: Find shortest path from u to v. Note: No way to known that is better in worst case than solving the single-source problem! All-pairs: Find shortest path from u to v for all u, v V.

Negative weight edges Negative weight edges can create issues. Why? If we have a negative-weight cycle, we can just keep going around it, and get w(s, v) = for all v on the cycle. When? If they are reachable from the source. Corollary: OK to have a negative-weight cycles if it is not reachable from the source. What? Some algorithms work only if there are no negativeweight edges in the graph. We must specify when they are allowed and not.

Cycles Shortest paths cannot contain cycles: Negative-weight: Already ruled out. Positive-weight: we can get a shorter path by omitting the cycle. Zero-weight: no reason to use them assume that our solutions will not use them.

Optimal substructure Lemma Any subpath of a shortest path is a shortest path. Proof: (cut and paste) p y u y v Suppose this path p is a shortest path from u to v. Then δ(u,v) = w(p) = w(p u ) + w(p y ) + w(p yv ).

Optimal substructure Lemma Any subpath of a shortest path is a shortest path. Proof: (cont d) p y u y v p y Now suppose there eists a shorter path y. Then w(p y )<w(p y ). w(p ) = w(p u ) + w(p y ) + w(p yv ) < w(p u ) + w(p y ) + w(p yv ) = w(p). Contradiction of the hypothesis that p is the shortest path! p y

Customized breadth-first search t s 0 y z Vertices count the number of edges used to reach them.

Customized breadth-first search t 1 s 0 1 y z

Customized breadth-first search t 1 s 0 1 y z

Customized breadth-first search t 1 s 0 1 y z

Customized breadth-first search t 1 s 0 1 y z Can we generalize BFS to use edge weights?

Output of single-source shortest-path algorithm For each verte v V : d[v] = δ(s,v). Initially, d[v] =. Reduces as algorithms progress, but always maintain d[v] δ(s,v). Call d[v] a shortest-path estimate. π[v] = predecessor of v on a shortest path from s. If no predecessor, π[v] = NIL. π induces a tree - shortest-path tree (see proof in tetbook).

Algorithm structure 1. Initialization. Scan vertices and rela edges The algorithms differ in the order and how many times they rela each edge.

Initialization INIT-SINGLE-SOURCE(V,s) for each v V do d[v]ß π[v]ß NIL d[s]ß0

Relaing an edge RELAX(u,v,w) if d[v]>d[u]+w(u,v) then d[v] ß d[u]+w(u,v) π[v]ßu u 3 v 4 9 Rela 4 7 u 3 v 4 6 4 6

Triangle inequality For all (u,v) E, we have δ(u,v) δ(u,) + δ(,v). Proof: Weight of shortest path u v is weight of any path u v. Path u v is a path u v, and if we use a shortest path u and v, its weight is δ(u, ) + δ (, v). u δ(u,v) v δ(u,) δ(, v)

Proof: Initially true. Upper bound property Always have δ(s, v) d[v] for all v. Once d[v] = δ(s, v), it never changes. Suppose there eists a verte such that d[v] < δ(s, v). Assume v is first verte for which this happens. Let u be the verte that causes d[v] to change. Then d[v] = d[u] + δ(u, v). d[v] < δ(s,v) δ(s, u) + δ(u, v) d[u] + δ(u, v) d[v] < d[u] + δ(u,v). (triangle inequality) Contradicts d[v] = d[u] + δ(u, v). (v is first violation)

No-path property If δ(s, v) =, then d[v] = always. Proof: d[v] δ(s,v) = d[v] =.

Convergence property If: 1. s u v is a shortest path,. d[u] = δ(s,u), 3. we call RELAX(u,v,w), then d[v] = δ(s,v) afterward. Proof: After relaation: d[v] d[u] + w(u,v) = δ(s, u) + w(u, v) (RELAX code) = δ(s, v) (lemma-optimal substructure) Since d[v] δ(s, v), must have d[v] = δ(s, v).

Path-relaation property Let p = v 0,v 1,...,v k be a shortest path from s = v 0 to v k. If we rela, in order, (v 0,v 1 ), (v 1,v ),..., (v k 1,v k ), even intermied with other relaations, then d[v k ] = δ(s,v k ). Proof: Induction to show that d[v i ]=δ(s,v i ) after (v i 1,v i ) is relaed. Basis: i = 0. Initially, d[v 0 ] = 0 = δ(s, v 0 ) = δ(s,s). Inductive step: Assume d[v i 1 ] = δ(s,v i 1 ). Rela (v i 1,v i ). By convergence property, d[v i ] = δ(s, v i ) afterward and d[v i ] never changes.

Single-source shortest paths in a DAG Since a DAG, we are guaranteed no negative-weight cycles. DAG-SHORTEST-PATHS(V,E,w,s) topologically sort the vertices INIT-SINGLE-SOURCE(V,s) for each verte u in topological order do for each verte v Adj[u] do RELAX(u,v,w) 6 1 s t y 7-1 - z 4

Eample 6 1 s 0 t y 7-1 - 4 z

Eample 6 1 s 0 t y 7-1 - 6 4 z

Eample 6 1 s 0 t 7 6-1 4 y 6-4 z

Eample 6 1 s 0 t 7 6-1 4 y 5-4 z

Eample 6 1 s 0 t 7 6-1 4 y 5-3 z

Eample 6 1 s 0 t 7 6-1 4 y 5-3 z

Single-source shortest paths in a DAG DAG-SHORTEST-PATHS(V,E,w,s) topologically sort the vertices INIT-SINGLE-SOURCE(V,s) for each verte u in topological order do for each verte v Adj[u] do RELAX(u,v,w) Time: (V + E). Correctness: Because we process vertices in topologically sorted order, edges of any path must be relaed in order of appearance in the path. Edges on any shortest path are relaed in order. By path-relaation property, correct.

Dijkstra s algorithm No negative-weight edges. Weighted version of BFS: Instead of a FIFO queue, uses a priority queue. Keys are shortest-path weights (d[v]). Have two sets of vertices: S = vertices whose final shortest-path weights are determined, Q = priority queue = V S. Similar Prim s algorithm, but computing d[v], and using shortestpath weights as keys. Greedy choice: At each step we choose the light edge.

Dijkstra s algorithm DIJKSTRA(V, E,w,s) INIT-SINGLE-SOURCE(V,s) S Q V while Q do u EXTRACT-MIN(Q) S S {u} for each verte v Adj[u] do RELAX(u,v,w)

Eample 10 t s 0 4 1 7 3 5 y 6 z Q s t y z

Eample 10 t 10 s 0 4 1 7 3 5 5 y 6 z Q y t z

Eample 10 t 6 9 s 0 4 1 7 3 5 5 y 6 11 z Q t z

Eample 10 t 6 8 s 0 4 1 7 3 5 5 y 6 11 z Q z

Eample 10 t 6 8 s 0 4 1 7 3 5 5 y 6 10 z Q z

Eample 10 t 6 8 s 0 4 1 7 3 5 5 y 6 10 z Q

Eample 10 t 6 8 s 0 4 1 7 3 5 5 y 6 10 z

Correctness Loop invariant: At the start of each iteration of the while loop, d[v] = δ(s,v) for all v S. Initialization: Initially, S =, so trivially true. Termination: At end, Q= S = V d[v] = δ(s,v) for all v V. Maintenance: Show that d[u] = δ(s,u) when u is added to S in each Iteration.

Correctness (cont d) Show that d[u] = δ(s,u) when u is added to S in each iteration. Suppose there eists u such that d[u] δ(s,u). Let u be the first verte for which d[u] δ(s, u) when u is added to S. u s, since d[s] = δ(s,s) = 0. Therefore, s S, so S. There must be some path s u. Otherwise d[u] = δ(s,u) = by no-path property. So, there is a path s u. Thus, there is a shortest p path s u.

Correctness (cont d) Show that d[u] = δ(s,u) when u is added to S in each iteration. Just before u is added to S, path p connects a verte in S (i.e., s) to a verte in V S (i.e., u). Let y be first verte along p that is in V S, and let S be y is predecessor. p s u p 1 S y p 1 p Decompose p into s y u.

Correctness (cont d) Claim: d[y] = δ(s, y) when u is added to S. Proof: S and u is the first verte such that d[u] δ(s, u) when u is added to S d[] = δ(s, ) when is added to S. Relaed (, y) at that time, so by the convergence property, d[y] = δ(s, y). p s u p 1 S y

Correctness (cont d) Show that d[u] = δ(s,u) when u is added to S in each iteration. Now, we can get a contradiction to d[u] δ(s, u): y is on shortest path s δ(s, y) δ(s, u) d[y] = δ(s,y) δ(s,u) d[u] u, and all edge weights are nonnegative. (upper-bound property) In addition, y and u were in Q when we chose u, thus d[u] d[y]. We have d[y] d[u] & d[u] d[y] d[u] = d[y]. Therefore, d[y] = δ(s, y) = δ(s, u) = d[u]. Contradicts assumption that d[u] δ(s,u). n

Analysis Like Prim s algorithm, it depends on implementation of priority queue. If binary heap, each operation takes O(lg V ) time O(E lg V ). Note: We can achieve O(V lg V + E) with Fibonacci heaps.