COT 6405 Introduction to Theory of Algorithms

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

Single Source Shortest Path

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

COMP251: Single source shortest paths

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

Shortest Path Problem

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

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

Introduction to Algorithms. Lecture 11

Minimum Spanning Trees

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

Data Structures and Algorithms. Werner Nutt

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

Shortest path problems

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

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

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

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

Single Source Shortest Paths

Unit 2: Algorithmic Graph Theory

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

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

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

Lecture 11: Analysis of Algorithms (CS ) 1

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

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

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.

Shortest Path Problems

1 Dijkstra s Algorithm

COT 6405 Introduction to Theory of Algorithms

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

ECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm

24 Single-Source Shortest Paths

Algorithm Design and Analysis

Minimum Spanning Tree

Single Source Shortest Path (SSSP) Problem

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

Depth-first Search (DFS)

Graphs: Finding shortest paths. Tecniche di Programmazione A.A. 2016/2017

Algorithm Design, Anal. & Imp., Homework 4 Solution

1 i n (p i + r n i ) (Note that by allowing i to be n, we handle the case where the rod is not cut at all.)

Algorithm Design and Analysis

Shortest Path Algorithms

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Design and Analysis of Algorithms 演算法設計與分析. Lecture 13 December 18, 2013 洪國寶

Lecture 18 Graph-Based Algorithms. CSE373: Design and Analysis of Algorithms

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

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

Dijkstra s Shortest Path Algorithm

Chapter 25: All-Pairs Shortest-Paths

6.006 Introduction to Algorithms Spring 2008

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

Classical Shortest-Path Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Minimum Spanning Tree (undirected graph)

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

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

TIE Graph algorithms

Minimum Spanning Trees Shortest Paths

Week 12: Minimum Spanning trees and Shortest Paths

Introduction to Algorithms

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

Dijkstra s Algorithm. Dijkstra s algorithm is a natural, greedy approach towards

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.

Elementary Graph Algorithms

Lecture I: Shortest Path Algorithms

Introductory Computer Programming: Shortest Path Algorithms

Routing Protocols and the IP Layer

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

CS 4349 Lecture October 23rd, 2017

Analysis of Algorithms, I

Introduction to Algorithms

The Shortest Path Problem

CS4800: Algorithms & Data Jonathan Ullman

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

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

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

Sample Solutions to Homework #4

Week 11: Minimum Spanning trees

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

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

1 The Shortest Path Problem

More Graph Algorithms: Topological Sort and Shortest Distance

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Exam 3 Practice Problems

Dr. Alexander Souza. Winter term 11/12

Algorithms for Data Science

All Shortest Paths. Questions from exercises and exams

1 Linear programming relaxation

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

Minimum Spanning Trees My T. UF

Transcription:

COT 6405 Introduction to Theory of Algorithms Topic 16. Single source shortest path 11/18/2015 1

Problem definition Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v Shortest-path -> Weight of the path is minimum Weight of a path is the sum of the weight of edges E.g., a road map: what is the shortest path from USF ENB to USF water tower? 11/18/2015 2

Formal definition W(p), Weight of path p = (v 0, v 1,..., v k ) W(p) = sum of edge weights on path p Shortest-path weight, δ(u, v), from u to v 3

This example shows that the shortest path might not be unique It also shows that when we look at shortest paths from one vertex to all other vertices, the shortest paths are organized as a tree. 4

Single source shortest path We can think of weights as representing any measure that accumulates linearly along a path we want to minimize Examples: time, cost, penalties, loss. We can use the breadth-first search to find shortest paths for un-weighted graphs 5

Variants can be solved by SSSP Single-source: Find shortest paths from a given source vertex s V to every vertex v V. Single-destination: Find shortest paths to a given destination vertex. Single-pair: Find shortest path from u to v. All-pairs: Find shortest path from u to v for all u, v V. 6

Shortest path properties: optimal substructure

Cont d 8

Shortest path properties In graphs with negative weight cycles, some shortest paths will not exist: No shortest path from s to e: (s,e), (s,e,f,e), 9

Negative-weight edges Negative weight edges are ok for some cases as long as no negative-weight cycles are reachable from the source If we have a negative-weight cycle, we can just keep going around it, and get w(s, v) = for all v on the cycle. Some algorithms work only if there are no negativeweight edges in the graph. Dijkstra algorithm works on nonnegative weights We ll be clear when they re allowed and not allowed Normally, we assume nonnegative weights 10

