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

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

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

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Design and Analysis of Algorithms

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

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

Design and Analysis of Algorithms

Introduction to Algorithms

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

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

Examination paper for TDT4120 Algorithms and Data Structures

Introduction to Algorithms

Introduction to Algorithms

Introduction to Algorithms

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

Shortest Path Algorithms

Introduction to Algorithms

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

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Single Source Shortest Paths

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

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

Single Source Shortest Paths

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

BFS Dijkstra. Oct abhi shelat

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

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

Myriad of applications

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

CS 241 Analysis of Algorithms

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

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

Query Processing in Spatial Network Databases

1. (7pts) Find the points of intersection, if any, of the following planes. 3x + 9y + 6z = 3 2x 6y 4z = 2 x + 3y + 2z = 1

Shortest paths with negative lengths

Algorithm Design and Analysis

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Lecture 7: Shortest Paths in Graphs with Negative Arc Lengths. Reading: AM&O Chapter 5

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

Data Structures and and Algorithm Xiaoqing Zheng

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Discrete Optimization 2010 Lecture 8 Lagrangian Relaxation / P, N P and co-n P

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

7.4 DO (uniqueness of minimum-cost spanning tree) Prove: if all edge weights are distinct then the minimum-cost spanning tree is unique.

CSE 421 Introduction to Algorithms Final Exam Winter 2005

CS 320, Fall Dr. Geri Georg, Instructor 320 NP 1

Midterm Exam 2 Solutions

Decision Mathematics D1 Advanced/Advanced Subsidiary. Tuesday 9 June 2015 Morning Time: 1 hour 30 minutes

CS781 Lecture 3 January 27, 2011

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Decision Mathematics D1 Advanced/Advanced Subsidiary. Wednesday 23 January 2013 Morning Time: 1 hour 30 minutes

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

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

Problem set 1. (c) Is the Ford-Fulkerson algorithm guaranteed to produce an acyclic maximum flow?

Partha Sarathi Mandal

Flows. Chapter Circulations

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

Optimization Prof. A. Goswami Department of Mathematics Indian Institute of Technology, Kharagpur. Lecture - 20 Travelling Salesman Problem

9.5. Polynomial and Rational Inequalities. Objectives. Solve quadratic inequalities. Solve polynomial inequalities of degree 3 or greater.

NP-completeness. Chapter 34. Sergey Bereg

Combinatorial Optimization

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Progress on Parallel Chip-Firing

COMP251: Bipartite graphs

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

Review Questions, Final Exam

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

A Simple Implementation Technique for Priority Search Queues

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

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

Mathematics for Decision Making: An Introduction. Lecture 13

Fiedler s Theorems on Nodal Domains

Exact Algorithms for Dominating Induced Matching Based on Graph Partition

Assignment 5: Solutions

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Review Questions, Final Exam

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

Running Time. Assumption. All capacities are integers between 1 and C.

The Budget-Constrained Maximum Flow Problem

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

CS1800: Strong Induction. Professor Kevin Gold

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

Algorithm Design and Analysis

CSC 5170: Theory of Computational Complexity Lecture 4 The Chinese University of Hong Kong 1 February 2010

Analysis of Algorithms I: All-Pairs Shortest Paths

A New Approximation Algorithm for the Asymmetric TSP with Triangle Inequality By Markus Bläser

4 Packing T-joins and T-cuts

CS1800: Mathematical Induction. Professor Kevin Gold

CMPUT 675: Approximation Algorithms Fall 2014

Decision Mathematics D1 Advanced/Advanced Subsidiary. Tuesday 13 January 2009 Morning Time: 1 hour 30 minutes

cs/ee/ids 143 Communication Networks

CS 580: Algorithm Design and Analysis

Finite Math - J-term Section Systems of Linear Equations in Two Variables Example 1. Solve the system

CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms

Data Mining Prof. Pabitra Mitra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Name: Section Registered In:

CHAPTER 3 FUNDAMENTALS OF COMPUTATIONAL COMPLEXITY. E. Amaldi Foundations of Operations Research Politecnico di Milano 1

Discrete Optimization 2010 Lecture 7 Introduction to Integer Programming

Transcription:

Shortest Paths CS 320, Fall 2017 Dr. Geri Georg, Instructor georg@colostate.edu 320 ShortestPaths 3

Preliminaries Weighted, directed graphs Weight function: maps edges to real numbers Shortest path weight: δ(u,v) Shortest path estimate: v.d, v V Predecessor: v., v V 320 ShortestPaths 4

Negative weight edges & cycles a 3 1 b c d 2 7 3 i h g f 1 2 3 4 9 no negative weight cycles reachable from a means well defined δ(a,v i ) a 3 1 1 e b c d 2 i 2 7 3 h g f 1 3 δ(a, d) = δ(a, e) = δ(a, f) = negative weight cycles reachable from a means δ(a,v i ) NOT well defined, so δ(a,v i ) where v i = {h, g, i, c} is defined as 4 9 1 e 320 ShortestPaths 7

Cycles in shortest paths? Short answer: No. Last slide showed no for a negative weight cycle. For a 0 weight cycle, we can remove the cycle and get a path with the same weight. Positive weight cycle: d 5 1 e f 2 320 ShortestPaths 8

Shortest path properties 1 Triangle inequality: (u,v) E, δ(s, v) δ(s, u) + w(u,v) Upper bound: v V, v.d δ(s, v), and once v.d = δ(s, v) it never changes No path: If there is no path from s to v then δ(s, v) = Convergence: If s u v is a shortest path for some u, v V and if u.d = δ(s, u) at any time prior to relaxing (u,v) then u.d = δ(s, u) ever after 320 ShortestPaths 9

Shortest path properties 2 Path relaxation: If p = v 0,v 1, v k is a shortest path from s = v 0 to v k, and we relax the edges of p in the order (v 0, v 1 ), (v 1, v 2 ), (v k 1, v k ), then v k.d = δ(s, v k ) Predecessor subgraph: Once v.d = δ(s, v) for all v V, the predecessor subgraph is a shortest paths tree rooted at s 320 ShortestPaths 10

Relaxing Check to see if we can improve the shortest path to v found so far by going through u: Yes update v.d and v. No make no changes 320 ShortestPaths 11

Questions As we work through the Bellman Ford and Dijkstra algorithms, answer these questions: Why does Bellman Ford work with negative edges? Why doesn t Dijkstra work with negative edges? 320 ShortestPaths 12

Procedures INITIALIZE SINGLE SOURCE(G, s) 1 for each vertex v G.V 2 v.d = 3 v. = NIL 4 s.d = 0 RELAX(u, v, w) 1 if v.d > u.d + w(u,v) 2 v.d = u.d + w(u,v) 3 v. = u Complexity? 320 ShortestPaths 13

Bellman Ford BELLMAN FORD(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 for i = 1 to G.V - 1 3 for each edge (u,v) G.E 4 RELAX(u,v,w) 5 for each edge (u,v) G.E 6 if v.d > u.d + w(u,v) 7 return FALSE 8 return TRUE?? 320 ShortestPaths 14

Bellman Ford BELLMAN FORD(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 for i = 1 to G.V - 1 3 for each edge (u,v) G.E 4 RELAX(u,v,w) 5 for each edge (u,v) G.E 6 if v.d > u.d + w(u,v) 7 return FALSE 8 return TRUE Complexity??? 320 ShortestPaths 15

INITIALIZE SINGLE SOURCE(G, s) for each vertex v G.V v.d = v. = NIL s.d = 0 RELAX(u, v, w) if v.d > u.d + w(u,v) v.d = u.d + w(u,v) v. = u BELLMAN FORD(G, w, s) INITIALIZE-SINGLE-SOURCE(G, s) for i = 1 to G.V - 1 for each edge (u,v) G.E RELAX(u,v,w) for each edge (u,v) G.E if v.d > u.d + w(u,v) return FALSE return TRUE 320 ShortestPaths 17

320 ShortestPaths 18

Dijkstra s Algorithm INITIALIZE SINGLE SOURCE(G, s) S = {} MinPQ = G.V while MinPQ u = EXTRACT-MIN(MinPQ) S = S {u} for each v G.Adj[u] RELAX(u,v,w) Complexity? min PQ insert min PQ extract min PQ decrease key If min PQ is an array from 1 to V? Insert, decrease key are O(1), extract min is O(V) 320 ShortestPaths 19

Dijkstra s Algorithm INITIALIZE SINGLE SOURCE(G, s) S = {} MinPQ = G.V while MinPQ u = EXTRACT-MIN(MinPQ) S = S {u} for each v G.Adj[u] RELAX(u,v,w) Complexity? min PQ insert min PQ extract min PQ decrease key If min PQ is an array from 1 to V? Insert, decrease key are O(1), extract min is O(V) If min PQ is binary min heap? Build is O(V), extract min, decrease key are O(lg V) 320 ShortestPaths 20

Dijkstra s Algorithm INITIALIZE SINGLE SOURCE(G, s) S = {} MinPQ = G.V while MinPQ u = EXTRACT-MIN(MinPQ) S = S {u} for each v G.Adj[u] RELAX(u,v,w) Complexity? min PQ insert min PQ extract min PQ decrease key If min PQ is an array from 1 to V? Insert, decrease key are O(1), extract min is O(V) If min PQ is binary min heap? Build is O(V), extract min, decrease key are O(lg V) If min PQ is a Fibonacci min heap? V extract min ops of O(lg V), E decrease key is O(1) 320 ShortestPaths 21

INITIALIZE SINGLE SOURCE(G, s) for each vertex v G.V v.d = v. = NIL s.d = 0 RELAX(u, v, w) if v.d > u.d + w(u,v) v.d = u.d + w(u,v) v. = u DIJKSTRA(G, w, s) INITIALIZE SINGLE SOURCE(G, s) S = {} MinPQ = G.V while MinPQ u = EXTRACT-MIN(MinPQ) S = S {u} for each v G.Adj[u] RELAX(u,v,w) c a 2 4 1 2 b causes re build Dijkstra s 6 d 6 h 3 1 f 4 1 1 2 g e 3 320 ShortestPaths 22

320 ShortestPaths 23

Difference Constraints Special case of linear programming Search for a feasible solution to Ax b Value in each node is δ(v 0, v i ) which is a feasible solution for x 320 ShortestPaths 24

320 ShortestPaths 25

Micro Survey 1 Can you just add to negative weight edges to make them have weights 0 so that Dijkstra s algorithm will work? If we add the absolute value of the largest negative weight (call it x) to each edge to get rid of negative weight, this won t work unless all shortest paths have the same length. As an example, look at a shortest path of 2 edges, then the total path length will be increased by 2x. But if we have another shortest path of 3 edges, then that total path length will be increased by 3x. In addition, if a longer path originally has a smaller weight, you can switch which path has the smaller weight: Consider a graph where there are 2 paths between 2 nodes, a direct edge of length 2, and an indirect route through 2 other nodes with lengths 1, 2, and 2. The second path is shorter. Now say you increase all edge weights by 2, so the direct path is now length 4, and the indirect path is length 6. The direct path is shorter now. 320 AllPairsShortestPaths 27

Micro Survey 2 What s the w in Dijkstra s algorithm? It s a function that returns the weight of the edge indicated by its arguments. What s the goal for difference constraints? To find a feasible solution for the x vector, or return false if there is no feasible solution. Recall the equation is: Ax b where A is a matrix and x and b are vectors. With the Bellman Ford algorithm, do we call edges on a negative cycle? No matter how many times we loop will the value ever change? What happens is that we use the edge values we have, and we go through, checking all the edges for V 1 times. Then we check one more time, and we find an instance of v.d > u.d + w(u,v) so we know there s a negative cycle. 320 AllPairsShortestPaths 28

Micro Survey 3 Is it true that difference constraints only work for matrices that reduce to each row containing a 1 and a 1? What really happens is that a real problem can directly be stated as a set of difference constraints. What real problems exist for difference constraints? Consider a VLSI layout problem. Constrain it to horizontal layout of a bunch of features and you measure them all from the left edge. You have to have some specified distance between them, maybe so that there s room for routing lines. In that case you can talk about the difference in the horizontal distance of the left edges for each pair of features. This yields difference constraints. And the minimum path will make for a smaller chip. Plus this algorithm tells you if there is no feasible solution to the constraints. 320 AllPairsShortestPaths 29

320 ShortestPaths 30

Image Credits seattlespt.png: http://graphserver.sourceforge.net/gallery.html critical path trainset.jpg, trainset.jpg: http://www.pmexamsmartnotes.com/how to calculate critical path float and early and late startsand finishes/ 320 ShortestPaths 32