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

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

Outline. 1 Introduction. 2 Algorithm. 3 Example. 4 Analysis. 5 References. Idea

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Single Source Shortest Path

1 Dijkstra s Algorithm

Dijkstra s Shortest Path Algorithm

Lecture 11: Analysis of Algorithms (CS ) 1

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

Introduction to Algorithms. Lecture 11

Week 12: Minimum Spanning trees and Shortest Paths

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

Algorithm Design and Analysis

Shortest Path Problem

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

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

Single Source Shortest Path (SSSP) Problem

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

TIE Graph algorithms

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

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

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

Minimum Spanning Trees

COMP251: Single source shortest paths

Algorithm Design and Analysis

Shortest path problems

(Refer Slide Time: 00:18)

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

Shortest Path Algorithms

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

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

22 Elementary Graph Algorithms. There are two standard ways to represent a

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Connectivity. Use DFS or BFS. 03/11/04 Lecture 18 1

Single Source Shortest Paths

Final Exam. EECS 2011 Prof. J. Elder - 1 -

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

Lecture I: Shortest Path Algorithms

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

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

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

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

1 The Shortest Path Problem

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Elementary Graph Algorithms

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Week 11: Minimum Spanning trees

Unit 2: Algorithmic Graph Theory

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

Dijkstra s Algorithm

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

Algorithms for Data Science

Outline. Binary Tree

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

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

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

Description of The Algorithm

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

Design and Analysis of Algorithms

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

Minimum Spanning Trees

Shortest Paths. Shortest Path. Applications. CSE 680 Prof. Roger Crawfis. Given a weighted directed graph, one common problem is finding the shortest

COT 6405 Introduction to Theory of Algorithms

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

CS2210 Data Structures and Algorithms

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 7 Lecturer: David Wagner February 13, Notes 7 for CS 170

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

Algorithms for Data Science

Outlines: Graphs Part-2

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Analysis of Algorithms, I

Three Graph Algorithms

Three Graph Algorithms

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

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

CSC263 Week 8. Larry Zhang.

Announcements Problem Set 4 is out!

CHAPTER 13 GRAPH ALGORITHMS

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

CSE 502 Class 23. Jeremy Buhler Steve Cole. April BFS solves unweighted shortest path problem. Every edge traversed adds one to path length

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

Minimum Spanning Trees

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

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Minimum Spanning Trees

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

Artificial Intelligence

Depth-first Search (DFS)

CS4800: Algorithms & Data Jonathan Ullman

Unit 5F: Layout Compaction

Transcription:

Outline Computer Science 33 Computation of Minimum-Cost Paths Dijkstra s Mike Jacobson Department of Computer Science University of Calgary Lecture #34 Dijkstra s to Find Min-Cost Paths 3 Example 4 5 References Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 / 5 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 / 5 Computation of Minimum Cost Paths Definitions: Paths and Their Costs Presented Here: Dijkstra s : a generalization of breadth-first search to weighed graphs Rather than looking for paths with minimum length we will look for paths with minimum cost, that is, minimum total weight Application: finding the best route from one place to another on a map, when multiple routes are available (single-source shortest path problem) This is also an interesting application of priority queues Suppose now that G = (V, E) is a weighted graph. Consider a path, that is, a sequence of edges (u 0, u ), (u, u ),..., (u k, u k ), (u k, u k ) in E where k 0. Recall that this is a path from u to v if u 0 = u and u k = v. The cost of this path is defined to be k w((u i, u i+ )). i=0 Note that if k = 0 then the path has length 0 and it also has cost 0 (because the above sum has no terms). Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 3 / 5 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 4 / 5

Example Consider the following graph G and the weights shown near the edges. 5 b e 3 a d 3 g c f 4 The following are paths from a to g with cost 6 : a, c, d, e, g (consists of edges (a, c), (c, d), (d, e), (e, g)) a, c, d, f, g (consists of edges (a, c), (c, d), (d, f ), (f, g)) Minimum Cost Paths The path (u 0, u ), (u, u ),..., (u k, u k ) is a minimum-cost path from u to v if Note: this is a path from u to v (as defined above), and the cost of this path is less than or equal to the cost of any other path from u to v (in this graph). If some weights of edges are negative then minimum cost paths might not exist (because there may be paths from u to v that include negative-cost cycles, whose costs are smaller than any bound you could choose)! In this lecture we will consider a version of the problem where edges weights are all nonnegative, in order to avoid this problem. Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 5 / 5 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 6 / 5 Specification of Requirements Specification of Requirements (cont.) Inputs and Outputs Inputs and outputs have the same names and types as for Breadth First Search but somewhat different meanings. Pre-Condition G = (V, E) is a weighted graph such that for every edge (u, v) E s V w((u, v)) 0 Post-Condition: The predecessor graph G p = (V p, E p ) corresponding to the function π and vertex s is a spanning tree for the connected component of G that contains s. For every vertex v V, d[v] is the cost of a minimum-cost path from s to v in G. In particular, d[v] = + if and only if v is not reachable from s in G at all. For every vertex v V that is reachable from s, the path from s to v in the predecessor graph G p is a minimum-cost path from s to v in G. Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 7 / 5 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 8 / 5

Data Structures Data Structures The algorithm (to be presented next) will use a priority queue to store information about costs of paths that have been found. The priority queue will be a MinHeap: the entry with the smallest priority will be at the top of the heap. Each node in the priority queue will store a vertex in G and the cost of a path to this vertex. The cost will be used as the node s priority. An array-based representation of the priority queue will be used. A second array will be used to locate each entry of the priority queue for a given vertex in constant time. Example: (,) (3,6) (4,3) heap-size(a) = 3 0 3 4 A: (, ) (3, 6) (4, 3)?? 0 3 4 B: NIL 0 NIL Explanation: element (v, c) in the priority queue consists of vertex v and cost c of a path from s to v A contains an array representation of the min-heap B gives the index of a vertex in the array representation of the priority queue. Examples: vertex 3 is in the priority queue (at index B[3] = ) vertex 0 is not in the priority queue (B[0] = NIL) Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 9 / 5 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 0 / 5 The Decrease-Priority Problem has inputs A, i and p and is defined as follows. Precondition : a) A is a Min-Heap (representing a min-priority queue Q) b) i is an integer such that 0 i < heap-size(a) c) p is a value of the same type as the priorities in A d) The priority q of the value that is currently stored at location i of A is greater than or equal to p Postcondition : a) A is now a Min-Heap storing a set in which the priority of the value originally at location i has been decreased from q to p (and such the set is otherwise unchanged) Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 / 5 Precondition : a) (a), (b) and (c) are the same as for Precondition # b) The priority q of the value currently stored at location i is already less than p Postcondition : a) A is not changed b) A LargePriorityException is thrown Precondition 3: a) (a) is the same as for Precondition # b) i is an integer such that either i < 0 or i heap-size(a) Postcondition 3: a) A is not changed b) A RangeException is thrown Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 / 5

Idea and Pseudocode Idea: Move the modified value up in the heap until it is place. Notation: P(y) will denote the priority of a value y. void Decrease-Priority (A,i,p) if i < 0 or i heapsize(a) then throw RangeException else if p > P(A[i]) then throw LargePriorityException else Change P(A[i]) to p j = i while j > 0 and P(A[parent(j)]) > P(A[j]) do tmp = A[j]; A[j] = A[parent(j)]; A[parent(j)] = tmp j = parent(j) end while end if Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 3 / 5 Correctness and Efficiency Properties of This : The given algorithm is correct. If A stores a set with size n then the number of steps used by the algorithm is in Θ(logn) in the worst case. Details of the proof of correctness and the analysis of this algorithm will be included in the tutorial exercise on this topic. Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 4 / 5 Dijkstra s to Find Min-Cost Paths Dijkstra s to Find Min-Cost Paths Dijkstra s : Pseudocode Pseudocode, Continued MCP(G, s) for v V do colour[v] = white d[v] = + π[v] = NIL end for Initialize an empty priority queue Q colour[s] = grey d[s] = 0 add vertex s with priority 0 to Q while (Q is not empty) do (u, c) = extract-min(q) {Note: c = d[u]} for each v Adj[u] do if (colour[v] == white) then d[v] = c + w((u, v)) colour[v] = grey; π[v] = u add vertex s with priority d[v] to Q else if (colour[v] == grey) then Update information about v (shown on next slide) end if end for colour[u] = black end while return π, d Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 5 / 5 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 6 / 5