Cycles Shortest paths cannot contain cycles: We already ruled out negative-weight cycles Positive-weight cycle Removing the cycle will give us a path with less weight Zero-weight cycle: no reason to use them assume that our solutions won t use them. 11/18/2015 11

Output of SSSP algorithm For each vertex v V, v.d = δ(s, v) Initially, v.d = Reduces as algorithms progress. But always maintain v.d δ(s, v) Call v.d a shortest-path estimate π[v] = predecessor of v on a shortest path from s If no predecessor, π[v] = NIL. π induces a tree: shortest-path tree. 12

Initialization All the shortest-paths algorithms start with INIT-SINGLE-SOURCE INIT-SINGLE-SOURCE(G, s) for each vertex v G.V s.d = 0 v.d = v.π = NIL 13

Initialization For all the single-source shortest-paths algorithms we ll look at, start by calling INIT-SINGLE-SOURCE, then relax edges by decreasing the path weight if possible The algorithms differ in the order and how many times they relax each edge. 14

Relaxation: reach v by u Relax(u, v, w) { } if (v.d > u.d + w(u,v)) v.d = u.d + w(u,v) v. = u u 5 2 9 v u 5 2 6 v Relax Relax u 5 2 v u 5 2 6 v decrease by 2 unchanged 15

Properties of shortest paths Triangle inequality u s v 16

Upper-bound property Always have v.d (s,v) Once v.d = (s,v), it never changes Proof: Initially, it is true: v.d = Supposed v.d < (s,v) Without loss of generality, v is the first vertex for this happens Let u be the vertex that causes v.d to change Then v.d = u.d + w(u,v) So, v.d < (s,v) (s,u) + w (u,v) < u.d + w(u,v) Then v.d < u.d + w(u,v) Contradict to v.d = u.d + w(u,v) 1

No-path property If (s,v) =, then v.d = always Proof: v.d (s,v) = v.d = 19

Convergence property 20

Path relaxation property 21

Bellman-Ford Algorithm Allows negative-weight edges. Computes v.d and v.π for all v V. Returns TRUE, if no negative-weight cycles reachable from s ; FALSE, otherwise. 22

Bellman-Ford algorithm BellmanFord(G, w, s) INIT-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 no solution ; Relaxation: Make V -1 passes, relaxing each edge Test for solution Under what condition do we get a solution? Relax(u,v,w): if (v.d > u.d + w(u,v)) v.d = u.d + w(u,v) 23

Bellman-Ford Algorithm BellmanFord(G, w, s) INIT-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 no solution ; What will be the running time? A: O(VE) Relax(u,v,w): if (v.d > u.d + w(u,v)) v.d = u.d + w(u,v) 24

Example (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y) s 0 6 8 t y -2 2 9 5-3 -4 x z inital 0 After Pass 1 After Pass 2 After Pass 3 After Pass 4 d s d t d x d y d z 25

Pass 1 (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y) s 0 6 6 8 t -2 2 5-3 -4 x y 9 z 26

Example (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y) s 0 6 8 t 6 y 5-2 2 9-3 -4 x z inital After Pass 1 After Pass 2 After Pass 3 After Pass 4 d s d t d x d y d z 0 6,s,s 0 0 0 0 2

Pass 2 (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y) s 0 6 t -2 5 x 6 11 4-3 8-4 2 y 9 2 z 28

Example (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y) s 0 6 5 t x -2 6 4 8 2 9 y -3-4 z 2 inital After Pass 1 After Pass 2 After Pass 3 After Pass 4 d s d t d x d y d z 0 0 6,s,s 0 6,s 4,y,s 2,t 29

Pass 3 (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y) s 0 6 t -2 5 x 62 4-3 8-4 2 y 9 2 z 30

(t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y) Example s 0 6 5 t 2-2 4-3 8-4 2 2 9 y z x inital 0 After Pass 1 After Pass 2 After Pass 3 After Pass 4 d s d t d x d y d z 0 6,s,s 0 6,s 4,y,s 2,t 0 2,x 4,y,s 2,t 0 31

Pass 4 (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y) s 0 6 t -2 5 x 2 4-3 8-4 2 y 9 2-2 z 32

Example (t,x), (t,y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y) s 0 6 5 t x -2 2 4 8 2 9 y -3-4 z -2 inital 0 After Pass 1 After Pass 2 After Pass 3 After Pass 4 d s d t d x d y d z 0 6,s,s 0 6,s 4,y,s 2,t 0 2,x 4,y,s 2,t 0 2,x 4,y,s -2,t 33

