Shortest Path Algorithms

Similar documents
Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Single Source Shortest Paths

CS 4407 Algorithms Lecture: Shortest Path Algorithms

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Single Source Shortest Paths

Design and Analysis of Algorithms

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

Introduction to Algorithms

Introduction to Algorithms

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

Introduction to Algorithms

Introduction to Algorithms

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

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

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

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

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

Algorithm Design and Analysis

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

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

Algorithm Design and Analysis

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

Partha Sarathi Mandal

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Design and Analysis of Algorithms

Algorithms: Lecture 12. Chalmers University of Technology

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

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

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

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

25. Minimum Spanning Trees

25. Minimum Spanning Trees

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

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

Greedy Alg: Huffman abhi shelat

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

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

CS781 Lecture 3 January 27, 2011

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

Shortest paths with negative lengths

Data Structures and and Algorithm Xiaoqing Zheng

BFS Dijkstra. Oct abhi shelat

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

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

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

L feb abhi shelat

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

Breadth-First Search of Graphs

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

CS 580: Algorithm Design and Analysis

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Dynamic Programming: Shortest Paths and DFA to Reg Exps

CS 580: Algorithm Design and Analysis

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

CS 161: Design and Analysis of Algorithms

Discrete Optimization 2010 Lecture 3 Maximum Flows

MA015: Graph Algorithms

Myriad of applications

Chapter 6. Dynamic Programming. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithm Design Strategies V

ICS 252 Introduction to Computer Design

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

Matching Residents to Hospitals

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

NP-Completeness. Andreas Klappenecker. [based on slides by Prof. Welch]

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

Query Processing in Spatial Network Databases

University of New Mexico Department of Computer Science. Final Examination. CS 362 Data Structures and Algorithms Spring, 2007

6.889 Lecture 4: Single-Source Shortest Paths

6. DYNAMIC PROGRAMMING II

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

Divide-and-Conquer Algorithms Part Two

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

1 Some loose ends from last time

22 Max-Flow Algorithms

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

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

COMP251: Bipartite graphs

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

Assignment 5: Solutions

CMU Lecture 4: Informed Search. Teacher: Gianni A. Di Caro

SI545 VLSI CAD: Logic to Layout. Algorithms

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

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006

CSE548, AMS542: Analysis of Algorithms, Fall 2016 Date: Nov 30. Final In-Class Exam. ( 7:05 PM 8:20 PM : 75 Minutes )

Space Complexity of Algorithms

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Discrete Optimization Lecture 5. M. Pawan Kumar

Part IA Algorithms Notes

1 Introduction A priority queue is a data structure that maintains a set of elements and supports operations insert, decrease-key, and extract-min. Pr

Fly Cheaply: On the Minimum Fuel Consumption Problem

Introduction to Algorithms

ICS141: Discrete Mathematics for Computer Science I

Directed Graphs (Digraphs) and Graphs

COMP251: Bipartite graphs

Mon Tue Wed Thurs Fri

Fundamental Algorithms 11

Theoretical Computer Science. Partially dynamic efficient algorithms for distributed shortest paths

Sublinear Algorithms Lecture 3. Sofya Raskhodnikova Penn State University

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

Transcription:

Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch] 1

Single Source Shortest Path 2

Single Source Shortest Path Given: a directed or undirected graph G = (V,E) a source node s in V a weight function w: E -> R. 2

Single Source Shortest Path Given: a directed or undirected graph G = (V,E) a source node s in V a weight function w: E -> R. Goal: For each vertex t in V, find a path from s to t in G with minimum weight 2

Single Source Shortest Path Given: a directed or undirected graph G = (V,E) a source node s in V a weight function w: E -> R. Goal: For each vertex t in V, find a path from s to t in G with minimum weight Warning! Negative weight cycles are a problem: s 4 5 t 2

Constant Weight Functions 3

Constant Weight Functions Suppose that the weights of all edges are the same. How can you solve the singlesource shortest path problem? 3

Constant Weight Functions Suppose that the weights of all edges are the same. How can you solve the singlesource shortest path problem? Breadth-first search can be used to solve the single-source shortest path problem. 3

Constant Weight Functions Suppose that the weights of all edges are the same. How can you solve the singlesource shortest path problem? Breadth-first search can be used to solve the single-source shortest path problem. Indeed, the tree rooted at s in the BFS forest is the solution. 3

Intermezzo: Priority Queues 4

Priority Queues A min-priority queue is a data structure for maintaining a set S of elements, each with an associated value called key. This data structure supports the operations: insert(s,x) which realizes S := S {x} minimum(s) which returns the element with the smallest key. extract-min(s) which removes and returns the element with the smallest key from S. decrease-key(s,x,k) which decreases the value of x s 5

Simple Array Implementation Suppose that the elements are numbered from 1 to n, and that the keys are stored in an array key[1..n]. insert and decrease-key take O(1) time. extract-min takes O(n) time, as the whole array must be searched for the minimum. 6

Binary min-heap Implementation Suppose that we realize the priority queue of a set with n element with a binary min-heap. extract-min takes O(log n) time. decrease-key takes O(log n) time. insert takes O(log n) time. Building the heap takes O(n) time. 7

Fibonacci-Heap Implementation Suppose that we realize the priority queue of a set with n elements with a Fibonacci heap. Then extract-min takes O(log n) amortized time. decrease-key takes O(1) amortized time. insert takes O(1) time. [One can realize priority queues with worst case times as above] 8

Dijkstra s Single Source Shortest Path Algorithm 9

Dijkstra's SSSP Algorithm Assumes all edge weights are nonnegative Similar to Prim's MST algorithm Start with source node s and iteratively construct a tree rooted at s Each node keeps track of tree node that provides cheapest path from s (not just cheapest path from any tree node) At each iteration, include the node whose cheapest path from s is the overall cheapest 10

Prim's vs. Dijkstra's 4 5 1 s 6 Prim's MST 4 5 1 s 6 Dijkstra's SSSP 11

Prim's vs. Dijkstra's 4 5 1 s 6 Prim's MST 4 5 1 s 6 Dijkstra's SSSP 11

Prim's vs. Dijkstra's 4 5 1 s 6 Prim's MST 4 5 1 s 6 Dijkstra's SSSP 11

Implementing Dijkstra's Alg. How can each node u keep track of its best path from s? Keep an estimate, d[u], of shortest path distance from s to u Use d as a key in a priority queue When u is added to the tree, check each of u's neighbors v to see if u provides v with a cheaper path from s: compare d[v] to d[u] + w(u,v) 12

Dijkstra's Algorithm input: G = (V,E,w) and source node s // initialization d[s] := 0 d[v] := infinity for all other nodes v initialize priority queue Q to contain all nodes using d values as keys 13

Dijkstra's Algorithm while Q is not empty do u := extract-min(q) for each neighbor v of u do if d[u] + w(u,v) < d[v] then // relax d[v] := d[u] + w(u,v) decrease-key(q,v,d[v]) parent(v) := u 14

Dijkstra's Algorithm Example a 2 12 8 b 10 6 c 4 3 9 d 2 4 e a is source node iteration 0 1 2 3 4 5 Q abcde bcde cde de d Ø d[a] 0 0 0 0 0 0 d[b] 2 2 2 2 2 d[c] 12 10 10 10 10 d[d] 16 13 13 d[e] 11 11 11 11 15

Dijkstra's Algorithm Example a 2 12 8 b 10 6 c 4 3 9 d 2 4 e a is source node iteration 0 1 2 3 4 5 Q abcde bcde cde de d Ø d[a] 0 0 0 0 0 0 d[b] 2 2 2 2 2 d[c] 12 10 10 10 10 d[d] 16 13 13 d[e] 11 11 11 11 15

Dijkstra's Algorithm Example a 2 12 8 b 10 6 c 4 3 9 d 2 4 e a is source node iteration 0 1 2 3 4 5 Q abcde bcde cde de d Ø d[a] 0 0 0 0 0 0 d[b] 2 2 2 2 2 d[c] 12 10 10 10 10 d[d] 16 13 13 d[e] 11 11 11 11 15

Dijkstra's Algorithm Example a 2 12 8 b 10 6 c 4 3 9 d 2 4 e a is source node iteration 0 1 2 3 4 5 Q abcde bcde cde de d Ø d[a] 0 0 0 0 0 0 d[b] 2 2 2 2 2 d[c] 12 10 10 10 10 d[d] 16 13 13 d[e] 11 11 11 11 15

Dijkstra's Algorithm Example a 2 12 8 b 10 6 c 4 3 9 d 2 4 e a is source node iteration 0 1 2 3 4 5 Q abcde bcde cde de d Ø d[a] 0 0 0 0 0 0 d[b] 2 2 2 2 2 d[c] 12 10 10 10 10 d[d] 16 13 13 d[e] 11 11 11 11 15

Dijkstra's Algorithm Example a 2 12 8 b 10 6 c 4 3 9 d 2 4 e a is source node iteration 0 1 2 3 4 5 Q abcde bcde cde de d Ø d[a] 0 0 0 0 0 0 d[b] 2 2 2 2 2 d[c] 12 10 10 10 10 d[d] 16 13 13 d[e] 11 11 11 11 15

Dijkstra's Algorithm Example a 2 12 8 b 10 6 c 4 3 9 d 2 4 e a is source node iteration 0 1 2 3 4 5 Q abcde bcde cde de d Ø d[a] 0 0 0 0 0 0 d[b] 2 2 2 2 2 d[c] 12 10 10 10 10 d[d] 16 13 13 d[e] 11 11 11 11 15

Dijkstra's Algorithm Example a 2 12 8 b 10 6 c 4 3 9 d 2 4 e a is source node iteration 0 1 2 3 4 5 Q abcde bcde cde de d Ø d[a] 0 0 0 0 0 0 d[b] 2 2 2 2 2 d[c] 12 10 10 10 10 d[d] 16 13 13 d[e] 11 11 11 11 15

Dijkstra's Algorithm Example a 2 12 8 b 10 6 c 4 3 9 d 2 4 e a is source node iteration 0 1 2 3 4 5 Q abcde bcde cde de d Ø d[a] 0 0 0 0 0 0 d[b] 2 2 2 2 2 d[c] 12 10 10 10 10 d[d] 16 13 13 d[e] 11 11 11 11 15

Dijkstra's Algorithm Example a 2 12 8 b 10 6 c 4 3 9 d 2 4 e a is source node iteration 0 1 2 3 4 5 Q abcde bcde cde de d Ø d[a] 0 0 0 0 0 0 d[b] 2 2 2 2 2 d[c] 12 10 10 10 10 d[d] 16 13 13 d[e] 11 11 11 11 15

Correctness of Dijkstra's Alg. Let T i be the tree constructed after i-th iteration of the while loop: The nodes in T i are not in Q The edges in T i are indicated by parent variables Show by induction on i that the path in T i from s to u is a shortest path and has distance d[u], for all u in T i. Basis: i = 1. s is the only node in T 1 and d[s] = 0. 16

Correctness of Dijkstra's Alg. Induction: Assume T i is a correct shortest path tree. We need to show that T i+1 is a correct shortest path tree as well. Let u be the node added in iteration i. Let x = parent(u). Need to show path in T i+1 from s s T i x T i+1 u to u is a shortest path, and has distance d[u] 17

Correctness of Dijkstra's Alg s T i x P, path in T i+1 T i+1 from s to u u 18

Correctness of Dijkstra's Alg s a T i x P, path in T i+1 T i+1 from s to u u P', another path from s to u b 18

Correctness of Dijkstra's Alg s a T i x P, path in T i+1 T i+1 from s to u u P', another path from s to u b (a,b) is first edge in P' that leaves T i 18

