Single Source Shortest Paths

Similar documents
CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

Single Source Shortest Paths

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Introduction to Algorithms

Introduction to Algorithms

Introduction to Algorithms

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

Introduction to Algorithms

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Design and Analysis of Algorithms

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Shortest Path Algorithms

Algorithm Design and Analysis

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Algorithm Design and Analysis

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Data Structures and and Algorithm Xiaoqing Zheng

Algorithms: Lecture 12. Chalmers University of Technology

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Lecture 11. Single-Source Shortest Paths All-Pairs Shortest Paths

COMP251: Bipartite graphs

A faster algorithm for the single source shortest path problem with few distinct positive lengths

CS 253: Algorithms. Chapter 24. Shortest Paths. Credit: Dr. George Bebis

BFS Dijkstra. Oct abhi shelat

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

Breadth-First Search of Graphs

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Query Processing in Spatial Network Databases

Myriad of applications

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Design and Analysis of Algorithms

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

COMP251: Bipartite graphs

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

Discrete Optimization 2010 Lecture 3 Maximum Flows

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Introduction to Algorithms

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Algorithm Design and Analysis

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Lecture Notes Discrete Optimization

6.889 Lecture 4: Single-Source Shortest Paths

CS781 Lecture 3 January 27, 2011

ne a priority queue for the nodes of G; ile (! PQ.is empty( )) select u PQ with d(u) minimal and remove it; I Informatik 1 Kurt Mehlhorn

Algorithm Design and Analysis

Shortest Paths. CS 320, Fall Dr. Geri Georg, Instructor 320 ShortestPaths 3

IS 709/809: Computational Methods in IS Research Fall Exam Review

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

CS 580: Algorithm Design and Analysis

CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT UNIT V GRAPHS

Running Time. Assumption. All capacities are integers between 1 and C.

CSE 4502/5717 Big Data Analytics Spring 2018; Homework 1 Solutions

directed weighted graphs as flow networks the Ford-Fulkerson algorithm termination and running time

CS675: Convex and Combinatorial Optimization Fall 2014 Combinatorial Problems as Linear Programs. Instructor: Shaddin Dughmi

arxiv: v1 [cs.ds] 20 Nov 2016

Matching Residents to Hospitals

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

Shortest paths with negative lengths

Centrality measures in graphs or social networks

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

MA015: Graph Algorithms

6. DYNAMIC PROGRAMMING II. sequence alignment Hirschberg's algorithm Bellman-Ford distance vector protocols negative cycles in a digraph

Discrete Optimization Lecture 5. M. Pawan Kumar

Trees. A tree is a graph which is. (a) Connected and. (b) has no cycles (acyclic).

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Partha Sarathi Mandal

6. DYNAMIC PROGRAMMING II

Solutions to Exercises

Algorithms to Compute Minimum Cycle Basis in Directed Graphs

Space Complexity of Algorithms

Scribes: Po-Hsuan Wei, William Kuzmaul Editor: Kevin Wu Date: October 18, 2016

CS 161: Design and Analysis of Algorithms

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

ICS 252 Introduction to Computer Design

Assignment 5: Solutions

L feb abhi shelat

Math 391: Midterm 1.0 Spring 2016

Question Paper Code :

A Simple Implementation Technique for Priority Search Queues

d 2 -coloring of a Graph

Chapter 11. Approximation Algorithms. Slides by Kevin Wayne Pearson-Addison Wesley. All rights reserved.

FIBONACCI HEAPS. preliminaries insert extract the minimum decrease key bounding the rank meld and delete. Copyright 2013 Kevin Wayne

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

Observation 4.1 G has a proper separation of order 0 if and only if G is disconnected.

Analysis of Algorithms I: All-Pairs Shortest Paths

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

Lecture 7: Shortest Paths in Graphs with Negative Arc Lengths. Reading: AM&O Chapter 5

Observation 4.1 G has a proper separation of order 0 if and only if G is disconnected.

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

CMPSCI 611: Advanced Algorithms

1 Some loose ends from last time

CS675: Convex and Combinatorial Optimization Fall 2016 Combinatorial Problems as Linear and Convex Programs. Instructor: Shaddin Dughmi

Transcription:

CMPS 00 Fall 015 Single Source Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 1

Paths in graphs Consider a digraph G = (V, E) with an edge-weight function w : E R. The weight of path p = v 1 v... v k is defined to be Example: k 1 i 1 w ( p) w( v i, v i 1). v 1 4 v 5 1 v 5 v v 4 w(p) =

Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as (u, v) = min{w(p) : p is a path from u to v}. Note: (u, v) = if no path from u to v exists.

Optimal substructure Theorem. A subpath of a shortest path is a shortest path. Proof. Cut and paste: 4

Triangle inequality Theorem. For all u, v, x V, we have (u, v) (u, x) + (x, v). Proof. (u,v) minimizes over all paths from u to v u (u, v) v Concatenating two shortest paths from u to x and from x to v yields one specific path from u to v (u, x) (x, v) x 5

