1、Greedy Strategy,Algorithm : Design & Analysis 15,In the last class,Undirected and Symmetric Digraph UDF Search Skeleton Biconnected Components Articulation Points and Biconnectedness Biconnected Component Algorithm Analysis of the Algorithm,Greedy Strategy,Optimization Problem MST Problem Prims Algo
2、rtithm Kruskals Algorithm Single-Source Shortest Path Problem Dijstras Algorithm Greedy Stretegy,Optimizing by Greedy,Coin Change Problem candidatesA finite set of coins, of 1, 5, 10 and 25 units, with enough number for each value constraintsPay an exact amount by a selected set of coins optimizatio
3、na smallest possible number of coins in the selected set Solution by greedy strategy For each selection, choose the highest-valued coin as possible.,Greedy Strategy,Constructing the final solution by expanding the partial solution step by step, in each of which a selection is made from a set of cand
4、idates, with the choice made must be: feasible it has to satisfy the problems constraints locally optimal it has to be the best local choice among all feasible choices on the step irrevocable the candidate selected can never be de-selected on subsquent steps,set greedy(set candidate)set S=;while not
5、 solution(S) and candidateselect locally optimazing x from candidate;candidate=candidate-x;if feasible(x) then S=Sx;if solution(S) then return Selse return (“no solution”),Greedy Fails Sometimes,If the availabe coins are of 1,5,12 units, and we have to pay 15 units totally, then the smallest set of
6、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.,Weighted Graph and MST,27,26,42,21,21,53,25,33,28,36,18,17,34,29,22,A,I,H,J,E,C,B,G,F,D,16,27,26,42,21,21,53,25,33,28,36,18,17,34,29,22,A,I,H,J,E,C,B,G,F,D,16,27,26,42,21,21,
7、53,25,33,28,36,18,17,34,29,22,A,I,H,J,E,C,B,G,F,D,16,A weighted graph The nearest neighbor of vertex I is H The nearest neighbor of shaded subset of vertex is G,21,21,21,25,25,25,A Spanning Tree: W(T)=257,A MST: W(T)=190,Graph Traversal and MST,There are cases that graph traversal tree cannot be min
8、imum spanning tree, with the vertices explored in any order.,1,1,1,1,1,All other edges with weight 5,DFS tree,BFS tree,in any ordering of vertex,Merging Two Vertices,v0,v6,v2,v5,v4,v1,v3,v6,v2,v5,v4,v3,v0,v6,v5,v4,v3,v0”,Constructing a Spanning Tree,a,b,c,d,a,a,a,b,b,b,c,c,c,d,d,d,0. Let a be the st
9、arting vertex, selecting edges one by one in original R 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) 3. Merging a” and d into a”(a,c,b,d), selecting (a,d) or (d,b) Ending, as only one vertex left,(0),(1),(2),(3),Prims Algorithm for MST,A,B,I,F,G,
10、H,C,E,D,2,3,4,1,2,1,5,6,7,4,2,8,2,6,3,edges included in the MST,Greedy stretegy: For each set of fringe vertex, select the edge with the minimal weight, that is, local optimal.,Minimum Spanning Tree Property,A spanning tree T of a connected, weighted graph has MST property if and only if for any non
11、-tree edge uv, Tuv contain a cycle in which uv is one of the maximum-weight edge. All the spanning trees having MST property have the same weight. For any two spanning tree T1 and T2 having MST property, let k be the number of edges that are in T1 but not in T2. When k=0, both trees have the same we
12、ight triavially. If k0, select an minimum-weight edge uv, say in T2 but not in T1, then T1uv has a cycle, in which there must be an edge, say, wiwi+1, with the same weight as uv, and not in T2. Exchange the two edge, we get a new spanning tree with less different edges from that of T1, but with the
13、same weight as T2. By induction, T1 and T2 has the same weight.,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, Tuv contain a cycle in which uv is one of the maximum-weight edge. All the spanning trees having M
14、ST property have the same weight.,u,v,edge uv in T2 but not in T1 , with minimum weight among all different edges,wi,wi+1,uv-path in T1,u,v,wi,wi+1,not in T2,edge exchange,a new spanning tree: same weight as T1, less different edges from that of T2,Must have same weight,MST Property and Minimum Span
15、ning 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. Proor For a minimum spanning tree T, if it doesnt has MST property. So, there is a non-tree edge uv, and Tuv contain an edge xy with weight larger than that of uv. Substitut
16、ing 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.,Note: w(uiv)w(u1v), and if wa adde
17、d earlier than wb, then wa added later than any edges in u1wa-path, and v as well,Correctness of Prims Algorithm,Let Tk be the tree constructed after the kth step of Prims algorithm is excuted, then Tk has the MST property in Gk, the subgraph of G induced by vertices of Tk.,wa+1,wb-1,wa,wb,v, added
18、in Tk,Tk-1,edge added in Tk,u1(w1),ui(wp),added in Tk to form a cycle, only these need be considered,assumed first and last edges with larger weight than w(uiv), resulting contradictions.,Implementing Prims Algorithm,Main Procedure primMST(G,n)Initialize the priority queue pq as empty;Select vertex
19、s to start the tree;Set its candidate edge to (-1,s,0);insert(pq,s,0);while (pq is not empty)v=getMin(pq); deleteMin(pq);add the candidate edge of v to the tree;updateFringe(pq,G,v);return,Updating the Queue updateFringe(pq,G,v)For all vertices w adjcent to v /2m loopsnewWgt=w(v,w);if w.staturs is u
20、nseen thenSet its candidate edge to (v,w,newWgt);insert(pq,w,newWgt)else if newWgtgetPriorty(pq,w)Revise its candidate edge to (v,w,newWgt);decreaseKey(pq,w,newWgt)return,getMin(pq) always be the vertex with the smallest key in the fringe set.,ADT operation executions: insert, getMin, deleteMin: n t
21、imes getPriority: m times,Prims Algorithm for MST,A,B,I,F,G,H,C,E,D,2,3,4,1,2,1,5,6,7,4,2,8,2,6,3,edges included in the MST,Greedy stretegy: For each set of fringe vertex, select the edge with the minimal weight, that is, local optimal.,Complexity of Prims Algorithm,DecreaseKey operation will be exe
22、cuted m times. It is reasonably assumed that each decreaseKey is in O(1) DeleteMin operation will be executed n times, and each cost at worst in O(n), depending on the implementation The time complexity is in O(n2+m), which is O(n2) The lower bound of the MST problem is (m), since each edge must be
23、checked at least once. Note: m is in O(n2),Kruskals Algorithm for MST,A,B,I,F,G,H,C,E,D,2,3,4,1,2,1,5,6,7,4,2,8,2,6,3,edges included in the MST,Also Greedy stretegy: From the set of edges not yet included in the partially built MST, select the edge with the minimal weight, that is, local optimal, in
24、 another sense.,Single Source Shortest Paths,s,7,7,2,1,2,3,1,2,4,4,8,3,5,3,4,6,5,0,1,2,6,6,4,3,9,The single source,Note: The shortest 0, 3-path doesnt contain the shortest edge leaving s, the edge 0,1,Red labels on each vertex is the length of the shortest path from s to the vertex.,Dijstras Algorithm: an Example,s,7,7,2,1,2,5,1,2,4,4,8,3,5,3,4,6,3,0,2,1,8,4,4,7,3,9,6,6,Home Assignment,pp.416-: 8.7-8.9 8.14-15 8.25,