1 / 11

Chapter 8: The Disjoint Set Class

Chapter 8: The Disjoint Set Class. Equivalence Classes. Disjoint Set ADT. Kruskal’s Algorithm. Disjoint Set Implementation. CS 340. Page 132. The Disjoint Set Class. Background: Equivalence Relations. A relation R on a set S maps every pair of elements in S to either TRUE or FALSE.

enid
Download Presentation

Chapter 8: The Disjoint Set Class

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. Chapter 8: The Disjoint Set Class • Equivalence Classes • Disjoint Set ADT • Kruskal’s Algorithm • Disjoint Set Implementation CS 340 Page 132

  2. The Disjoint Set Class Background: Equivalence Relations A relation R on a set S maps every pair of elements in S to either TRUE or FALSE. For example, the greater than relation, >, is TRUE for the integer pair (5,3) (i.e., 5 > 3) but not for the pair (6,8) (since 6>8 is FALSE). An equivalence relation R is a relation satisfying the following three properties: • (Reflexive)  R  for every  in set S. • (Symmetric) For every pair  and  in set S, if  R , then  R . • (Transitive) For every triple , ,  in set S, if  R  and  R  , then  R . For example… • Modulo-10 equality is an equivalence relation for integers. • The greater than relation is not an equivalence relation for integers (since it lacks the symmetric property). • The does not equal relation is not an equivalence relation for integers (since it lacks both the reflexive and transitive properties). • The electrical connectivity relation is an equivalence relation for network system components (i.e., endstations, servers, switches). CS 340 Page 133

  3. The Equivalence Classes for Electrical Connectivity The Equivalence Classes for Mod-3 Equality 15 -6 3 0 9 21 18 6 -3 -1 12 ... 23 11 20 16 13 2 1 10 5 -4 14 4 22 -2 19 17 8 ... 7 -5 ... Equivalence Classes An equivalence relation R splits the set S up into disjoint (i.e., non-intersecting) subsets called equivalence classes, such that: • Every pair of elements in the same equivalence class is R-equivalent. • Every pair of elements from different equivalence classes is not R-equivalent. CS 340 Page 134

  4. A B C D E F G H I J K L M N O P Q The Disjoint Set ADT Let’s define an abstract data type to implement equivalence relations. The data will consist of the set S, divided into equivalence classes. The desired operations will be as follows: • A Find operation that determines which equivalence class contains a particular set element. • A Union operation that merges two previously separate equivalence classes. Example: Finding Nearest Common Ancestors in a Tree To find the nearest common ancestor for two nodes U and V in a tree: • Originally, set up each node as its own equivalence class. • Then traverse the tree in postorder… • Every time you return from traversing offspring Y of parent X, perform Union(Find(X),Find(Y)) and mark X as the “anchor” node for Find(X). • Continue until Find(U) and Find(V) are the same. At that point, their common anchor is their nearest common ancestor. CS 340 Page 135

  5. Example: Kruskal’s Algorithm A spanning tree for a graph is a tree formed from edges of the graph that connects all of the graph vertices. A minimum spanning tree is one in which the sum of the costs in the tree is minimized (where every edge is assumed to have an associated “cost”). Kruskal’s Algorithm finds a minimum spanning tree by repeatedly adding the previously excluded edge with the least cost without creating a loop. (Minimum spanning trees are particularly useful in such applications as network routing). Originally, each node is its own equivalence class; a Union operation is performed between distinct equivalence classes Find(X) and Find(Y) whenever X and Y have a minimum cost edge between them. CS 340 Page 136

  6. 6 B C 5 7 4 8 3 D 8 E 7 H A 6 3 9 4 F G 7 ORIGINAL GRAPH B C B C B C 3 D E H A D E H 3 D E H A A 3 F G F G F G B C B C B C 5 4 4 3 D E H A 3 D E H 3 D E H A A 3 4 3 3 4 4 F G F G F G 6 6 6 B C B C B C 5 4 5 4 5 4 3 D E H 3 D E 7 H D E 3 7 H A A A 3 3 3 4 4 4 F G F G F G MINIMUM SPANNING TREE Applying Kruskal’s Algorithm CS 340 Page 137

  7. 1 2 3 4 5 6 7 Element A Element B Element C Element D Element E Element F Element G 1 2 3 5 7 Element A Element B Element C Element E Element G 4 6 Element D Element F 1 2 3 7 Element A Element B Element C Element G 4 5 Element D Element E 6 Element F Disjoint Set Union Operations Seven elements, originally in distinct sets After Union(C,D) and Union(E,F) After Union(C,E) CS 340 Page 138

  8. 1 2 7 Element A Element B Element G 3 Element C 4 5 Element D Element E 6 Element F 1 3 7 Element A Element C Element G 4 2 5 Element D Element B Element E 6 Element F Disjoint Set Union Operation Choices After Union(B,C) with random union (Depth can become linear.) After Union(B,C) with union-by-size or union-by-height (Depth remains logarithmic.) CS 340 Page 139

  9. 1 2 7 Element A Element B Element G 3 Element C -1 0 0 0 3 3 -5 2 -2 3 3 3 3 3 3 5 5 5 0 0 -1 4 5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 Element D Element E 6 Element F 1 3 7 Element A Element C Element G 4 2 5 Element D Element B Element E 6 Element F Disjoint Set Implementation When using random union, merely use an array filled with the slot numbers of the parent nodes (with zero for nodes with no parent). When using union-by-size, merely use an array filled with the slot numbers of the parent nodes, and the negation of the size of the tree for nodes with no parent. When using union-by-height, merely use an array filled with the slot numbers of the parent nodes, the negation of the height of the tree for nodes with offspring but no parent, and zero for the nodes with no offspring and no parent. CS 340 Page 140

  10. Disjoint Set Application: Maze Generation Starting with a complete grid of walls, remove the two representing the entrance and the exit, and consider each cell in the grid as a disjoint set. Randomly remove walls that separate two disconnected cells, performing a union of the cells. Continue until there is only one cell left. CS 340 Page 141

  11. Disjoint Set Application: Colorization A monochrome image is analyzed one scanline at a time, with white pixels grouped in equivalence classes whenever it is determined that their respective regions are separated by black border pixels. Union operations occur whenever a non-border path is discovered between two regions. CS 340 Page 142

More Related