Dijkstra s to Find Min-Cost Paths Example Pseudocode, Concluded Example Updating Information About v if (c + w((u, v)) < d[v]) then old = d[v] d[v] = c + w((u, v)) π[v] = u Use Decrease-Priority to replace (v, old) on Q with (v, d[v]) end if a Step 7: b c 5 3 d 3 4 Extract-Min (returns (g, 6)) color g black done! e f g a b c d e f g d 0 3 5 4 6 π - a a c d d f Q: (empty) Eg. shortest path from a to g is a, c, d, f, g (cost d[g] = 6). Edges: (π(g), g), (π(f ), f ), (π(d), d), (π(c), c) = (f, g), (d, f ), (c, d), (a, c) Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 7 / 5 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 8 / 5 Easily Established Properties Each of the following is easily established by inspecting the code: Colour Properties: The initial colour of every node v V is white. The colour of a vertex can change from white to grey. The colour of a vertex can change from grey to black. No other changes in colour are possible. Contents of Queue: The following properties are part of the loop invariant for the while loop: If (u, d) is an element of the queue then u V, colour[u] = grey, and d = d[u]. If a vertex v (and its cost) were included on the queue but have been removed, then colour[v] = black. Vertices that have never been on the queue are white. Additional Properties (Proofs Not Too Hard) The following are also part of the loop invariant for the while loop. 3 All vertices that belong to the predecessor subgraph (for π and s) are either grey or black. 4 All neighbours of any black vertex are either black or grey. 5 If the colour of a vertex v is black or grey then there exists a path (u 0, u ), (u, u ),..., (u k, u k ) from s to v in the predecessor subgraph with cost d[v] such that colour[u i ] = black for i k (u = s, u k = v) Furthermore, all paths from s to v in G with the above form (i.e., all but the final vertex is black) have cost at least d[v]. 6 If colour[x] = black and colour[y] = grey then d[x] d[y]. 7 If colour[x] = white then d[x] = +. Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 9 / 5 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 0 / 5

One Final Property Final Piece of the Loop Invariant The next property is part of the loop invariant, as well. 8 Suppose that the colour of v is either grey or white. Then every path from s to v in G must begin with a sequence of edges (u 0, u ), (u, u ),..., (u k, u k ) where k, colour[u i ] = black for i k, and where colour[u k ] = grey. Indeed, this is a consequence of Property #4 (listed above). Undoubtedly, some of these properties do not seem very interesting. They are important because they help to establish the one that is given next. Here is the last piece of the loop invariant. 9 The following property is satisfied by every vertex v such that colour[v] = black, and also by the vertex v such that (v, d[v]) is at the top of the priority queue, if Q is nonempty: The unique path from s to v in the predecessor subgraph for π and s is a minimum-cost path from s to v in G, and the cost of this path is d[v]. The loop invariant consists of the pieces of it that have now been identified. One can establish that this is a loop invariant by induction on the number of executions of the loop body. Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 / 5 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 / 5 Application of the Loop Invariant Termination and Running Time Notice that, if the loop terminates, then The priority queue is empty. Therefore there are no grey vertices left! Therefore the only neighbours of black vertices are also black. This can be used to show that no white vertex is reachable from s. This, and various pieces of the loop invariant, can be used to establish partial correctness of the algorithm. It follows by a modification of the analysis of the breadth-first search algorithm that The total number of operations on the priority queue, and the total number of operations that do not involve the priority queue, are each in Θ( V + E ). Since the size of the priority queue never exceeds V each operation on the priority queue requires O(log V ) steps. Conclusion: This algorithm terminates (on inputs G = (V, E) and s V ) after using O(( V + E ) log V ) steps. O( V log V + E ) using a Fibonacci heap (amortized) Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 3 / 5 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 4 / 5

References References Further Reading and Java Code: to s, Chapter 4 This also includes information about a slower algorithm (The Bellman-Ford algorithm ) that solves this problem when edge weights are allowed to be negative. Data Structures: Abstraction and Design Using Java, Chapter 0.6 Mike Jacobson (University of Calgary) Computer Science 33 Lecture #34 5 / 5