The Disjoint Set Class S

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

The Disjoint Set Class j

SAMREEN DHILLON CS W3137 SPRING 08

Overview
Equivalence Relations q The Dynamic Equivalence Problem
Simple algorithms for solving
Find Union

Smart Union Algorithms Path Compression

Example p

Equivalence Relations (~)


Relation which satisfies the three properties: p p
Reflexive - a R a, for all a S Symmetric - a R b if and only if b R a Transitive a R b and b R c i li that a R c T ii d implies h

Examples: E l
Electrical Connectivity Same country: Two cities are related if they belong to the same country

The dynamic Equivalence Problem


Given an equivalence relation ~, decide if a~b for q , any a and b
Solving via a 2-d array of booleans

What if relation is implicit? Equivalence class of an element a S is a subset of S which contains all the elements that are related to a
check if a and b belong to the same equivalence class class.

Disjoint sets

disjoint set union/find algorithm


f find: returns the equivalence class for a given q g element. union: adds relations
Merges two equivalence classes

Dynamic : the sets change during the course of the algorithm l ith Observations: Ob ti
Values doesnt matter ,location matters find(a) == find(b) matters

Data structure for Solving the problem


f find in constant worst-time? union in constant worst-time? Consider
Each set as a tree Initially all elements are disjoint

-1 1

-1 1

-1 1

-1 1

-1 1

-1 1

-1 1

-1 1

Union operation
union : make parent link of root of one tree to the p f f root of the other tree

Union(4,5)

Union(6,7)

Union(4,6)

DisjSets, find and union operations

O(MN) in worst case

Smart Union Algorithms


union-by-size y
Always make smaller tree subtree of the larger No extra space : instead of -1 set as -(size of tree) for root

Union(3,4) -1 1 0 1 -1 1 -1 1 2 4 3 -5 5 4 4 5 4 6 6 7

Smart Union Algorithms


union-by-height: Make a shallow tree a subtree of a y g deep tree
Update height only if two equally deep trees are joined Instead of -1 , -(height) f root d f (h i h ) for

-1 0 1

-1

-1 2

4 3

-3 4

4 5

4 6

6 7

Smart find algorithm Path compression


Independent of union algorithm p g Make every node on the path from x to root have its parent changed to root.

Note that: union-by-height => union-by-rank

Path Compression

After fi d( ) f find(14)

Path Compression

O(MlogN) in Worst case

Example : Maze generator


http://www.math.com/students/puzzles/mazegen/mazege n.html

Generation of Mazes (50 X 88)

Maze Generator: Algorithm g


1. Start with walls everywhere, except for the entrance y p and exit) Continually, choose a wall randomly, and knock it down if the cells that the wall separates are not already connected to each other other. Repeat this process until the starting and ending cells are connected We have a maze.

2.

3. 3

4.

Maze Generator: Algorithm g


1. Start with walls everywhere, except for the entrance y p and exit).

Use the union/find data structure I iti ll each cell is in its own equivalence Initially, h ll i i it i l class

Initial State: All walls up, all cells in their own set.

Maze Generator: Algorithm g


2. Continually, choose a wall randomly, and knock it y y down if the cells that the wall separates are not already connected to each other.
At some point in the algorithm: several wall down, sets have merged. If at this point the , g p wall between squares 8 and 13 is randomly selected, this wall is not knocked down, because 8 and 13 are already connected.

Maze Generator: Algorithm g


3. Repeat this p p process until the starting and ending g g cells are connected
P f Performing two fi d operations 18 and i find i 8 d 13 are in different sets. Therefore, performing union to combine them. Wall between squares 18 and 13 is randomly selected. The wall is knocked down, because 18 and 13 are not already connected. Their sets d l d d h i are merged.

Maze Generator: Algorithm g


4. We have a maze

Running Time: O (N log N)

Eventually, 24 walls are knocked down. All elements are in the same set.

Questions?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy