Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8: The Disjoint Set Class Equivalence Classes Disjoint Set ADT CS 340 Page 132 Kruskal’s Algorithm Disjoint Set Implementation.

Similar presentations


Presentation on theme: "Chapter 8: The Disjoint Set Class Equivalence Classes Disjoint Set ADT CS 340 Page 132 Kruskal’s Algorithm Disjoint Set Implementation."— Presentation transcript:

1

2 Chapter 8: The Disjoint Set Class Equivalence Classes Disjoint Set ADT CS 340 Page 132 Kruskal’s Algorithm Disjoint Set Implementation

3 CS 340 Page 133 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 . The Disjoint Set Class For example… Modulo-10 equality is an equivalence relation for integers.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 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 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).The electrical connectivity relation is an equivalence relation for network system components (i.e., endstations, servers, switches).

4 CS 340 Page 134 Equivalence Classes 3 3 6 6 0 0 9 9 -3 15 12 21 18 -6... 1 1 22 7 7 4 4 16 13 -5 19 -2 10... 5 5 8 8 2 2 23 17 20 14 -4 11... The Equivalence Classes for Mod-3 Equality The Equivalence Classes for Electrical Connectivity 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.

5 CS 340 Page 135 The Disjoint Set ADT AA BBCCDD EEFFGGHHIIJJKK LLMMNNOO PPQQ 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.

6 CS 340 Page 136 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.

7 CS 340 Page 137 Applying Kruskal’s Algorithm 55 44 77667744 77 88 669933 8833 ORIGINAL GRAPH BB AA DD CC EE FFGG HH BB AA DD CC EE FFGG HH BBCC EE FFGG HH 33 AA DD 33 33 BB AADD CC EE FFGG HH 44 33 33 BB AADD CC EE FFGG HH 44 44 33 33 BB AADD CC EE FFGG HH 55 446644 33 33 BB AADD CC EE FFGG HH 55 446644 77 33 33 BB AADD CC EE FFGG HH 55 44 44 33 33 BB AADD CC EE FFGG HH 55 446644 77 33 33 MINIMUM SPANNING TREE BB AADD CC EE FFGG HH

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

9 CS 340 Page 139 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.) 1 1 Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G 1 1 Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G

10 CS 340 Page 140 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). 00002233335500 1 1 2 2 3 3 4 4 5 5 6 6 7 7 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. 33-5-5333355 1 1 2 2 3 3 4 4 5 5 6 6 7 7 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. 0033-2-233335500 1 1 2 2 3 3 4 4 5 5 6 6 7 7 1 1 Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G 1 1 Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G

11 CS 340 Page 141 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.

12 CS 340 Page 142 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.


Download ppt "Chapter 8: The Disjoint Set Class Equivalence Classes Disjoint Set ADT CS 340 Page 132 Kruskal’s Algorithm Disjoint Set Implementation."

Similar presentations


Ads by Google