250 likes | 454 Views
[ Kruksal’s Algorithm ]. Strategy of Kruskal’s Algorithm.
E N D
Strategy of Kruskal’s Algorithm • Kruskal’s algorithm again builds the tree T from “dust” by adding edges, but this time to join connected components into larger components that remain trees until only one is obtained(which must be a spanning tree). The new edge is obtained by examining the pool of available edges(initially all E) in increasing order of weight. If the edge joins two nodes in the same component it is discarded, while it joins vertices in different components, it is added to the tree T.
Strategy Cont’d • The algorithm requires sorting edge weights in increasing order, so it requires at least time o(|E|log|E|). It also requires procedures: • Merge(a,b,g) to merge components A and B in G; • Find(i,G) that returns the name of the component of C containing i; • Initial(A,i, G) to initialize the components A to contain only the vertex i initially. These operations can be implemented with sets so that the complexity of the algorithm is O(|E|log|E|).
Strategy Cont’d • This is more efficient than Prim’s algorithm when |E| is low compared to |v| = number of vertices.
Example of Kruskal’s Sort: 1 1 2 2 2 2 3 3 4 4 5 6 6 7
Kruskal’s Algorithm • The Idea The general outline of Kruskal’s algorithm is as follows: At each step it chooses the lowest weighted remaining edge from anywhere in the graph, but discards any edge that would form a cycle with those already chosen. At any time the edges chosen so far will form a forest but not necessarily one tree. It terminates when all edges have been processed.
Kruskal’s Algorithm • The Algorithm kruskal(MST(G, n) //outline R = E; //R is remaining edges F = Ø; //F is forest edges while(R is not empty) Remove the lightest(shortest)edge, vw, from R; if(vw does not make a cycle in F) Add vw to F; return F;
(Kruskal’s) Minimum Spanning Tree Input: G = (V, E, W), a weighted graph, with |V| = n, |E| = m Output: F, a subset of E which forms a minimum spanning tree collection if G is not connected. Remarks: The structure sets defined in the algorithm corresponds to the equivalence relation in the discussion. The class-name qualifiers are omitted from the operations of Union-Find ADT and the priority-queue ADT for easier readability.
(Kruskal’s) Minimum Spanning Tree(Cont’d) • The Algorithm 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 = Ø;
(Kruskal) Minimum Spanning Tree(Cont’d) cont’d while(isEmpty(pq) false) vwEdge = getMin(pq); deleteMin(pq); int vSet = find(sets, vwEdge.from); int wSet = find(sets,vwEdge.to); if(vSet wSet) Add vwEdge to F; union(sets, vSet, wSet); return;
Strategy of Union-Find Algorithms • Have an equivalence relation on a set that changes dynamically(classes are being merged together). Want a data structure to represent classes of all individuals so that combined FIND/MERGE procedures are optimal and efficient. arrays: label components 1, 2,… and update an array cc containing value of cc of i. O(n) pointer array: let each entry in cc be a pointer to another vertex in the same class. A pointer pointing to itself is a true value of cc. make take O(n^2) if done carelessly
Strategy of Union-Find Algorithms(Cont’d) trees: maintain a forest with each node pointing to another element of the same class. Reset pointer of root of smaller tree to root of larger component. O(log n) path compression: reset all pointers of smaller tree to root of larger tree. O(*(n))
Correctness of MST Algorithms •The general scheme of greedy algos: candidates: edges in G solution: edges in an MST feasible: does not include a cycle selection function: to choose next edge objective function: to be minimized(total weight) •A set of edges is promising if it is feasible and can be extended to a solution. •If T is a promising set of edges such that no edge in T leaves a proper set of vertices U, then extending T by an edge in the fringe of U keep T+e promising.
Correctness of MST Algorithms(Cont’d) •Prim’s invariant: chosen set of edges is always promising So by the time it has n-1 edges, must be MST •Kruskal’s invariant: chosen set of edges is always promising(same as above) So by the time it has n-1 edges, must be MST
Efficiency of MST Algorithms Prim’s: O(n^2) n loops, each taking O(n) to find max (assuming data structure is good) Kruskal’s: O(|E| log n) |E| loops, each taking O(log n) if one uses the right data structure As good as O(n log n) sparse ---- As bad as O(n^2 log n) dense
Optimality of MST Algos • Still faster algorithms are known. Prim’s never exercises freedom of edge choice, while Kruskal’s is slave to edge order(why not keep trees in forest of roughly equal size?) Can get O(|E| log log n) cheriton + tarjan’s • The Perfectionists’ Dilemma: The amount of effort required to improve solution by increases exponentially as the approach to optimal solution(s) increases.