Running time Initialization: Θ(V) Line 2-4 : Θ(E) * V -1 passes Line 5- : O(E) O(VE) 34

Correctness 35

Correctness 36

k i=1 w(v i 1, v i ) < 0 3

The contradiction k i=1 => => k i=1 k i=1 k v i. d i=1 k v i 1. d + i=1 k k v i. d i=1 v i 1. d i=1 w(v i 1, v i ) v k. d - v 0. d Since v 0 = v k (c is a cycle), k i=1 w(v i 1, v i ) 0 w(v i 1, v i ) w(v i 1, v i ) This contradicts c being a negative-weight cycle 11/18/2015 38

Dijkstra s Algorithm If no negative edge weights, we can beat Bellman Ford Similar to breadth-first search Grow a tree gradually, advancing from vertices taken from a queue Also similar to Prim s algorithm for MST Use a priority queue keyed on v.d 49

Dijkstra s Algorithm Assumes no negative-weight edges. Maintains a vertex set S whose shortest path from s has been determined. Repeatedly selects u in V S with minimum Shortest Path estimate (greedy choice). Store V S in priority queue Q. DIJKSTRA(G, w, s) Initialize-SINGLE-SOURCE(G, s); S = ; Q = G.V; while Q u = Extract-Min(Q); S = S {u}; for each v G.Adj[u] Relax(u, v, w) 50

Example u 1 v s 0 10 2 3 9 4 6 5 x 2 y 51

Example u 10 1 v s 0 10 2 3 9 4 6 5 5 x 2 y 52

Example u 8 1 v 14 s 0 10 2 3 9 4 6 5 5 x 2 y 53

Example u 8 1 v 13 s 0 10 2 3 9 4 6 5 5 x 2 y 54

Example u 8 1 v 9 s 0 10 2 3 9 4 6 5 5 x 2 y 55

Example u 8 1 v 9 s 0 10 2 3 9 4 6 5 5 x 2 y 56

Dijkstra s Algorithm Dijkstra(G) for each v V v.d = ; s.d = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q); Note: this is really a call to Q->DecreaseKey() S = S U {u}; for each v u->g.adj[] if (v.d > u.d+w(u,v)) v.d = u.d + w(u,v); Relaxation Step 5

Dijkstra s correctness We will prove that whenever u is added to S, u.d= (s,u), i.e., that d is minimum, and that equality is maintained thereafter Proof Note that "v, v.d (s,v) let u be the first vertex for which u.d δ(s, u) (i.e., u.d > (s,u)) when it is added to set S. We will show that the assumption of such a vertex leads to a contradiction 11/18/2015 58

Correctness (Cont d) A shortest path p from source s to vertex u can be decomposed into : p 1 s x, x y p 2 y u where y is the first vertex on the path that is not in S and x S immediately precedes y 11/18/2015 59

Correctness (Cont d) Then, it must be that y.d = (s,y) because X.d is set correctly for y's predecessor x S on the shortest path (by choice of u as the first vertex for which d is set incorrectly) when the algorithm inserted x into S, it relaxed the edge (x,y), assigning y.d the correct value 60

Correctness (Cont d) Thus, y.d = (s,y) (s,u) (y appears before u on the shortestpath) u.d (upper-bound property) But because both u and y are in V-S when u was chosen, we have u.d y.d, and therefore the two inequalities are in fact equalities, y.d = (s,y) = (s,u) = u.d Consequently, u.d = (s,u), which contradicts our choice of u 61

Dijkstra s running time Dijkstra(G) for each v V v.d= ; s.d = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q); S = S U {u}; for each v u->adj[] if (v.d > u.d+w(u,v)) How many times is ExtractMin() called? A: V How many times is DecreaseKey()called? DecreaseKey(v.d,u.d+w(u,v)); What will be the total running time? A: E 63

Dijkstra s Running Time Extract-Min executed V time Decrease-Key executed E time Time = V T Extract-Min + E T Decrease-Key Time = O(VlgV) + O (ElgV) = O(ElgV) 11/18/2015 64

Summary We learned Shortest-Path Problems Properties of Shortest Paths, Relaxation Bellman-Ford Algorithm Dijkstra s Algorithm Common mistakes: Do not forget to relax all edges in all algorithms. 65