Well-definedness of shortest paths If a graph G contains a negative-weight cycle, then some shortest paths may not exist. Example: u -6 5 < 0-8 v 6

Single-source shortest paths Problem. From a given source vertex s V, find the shortest-path weights (s, v) for all v V. Assumption: All edge weights w(u, v) are non-negative. It follows that all shortest-path weights must exist. IDEA: Greedy. 1. Maintain a set S of vertices whose shortest-path weights from s are known, i.e., d[v]= (s,v). At each step add to S the vertex u V S whose distance estimate d[u] from s is minimal.. Update the distance estimates d[v] of vertices v adjacent to u. 7

Dijkstra s algorithm d[s] 0 for each v V {s} do d[v] S Vertices for which d[v]=d(s,v) Q V Q is a priority queue maintaining V S sorted by d-values d[v] while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) relaxation step implicit DECREASE-KEY in Q 8

Example of Dijkstra s algorithm Graph with nonnegative edge weights: A B D 8 1 4 7 9 C E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 9

Example of Dijkstra s Initialize: algorithm B D S: {} 0 A 8 1 4 7 9 Q: A B C D E 0 C E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v)

Example of Dijkstra s algorithm A EXTRACT-MIN(Q): S: { A } 0 A B D 8 1 4 7 9 Q: A B C D E 0 C E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 11

Example of Dijkstra s algorithm Relax all edges leaving A: S: { A } 0 A B D 8 1 4 7 9 Q: A B C D E 0 C E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 1

Example of Dijkstra s algorithm C EXTRACT-MIN(Q): S: { A, C } 0 A B D 8 1 4 7 9 Q: A B C D E 0 C E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 1

Example of Dijkstra s algorithm Relax all edges leaving C: S: { A, C } 0 A 7 B 11 D 8 1 4 7 9 Q: A B C D E 0 7 11 5 C E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 14

Example of Dijkstra s algorithm E EXTRACT-MIN(Q): S: { A, C, E } 0 A 7 B 11 D 8 1 4 7 9 Q: A B C D E 0 7 11 5 C E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 15

Example of Dijkstra s algorithm Relax all edges leaving E: S: { A, C, E } 0 A 7 B 11 D 8 1 4 7 9 Q: A B C D E 0 7 11 5 7 11 C E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 16

Example of Dijkstra s algorithm B EXTRACT-MIN(Q): S: { A, C, E, B } 0 A 7 B 11 D 8 1 4 7 9 Q: A B C D E 0 7 11 5 7 11 C E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 17

Example of Dijkstra s algorithm Relax all edges leaving B: S: { A, C, E, B } 0 A 7 B 9 D 8 1 4 7 9 Q: A B C D E 0 7 11 5 7 11 9 C E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 18

Example of Dijkstra s algorithm D EXTRACT-MIN(Q): S: { A, C, E, B, D}0 A 7 B 9 D 8 1 4 7 9 Q: A B C D E 0 7 11 5 7 11 9 C E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 19

Analysis of Dijkstra V times degree(u) times while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) Handshaking Lemma ( E ) implicit DECREASE-KEY s. Time = ( V ) T EXTRACT -MIN + ( E ) T DECREASE-KEY 0

Analysis of Dijkstra (continued) Time = ( V ) T E XTRACT-MIN + ( E ) T D ECREASE-KEY Q T EXTRACT -MIN T DECREASE-KEY Total array O( V ) O(1) O( V ) binary heap Fibonacci heap O(log V ) O(log V ) O( E log V ) O(log V ) amortized O(1) O( E + V log V ) amortized worst case 1

Correctness Theorem. (i) For all v S: d[v] = (s, v) (ii) For all v S: d[v] = weight of shortest path from s to v that uses only (besides v itself) vertices in S. Corollary. Dijkstra s algorithm terminates with d[v] = (s, v) for all v V.

Correctness Theorem. (i) For all v S: d[v] = (s, v) (ii) For all v S: d[v] = weight of shortest path from s to v that uses only (besides v itself) vertices in S. Proof. By induction. Base: Before the while loop, d[s]=0 and d[v]= for all v s, so (i) and (ii) are true. Step: Assume (i) and (ii) are true before an iteration; now we need to show they remain true after another iteration. Let u be the vertex added to S, so d[u] d[v] for all other v S.

Correctness Theorem. (i) For all v S: d[v] = (s, v) (ii) For all v S: d[v] = weight of shortest path from s to v that uses only (besides v itself) vertices in S. (i) Need to show that d[u] = (s, u). Assume the contrary. There is a path p from s to u with w(p) < d[u]. Because of (ii) that path uses vertices S, in addition to u. Let y be first vertex on p such that y S. S, just before adding u. s x y u Path p from s to u 4

Correctness Theorem. (i) For all v S: d[v] = (s, v) (ii) For all v S: d[v] = weight of shortest path from s to v that uses only (besides v itself) vertices in S. u S, just before adding u. s x y Path p from s to u d[y] w(p) < d[u]. Contradiction to the choice of u. weights are nonnegative assumption about path 5

