introduction
play

Introduction CSCE423/823 CSCE423/823 Given a weighted, directed - PDF document

Introduction CSCE423/823 CSCE423/823 Given a weighted, directed graph G = ( V, E ) with weight function Computer Science & Engineering 423/823 w : E ! R The weight of path p = h v 0 , v 1 , . . . , v k i is the sum of the weights of


  1. Introduction CSCE423/823 CSCE423/823 Given a weighted, directed graph G = ( V, E ) with weight function Computer Science & Engineering 423/823 w : E ! R The weight of path p = h v 0 , v 1 , . . . , v k i is the sum of the weights of Introduction Design and Analysis of Algorithms Introduction Optimal Bellman-Ford its edges: Substructure of Algorithm a Shortest Path Negative-Weight k Lecture 05 — Single-Source Shortest Paths (Chapter 24) Edges SSSPs in X w ( p ) = w ( v i − 1 , v i ) Cycles Directed Relaxation Acyclic Graphs i =1 Bellman-Ford Dijkstra’s Algorithm Then the shortest-path weight from u to v is Algorithm Stephen Scott SSSPs in Di ff erence Directed p ⇢ Constraints min { w ( p ) : u v } if there is a path from u to v (Adapted from Vinodchandran N. Variyam) Acyclic Graphs � ( u, v ) = and Shortest Paths 1 otherwise Dijkstra’s Algorithm A shortest path from u to v is any path p with weight Di ff erence Constraints w ( p ) = � ( u, v ) and Shortest Paths Applications: Network routing, driving directions 1 / 36 2 / 36 Types of Shortest Path Problems Optimal Substructure of a Shortest Path CSCE423/823 CSCE423/823 Given G as described earlier, Introduction Single-Source Shortest Paths: Find shortest paths from source Introduction The shortest paths problem has the optimal substructure property : Optimal Optimal node s to every other node If p = h v 0 , v 1 , . . . , v k i is a SP from v 0 to v k , then for 0  i  j  k , Substructure of Substructure of a Shortest Path a Shortest Path p ij = h v i , v i +1 , . . . , v j i is a SP from v i to v j Negative-Weight Negative-Weight Single-Destination Shortest Paths: Find shortest paths from every Edges Edges Cycles Cycles p 0 i p ij p jk node to destination t Proof: Let p = v 0 v i v j v k with weight Relaxation Relaxation Bellman-Ford Can solve with SSSP solution. How? Bellman-Ford w ( p ) = w ( p 0 i ) + w ( p ij ) + w ( p jk ) . If there exists a path p 0 ij from v i to Algorithm Algorithm v j with w ( p 0 ij ) < w ( p ij ) , then p is not a SP since Single-Pair Shortest Path: Find shortest path from specific node u SSSPs in SSSPs in to specific node v p 0 Directed Directed p 0 i p jk ij v 0 v i v j v k has less weight than p Acyclic Graphs Acyclic Graphs Can solve via SSSP; no asymptotically faster algorithm known Dijkstra’s Dijkstra’s This property helps us to use a greedy algorithm for this problem Algorithm Algorithm All-Pairs Shortest Paths: Find shortest paths between every pair of Di ff erence Di ff erence nodes Constraints Constraints and Shortest and Shortest Can solve via repeated application of SSSP, but can do better Paths Paths 3 / 36 4 / 36 Negative-Weight Edges (1) Negative-Weight Edges (2) CSCE423/823 CSCE423/823 Introduction Introduction Optimal Optimal Substructure of Substructure of a Shortest Path a Shortest Path Negative-Weight Negative-Weight What happens if the graph G has edges with negative weights? Edges Edges Cycles Cycles Relaxation Relaxation Dijkstra’s algorithm cannot handle this, Bellman-Ford can, under the Bellman-Ford Bellman-Ford right circumstances (which circumstances?) Algorithm Algorithm SSSPs in SSSPs in Directed Directed Acyclic Graphs Acyclic Graphs Dijkstra’s Dijkstra’s Algorithm Algorithm Di ff erence Di ff erence Constraints Constraints and Shortest and Shortest Paths Paths 5 / 36 6 / 36

  2. Cycles Relaxation CSCE423/823 CSCE423/823 Introduction Introduction Optimal Optimal Substructure of Substructure of a Shortest Path a Shortest Path Given weighted graph G = ( V, E ) with source node s 2 V and other Negative-Weight What kinds of cycles might appear in a shortest path? Negative-Weight Edges Edges node v 2 V ( v 6 = s ), we’ll maintain d [ v ] , which is upper bound on Cycles Cycles Negative-weight cycle Relaxation Relaxation � ( s, v ) Zero-weight cycle Bellman-Ford Bellman-Ford Algorithm Algorithm Relaxation of an edge ( u, v ) is the process of testing whether we Positive-weight cycle SSSPs in SSSPs in can decrease d [ v ] , yielding a tighter upper bound Directed Directed Acyclic Graphs Acyclic Graphs Dijkstra’s Dijkstra’s Algorithm Algorithm Di ff erence Di ff erence Constraints Constraints and Shortest and Shortest Paths Paths 7 / 36 8 / 36 Initialize-Single-Source( G, s ) Relax( u, v, w ) CSCE423/823 CSCE423/823 Introduction Introduction Optimal Optimal for each vertex v 2 V do Substructure of Substructure of if d [ v ] > d [ u ] + w ( u, v ) then a Shortest Path a Shortest Path d [ v ] = 1 Negative-Weight Negative-Weight 1 d [ v ] = d [ u ] + w ( u, v ) Edges Edges 1 Cycles Cycles ⇡ [ v ] = nil 2 Relaxation Relaxation ⇡ [ v ] = u 2 Bellman-Ford Bellman-Ford 3 end Algorithm Algorithm 3 SSSPs in 4 d [ s ] = 0 SSSPs in Directed Directed Acyclic Graphs Acyclic Graphs Dijkstra’s Dijkstra’s How do we know that we can tighten d [ v ] like this? Algorithm Algorithm How is the invariant maintained? Di ff erence Di ff erence Constraints Constraints and Shortest and Shortest Paths Paths 9 / 36 10 / 36 Relaxation Example Bellman-Ford Algorithm CSCE423/823 CSCE423/823 Introduction Introduction Optimal Bellman-Ford Substructure of Greedy algorithm a Shortest Path Algorithm Negative-Weight Introduction Edges Works with negative-weight edges and detects if there is a The Algorithm Cycles Example Relaxation Analysis negative-weight cycle Bellman-Ford SSSPs in Algorithm Makes | V | � 1 passes over all edges, relaxing each edge during each Directed Acyclic Graphs SSSPs in pass Directed Dijkstra’s Acyclic Graphs Algorithm Dijkstra’s Di ff erence Algorithm Constraints Numbers in nodes are values of d and Shortest Di ff erence Constraints Paths and Shortest Paths 11 / 36 12 / 36

  3. Bellman-Ford( G, w, s ) Bellman-Ford Algorithm Example (1) CSCE423/823 CSCE423/823 Initialize-Single-Source ( G, s ) 1 for i = 1 to | V | − 1 do Introduction Introduction for each edge ( u, v ) ∈ E do 2 Bellman-Ford Bellman-Ford Algorithm Relax ( u, v, w ) Algorithm 3 Introduction Introduction end The Algorithm 4 The Algorithm Example Example 5 end Analysis Analysis SSSPs in 6 for each edge ( u, v ) ∈ E do SSSPs in Directed Directed if d [ v ] > d [ u ] + w ( u, v ) then 7 Acyclic Graphs Acyclic Graphs return false // G has a negative-wt cycle 8 Dijkstra’s Dijkstra’s Within each pass, edges relaxed in this order: Algorithm Algorithm 9 ( t, x ) , ( t, y ) , ( t, z ) , ( x, t ) , ( y, x ) , ( y, z ) , ( z, x ) , ( z, s ) , ( s, t ) , ( s, y ) Di ff erence Di ff erence 10 end Constraints Constraints and Shortest and Shortest 11 return true // G has no neg-wt cycle reachable frm Paths Paths s 13 / 36 14 / 36 Time Complexity of Bellman-Ford Algorithm Bellman-Ford Algorithm Example (2) CSCE423/823 CSCE423/823 Introduction Introduction Bellman-Ford Bellman-Ford Initialize-Single-Source takes how much time? Algorithm Algorithm Introduction Introduction Relax takes how much time? The Algorithm The Algorithm Example Example Analysis Analysis What is time complexity of relaxation steps (nested loops)? SSSPs in SSSPs in What is time complexity of steps to check for negative-weight cycles? Directed Directed Acyclic Graphs Acyclic Graphs What is total time complexity? Dijkstra’s Dijkstra’s Algorithm Algorithm Di ff erence Di ff erence Within each pass, edges relaxed in this order: Constraints Constraints and Shortest ( t, x ) , ( t, y ) , ( t, z ) , ( x, t ) , ( y, x ) , ( y, z ) , ( z, x ) , ( z, s ) , ( s, t ) , ( s, y ) and Shortest Paths Paths 15 / 36 16 / 36 Correctness of Bellman-Ford Algorithm Correctness of Bellman-Ford Algorithm (2) CSCE423/823 CSCE423/823 Let c = h v 0 , v 1 , . . . , v k = v 0 i be neg-weight cycle reachable from s : Assume no negative-weight cycles k X w ( v i − 1 , v i ) < 0 Introduction Introduction Since no cycles appear in SPs, every SP has at most | V | � 1 edges Bellman-Ford Bellman-Ford i =1 Then define sets S 0 , S 1 , . . . S | V | − 1 : Algorithm Algorithm If algorithm incorrectly returns true , then (due to Line 8) for all Introduction Introduction The Algorithm The Algorithm nodes in the cycle ( i = 1 , 2 , . . . , k ), Example p Example S k = { v 2 V : 9 s v s.t. � ( s, v ) = w ( p ) and | p |  k } Analysis Analysis d [ v i ]  d [ v i − 1 ] + w ( v i − 1 , v i ) SSSPs in SSSPs in Directed Directed Loop invariant: After i th iteration of outer relaxation loop (Line 2), Acyclic Graphs Acyclic Graphs By summing, we get for all v 2 S i , we have d [ v ] = � ( s, v ) Dijkstra’s Dijkstra’s Algorithm Algorithm k k k Can prove via induction X X X Di ff erence Di ff erence d [ v i ]  d [ v i − 1 ] + w ( v i − 1 , v i ) Implies that, after | V | � 1 iterations, d [ v ] = � ( s, v ) for all Constraints Constraints and Shortest and Shortest i =1 i =1 i =1 v 2 V = S | V | − 1 Paths Paths Since v 0 = v k , P k i =1 d [ v i ] = P k i =1 d [ v i − 1 ] This implies that 0  P k i =1 w ( v i − 1 , v i ) , a contradiction 17 / 36 18 / 36

Recommend


More recommend