Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Similar documents
Shortest Path Algorithms

Single Source Shortest Paths

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

Design and Analysis of Algorithms

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Introduction to Algorithms

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

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

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

Introduction to Algorithms

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

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

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.

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

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

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

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Algorithm Design and Analysis

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

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

Algorithms: Lecture 12. Chalmers University of Technology

25. Minimum Spanning Trees

25. Minimum Spanning Trees

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

CS 161: Design and Analysis of Algorithms

Priority queues implemented via heaps

Design and Analysis of Algorithms

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

BFS Dijkstra. Oct abhi shelat

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

Partha Sarathi Mandal

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

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

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

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

CS781 Lecture 3 January 27, 2011

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

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

Divide-and-Conquer Algorithms Part Two

MA015: Graph Algorithms

ICS141: Discrete Mathematics for Computer Science I

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

Dynamic Programming: Shortest Paths and DFA to Reg Exps

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

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

Shortest paths with negative lengths

CS 580: Algorithm Design and Analysis

Heaps and Priority Queues

Dynamic Programming: Shortest Paths and DFA to Reg Exps

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

Breadth-First Search of Graphs

6.889 Lecture 4: Single-Source Shortest Paths

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

L feb abhi shelat

COMP251: Bipartite graphs

Myriad of applications

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Examination paper for TDT4120 Algorithms and Data Structures

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

Greedy Alg: Huffman abhi shelat

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

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

Discrete Optimization Lecture 5. M. Pawan Kumar

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Assignment 5: Solutions

CS213d Data Structures and Algorithms

Part IA Algorithms Notes

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

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

Data Structures and and Algorithm Xiaoqing Zheng

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order?

Randomization in Algorithms and Data Structures

CS361 Homework #3 Solutions

Directed Graphs (Digraphs) and Graphs

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

Query Processing in Spatial Network Databases

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

Matching Residents to Hospitals

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Discrete Optimization 2010 Lecture 3 Maximum Flows

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

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

CS 580: Algorithm Design and Analysis

Mon Tue Wed Thurs Fri

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

6. DYNAMIC PROGRAMMING II

INF2220: algorithms and data structures Series 1

COMP251: Bipartite graphs

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

ICS 252 Introduction to Computer Design

Graph Search Howie Choset

Transcription:

Dijkstra s Single Source Shortest Path Algorithm Andreas Klappenecker

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 v in V, find a path of minimum total weight from the source node s to v.

Single Source Shortest Path Given: a directed or undirected graph G = (V,E) Problem: 1 a source node s in V a weight function w: E -> R. s - a Goal: For each v in V, find a path of minimum total weight from the source node s to v.

Special Case Suppose that the weights of all edges are the same. Then breadthfirst 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. Goal: Solve the more general problem of single-source shortest path problems with arbitrary (non-negative) edge weights.

Intermezzo: Priority Queues

Priority Queues A min-priority queue is a data structure for maintaining a set S of elements, each with an associated value called key. It 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 key to the lower value k, where k < key[x].

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.

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.

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 even realize priority queues with worst case times as above]

Dijkstra s Single Source Shortest Path Algorithm

Dijkstra's SSSP Algorithm We assume all edge weights are nonnegative. Start with source node s and iteratively construct a tree rooted at s. Each node keeps track of the tree node that provides cheapest path from s. At each iteration, we include the node into the tree whose cheapest path from s is the overall cheapest.

Implementation Questions How can each node keep track of its best path to s? How can we know which node that is not in the tree yet has the overall cheapest path? How can we maintain the shortest path information of each node after adding a node to the shortest path tree?

Dijkstra's Algorithm Input: G = (V,E,w) and source node s for all nodes v in V { d[v] := infinity } d[s] := 0 while (Q is not empty) { u := extract-min(q) for each neighbor v of u { if (d[u] + w(u,v) < d[v]) { // relax d[v] := d[u] + w(u,v); decrease-key(q,v,d[v]) parent(v) := u Enqueue all nodes in priority queue Q using d values as keys. } } }

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

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

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

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

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

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

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

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

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

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

Correctness 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.

Correctness 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 that path in T i+1 from s to u is a shortest path, and has distance d[u] s T i x T i+1 u

Correctness s T i x T i+1 u P, path in T i+1 from s to u

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

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

Correctness Let P1 be part of P' before (a,b). Let P be part of P' after (a,b). w(p') = w(p1) + w(a,b) + w(p) w(p1) + w(a,b) (since weight are nonnegative) 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 I.H.) = w(p). So P is a shortest path, and d[u] is accurate after iteration i+1.

Running Time 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 ) Total is O(V (T ins + T ex ) + E T dec ) // details depend on min-queue implementation

Running Time using Binary Heaps and Fibonacci Heaps Recall, total running time is 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)

Running Time using Simpler Heap In general, running time is O(V(T ins + T ex ) + E T dec ). If graph is dense, say E = Θ(V ), then T ins and T ex can be O(V), but T dec should be O(1). Implement priority queue with an unsorted array: T ins = O(1), T ex = O(V), T dec = O(1) Total running time is O(V )

Credits This set of slides is based on slides prepared by Jennifer Welch.