Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Similar documents
Shortest Path Algorithms

Graph Algorithms. Andreas Klappenecker

Partha Sarathi Manal

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

COMP251: Single source shortest paths

CS4800: Algorithms & Data Jonathan Ullman

Shortest Path Problem

Single Source Shortest Path

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

Introduction to Algorithms. Lecture 11

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

Minimum Spanning Trees My T. UF

Shortest Paths. Nishant Mehta Lectures 10 and 11

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

Minimum Spanning Trees

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Shortest Paths. Nishant Mehta Lectures 10 and 11

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

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

CSE 100 Minimum Spanning Trees Prim s and Kruskal

1 Dijkstra s Algorithm

Minimum Spanning Tree

Single Source Shortest Path (SSSP) Problem

Dijkstra s Shortest Path Algorithm

Week 12: Minimum Spanning trees and Shortest Paths

CS 4349 Lecture October 23rd, 2017

Minimum Spanning Trees

Minimum Spanning Trees

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

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

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

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

Shortest path problems

Algorithm Design and Analysis

Greedy Algorithms CSE 6331

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

Lecture 11: Analysis of Algorithms (CS ) 1

Partha Sarathi Mandal

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

Lecture Notes for Chapter 23: Minimum Spanning Trees

Lecture 5 Using Data Structures to Improve Dijkstra s Algorithm. (AM&O Sections and Appendix A)

Dijkstra s Algorithm

L22-23: Graph Algorithms

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

(Refer Slide Time: 00:18)

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

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

Outlines: Graphs Part-2

SHORTEST PATHS. Weighted Digraphs. Shortest paths. Shortest Paths BOS ORD JFK SFO LAX DFW MIA

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

Introduction to Algorithms

Dijkstra s algorithm for shortest paths when no edges have negative weight.

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

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

CSE 100: GRAPH ALGORITHMS

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

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

MST & Shortest Path -Prim s -Djikstra s

CS350: Data Structures Dijkstra s Shortest Path Alg.

Algorithm Design and Analysis

Sorting and Searching

Introduction to Algorithms

1 The Shortest Path Problem

Algorithm Design and Analysis

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

Depth-first Search (DFS)

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

Data Structure Lecture#24: Graphs 2 (Chapter 11) U Kang Seoul National University

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Sorting and Searching

CS2210 Data Structures and Algorithms

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

Routing Protocols and the IP Layer

Unit 2: Algorithmic Graph Theory

Lecture I: Shortest Path Algorithms

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

Single Source Shortest Paths

CHAPTER 23. Minimum Spanning Trees

Classical Shortest-Path Algorithms

Single-Source Shortest Paths. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Data Structures and Algorithms

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:

Three Graph Algorithms

Three Graph Algorithms

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Lecture 5: Graph algorithms 1

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

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

Description of The Algorithm

Greedy Algorithms CSE 780

Lecture 13: Weighted Shortest Paths. These slides include material originally prepared by Dr. Ron Cytron and Dr. Steve Cole.

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

NP Completeness. Andreas Klappenecker [partially based on slides by Jennifer Welch]

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

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Lab 8: Greedy Algorithms Due: Start of your next lab session

Binary Heaps in Dynamic Arrays

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

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

CSE 548: Analysis of Algorithms. Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps )

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. Problem:!! s 1-2! 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 } 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]) d[s] := 0 Enqueue all nodes in priority queue Q } } } parent(v) := u

Dijkstra's Algorithm Example a 2 12 8 b c 4 10 6 3 d 2 e 4 a is source node 9 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

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]! T i T i+1! s x 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 (a,b) is first edge in P' that leaves T i

Correctness 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) (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 2 ), 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 2 )

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