computer science engineering 423 823
play

Computer Science & Engineering 423/823 Introduction Design and - PowerPoint PPT Presentation

CSCE423/823 Computer Science & Engineering 423/823 Introduction Design and Analysis of Algorithms Bellman-Ford Algorithm Lecture 05 Single-Source Shortest Paths (Chapter 24) SSSPs in Directed Acyclic Graphs Dijkstras Algorithm


  1. CSCE423/823 Computer Science & Engineering 423/823 Introduction Design and Analysis of Algorithms Bellman-Ford Algorithm Lecture 05 — Single-Source Shortest Paths (Chapter 24) SSSPs in Directed Acyclic Graphs Dijkstra’s Algorithm Stephen Scott Difference Constraints (Adapted from Vinodchandran N. Variyam) and Shortest Paths 1 / 36

  2. Introduction CSCE423/823 Given a weighted, directed graph G = ( V, E ) with weight function w : E → R The weight of path p = � v 0 , v 1 , . . . , v k � is the sum of the weights of Introduction Optimal its edges: Substructure of a Shortest Path k Negative-Weight Edges � w ( p ) = w ( v i − 1 , v i ) Cycles Relaxation i =1 Bellman-Ford Algorithm Then the shortest-path weight from u to v is SSSPs in Directed p � min { w ( p ) : u � v } if there is a path from u to v Acyclic Graphs δ ( u, v ) = ∞ otherwise Dijkstra’s Algorithm Difference A shortest path from u to v is any path p with weight Constraints w ( p ) = δ ( u, v ) and Shortest Paths Applications: Network routing, driving directions 2 / 36

  3. Types of Shortest Path Problems CSCE423/823 Given G as described earlier, Single-Source Shortest Paths: Find shortest paths from source Introduction Optimal node s to every other node Substructure of a Shortest Path Negative-Weight Single-Destination Shortest Paths: Find shortest paths from every Edges Cycles node to destination t Relaxation Bellman-Ford Can solve with SSSP solution. How? Algorithm Single-Pair Shortest Path: Find shortest path from specific node u SSSPs in Directed to specific node v Acyclic Graphs Can solve via SSSP; no asymptotically faster algorithm known Dijkstra’s Algorithm All-Pairs Shortest Paths: Find shortest paths between every pair of Difference nodes Constraints and Shortest Can solve via repeated application of SSSP, but can do better Paths 3 / 36

  4. Optimal Substructure of a Shortest Path CSCE423/823 Introduction The shortest paths problem has the optimal substructure property : Optimal If p = � v 0 , v 1 , . . . , v k � is a SP from v 0 to v k , then for 0 ≤ i ≤ j ≤ k , Substructure of a Shortest Path p ij = � v i , v i +1 , . . . , v j � is a SP from v i to v j Negative-Weight Edges Cycles p ij p jk p 0 i Proof: Let p = v 0 � v k with weight � v i � v j Relaxation Bellman-Ford w ( p ) = w ( p 0 i ) + w ( p ij ) + w ( p jk ) . If there exists a path p ′ ij from v i to Algorithm v j with w ( p ′ ij ) < w ( p ij ) , then p is not a SP since SSSPs in p ′ Directed p 0 i p jk ij v 0 � v i � v j � v k has less weight than p Acyclic Graphs Dijkstra’s This property helps us to use a greedy algorithm for this problem Algorithm Difference Constraints and Shortest Paths 4 / 36

  5. Negative-Weight Edges (1) CSCE423/823 Introduction Optimal Substructure of a Shortest Path Negative-Weight What happens if the graph G has edges with negative weights? Edges Cycles Relaxation Dijkstra’s algorithm cannot handle this, Bellman-Ford can, under the Bellman-Ford right circumstances (which circumstances?) Algorithm SSSPs in Directed Acyclic Graphs Dijkstra’s Algorithm Difference Constraints and Shortest Paths 5 / 36

  6. Negative-Weight Edges (2) CSCE423/823 Introduction Optimal Substructure of a Shortest Path Negative-Weight Edges Cycles Relaxation Bellman-Ford Algorithm SSSPs in Directed Acyclic Graphs Dijkstra’s Algorithm Difference Constraints and Shortest Paths 6 / 36

  7. Cycles CSCE423/823 Introduction Optimal Substructure of a Shortest Path Negative-Weight What kinds of cycles might appear in a shortest path? Edges Cycles Negative-weight cycle Relaxation Zero-weight cycle Bellman-Ford Algorithm Positive-weight cycle SSSPs in Directed Acyclic Graphs Dijkstra’s Algorithm Difference Constraints and Shortest Paths 7 / 36

  8. Relaxation CSCE423/823 Introduction Optimal Substructure of a Shortest Path Given weighted graph G = ( V, E ) with source node s ∈ V and other Negative-Weight Edges node v ∈ V ( v � = s ), we’ll maintain d [ v ] , which is upper bound on Cycles Relaxation δ ( s, v ) Bellman-Ford Algorithm Relaxation of an edge ( u, v ) is the process of testing whether we SSSPs in can decrease d [ v ] , yielding a tighter upper bound Directed Acyclic Graphs Dijkstra’s Algorithm Difference Constraints and Shortest Paths 8 / 36

  9. Initialize-Single-Source( G, s ) CSCE423/823 Introduction Optimal for each vertex v ∈ V do Substructure of a Shortest Path d [ v ] = ∞ Negative-Weight 1 Edges Cycles π [ v ] = nil 2 Relaxation Bellman-Ford 3 end Algorithm SSSPs in 4 d [ s ] = 0 Directed Acyclic Graphs Dijkstra’s Algorithm How is the invariant maintained? Difference Constraints and Shortest Paths 9 / 36

  10. Relax( u, v, w ) CSCE423/823 Introduction Optimal Substructure of if d [ v ] > d [ u ] + w ( u, v ) then a Shortest Path Negative-Weight d [ v ] = d [ u ] + w ( u, v ) Edges 1 Cycles Relaxation π [ v ] = u 2 Bellman-Ford Algorithm 3 SSSPs in Directed Acyclic Graphs Dijkstra’s How do we know that we can tighten d [ v ] like this? Algorithm Difference Constraints and Shortest Paths 10 / 36

  11. Relaxation Example CSCE423/823 Introduction Optimal Substructure of a Shortest Path Negative-Weight Edges Cycles Relaxation Bellman-Ford Algorithm SSSPs in Directed Acyclic Graphs Dijkstra’s Algorithm Numbers in nodes are values of d Difference Constraints and Shortest Paths 11 / 36

  12. Bellman-Ford Algorithm CSCE423/823 Introduction Bellman-Ford Greedy algorithm Algorithm Introduction Works with negative-weight edges and detects if there is a The Algorithm Example negative-weight cycle Analysis SSSPs in Makes | V | − 1 passes over all edges, relaxing each edge during each Directed Acyclic Graphs pass Dijkstra’s Algorithm Difference Constraints and Shortest Paths 12 / 36

  13. Bellman-Ford( G, w, s ) CSCE423/823 Initialize-Single-Source ( G, s ) 1 for i = 1 to | V | − 1 do Introduction for each edge ( u, v ) ∈ E do 2 Bellman-Ford Relax ( u, v, w ) Algorithm 3 Introduction end 4 The Algorithm Example 5 end Analysis SSSPs in 6 for each edge ( u, v ) ∈ E do Directed if d [ v ] > d [ u ] + w ( u, v ) then 7 Acyclic Graphs return false // G has a negative-wt cycle 8 Dijkstra’s Algorithm 9 Difference 10 end Constraints and Shortest 11 return true // G has no neg-wt cycle reachable frm Paths s 13 / 36

  14. Bellman-Ford Algorithm Example (1) CSCE423/823 Introduction Bellman-Ford Algorithm Introduction The Algorithm Example Analysis SSSPs in Directed Acyclic Graphs Dijkstra’s Within each pass, edges relaxed in this order: Algorithm ( t, x ) , ( t, y ) , ( t, z ) , ( x, t ) , ( y, x ) , ( y, z ) , ( z, x ) , ( z, s ) , ( s, t ) , ( s, y ) Difference Constraints and Shortest Paths 14 / 36

  15. Bellman-Ford Algorithm Example (2) CSCE423/823 Introduction Bellman-Ford Algorithm Introduction The Algorithm Example Analysis SSSPs in Directed Acyclic Graphs Dijkstra’s Algorithm Difference Within each pass, edges relaxed in this order: Constraints and Shortest ( t, x ) , ( t, y ) , ( t, z ) , ( x, t ) , ( y, x ) , ( y, z ) , ( z, x ) , ( z, s ) , ( s, t ) , ( s, y ) Paths 15 / 36

  16. Time Complexity of Bellman-Ford Algorithm CSCE423/823 Introduction Bellman-Ford Initialize-Single-Source takes how much time? Algorithm Introduction Relax takes how much time? The Algorithm Example Analysis What is time complexity of relaxation steps (nested loops)? SSSPs in What is time complexity of steps to check for negative-weight cycles? Directed Acyclic Graphs What is total time complexity? Dijkstra’s Algorithm Difference Constraints and Shortest Paths 16 / 36

  17. Correctness of Bellman-Ford Algorithm CSCE423/823 Assume no negative-weight cycles Introduction Since no cycles appear in SPs, every SP has at most | V | − 1 edges Bellman-Ford Then define sets S 0 , S 1 , . . . S | V |− 1 : Algorithm Introduction The Algorithm Example p S k = { v ∈ V : ∃ s � v s.t. δ ( s, v ) = w ( p ) and | p | ≤ k } Analysis SSSPs in Directed Loop invariant: After i th iteration of outer relaxation loop (Line 2), Acyclic Graphs for all v ∈ S i , we have d [ v ] = δ ( s, v ) Dijkstra’s Algorithm Can prove via induction Difference Implies that, after | V | − 1 iterations, d [ v ] = δ ( s, v ) for all Constraints and Shortest v ∈ V = S | V |− 1 Paths 17 / 36

  18. Correctness of Bellman-Ford Algorithm (2) CSCE423/823 Let c = � v 0 , v 1 , . . . , v k = v 0 � be neg-weight cycle reachable from s : k � w ( v i − 1 , v i ) < 0 Introduction Bellman-Ford i =1 Algorithm If algorithm incorrectly returns true , then (due to Line 8) for all Introduction The Algorithm nodes in the cycle ( i = 1 , 2 , . . . , k ), Example Analysis d [ v i ] ≤ d [ v i − 1 ] + w ( v i − 1 , v i ) SSSPs in Directed Acyclic Graphs By summing, we get Dijkstra’s Algorithm k k k � � � Difference d [ v i ] ≤ d [ v i − 1 ] + w ( v i − 1 , v i ) Constraints and Shortest i =1 i =1 i =1 Paths Since v 0 = v k , � k i =1 d [ v i ] = � k i =1 d [ v i − 1 ] This implies that 0 ≤ � k i =1 w ( v i − 1 , v i ) , a contradiction 18 / 36

Recommend


More recommend