0% found this document useful (0 votes)
82 views10 pages

Disjoint Set Data Structure: Piyali Chandra Assistan Professor Uemk

This document discusses disjoint set data structures, which store a collection of disjoint (non-overlapping) sets. It describes three main operations - MakeUnionFind to set up initial components, Find to return the component containing an element, and Union to merge two sets. A common implementation uses a disjoint-set forest with each node having a parent pointer. Find follows parent pointers to the root, while Union sets the parent of one root to the other. Disjoint set structures are useful for applications like tracking connected components in graphs.

Uploaded by

Ayesha Firdaus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views10 pages

Disjoint Set Data Structure: Piyali Chandra Assistan Professor Uemk

This document discusses disjoint set data structures, which store a collection of disjoint (non-overlapping) sets. It describes three main operations - MakeUnionFind to set up initial components, Find to return the component containing an element, and Union to merge two sets. A common implementation uses a disjoint-set forest with each node having a parent pointer. Find follows parent pointers to the root, while Union sets the parent of one root to the other. Disjoint set structures are useful for applications like tracking connected components in graphs.

Uploaded by

Ayesha Firdaus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Disjoint Set Data Structure

Piyali Chandra
Assistan Professor
UEMK
Disjoint set data structures


Also called Union-find data structure or merge-find set.

Stores a collection of disjoint (non-overlapping) sets.

A set of elements S partitioned into subsets, or components, {C1, C2,
C3,….., Ck}.
• Each s in S belongs to exactly one Cj.

Support the following operations:
• MakeUnionFind(S): set up initial components, each s in S is a separate
singleton component {s}
• Find(s): returns the component containing s
Implementation

Common implementation using disjoint-set forest, which performs union and finds in near constant
amortized time.


Each operation causes the disjoint-set forest to adjust itself so that successive operations are faster.


Disjoint-set data structures play a key role in Kruskal's algorithm for finding the minimum spanning tree of a
graph.


Each node in a disjoint-set forest consists of a pointer and some auxiliary information, either a size or a rank
(but not both).


Two nodes are in the same set if and only if the roots of the trees containing the nodes are equal.


Common technique to store nodes in the forest is to store them in an array.


Parents can be indicated by their array index.


Every array entry requires a minimum of O(log n) bits of storage for the parent pointer.


A comparable or lesser amount of storage is required for the rest of the entry, so the number of bits
Operations


Supports 3 operations:

• Making a new set containing a new element

• Finding the representative of the set containing a given element

• Merging two sets


Making new sets


The MakeSet operation adds a new element. This element is placed
into a new set containing only the new element, and the new set is
added to the data structure.


MakeSet(x)
• If x is not already in the forest then
• x.parent := x
• x.size := 1

• End if
Finding set representatives


The Find operation follows the chain of parent pointers from a
specified query node x until it reaches a root element. This root
element represents the set to which x belongs and may
be x itself. Find returns the root element it reaches.

One family of algorithms, known as path compression, makes every
node between the query node and the root point to the root. Path
compression can be implemented using a simple recursion as follows:

Find(x)
• if x.parent ≠ x then
• x.parent := Find(x.parent)
Merging two sets

The operation Union(x, y) replaces the set containing x and the set
containing y with their union. Union first uses Find to determine the
roots of the trees containing x and y. If the roots are the same, there
is nothing more to do. Otherwise, the two trees must be merged. This
is done by either setting the parent pointer of x to y, or setting the
parent pointer of y to x.

Union(x,y)

//Replace nodes by roots

x := Find(x)

y := Find(y)
ALGORITHM

Read n
For each object x from 1 to n
Make set(x)
Do until all related objects are in a set
For each pair(x,y)
If (x=y)
and (find set x != find set y)
Application


A disjoint-set forest implementation in which Find does not update
parent pointers, and in which Union does not attempt to control tree
heights, can have trees with height O(n). In such a situation,
the Find and Union operations require O(n) time.

Disjoint-set data structures model the partitioning of a set, for
example to keep track of the connected components of an undirected
graph.

This model can then be used to determine whether two vertices
belong to the same component, or whether adding an edge between
them would result in a cycle.

Thank You

You might also like