0% found this document useful (0 votes)
9 views

Disjoint Sets / Union Find in Data Structures

Disjoint Sets in Data Structures, Union Find in Data Structures

Uploaded by

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

Disjoint Sets / Union Find in Data Structures

Disjoint Sets in Data Structures, Union Find in Data Structures

Uploaded by

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

Disjoint Sets

2
What is a Disjoint Set?
• A disjoint-set data structure (also known as a union-find
data structure) is a specialized data structure that
efficiently represents a collection of disjoint
(non-overlapping) sets.

• It is primarily used to track and manipulate disjoint sets of


elements, where each element belongs to a specific set.

3
Disjoint Set Operations

• Union: This operation combines two disjoint sets into a single set. It
takes two elements (or their representative elements) from
different sets and merges the sets they belong to.

• Find: This operation determines which set an element belongs to. It


returns the representative element of the set to which the given
element belongs. The representative element is usually chosen as
the root element of the set.

4
Union Operation – Disjoint Sets

5
Find Operation – Disjoint Sets

6
Find Operation – Disjoint Sets

7
Applications of Disjoint Sets

8
Is B reachable from A?

9
Is B reachable from A?

10
Preprocess Maze – Using Disjoint Data Structure

11
Building a Network

12
Building a Network

13
Building a Network

14
Building a Network

15
Building a Network

16
Building a Network

17
Building a Network

18
Building a Network

19
Building a Network

20
Building a Network

21
Building a Network

22
1. Disjoint Set [Parent-child relationship]

•The disjoint set (DS) data structure is also known as


union-find data structure and merge-find set.

•DS uses chaining to define a set. The chaining is defined by a


Parent-Child relationship.

23
1. Disjoint Set [Parent-child relationship]

24
1. Disjoint Set [Parent-child relationship]

25
Disjoint Set Representation

❖ There are mainly 2 representation of Disjoint Sets.


1) Linked List Representation
2) Array Representation

26
Disjoint Set Representation [Linked List
Representation]
[Example 1]

27
Disjoint Set Representation [Linked List
Representation]
[Example 1]

28
Union Operation - Pseudo Code
function union(x, y):
root_x = find(x)
// Find the representative (root) element of set containing 'x‘
root_y = find(y)
// Find the representative (root) element of set containing 'y'
if root_x != root_y:
parent[root_x] = root_y
// 'root_x' and 'root_y' were in different sets, so merge them by making
'root_y' the parent of 'root_x'
return true
// Elements 'x' and 'y' were successfully merged into one set
else:
return false
// Elements 'x' and 'y' were already in the same set
29
Find Operation - Pseudo Code
function find(x):

if x is not equal to parent[x]:


parent[x] = find(parent[x])
// Path compression: Update parent to the root of the set

return parent[x]
// Return the representative element (root) of the set containing 'x'

30
Solved Example

31
Solved Example

32
Solved Example

33
Solved Example

34
End of Topic : Disjoint Sets

Thank You

35

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