1 / 17

Kruskal’s algorithm

Kruskal’s algorithm. Begin with all of the vertices of G and no edges Apply the greedy rule Add an edge of min weight that does not make a cycle Continue until you get a single tree T T contains |V|-1 edges. To implement Kruskal’s algorithm:. 7. 3. 6. 4. 1. 3. 2. 2. 4. 5. 7.

gudrun
Download Presentation

Kruskal’s algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Kruskal’s algorithm • Begin with all of the vertices of G and no edges • Apply the greedy rule • Add an edge of min weight that does not make a cycle • Continue until you get a single tree T • T contains |V|-1 edges

  2. To implement Kruskal’s algorithm: 7 3 6 4 1 3 2 2 4 5 7 6 8 • We must select the edges in increasing order of weight (Sort the edges) • We must be able to determine whether adding an edge will create a cycle • Each component of T is a tree • When u and v are in the same component, the addition of the edge (u, v) creates a cycle • When u and v are in different components, the addition of the edge (u, v) does not create a cycle

  3. Kruskal's Algorithm MST-Kruskal(G,w) 1A¬Æ 2 for each vertex vÎV[G] do 3 Make-Set(v) 4 sort the edges of E by nondecreasing weight w 5 foreach (u,v)ÎE, in nondecreasing of weight do 6 if Find-Set(u) ¹Find-Set(v) then 7 A¬AÈ {(u,v)} 8 Union(Set(u),Set(v)) 9 return A

  4. e.g.1:build a MST on the graph below 1 1 3 3 5 5 7 7 2 2 4 4 6 6 • Initial sets are: {1} {2} {3} {4} {5} {6} {7}

  5. 1 1 3 3 5 5 7 7 2 2 4 4 6 6 • Step 1:{1} {2} {3} {4} {5} {6,7} • Step 2:{1,3} {2} {4} {5} {6,7}

  6. 1 1 3 3 5 5 7 7 2 2 4 4 6 6 • Step 3:{1,3} {2} {4} {6,7,5} • Step 4:{1,3,2} {4} {6,7,5}

  7. 1 1 3 3 5 5 7 7 2 2 4 4 6 6 • Step 5:{1,3,2} {6,7,5,4} • Step 6:{1,3,2,4,5,6,7}

  8. Disjoint-sets • Some applications involve grouping n distinct elements into a collection of disjoint sets S = {S1,S2,....,Sn} • Each set is identified by a representative, which is some member of the set. • Two main operations needed • Which set contains a given element? • Uniting two sets • Two different representations • Tree-based • Linked-List

  9. Required Operations • Make_set (x) • Creates a new set whose only member (and thus representative) is x. • Union (x,y) • Unions the sets that contain x and y. New representative needs to be chosen, and the old sets need to be removed. • Find_set (x) • Returns a pointer to the representative of the set containing x.

  10. disjoint-set Forests(Tree-based representation) • Each set is represented by a tree data structure. • Each node points only to its parent. • The representative of each set is the root of that set's tree • Not so good – could get a linear chain of nodes. • Make_set • Create a tree with one node • Find_set • Follow parent pointers until root is reached • Union • Make root of one tree point to the root of the other

  11. O(1) O(n) O(n) • MAKE_SET (X) X.parent= null; • FIND_SET (X) if (X.parent= = null) return X; return FIND_SET (X.parent); • UNION (X, Y) X.parent= FIND_SET (X); Y.parent=FIND_SET (Y); X.parent= Y.parent;

  12. head a c b / tail disjoint-sets (Linked-List representation) • Each set is a linked-list, with head and tail • Each linked list has a head pointer to the first element in the linked list • Each linked list also has a tail pointer to the last element in the linked list • The representative of a linked list is the first element of that set • Each element (node) contains • value, • next node pointer and, • back-to-representative node pointer. e.g.) set {a,b,c}

  13. head a c b / tail disjoint-sets operations (Linked-List representation) • Make-set and Find-set take constant time, • MAKE-SET costs O(1): just create a single element list. • FIND-SET costs O(1): just return back-to-representative set pointer

  14. c h f f g h e head tail head tail g • Union (X, Y) • Append X’s list onto the end of Y. • Use tail pointer to find the end of Y. • New representative is the representative of Y • Have to update every pointer in X to point to the new representative • Each UNION takes time linear in the X’s length. Set Y {f,g} Set X {c,h,e} UNION of two Sets head c e tail

  15. |V| Make-Set () operations O(E lg E) |E| Find-Set () operations |V|-1 Union () operations

  16. Tree-List representation Sorting = O(E lg E) |V| Make-Set () operations = O(V) |E| Find-Set () operations = O(EV) |V|-1 Union () operations = O(V2) • Total run time = O(V2)OR O(EV)

  17. Linked-List representation Sorting = O(E lg E) |V| Make-Set () operations = O(V) |E| Find-Set () operations = O(E) |V|-1 Union () operations = O(V2) • Total run time = O(V2)

More Related