March 24, 2010
Equivalence Classes Some Applications Forming Equivalence Classes
Equivalence Classes Some Applications Forming Equivalence Classes Equivalence Classes The class of all things equivelant to a given item Assume some function F(x,y) which returns true if x and y are equivelant, false otherwise F defines a relation Can be seen as a mapping: a, b S : F (a, b) true, false An equivelance relation must be: Reflexive: a S : ara = true Symmetric: arb <=> bra Transitive: arb brc (arc) arb is true if F(a,b) is true Example relation: Integer equality Another example: a%3 = b%3
Equivalence Classes Some Applications Forming Equivalence Classes Some Applications Disjoint graph components What sort of graph? Server layout, islands, problem solvability, etc Groupings (car types, etc) Applications turn up
Equivalence Classes Some Applications Forming Equivalence Classes Forming Equivalence Classes Given a set S and relation R (defined by some function F(a,b)) Find subsets S i of S such that a, b Si : arb Sj : a S i, b S j : i j (arb) The S i are equivalence classes They are also disjoint sets (no item is in more than one S i ) Example: Modulo 3 sets
Outline Main Operation: Determine if a and b are in the same equivalence class Approach: Put each element of S into a set of it s own. Union equivalent sets. Like example earlier Operations: Find(a) : Return a representitive of the equivalence class containing a Union(S i, S j ) : Create a new set S k = S i S j For now, assume each item can be associated with a unique integer If not, extension to hash tables?
Solution 1: Keep an array containing the representitive of each element Find is O(1) lookup May need hash function to locate items Union is O(N) per union Only ok if all unions can be done in one pass If Ω(N 2 ) find operations, O(1) per union/find operation Solution 2: Speed unions by keeping a linked list for each set Faster union Slower find
Represent each set as a tree (root is the representitive) Disjoint sets are a forest Find(a) returns root element of tree containing a Union(a,b) points root node of tree containing b to root node of tree containing a Implemented as an array as before, but with entry in array indicating parent Example: mod 3 example, then merge with another structure Find takes time proportional to tree depth (worse case O(N))
Outline Link smaller tree to larger tree (limits maximum depth) Or, shorter tree This route requires height to be tracked Height only increases when unioning two equal-height trees (and then, by only 1) O(log N) max depth Implementation: Store negative of height for root nodes Examples both ways (mod 3)
Outline All nodes accessed during a find are linked directly to the root Unlike a splay tree, this is not bad at all! Find 14:
Continued Path compression doesn t change the size of trees, so it works with union-by-size Using with union-by-height requires expensive recomputation of heights Solution: Don t recompute heights Heights may be an overestimate after this This is the usual way to determine ranks
Disjoint-set forests were described in 1964 Analysis of worst case was finished in 1975 Worse case is Θ(α(N)) α is the inverse of Ackermann s function N is the number of elements In practice, α(n) 4 for all practical values of N, so worst case is in practice is Θ(1) The Ackermann Function:
Start with walls everywhere (a grid) Remove a random wall Continue until start and finish are in the same set To avoid unreachable areas, continue only one set of locations remains