Correctness of Dijkstra's Alg Let P1 be part of P' before (a,b). Let P2 be part of P' after (a,b). w(p') = w(p1) + w(a,b) + w(p2) w(p1) + w(a,b) (nonneg wts) s a T i b x P P' T i+1 u wt of path in T i from s to a + w(a,b) (inductive hypothesis) w(s->x path in T i ) + w(x,u) (alg chose u in iteration i and d-values are accurate, by inductive hypothesis = w(p). So P is a shortest path, and d[u] is accurate after iteration i+1. 19

Running Time of Dijstra's Alg. initialization: insert each node once O(V T ins ) O(V) iterations of while loop one extract-min per iteration => O(V T ex ) for loop inside while loop has variable number of iterations For loop has O(E) iterations total one decrease-key per iteration => O(E T dec ) 20

Running Time using Binary Heaps and Fibonacci Heaps O(V(T ins + T ex ) + E T dec ) If priority queue is implemented with a binary heap, then T ins = T ex = T dec = O(log V) total time is O(E log V) There are fancier implementations of the priority queue, such as Fibonacci heap: T ins = O(1), T ex = O(log V), T dec = O(1) (amortized) total time is O(V log V + E) 21

Using Simpler Heap O(V(T ins + T ex ) + E T dec ) If graph is dense, so that E = Θ(V 2 ), then it doesn't help to make T ins and T ex to be at most O(V). Instead, focus on making T dec be small, say constant. Implement priority queue with an unsorted array: T ins = O(1), T ex = O(V), T dec = O(1) 22

The Bellman-Ford Algorithm 23

What About Negative Edge Dijkstra's SSSP algorithm requires all edge weights to be nonnegative. This is too restrictive, since it suffices to outlaw negative weight cycles. Bellman-Ford SSSP algorithm can handle negative edge weights. [It even can detect negative weight cycles if they exist.] 24

Bellman-Ford: The Basic Idea Consider each edge (u,v) and see if u offers v a cheaper path from s compare d[v] to d[u] + w(u,v) Repeat this process V - 1 times to ensure that accurate information propgates from s, no matter what order the edges are considered in 25

Bellman-Ford SSSP Algorithm input: directed or undirected graph G = (V,E,w) //initialization initialize d[v] to infinity and parent[v] to nil for all v in V other than the source initialize d[s] to 0 and parent[s] to s // main body for i := 1 to V - 1 do for each (u,v) in E do if d[u] + w(u,v) < d[v] then d[v] := d[u] + w(u,v) parent[v] := u // consider in arbitrary order 26

Bellman-Ford SSSP Algorithm // check for negative weight cycles for each (u,v) in E do if d[u] + w(u,v) < d[v] then output "negative weight cycle exists" 27

Running Time of Bellman-Ford O(V) iterations of outer for loop O(E) iterations of inner for loop O(VE) time total 28

Correctness of Bellman-Ford Assume no negative-weight cycles. Lemma: d[v] is never an underestimate of the actual shortest path distance from s to v. Lemma: If there is a shortest s-to-v path containing at most i edges, then after iteration i of the outer for loop, d[v] is at most the actual shortest path distance from s to v. Theorem: Bellman-Ford is correct. This follows from the two lemmas and the fact 29

Bellman-Ford Example s 3 4 4 c a 2 1 b process edges in order (c,b) (a,b) (c,a) (s,a) (s,c) Exercise! 30

Correctness of Bellman-Ford Suppose there is a negative weight cycle. Then the distance will decrease even after iteration V - 1 shortest path distance is negative infinity This is what the last part of the code checks for. 31

The Boost Graph Library The BGL contains generic implementations of all the graph algorithms that we have discussed: Breadth-First-Search Depth-First-Search Kruskal s MST algorithm Prim s MST algorithm Strongly Connected Components Dijkstra s SSSP algorithm Bellman-Ford SSSP algorithm I recommend that you gain experience with this useful library. Recommended reading: The Boost Graph Library by J.G. Siek, L.-Q. Lee, and A. Lumsdaine, Addison-Wesley, 2002. 32