Design and Analysis of Algorithms

Similar documents
CS 4407 Algorithms Lecture: Shortest Path Algorithms

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

Single Source Shortest Paths

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

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.

Introduction to Algorithms

Introduction to Algorithms

Introduction to Algorithms

Introduction to Algorithms

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

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

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

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

Introduction to Algorithms

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

Shortest Path Algorithms

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

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

Myriad of applications

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

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

Design and Analysis of Algorithms

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Data Structures and and Algorithm Xiaoqing Zheng

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Algorithm Design and Analysis

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

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

BFS Dijkstra. Oct abhi shelat

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

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Discrete Optimization 2010 Lecture 3 Maximum Flows

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

COMP251: Bipartite graphs

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

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

Algorithms: Lecture 12. Chalmers University of Technology

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

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

Analysis of Algorithms I: All-Pairs Shortest Paths

Breadth-First Search of Graphs

Algorithm Design and Analysis

Partha Sarathi Mandal

COMP251: Bipartite graphs

Trees. A tree is a graph which is. (a) Connected and. (b) has no cycles (acyclic).

CS 580: Algorithm Design and Analysis

Matching Residents to Hospitals

CS781 Lecture 3 January 27, 2011

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

CMPSCI 611: Advanced Algorithms

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

Algorithms and Theory of Computation. Lecture 11: Network Flow

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

Observation 4.1 G has a proper separation of order 0 if and only if G is disconnected.

Maximum Flow. Reading: CLRS Chapter 26. CSE 6331 Algorithms Steve Lai

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

6. DYNAMIC PROGRAMMING II

Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 10

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

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

General Methods for Algorithm Design

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

CSE 202 Homework 4 Matthias Springer, A

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

Scribes: Po-Hsuan Wei, William Kuzmaul Editor: Kevin Wu Date: October 18, 2016

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

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

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

Part IA Algorithms Notes

Algorithm Design and Analysis

CSE 421 Introduction to Algorithms Final Exam Winter 2005

6.889 Lecture 4: Single-Source Shortest Paths

Branch-and-Bound for the Travelling Salesman Problem

ICS 252 Introduction to Computer Design

CSE 332. Data Abstractions

Observation 4.1 G has a proper separation of order 0 if and only if G is disconnected.

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Small distortion and volume preserving embedding for Planar and Euclidian metrics Satish Rao

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

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

MA015: Graph Algorithms

Examination paper for TDT4120 Algorithms and Data Structures

All-Pairs Shortest Paths

Lecture Notes Discrete Optimization

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

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

Solutions to Exercises

SI545 VLSI CAD: Logic to Layout. Algorithms

CS367 Lecture 3 (old lectures 5-6) APSP variants: Node Weights, Earliest Arrivals, Bottlenecks Scribe: Vaggos Chatziafratis Date: October 09, 2015

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

Chapter 6. Dynamic Programming. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Midterm Exam 2 Solutions

Algorithm Design and Analysis

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

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

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

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Transcription:

Design and Analysis of Algorithms CSE 5311 Lecture 21 Single-Source Shortest Paths Junzhou Huang, Ph.D. Department of Computer Science and Engineering CSE5311 Design and Analysis of Algorithms 1

Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible destination. Similar to BFS. We will assume either no negative-weight edges, or no reachable negative-weight cycles. Algorithm will compute a shortest-path tree. Similar to BFS tree. CSE5311 Design and Analysis of Algorithms 2

Outline General Lemmas and Theorems. Bellman-Ford algorithm. DAG algorithm. Dijkstra s algorithm. CSE5311 Design and Analysis of Algorithms 3

General Results (Relaxation) Lemma 24.1: Let p = v 1, v 2,, v k be a SP from v 1 to v k. Then, p ij = v i, v i+1,, v j is a SP from v i to v j, where 1 i j k. So, we have the optimal-substructure property. Bellman-Ford s algorithm uses dynamic programming. Dijkstra s algorithm uses the greedy approach. Let δ(u, v) = weight of SP from u to v. Corollary: Let p = SP from s to v, where p = s u v. Then, δ(s, v) = δ(s, u) + w(u, v). p' Lemma 24.10: Let s V. For all edges (u,v) E, we have δ(s, v) δ(s, u) + w(u,v). CSE5311 Design and Analysis of Algorithms 4

Lemma 24.1 holds because one edge gives the shortest path, so the other edges must give sums that are at least as large. CSE5311 Design and Analysis of Algorithms 5

Relaxation Algorithms keep track of d[v], π[v]. Initialized as follows: Initialize(G, s) for each v V[G] do d[v] := ; π[v] := NIL end; d[s] := 0 These values are changed when an edge (u, v) is relaxed: Relax(u, v, w) if d[v] > d[u] + w(u, v) then d[v] := d[u] + w(u, v); π[v] := u end CSE5311 Design and Analysis of Algorithms 6

Properties of Relaxation d[v], if not, is the length of some path from s to v. d[v] either stays the same or decreases with time Therefore, if d[v] = δ(s, v) at any time, this holds thereafter Note that d[v] δ(s, v) always After i iterations of relaxing on all (u,v), if the shortest path to v has i edges, then d[v] = δ(s, v). CSE5311 Design and Analysis of Algorithms 7

Properties of Relaxation Consider any algorithm in which d[v], and π[v] are first initialized by calling Initialize(G, s) [s is the source], and are only changed by calling Relax. We have: Lemma 24.11: ( v:: d[v] δ(s, v)) is an invariant. Implies d[v] doesn t change once d[v] = δ(s, v). Proof: Initialize(G, s) establishes invariant. If call to Relax(u, v, w) changes d[v], then it establishes: d[v] = d[u] + w(u, v) δ(s, u) + w(u, v), invariant holds before call. δ(s, v), by Lemma 24.10. Corollary 24.12: If there is no path from s to v, then d[v] = δ(s, v) = is an invariant. CSE5311 Design and Analysis of Algorithms 8

For lemma 24.11, note that initialization makes the invariant true at the beginning. CSE5311 Design and Analysis of Algorithms 9

More Properties Lemma 24.13: Immediately after relaxing edge (u, v) by calling Relax(u, v, w), we have d[v] d[u] + w(u, v). p' Lemma 24.14: Let p = SP from s to v, where p = s u v. If d[u] = δ(s, u) holds at any time prior to calling Relax(u, v, w), then d[v] = δ(s, v) holds at all times after the call. Proof: After the call we have: d[v] d[u] + w(u, v), by Lemma 24.13. = δ(s, u) + w(u, v), d[u] = δ(s, u) holds. = δ(s, v), by corollary to Lemma 24.1. By Lemma 24.11, d[v] δ(s, v), so d[v] = δ(s, v). CSE5311 Design and Analysis of Algorithms 10

Lemma 24.13 follows simply from the structure of Relax. Lemma 24.14 shows that the shortest path will be found one vertex at a time, if not faster. Thus after a number of iterations of Relax equal to V(G) - 1, all shortest paths will be found. CSE5311 Design and Analysis of Algorithms 11

Bellman-Ford returns a compact representation of the set of shortest paths from s to all other vertices in the graph reachable from s. This is contained in the predecessor subgraph. CSE5311 Design and Analysis of Algorithms 12

Predecessor Subgraph Lemma 24.16: Assume given graph G has no negative-weight cycles reachable from s. Let G π = predecessor subgraph. G π is always a tree with root s (i.e., this property is an invariant). Proof: Two proof obligations: (1) G π is acyclic. (2) There exists a unique path from source s to each vertex in V π. Proof of (1): Suppose there exists a cycle c = v 0, v 1,, v k, where v 0 = v k. We have π[v i ] = v i-1 for i = 1, 2,, k. Assume relaxation of (v k-1, v k ) created the cycle. We show cycle has a negative weight. Note: Cycle must be reachable from s. CSE5311 Design and Analysis of Algorithms 13

Proof of (1) (Continued) Before call to Relax(v k-1, v k, w): π[v i ] = v i-1 for i = 1,, k 1. Implies d[v i ] was last updated by d[v i ] := d[v i-1 ] + w(v i-1, v i ) for i = 1,, k 1. [Because Relax updates π.] Implies d[v i ] d[v i-1 ] + w(v i-1, v i ) for i = 1,, k 1. [Lemma 24.13] Because π[v k ] is changed by call, d[v k ] > d[v k-1 ] + w(v k-1, v k ). Thus, k d[v Because ] > i i= 1 i= 1 = k i= 1 k k (d[v d[v d[v i i 1 ] = ] + ] + k i= 1 w(v k i 1 i= 1 i= 1 d[v i 1 w(v i 1 ],, v i i 1 )), v k i= 1 i ) w(v i 1, v i ) < 0, i.e., neg.- weight cycle! CSE5311 Design and Analysis of Algorithms 14

Comment on Proof d[v i ] d[v i-1 ] + w(v i-1, v i ) for i = 1,, k 1 because when Relax(v i-1, v i, w) was called, there was an equality, and d[v i-1 ] may have gotten smaller by further calls to Relax. d[v k ] > d[v k-1 ] + w(v k-1, v k ) before the last call to Relax because that last call changed d[v k ]. CSE5311 Design and Analysis of Algorithms 15

Lemma 24.17 Lemma 24.17: Same conditions as before. Call Initialize & repeatedly call Relax until d[v] = δ(s, v) for all v in V. Then, G π is a shortest-path tree rooted at s. Proof: Key Proof Obligation: For all v in V π, the unique simple path p from s to v in G π (path exists by Lemma 24.16) is a shortest path from s to v in G. Let p = v 0, v 1,, v k, where v 0 = s and v k = v. We have d[v i ] = δ(s, v i ) d[v i ] d[v i-1 ] + w(v i-1, v i ) (reasoning as before) Implies w(v i-1, v i ) δ(s, v i ) δ(s, v i-1 ). CSE5311 Design and Analysis of Algorithms 16

Proof (Continued) = w(p) k i= 1 k i= 1 w(v = δ(s, v = δ(s, v (δ(s, v k k i 1, v i ) ) δ(s, v ) δ(s, v ) i 0 So, equality holds and p is a shortest path. ) i-1 )) CSE5311 Design and Analysis of Algorithms 17

And note that this shortest path tree will be found after V(G) - 1 iterations of Relax. CSE5311 Design and Analysis of Algorithms 18

Bellman-Ford Algorithm Can have negative-weight edges. Will detect reachable negativeweight cycles. Initialize(G, s); for i := 1 to V[G] 1 do for each (u, v) in E[G] do Relax(u, v, w) od od; for each (u, v) in E[G] do if d[v] > d[u] + w(u, v) then return false fi od; return true Time Complexity is O(VE). CSE5311 Design and Analysis of Algorithms 19

So if Bellman-Ford has not converged after V(G) - 1 iterations, then there cannot be a shortest path tree, so there must be a negative weight cycle. CSE5311 Design and Analysis of Algorithms 20

Example z 0 6 7 u 8 5 2 2 3 4 v 7 x 9 y CSE5311 Design and Analysis of Algorithms 21

Example z 0 6 7 u 6 8 5 2 2 3 4 v 7 7 x 9 y CSE5311 Design and Analysis of Algorithms 22

Example z 0 6 7 u 6 8 5 2 2 3 4 v 4 7 7 x 9 2 y CSE5311 Design and Analysis of Algorithms 23

Example z 0 6 7 u 2 8 5 2 2 3 4 v 4 7 7 x 9 2 y CSE5311 Design and Analysis of Algorithms 24

Example z 0 6 7 u 2 8 5 2 2 3 4 v 4 7 7 x 9-2 y CSE5311 Design and Analysis of Algorithms 25

Another Look Note: This is essentially dynamic programming. Let d(i, j) = cost of the shortest path from s to i that is at most j hops. d(i, j) = 0 if i = s j = 0 if i s j = 0 min({d(k, j 1) + w(k, i): i Adj(k)} {d(i, j 1)}) if j > 0 j i z u v x y 1 2 3 4 5 0 0 1 0 6 7 2 0 6 4 7 2 3 0 2 4 7 2 4 0 2 4 7 2 CSE5311 Design and Analysis of Algorithms 26

Lemma 24.2 Lemma 24.2: Assuming no negative-weight cycles reachable from s, d[v] = δ(s, v) holds upon termination for all vertices v reachable from s. Proof: Consider a SP p, where p = v 0, v 1,, v k, where v 0 = s and v k = v. Assume k V 1, otherwise p has a cycle. Claim: d[v i ] = δ(s, v i ) holds after the i th pass over edges. Proof follows by induction on i. By Lemma 24.11, once d[v i ] = δ(s, v i ) holds, it continues to hold. CSE5311 Design and Analysis of Algorithms 27

Correctness Claim: Algorithm returns the correct value. (Part of Theorem 24.4. Other parts of the theorem follow easily from earlier results.) Case 1: There is no reachable negative-weight cycle. Upon termination, we have for all (u, v): d[v] = δ(s, v), by lemma 24.2 (last slide) if v is reachable; d[v] = δ(s, v) = otherwise. δ(s, u) + w(u, v), by Lemma 24.10. = d[u] + w(u, v) So, algorithm returns true. CSE5311 Design and Analysis of Algorithms 28

Case 2 Case 2: There exists a reachable negative-weight cycle c = v 0, v 1,, v k, where v 0 = v k. We have Σ i = 1,, k w(v i-1, v i ) < 0. (*) Suppose algorithm returns true. Then, d[v i ] d[v i-1 ] + w(v i-1, v i ) for i = 1,, k. (because Relax didn t change any d[v i ] ). Thus, Σ i = 1,, k d[v i ] Σ i = 1,, k d[v i-1 ] + Σ i = 1,, k w(v i-1, v i ) But, Σ i = 1,, k d[v i ] = Σ i = 1,, k d[v i-1 ]. Can show no d[v i ] is infinite. Hence, 0 Σ i = 1,, k w(v i-1, v i ). Contradicts (*). Thus, algorithm returns false. CSE5311 Design and Analysis of Algorithms 29

