chapter 8: the disjoint set class

11
Chapter 8: The Disjoint Set Cla •Equivalence Classes •Disjoint Set ADT CS 340 Page 1 •Kruskal’s Algorithm •Disjoint Set Implementation

Upload: enid

Post on 16-Feb-2016

77 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 8: The Disjoint Set Class

Chapter 8: The Disjoint Set Class• Equivalence Classes• Disjoint Set ADT

CS 340 Page 1

• Kruskal’s Algorithm• Disjoint Set Implementation

Page 2: Chapter 8: The Disjoint Set Class

CS 340 Page 2

Background: Equivalence RelationsA 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.• 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).

Page 3: Chapter 8: The Disjoint Set Class

CS 340 Page 3

Equivalence Classes

3

6

0 9

-3

15

12

2118

-6

...

122

7

4

16 13

-519 -2

10

...

58

223

17

-120

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.

Page 4: Chapter 8: The Disjoint Set Class

CS 340 Page 4

The Disjoint Set ADT

AB C D

E F G H I J K

L M N O

P Q

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.

Page 5: Chapter 8: The Disjoint Set Class

CS 340 Page 5

Example: Kruskal’s AlgorithmA 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.

Page 6: Chapter 8: The Disjoint Set Class

CS 340 Page 6

Applying Kruskal’s Algorithm5

4

7

6

7 4

7

8

6 93

83

ORIGINAL GRAPH

B

A D

C

E

F G

H

B

A D

C

E

F G

H

B C

E

F G

H3A D3

3

B

A D

C

E

F G

H

4 3

3

B

A D

C

E

F G

H4

4

3

3

B

A D

C

E

F G

H

5

4

6

4

3

3

B

A D

C

E

F G

H

5

4

6

4

7

3

3

B

A D

C

E

F G

H

5

4

4

3

3

B

A D

C

E

F G

H

5

4

6

4

7

3

3

MINIMUM SPANNING TREE

B

A D

C

E

F G

H

Page 7: Chapter 8: The Disjoint Set Class

CS 340 Page 7

Disjoint Set Union OperationsSeven elements, originally in distinct sets

After Union(C,D) and Union(E,F)

1Element

A

2Element

B

3Element

C

4Element

D

5Element

E

6Element

F

7Element

G

After Union(C,E)

1Element

A

2Element

B

3Element

C 4

Element D

5Element

E 6

Element F

7Element

G

1Element

A

2Element

B

3Element

C 4

Element D

5Element

E 6

Element F

7Element

G

Page 8: Chapter 8: The Disjoint Set Class

CS 340 Page 8

Disjoint Set Union Operation ChoicesAfter 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.)

1Element

A

2Element

B 3

Element C

4Element

D

5Element

E 6

Element F

7Element

G

1Element

A 2

Element B

3Element

C 4

Element D

5Element

E 6

Element F

7Element

G

Page 9: Chapter 8: The Disjoint Set Class

CS 340 Page 9

Disjoint Set ImplementationWhen using random union, merely use an array filled with the slot numbers of the parent nodes (with zero for nodes with no parent).0 0 2 3 3 5 0

1 2 3 4 5 6 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.-1 3 -5 3 3 5 -1

1 2 3 4 5 6 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.

0 3 -2 3 3 5 01 2 3 4 5 6 7

1Elemen

t A

2Element

B

3Element

C

4Element

D

5Element

E

6Element

F

7Element

G

1Elemen

t A

2Element

B

3Element

C

4Element

D

5Element

E

6Element

F

7Element

G

Page 10: Chapter 8: The Disjoint Set Class

CS 340 Page 10

Disjoint Set Application: Maze GenerationStarting 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.

Page 11: Chapter 8: The Disjoint Set Class

CS 340 Page 11

Disjoint Set Application: ColorizationA 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.