introduction computer science engineering 423 823
play

Introduction Computer Science & Engineering 423/823 I Given a - PDF document

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


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

  2. Cycles Relaxation I Given weighted graph G = ( V , E ) with source node s 2 V and other I What kinds of cycles might appear in a shortest path? node v 2 V ( v 6 = s ), we’ll maintain d [ v ], which is upper bound on I Negative-weight cycle � ( s , v ) I Zero-weight cycle I Relaxation of an edge ( u , v ) is the process of testing whether we can I Positive-weight cycle decrease d [ v ], yielding a tighter upper bound Initialize-Single-Source( G , s ) Relax( u , v , w ) 1 for each vertex v 2 V do 1 if d [ v ] > d [ u ] + w ( u , v ) then d [ v ] = 1 2 d [ v ] = d [ u ] + w ( u , v ) 2 ⇡ [ v ] = nil ⇡ [ v ] = u 3 3 4 end 4 5 d [ s ] = 0 How do we know that we can tighten d [ v ] like this? Relaxation Example Bellman-Ford Algorithm I Works with negative-weight edges and detects if there is a negative-weight cycle I Makes | V | � 1 passes over all edges, relaxing each edge during each pass I No cycles implies all shortest paths have  | V | � 1 edges, so that number of relaxations is su ffi cient Numbers in nodes are values of d

  3. Bellman-Ford( G , w , s ) Bellman-Ford Algorithm Example (1) 1 Initialize-Single-Source ( G , s ) 2 for i = 1 to | V | − 1 do for each edge ( u , v ) ∈ E do 3 Relax ( u , v , w ) 4 end 5 6 end 7 for each edge ( u , v ) ∈ E do if d [ v ] > d [ u ] + w ( u , v ) then Within each pass, edges relaxed in this order: 8 return false // G has a negative-wt cycle 9 ( t , x ) , ( t , y ) , ( t , z ) , ( x , t ) , ( y , x ) , ( y , z ) , ( z , x ) , ( z , s ) , ( s , t ) , ( s , y ) 10 11 end 12 return true // G has no neg-wt cycle reachable frm s Bellman-Ford Algorithm Example (2) Time Complexity of Bellman-Ford Algorithm I Initialize-Single-Source takes how much time? I Relax takes how much time? I What is time complexity of relaxation steps (nested loops)? I What is time complexity of steps to check for negative-weight cycles? I What is total time complexity? Within each pass, edges relaxed in this order: ( t , x ) , ( t , y ) , ( t , z ) , ( x , t ) , ( y , x ) , ( y , z ) , ( z , x ) , ( z , s ) , ( s , t ) , ( s , y ) Correctness of Bellman-Ford: Finds SP Lengths Correctness of Bellman-Ford: Detects Negative-Weight Cycles I Let c = h v 0 , v 1 , . . . , v k = v 0 i be neg-weight cycle reachable from s : I Assume no negative-weight cycles k I Since no cycles appear in SPs, every SP has at most | V | � 1 edges X w ( v i � 1 , v i ) < 0 I Then define sets S 0 , S 1 , . . . S | V | � 1 : i =1 p I If algorithm incorrectly returns true , then (due to Line 8) for all nodes S k = { v 2 V : 9 s v s.t. � ( s , v ) = w ( p ) and | p |  k } in the cycle ( i = 1 , 2 , . . . , k ), I Loop invariant: After i th iteration of outer relaxation loop (Line 2), for d [ v i ]  d [ v i � 1 ] + w ( v i � 1 , v i ) all v 2 S i , we have d [ v ] = � ( s , v ) I aka path-relaxation property (Lemma 24.15) I By summing, we get I Can prove via induction on i : I Obvious for i = 0 k k k X X X I If holds for v ∈ S i � 1 , then definition of relaxation and optimal substructure d [ v i ]  d [ v i � 1 ] + w ( v i � 1 , v i ) ⇒ holds for v ∈ S i i =1 i =1 i =1 I Implies that, after | V | � 1 iterations, d [ v ] = � ( s , v ) for all I Since v 0 = v k , P k i =1 d [ v i ] = P k i =1 d [ v i � 1 ] v 2 V = S | V | � 1 I This implies that 0  P k i =1 w ( v i � 1 , v i ), a contradiction

  4. SSSPs in Directed Acyclic Graphs Dag-Shortest-Paths( G , w , s ) I Why did Bellman-Ford have to run | V | � 1 iterations of edge relaxations? I To confirm that SP information fully propagated to all nodes (path-relaxation property) 1 topologically sort the vertices of G 2 Initialize-Single-Source ( G , s ) 3 for each vertex u 2 V , taken in topo sorted order do for each v 2 Adj [ u ] do 4 Relax ( u , v , w ) 5 end 6 I What if we knew that, after we relaxed an edge just once, we would be 7 end completely done with it? I Can do this if G a dag and we relax edges in correct order (what order?) SSSP dag Example (1) SSSP dag Example (2) Analysis Dijkstra’s Algorithm I Greedy algorithm I Correctness follows from path-relaxation property similar to I Faster than Bellman-Ford Bellman-Ford, except that relaxing edges in topologically sorted order implies we relax the edges of a shortest path in order I Requires all edge weights to be nonnegative I Topological sort takes how much time? I Maintains set S of vertices whose final shortest path weights from s have been determined I Initialize-Single-Source takes how much time? I Repeatedly select u 2 V \ S with minimum SP estimate, add u to S , and I How many calls to Relax ? relax all edges leaving u I What is total time complexity? I Uses min-priority queue to repeatedly make greedy choice

  5. Dijkstra( G , w , s ) Dijkstra’s Algorithm Example (1) 1 Initialize-Single-Source ( G , s ) 2 S = ; 3 Q = V 4 while Q 6 = ; do u = Extract-Min ( Q ) 5 S = S [ { u } 6 for each v 2 Adj [ u ] do 7 Relax ( u , v , w ) 8 end 9 10 end Dijkstra’s Algorithm Example (2) Time Complexity of Dijkstra’s Algorithm I Using array to implement priority queue, I Initialize-Single-Source takes how much time? I What is time complexity to create Q ? I How many calls to Extract-Min ? I What is time complexity of Extract-Min ? I How many calls to Relax ? I What is time complexity of Relax ? I What is total time complexity? I Using heap to implement priority queue, what are the answers to the above questions? I When might you choose one queue implementation over another? Correctness of Dijkstra’s Algorithm Linear Programming I Given an m ⇥ n matrix A and a size- m vector b and a size- n vector c , find I Invariant: At the start of each iteration of the while loop, d [ v ] = � ( s , v ) a vector x of n elements that maximizes P n for all v 2 S i =1 c i x i subject to Ax  b I Proof: Let u be first node added to S where d [ u ] 6 = � ( s , u ) 2 1 1 3 2 22 3 p 1 p 2 I Let p = s x ! y u be SP to u and y first node on p in V � S I E.g., c = 5 implies: ⇥ 2 � 3 ⇤ , A = 1 � 2 5 , b = 4 4 4 I Since y ’s predecessor x 2 S , d [ y ] = � ( s , y ) due to relaxation of ( x , y ) � 1 0 � 8 maximize 2 x 1 � 3 x 2 subject to I Since y precedes u in p and edge wts non-negative: x 1 + x 2  22 d [ y ] = � ( s , y )  � ( s , u )  d [ u ] x 1 � 2 x 2  4 I Since u was chosen before y in line 5, d [ u ]  d [ y ], so � 8 x 1 d [ y ] = � ( s , y ) = � ( s , u ) = d [ u ], a contradiction I Solution: x 1 = 16, x 2 = 6 Since all vertices eventually end up in S , get correctness of the algorithm

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend