greedy strategy
play

Greedy Strategy [14] In the last class Undirected and Symmetric - PDF document

Algorithm : Design & Analysis Greedy Strategy [14] In the last class Undirected and Symmetric Digraph UDF Search Skeleton Biconnected Components Articulation Points and Biconnectedness Biconnected Component Algorithm


  1. Algorithm : Design & Analysis Greedy Strategy [14]

  2. In the last class… � Undirected and Symmetric Digraph � UDF Search Skeleton � Biconnected Components � Articulation Points and Biconnectedness � Biconnected Component Algorithm � Analysis of the Algorithm

  3. Greedy Strategy � Optimization Problem � MST Problem � Prim’s Algorithm � Kruskal’s Algorithm � Single-Source Shortest Path Problem � Dijstra’s Algorithm � Greedy Strategy

  4. Optimizing by Greedy � Coin Change Problem � [candidates] A finite set of coins, of 1, 5, 10 and 25 units, with enough number for each value � [constraints] Pay an exact amount by a selected set of coins � [optimization] a smallest possible number of coins in the selected set � Solution by greedy strategy � For each selection, choose the highest-valued coin as possible.

  5. Greedy Fails Sometimes � If the available coins are of 1,5,12 units, and we have to pay 15 units totally, then the smallest set of coins is {5,5,5}, but not {12,1,1,1} � However, the correctness of greedy strategy on the case of {1,5,10,25} is not straightly seen.

  6. Greedy Strategy � Constructing the final set greedy( set candidate) set greedy( set candidate) solution by expanding the n set S=Ø; set S=Ø; o partial solution step by step, f f ” o n while not solution (S) and in each of which a selection while not solution (S) and o g i n t candidate ≠ Ø i a candidate ≠ Ø is made from a set of d z a i m r ” candidates, with the choice t i y select locally optimizing x : t select locally optimizing x y t p i e made must be: o l K i b from candidate; l from candidate; a i s c � [feasible] it has to satisfy the a o e l candidate=candidate-{ x }; “ f candidate=candidate-{ x }; problem’s constraints “ d if feasible ( x ) then S=S ∪ { x }; n � [locally optimal] it has to be if feasible ( x ) then S=S ∪ { x }; a the best local choice among all if solution (S) then return S if solution (S) then return S feasible choices on the step � [irrevocable] the candidate else return (“no solution”) else return (“no solution”) selected can never be de- selected on subsequent steps

  7. Weighted Graph and MST A Spanning Tree: W ( T )=257 B A Spanning Tree: W ( T )=257 26 27 21 E C 42 A 36 D 25 B 34 29 22 33 16 I 21 18 21 26 H 27 F 21 J 25 E C 28 42 A 17 36 53 D G 34 25 33 29 22 16 I 21 18 B A MST: W ( T )=190 21 A MST: W ( T )=190 H F J 25 26 27 28 17 21 E C 42 A 53 36 D G 29 22 34 25 33 16 I 21 18 21 A weighted graph H F 25 J 28 The nearest neighbor of vertex I is H 17 53 The nearest neighbor of shaded G subset of vertex is G

  8. Graph Traversal and MST All other edges There are cases that with weight 5 graph traversal tree 1 1 cannot be minimum spanning tree, with 1 the vertices explored 1 in any order. 1 BFS tree DFS tree in any ordering of vertex

  9. Greedy Algorithms for MST � Prim’s algorithm: � Difficult selecting: “best local optimization means no cycle and small weight under limitation . � Easy checking: doing nothing � Kruskal’s algorithm: � Easy selecting: smallest in primitive meaning � Difficult checking: no cycle

  10. v 6 v 5 v 4 v 0 ” v 3 Merging Two Vertices v 2 v 6 v 5 v 4 v 0 ’ v 3 v 2 v 6 v 5 v 4 v 0 v 3 v 1

  11. Constructing a Spanning Tree a b a a a b b b d c c c d c d d (1) (2) (3) (0) 0. Let a be the starting vertex, selecting edges one by one in original graph 0. Let a be the starting vertex, selecting edges one by one in original graph 1. Merging a and c into a’({a,c}), selecting (a,c) 1. Merging a and c into a’({a,c}), selecting (a,c) 2. Merging a’ and b into a”({a,c,b}), selecting (c,b) 2. Merging a’ and b into a”({a,c,b}), selecting (c,b) 3. Merging a” and d into a”’({a,c,b,d}), selecting (a,d) or (d,b) 3. Merging a” and d into a”’({a,c,b,d}), selecting (a,d) or (d,b) Ending, as only one vertex left Ending, as only one vertex left

  12. Prim’s Algorithm for MST 2 B A 6 3 Greedy strategy: 7 4 Greedy strategy: G 3 1 For each set of fringe For each set of fringe 5 F I H C vertex, select the edge 2 4 vertex, select the edge 8 with the minimal 2 2 6 with the minimal weight, that is, local E D weight, that is, local 1 optimal. optimal. edges included in the MST

  13. Minimum Spanning Tree Property A spanning tree T of a connected, weighted graph has MST property if and � only if for any non-tree edge uv , T ∪ { uv } contain a cycle in which uv is one of the maximum-weight edge. All the spanning trees having MST property have the same weight. � uv -path in T 1 u edge exchange u w i × Must have same weight w i v w i+1 v not in T 2 w i+1 a new spanning tree: same edge uv in T 2 but not in T 1 , with minimum weight as T 1 , less different weight among all different edges edges from that of T 2

  14. MST Property and Minimum Spanning Tree � In a connected, weighted graph G =( V , E , W ), a tree T is a minimum spanning tree if and only if T has the MST property. � Proof � ⇒ For a minimum spanning tree T , if it doesn’t has MST property. So, there is a non-tree edge uv, and T ∪ { uv } contain an edge xy with weight larger than that of uv . Substituting uv for xy results a spanning tree with less weight than T . Contradiction. � ⇐ As claimed above, any minimum spanning tree has the MST property. Since T has MST property, it has the same weight as any minimum spanning tree, i.e. T is a minimum spanning tree as well.

  15. Correctness of Prim’s Algorithm � Let T k be the tree constructed after the k th step of Prim’s algorithm is executed, then T k has the MST property in G k , the subgraph of G induced by vertices of T k . w a+1 w b-1 assumed first and last edges with w a w b larger weight than T k-1 w( u i v ), resulting …… contradictions. u 1 ( w 1 ) u i ( w p ) edge added in T k Note: w( u i v ) ≥ w( u 1 v ), and if w a added in T k to form a cycle, added earlier than w b , then only these need be considered v , added in T k w a w a+1 and w b-1 w b added later than any edges in u 1 w a -path, and v as well

  16. Key Issue in Implementation � Maintaining the set of fringe vertices � Create the set and update it after each vertex is “selected” ( deleting the vertex having been selected and inserting new fringe vertices) � Easy to decide the vertex with “highest priority” � Changing the priority of the vertices ( decreasing key ). � The choice: priority queue

  17. Implementing Prim’s Algorithm ADT operation executions: Main Procedure primMST( G , n ) insert, getMin, deleteMin: n times Initialize the priority queue pq as empty; decreaseKey: m times Select vertex s to start the tree; Set its candidate edge to (-1, s ,0); Updating the Queue insert( pq , s ,0); while ( pq is not empty) updateFringe( pq , G , v ) v =getMin( pq ); deleteMin( pq ); For all vertices w adjcent to v //2 m loops newWgt =w( v , w ); add the candidate edge of v to the tree; if w .status is unseen then updateFringe( pq , G , v ); Set its candidate edge to ( v , w , newWgt ); return insert( pq , w , newWgt ) else getMin( pq ) always be the if newWgt <getPriorty( pq , w ) vertex with the smallest Revise its candidate edge to ( v , w ,newWgt); key in the fringe set. decreaseKey( pq , w , newWgt ) return

  18. Prim’s Algorithm for MST 2 B A × 6 × 3 Greedy strategy: 7 4 Greedy strategy: G 3 1 × For each set of fringe For each set of fringe 5 F I H C vertex, select the edge 2 4 vertex, select the edge 8 with the minimal 2 2 6 with the minimal weight, that is, local E D weight, that is, local 1 optimal. optimal. edges included in the MST

  19. Complexity of Prim’s Algorithm � Operations on ADT priority queue: (for a graph with n vertices and m edges) � insert: n � getMin: n � deleteMin: n � decreasKey: m (appears in 2 m loops, but execute at most m ) � So, T ( n,m ) = O ( nT (getMin)+ nT (deleteMin+insert)+ mT (decreaseKey)) � Implementing priority queue using heap, we can get Θ ( n 2 + m )

  20. Kruskal’s Algorithm for MST 2 B A Also Greedy strategy: Also Greedy strategy: 6 3 From the set of edges 7 4 From the set of edges G 3 1 not yet included in the not yet included in the 5 F I H C partially built MST, partially built MST, 2 4 select the edge with 8 select the edge with 2 2 6 the minimal weight, the minimal weight, E D 1 that is, local optimal, that is, local optimal, in another sense. in another sense. edges included in the MST

  21. Key Issue in Implementation � How to know an insertion of edge will result in a cycle efficiently ? � For correctness: the two endpoints of the selected edge can not be in the same connected components. � For the efficiency: connected components are implemented as dynamic equivalence classes using union-find.

  22. Kruskal’s Algorithm: the Procedure kruskalMST(G,n,F) // outline � int count; � Build a minimizing priority queue, pq, of edges of G, prioritized by weight. � Initialize a Union-Find structure, sets, in which each vertex of G is in its own set. � � F= φ ; � while (isEmpty(pq) == false ) � vwEdge = getMin(pq); � deleteMin(pq); � Simply sorting, the Simply sorting, the int vSet = find(sets, vwEdge.from); � cost will be Θ ( m log m ) cost will be Θ ( m log m ) int wSet = find(sets, vwEdge.to); � if (vSet ≠ wSet) � Add vwEdge to F; � union(sets, vSet, wSet) � return �

Recommend


More recommend