3/31/2011 Relation Disjoint Set Class A relation R on set S is a subset of S × S : ( a,b ) is in R iff a is related to b We can write a R b Chapter 8 CPTR 318 CPTR 314 2 Equivalence Relation Equivalence Relation Examples and Applications An equivalence relation: =, normal equality a R a , for all a in S (reflexive property) Equivalent fractions: 1/2, 2/4, 3/6, etc. a R b iff b R a (symmetric property) 0, 3, 6, 9 are all equivalent mod 3, as are 1, 4, 7, 10 If a R b and b R c , then a R c (transitive property) Electical connectivity Graph connectivity Type resolution in a dynamically-typed language Colorizing black and white movies Making mazes CPTR 314 CPTR 314 Equivalence Class Operations The equivalence class of a an element of S is find the subset of S that contains all the elements Returns the equivalence class of an element that are related to a . find( a ) = find( b ) iff a R b Equivalence classes are disjoint union Equivalence classes form a partition of set S Merges two equivalence classes CPTR 314 CPTR 314 1
3/31/2011 Representation Representation Only need to know the location of elements Relative value of elements unimportant 3 1 Number all elements sequentially and store 0 information about them in an array 2 6 4 Information stored is equivalence class 5 7 information 3 1 0 2 6 4 5 7 CPTR 314 CPTR 314 Building the Sets Building the Sets CPTR 314 CPTR 314 Union Strategy Union-by-size Merging the subtrees Add the smaller subtree to larger one union(3, 4) CPTR 314 CPTR 314 2
3/31/2011 Union-by-height Path Compression Make shorter tree a subtree of the taller tree find(14) CPTR 314 CPTR 314 Maze Application Maze Construction Begin with walls everywhere; Remove wall into entrance ; Remove wall out of exit ; while ( not all rooms are connected ) { Choose a wall at random; if ( the rooms separated by the wall are not connected ) Remove the wall; } CPTR 314 CPTR 314 Initial State Intermediate State CPTR 314 CPTR 314 3
3/31/2011 Intermediate State (2) Final State CPTR 314 CPTR 314 4
Recommend
More recommend