Correctness Theorem. (i) For all v S: d[v] = (s, v) (ii) For all v S: d[v] = weight of shortest path from s to v that uses only (besides v itself) vertices in S. (ii) Let v S. Let p be a shortest path from s to v that uses only (besides v itself) vertices in S. p does not contain u: (ii) true by inductive hypothesis p contains u: p consists of vertices in S\{u} and ends with an edge from u to v. w(p)=d[u]+w(u,v), which is the value of d[v] after adding u. So (ii) is true. 6

Unweighted graphs Suppose w(u, v) = 1 for all (u, v) E. Can the code for Dijkstra be improved? Use a simple FIFO queue instead of a priority queue. Breadth-first search while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + 1 ENQUEUE(Q, v) Analysis: Time = O( V + E ). while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) 7

Example of breadth-first search a b c d e f g h i Q: 8

Example of breadth-first search 0 a f h d b g e i c 0 Q: a 9

Example of breadth-first search 0 a 1 d f h 1 b g e i c 1 1 Q: a b d 0

Example of breadth-first search 0 1 a b c 1 d e 1 Q: a b d c e f g h i 1

Example of breadth-first search 0 1 a b c 1 d e Q: a b dc e f g h i

Example of breadth-first search 0 1 a b c 1 d e Q: a b dce f g h i

Example of breadth-first search 0 1 a b c 1 d e Q: a b d c e g i f g h i 4

Example of breadth-first search 0 1 a b c 1 d e 4 f g 4 Q: a b d c e g i f h i 5

0 1 Example of breadth-first search a b c 1 d e f g Q: a b d c e g i f h 4 4 4 4 h i 6

0 1 Example of breadth-first search a b c 1 d e f g Q: a b d c e g i f h 4 4 4 h i 7

Example of breadth-first search 0 1 a b c 1 d e 4 4 f h g i Q: a b d c e g i f h 8

Example of breadth-first search 0 1 a b c 1 d e 4 4 f h g i Q: a b d c e g i f h 9

Correctness of BFS while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + 1 ENQUEUE(Q, v) Key idea: The FIFO Q in breadth-first search mimics the priority queue Q in Dijkstra. Invariant: v comes after u in Q implies that d[v] = d[u] or d[v] = d[u] + 1. 40

How to find the actual shortest paths? Store a predecessor tree: d[s] 0 for each v V {s} do d[v] S Q V while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u Q is a priority queue maintaining V S 41

Example of Dijkstra s algorithm Graph with nonnegative edge weights: A B D 8 1 4 7 9 C E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 4

Example of Dijkstra s Initialize: algorithm B D S: {} 0 A 8 1 4 7 9 Q: A B C D E 0 C E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 4

Example of Dijkstra s algorithm A EXTRACT-MIN(Q): S: { A } 0 : A B C D E Q: A B C D E 0 A B C D 8 1 4 7 9 E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 44

Example of Dijkstra s algorithm Relax all edges leaving A: S: { A } 0 : A B C D E Q: - A B C D E 0 A B C D 8 1 4 7 9 E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 45

Example of Dijkstra s algorithm Relax all edges leaving A: S: { A } 0 : A B C D E Q: - A B C D E 0 A B C D 8 1 4 7 9 E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 46

Example of Dijkstra s algorithm C EXTRACT-MIN(Q): S: { A, C } 0 : A B C D E Q: - A B C D E 0 A B C D 8 1 4 7 9 E while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 47

Example of Dijkstra s algorithm Relax all edges leaving C: S: { A, C } 0 : A B C D E Q: - A B C D E 0 7 11 5 A 7 B C 11 D 8 1 4 7 9 E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 48

Example of Dijkstra s algorithm Relax all edges leaving C: S: { A, C } 0 : A B C D E Q: - A B C D E 0 7 11 5 A 7 B C 11 D 8 1 4 7 9 E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 49

Example of Dijkstra s algorithm E EXTRACT-MIN(Q): S: { A, C, E } 0 : A B C D E Q: C C C A B C D E 0 7 11 5 A 7 B C 11 D 8 1 4 7 9 E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 50

Example of Dijkstra s algorithm Relax all edges leaving E: S: { A, C, E } 0 : A B C D E Q: C C C A B C D E 0 7 11 5 7 11 A 7 B C 11 D 8 1 4 7 9 E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 51

Example of Dijkstra s algorithm B EXTRACT-MIN(Q): S: { A, C, E, B } 0 : A B C D E Q: C C C A B C D E 0 7 11 5 7 11 A 7 B C 11 D 8 1 4 7 9 E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 5

Example of Dijkstra s algorithm Relax all edges leaving B: S: { A, C, E, B } 0 : A B C D E Q: C B C A B C D E 0 7 11 5 7 11 9 A 7 B C 9 D 8 1 4 7 9 E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 5

Example of Dijkstra s algorithm D EXTRACT-MIN(Q): S: { A, C, E, B, D}0 : A B C D E Q: C B C A B C D E 0 7 11 5 7 11 9 A 7 B C 9 D 8 1 4 7 9 E 5 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u 54