Disjoint Set Class A relation R on set S is a subset of S S : ( a,b - - PDF document

disjoint set class
SMART_READER_LITE
LIVE PREVIEW

Disjoint Set Class A relation R on set S is a subset of S S : ( a,b - - PDF document

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


slide-1
SLIDE 1

3/31/2011 1

Disjoint Set Class

Chapter 8

CPTR 318

2 CPTR 314

Relation

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

Equivalence Relation

An equivalence relation:

 a R a, for all a in S

(reflexive property)

 a R b iff b R a

(symmetric property)

 If a R b and b R c, then a R c

(transitive property)

CPTR 314

Equivalence Relation Examples and Applications

 =, normal equality  Equivalent fractions: 1/2, 2/4, 3/6, etc.  0, 3, 6, 9 are all equivalent mod 3, as are 1, 4, 7, 10  Electical connectivity  Graph connectivity  Type resolution in a dynamically-typed language  Colorizing black and white movies  Making mazes CPTR 314

Equivalence Class

 The equivalence class of a an element of S is

the subset of S that contains all the elements that are related to a.

 Equivalence classes are disjoint

 Equivalence classes form a partition of set S CPTR 314

Operations

 find

 Returns the equivalence class of an element  find(a) = find(b) iff a R b

 union

 Merges two equivalence classes CPTR 314

slide-2
SLIDE 2

3/31/2011 2

 Only need to know the location of elements  Relative value of elements unimportant  Number all elements sequentially and store

information about them in an array

 Information stored is equivalence class

information

Representation

CPTR 314

2 3 1 4 5 6 7

Representation

CPTR 314

2 3 1 4 5 6 7

Building the Sets

CPTR 314

Building the Sets

CPTR 314

 Merging the subtrees  union(3, 4)

Union Strategy

CPTR 314

Union-by-size

 Add the smaller subtree to larger one

CPTR 314

slide-3
SLIDE 3

3/31/2011 3

Union-by-height

 Make shorter tree a subtree of the taller tree

CPTR 314

Path Compression

 find(14)

CPTR 314

Maze Application

CPTR 314

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

Initial State

CPTR 314

Intermediate State

CPTR 314

slide-4
SLIDE 4

3/31/2011 4

Intermediate State (2)

CPTR 314

Final State

CPTR 314