Greedy algorithms: greed is good? Greedy algorithms Shortest paths in weighted graphs Greed, for lack of a better word, is good. Greed is right. Greed works. Greed clari fi es, cuts Tyler Moore through, and captures, the essence of the evolutionary spirit. CSE 3353, SMU, Dallas, TX Greed, in all of its forms; greed for life, for money, for love, Lecture 13 knowledge, has marked the upward surge of mankind and greed, you mark my words, will not only save Teldar Paper, but Some slides created by or adapted from Dr. Kevin Wayne. For more information see that other malfunctioning http://www.cs.princeton.edu/~wayne/kleinberg-tardos . Some code reused from Python Algorithms by Magnus Lie corporation called the U.S.A. Hetland. 2 / 24 Greedy algorithms ���������������������� Problem. Given a digraph � ���� � �� � � , edge lengths � �� � ≥ ��� , source �� ∈ �� , and destination �� ∈ �� , find the shortest directed path from � to � . A greedy algorithm builds a solution incrementally, making the best local decision to construct a global solution The clever thing about greedy algorithms is that they fi nd ways to 3 1 15 consider only a portion of the solution space at each step 5 4 12 We’ve already seen two greedy algorithms �������� 0 3 8 Gale-Shapley algorithm to solve stable-matching problem: men propose 1 9 7 7 2 to their best choice, women accept/decline without considering other 9 prospective o ff ers 6 1 11 5 Earliest- fi nish algorithm to solve interval-scheduling problem: choose 2 5 the job that fi nishes fi rst and doesn’t con fl ict with jobs already 4 13 accepted 6 4 20 ������������� ������������������������������������ 3 3 / 24 4 / 24
�������������� �������������������������� � PERT/CPM. � Map routing. � Seam carving. � Robot navigation. � Texture mapping. � Typesetting in LaTeX. � Urban traffic planning. � Telemarketer operator scheduling. � Routing of telecommunications messages. � Network routing protocols (OSPF , BGP , RIP). � Optimal truck routing through given traffic congestion pattern. ������������������������������������������������������������������������������������������������������������������������������ ����� 4 5 5 / 24 6 / 24 The many cases of fi nding shortest paths Weighted Graph Data Structures Nested Adjacency Dictionaries w/ Edge Weights N = { We’ve already seen how to calculate the shortest path in an ’ a ’ : { ’ b ’ : 2 , ’ c ’ : 1 , ’ d ’ : 3 , ’ e ’ : 9 , ’ f ’ :4 } , ’ b ’ : { ’ c ’ : 4 , ’ e ’ :3 } , unweighted graph (BFS traversal) ’ c ’ : { ’ d ’ : 8 } , 4 We’ll now study how to compute the shortest path in di ff erent g c ’ d ’ : { ’ e ’ : 7 } , b 2 ’ e ’ : { ’ f ’ : 5 } , circumstances for weighted graphs 1 ’ f ’ : { ’ c ’ : 2 , ’ g ’ : 2 , ’ h ’ : 2 } , 2 2 1 Single-source shortest path on a weighted DAG 1 ’ g ’ : { ’ f ’ : 1 , ’ h ’ :6 } , 4 Single-source shortest path on a weighted graph with nonnegative ’ h ’ : { ’ f ’ : 9 , ’ g ’ :8 } 2 a 8 3 f 8 6 } weights (Dijkstra’s algorithm) 2 9 > ’ b ’ i n N[ ’ a ’ ] # Neighborhood membership > > 3 5 Single-source shortest path on a weighted graph including negative 3 9 True weights (Bellman-Ford algorithm) > l e n (N[ ’ f ’ ] ) # Degree 7 > > e d h 3 > N[ ’ a ’ ] [ ’ b ’ ] > > # Edge weight f o r ( a , b ) 2 7 / 24 8 / 24
Shortest paths in DAGs Recursive solution to fi nding shortest path in DAGs def r e c d a g s p (W, s , t ) : #Shortest path from s to t @memo #Memoize f Recursive approach to fi nding the shortest path from a to z def d(u ) : #Distance from u to t Assume we already know the distance d ( v ) to z for each of a ’s 1 i f u == t : return 0 # We ’ re the re ! neighbors v ∈ G [ a ] # Return the best of every f i r s t step Select the neighbor v that minimizes d ( v ) + W ( a , v ) 2 return min (W[ u ] [ v]+d( v ) for v in W[ u ] ) return d( s ) #Apply f to a c t u a l s t a r t node 9 / 24 10 / 24 Shortest paths in DAGs: Iterative approach Relaxing edges s 1: d[v] = 13 2: d[u] = 7 The iterative solution is a bit more complicated We must start with a topological sort 1 v 3 = Keep track of an upper bound on the distance from a to each node, 2 ] v [ ] u initialized to ∞ [ W : 2 Go through each vertex and relax the distance estimate by inspecting 3 the path from the vertex to its neighbor u In general, relaxing an edge ( u , v ) consists of testing whether we can shorten the path to v found so far by going through u ; if we can, we update d [ v ] with the new value Running time: Θ ( m + n ) 11 / 24 12 / 24
Relaxing edges Iterative solution to fi nding shortest path in DAGs s 3: d[v] = 13 10 2: d[u] = 7 def dag sp (W, s , t ) : #Shorte st path from s to t v 3 = d = { u : f l o a t ( ’ i n f ’ ) for u in W } # Distance e s t i m a t e s ] v [ ] u [ d [ s ] = 0 #S ta rt node : Zero d i s t a n c e W : 2 for u in t o p s o r t (W) : #In top − s o r t e d order . . . i f u == t : break #Have we a r r i v e d ? u for v in W[ u ] : #For each out − edge . . . d [ v ] = min (d [ v ] , d [ u ] + W[ u ] [ v ] ) # Relax the edge i n f = f l o a t ( ’ i n f ’ ) return d [ t ] #Distance to t ( from s ) def r e l a x (W, u , v , D, P ) : d = D. get (u , i n f ) + W[ u ] [ v ] # P o s s i b l e s h o r t c u t estimate i f d < D. get ( v , i n f ) : # I s i t r e a l l y a s h o r t c u t ? D[ v ] , P[ v ] = d , u # Update estimate and parent return True # There was a change ! 12 / 24 13 / 24 Shortest-paths on weighted DAG example Shortest-paths on weighted DAG: exercise 3 b b d d 5 3 1 7 1 6 5 a a c c e e Topological sort: a, c, b, d, e d[Node] : upper bd. dist. from a Node init. 1 (u=a) 2 (u=c) 3 (u=b) 4 (u=d) 5 (u=e) a 0 0 0 0 0 0 b 15 13 13 13 13 ∞ c 6 6 6 6 6 ∞ d 9 9 9 9 ∞ ∞ e 11 11 10 10 ∞ ∞ 14 / 24 15 / 24
Recommend
More recommend