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

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

Embed Size (px)

Citation preview

Chapter 8: The Disjoint Set Class

• Equivalence Classes• Disjoint Set ADT

CS 340 Page 1

• Kruskal’s Algorithm• Disjoint Set Implementation

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).

CS 340 Page 3

Equivalence Classes

33

66

00 99

-3-3

1515

1212

21211818

-6-6

......

11

2222

77

44

1616 1313

-5-51919 -2-2

1010

......

55

88

222323

1717

-1-1

2020

1414 -4-4

1111

......

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.

CS 340 Page 4

The Disjoint Set ADT

A

B 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.

CS 340 Page 5

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.

CS 340 Page 6

Applying Kruskal’s Algorithm55

44

77

66

77 44

77

88

66 9933

8833

ORIGINAL GRAPHORIGINAL GRAPH

B

A D

C

E

F G

H

B

A D

C

E

F G

H

B C

E

F G

H33A D33

33

B

A D

C

E

F G

H

44 33

33

B

A D

C

E

F G

H

44

44

33

33

B

A D

C

E

F G

H

55

44

66

44

33

33

B

A D

C

E

F G

H

55

44

66

44

77

33

33

B

A D

C

E

F G

H

55

44

44

33

33

B

A D

C

E

F G

H

55

44

66

44

77

33

33

MINIMUM SPANNING TREEMINIMUM SPANNING TREE

B

A D

C

E

F G

H

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

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

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 011 22 33 44 55 66 77

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 -111 22 33 44 55 66 77

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 011 22 33 44 55 66 77

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

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.

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.