Shortest Paths in DAGs Topologically sort vertices in G; Initialize(G, s); for each u in V[G] (in order) do for each v in Adj[u] do Relax(u, v, w) od od CSE5311 Design and Analysis of Algorithms 30

Example 6 1 r s t u v w 5 2 7 1 2 0 3 4 2 CSE5311 Design and Analysis of Algorithms 31

Example 6 1 r s t u v w 5 2 7 1 2 0 3 4 2 CSE5311 Design and Analysis of Algorithms 32

Example 6 1 r s t u v w 5 2 7 1 2 0 2 6 3 4 2 CSE5311 Design and Analysis of Algorithms 33

Example 6 1 r s t u v w 5 2 7 1 2 0 2 6 6 4 3 4 2 CSE5311 Design and Analysis of Algorithms 34

Example 6 1 r s t u v w 5 2 7 1 2 0 2 6 5 4 3 4 2 CSE5311 Design and Analysis of Algorithms 35

Example 6 1 r s t u v w 5 2 7 1 2 0 2 6 5 3 3 4 2 CSE5311 Design and Analysis of Algorithms 36

Example 6 1 r s t u v w 5 2 7 1 2 0 2 6 5 3 3 4 2 CSE5311 Design and Analysis of Algorithms 37

Dijkstra s Algorithm Assumes no negative-weight edges. Maintains a set S of vertices whose SP from s has been determined. Repeatedly selects u in V S with minimum SP estimate (greedy choice). Store V S in priority queue Q. Initialize(G, s); S := ; Q := V[G]; while Q do u := Extract-Min(Q); S := S {u}; for each v Adj[u] do Relax(u, v, w) od od CSE5311 Design and Analysis of Algorithms 38

Example u 1 v s 0 10 2 3 9 4 6 5 7 x 2 y CSE5311 Design and Analysis of Algorithms 39

Example u 10 1 v s 0 10 2 3 9 4 6 5 7 5 x 2 y CSE5311 Design and Analysis of Algorithms 40

Example u 8 1 v 14 s 0 10 2 3 9 4 6 5 7 5 x 2 7 y CSE5311 Design and Analysis of Algorithms 41

Example u 8 1 v 13 s 0 10 2 3 9 4 6 5 7 5 x 2 7 y CSE5311 Design and Analysis of Algorithms 42

Example u 8 1 v 9 s 0 10 2 3 9 4 6 5 7 5 x 2 7 y CSE5311 Design and Analysis of Algorithms 43

Example u 8 1 v 9 s 0 10 2 3 9 4 6 5 7 5 x 2 7 y CSE5311 Design and Analysis of Algorithms 44

Correctness Theorem 24.6: Upon termination, d[u] = δ(s, u) for all u in V (assuming non-negative weights). Proof: By Lemma 24.11, once d[u] = δ(s, u) holds, it continues to hold. We prove: For each u in V, d[u] = δ(s, u) when u is inserted in S. Suppose not. Let u be the first vertex such that d[u] δ(s, u) when inserted in S. Note that d[s] = δ(s, s) = 0 when s is inserted, so u s. S just before u is inserted (in fact, s S). CSE5311 Design and Analysis of Algorithms 45

Proof (Continued) Note that there exists a path from s to u, for otherwise d[u] = δ(s, u) = by Corollary 24.12. there exists a SP from s to u. SP looks like this: p 2 s p 1 u S x y CSE5311 Design and Analysis of Algorithms 46

Proof (Continued) Claim: d[y] = δ(s, y) when u is inserted into S. We had d[x] = δ(s, x) when x was inserted into S. Edge (x, y) was relaxed at that time. By Lemma 24.14, this implies the claim. Now, we have: d[y] = δ(s, y), by Claim. δ(s, u), nonnegative edge weights. d[u], by Lemma 24.11. Because u was added to S before y, d[u] d[y]. Thus, d[y] = δ(s, y) = δ(s, u) = d[u]. Contradiction. CSE5311 Design and Analysis of Algorithms 47

Complexity Running time is O(V 2 ) using linear array for priority queue. O((V + E) lg V) using binary heap. O(V lg V + E) using Fibonacci heap. (See book.) CSE5311 Design and Analysis of Algorithms 48