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

Similar documents
Single Source Shortest Paths

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Introduction to Algorithms

Single Source Shortest Paths

Introduction to Algorithms

Introduction to Algorithms

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

Introduction to Algorithms

Design and Analysis of Algorithms

CS 4407 Algorithms Lecture: Shortest Path 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

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

Data Structures and and Algorithm Xiaoqing Zheng

Algorithm Design and Analysis

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

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

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

BFS Dijkstra. Oct abhi shelat

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Myriad of applications

COMP251: Bipartite graphs

Algorithms: Lecture 12. Chalmers University of Technology

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

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

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

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

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

Discrete Optimization 2010 Lecture 3 Maximum Flows

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

Breadth-First Search of Graphs

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

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

COMP251: Bipartite graphs

Query Processing in Spatial Network Databases

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

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Lecture Notes Discrete Optimization

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

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Design and Analysis of Algorithms

MA015: Graph Algorithms

Dynamic Programming: Shortest Paths and DFA to Reg Exps

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

Introduction to Algorithms

CS781 Lecture 3 January 27, 2011

Matching Residents to Hospitals

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

ICS 252 Introduction to Computer Design

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

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

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

CS 580: Algorithm Design and Analysis

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

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)}

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

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

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

Partha Sarathi Mandal

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

L feb abhi shelat

arxiv: v1 [cs.ds] 20 Nov 2016

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Algorithm Design and Analysis

6.889 Lecture 4: Single-Source Shortest Paths

6. DYNAMIC PROGRAMMING II

General Methods for Algorithm Design

Shortest paths with negative lengths

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

Algorithm Design and Analysis

Question Paper Code :

Math 391: Midterm 1.0 Spring 2016

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

1 Some loose ends from last time

Solutions to Exercises

An Õ m 2 n Randomized Algorithm to compute a Minimum Cycle Basis of a Directed Graph

Ngày 20 tháng 7 năm Discrete Optimization Graphs

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

A Note on the Connection between the Primal-Dual and the A* Algorithm

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

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

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

ABHELSINKI UNIVERSITY OF TECHNOLOGY

2-INF-237 Vybrané partie z dátových štruktúr 2-INF-237 Selected Topics in Data Structures

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

Space Complexity of Algorithms

CS 161: Design and Analysis of Algorithms

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

Greedy. Outline CS141. Stefano Lonardi, UCR 1. Activity selection Fractional knapsack Huffman encoding Later:

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

Discrete Optimization Lecture 5. M. Pawan Kumar

Branch-and-Bound for the Travelling Salesman Problem

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

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

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

Transcription:

CMPS 00 Fall 01 Single Source Shortest Paths Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk 1

Paths in graphs Consider a digraph G = (V, E) with 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= 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 shortestpath 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, v) over all paths from u u 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 g 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 nonnegative. 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 while Q do u EXTRACT-MIN(Q) for each v Adj[u] do if d[v] > d[u] + w(u, v) then relaxation step d[v] d[u] + w(u, v) Implicit DECREASE-KEY 8

Example of Dijkstra s algorithm Graph with nonnegative edge weights: A 10 B D 1 4 8 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 algorithm Initialize: S: {} 0 A 10 B D 1 4 8 Q: C E A B C D E 0 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) 10

Example of Dijkstra s algorithm A EXTRACT-MIN(Q): S: { A } 0 A 10 B D 1 4 8 Q: C E A B C D E 0 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 10 10 B D 1 4 8 Q: C E A B C D E 0 10 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 10 10 B D 1 4 8 Q: C E A B C D E 0 10 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 10 7 11 B D 1 4 8 Q: C E A B C D E 0 5 10 7 11 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 10 7 11 B D 1 4 8 Q: C E A B C D E 0 5 10 7 11 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 10 7 11 B D 1 4 8 Q: C E A B C D E 0 5 10 7 11 5 7 11 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 10 7 11 B D 1 4 8 Q: C E A B C D E 0 5 10 7 11 5 7 11 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 10 B D 1 4 8 Q: C E A B C D E 0 5 10 7 11 5 7 11 9 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 10 B D 1 4 8 Q: C E A B C D E 0 5 10 7 11 5 7 11 9 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 ) TT EXTRACT -MIN + Θ( E ) T TDECREASE-KEY Note: Same formula as in the analysis of Prim s minimum spanning tree algorithm. 0

Analysis of Dijkstra (continued) Time = Θ( V ) T ) EXTRACT-MIN + Θ( E ) T ) DECREASE-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 t 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 t 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 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 dto 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 t 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 S,,just before x Path p from y adding u. s to u 4 u

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

Correctness Theorem. (i) For all v S: d[v] = δ(s, v) (ii) For all v S: d[v] ] = weight of shortest t 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. while Q do Breadth-first search while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = Analysis: Time = O( V + E ). u EXTRACT-MIN(Q) for each v Adj[u] do then d[v] d[u] + 1 ENQUEUE(Q, v) 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 a 0 1 1 d b e c 1 1 Q: a bd f g h i 0

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

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

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

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

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

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

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

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

Example of breadth-first search a 0 1 1 b c d e 4 4 f g Q: a b d c e g i f h h i 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 + 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 Q is a priority queue maintaining V S 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 + w(u, v) π[v] u 41

Example of Dijkstra s algorithm Graph with nonnegative edge weights: A 10 B D 1 4 8 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 Initialize: S: {} 0 A 10 B D 1 4 8 Q: C E A B C D E 0 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 π: A B C D E C E Q: A B C D E 0 10 B D 1 4 8 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: 10 10 B D 1 4 8 S: { A } 0 A π: A B C D E - C E Q: A B C D E 0 10 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: 10 10 B D 1 4 8 S: { A } 0 A π: A B C D E Α Α - C E Q: A B C D E 0 10 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): 10 B D S: { A,,C} } 0 A π: A B C D E Α Α - C E Q: A B C D E 0 10 10 1 4 8 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: 10 7 11 B D 1 4 8 S: { A,,C} } 0 A π: A B C D E Α Α - C E Q: A B C D E 0 5 10 7 11 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: 10 7 11 B D 1 4 8 S: { A,,C} } 0 A π: A B C D E Α Α - C E Q: A B C D E 0 5 10 7 11 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): 7 11 B D S: { A,,C, E } 0 A π: A B C D E C Α C C C E Q: A B C D E 0 5 10 7 11 5 10 1 4 8 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: 10 7 11 B D 1 4 8 S: { A,,C, E } 0 A π: A B C D E C Α C C C E Q: A B C D E 0 5 10 7 11 5 7 11 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): 7 11 B D S: { A,,C, E, B } 0 A π: A B C D E C Α C C C E Q: A B C D E 0 5 10 7 11 5 7 11 10 1 4 8 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: 10 B D 1 4 8 S: { A,,C, E, B } 0 A π: A B C D E C Α B C C E Q: A B C D E 0 5 10 7 11 5 7 11 9 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): B D S: { A,,C, E, B, D}0 A π: A B C D E C Α B C C E Q: A B C D E 0 5 10 7 11 5 7 11 10 9 1 4 8 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