Open In App

Disjoint Set Data Structures

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A disjoint-set data structure is defined as one that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets.

A union-find algorithm is an algorithm that performs two useful operations on such a data structure:

  • Find: Determine which subset a particular element is in. This can determine if two elements are in the same subset.
  • Union: Join two subsets into a single subset. Here first we have to check if the two subsets belong to the same set. If not, then we cannot perform union. 

Applications of Disjoint set Union: 

S. No.

Problem

Practice link

1

Kruskal’s Minimum Spanning Tree Algorithm.

solve

2

Job Sequencing Problem.

solve

3

Cycle Detection

solve

4

Number of pairs

solve

5

City With the Smallest Number of Neighbors at a Threshold Distance

solve

Practice problems for DSU:

Problem

Difficulty

Difference between Smallest and Largest Component of the graph after each Query

Easy

Equation satisfiability check

Easy

Evaluate the Fractions

Medium

Substring segment query & modification

Medium

Maximum number of soldier in a team

Medium

Minimum difference between components of the Graph after each query

Hard

Minimum cost to provide water

Hard

Minimum operations for adjacent E and E+1 pairing

Hard


Similar Reads

Linked List representation of Disjoint Set Data Structures
Prerequisites : Union Find (or Disjoint Set), Disjoint Set Data Structures (Java Implementation) A disjoint-set data structure maintains a collection S = {S1, S2,...., Sk} of disjoint dynamic sets. We identify each set by a representative, which is some member of the set. In some applications, it doesn’t matter which member is used as the represent
11 min read
Dynamic Disjoint Set Data Structure for large range values
Prerequisites: Disjoint Set Data StructureSetUnordered_Map Disjoint Set data structure is used to keep track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. In this article, we will learn about constructing the same Data Structure dynamically. This data structure basically helps in situations where we cannot si
14 min read
Disjoint Set Union on trees | Set 2
Given a tree, and the cost of a subtree is defined as |S|*AND(S) where |S| is the size of the subtree and AND(S) is bitwise AND of all indices of nodes from the subtree, task is to find maximum cost of possible subtree. Prerequisite : Disjoint Set Union Examples: Input : Number of nodes = 4 Edges = (1, 2), (3, 4), (1, 3) Output : Maximum cost = 4 E
14 min read
Disjoint Set Union on Trees
Given a tree and weights of nodes. Weights are non-negative integers. Task is to find maximum size of a subtree of a given tree such that all nodes are even in weights. Prerequisite : Disjoint Set Union Examples : Input : Number of nodes = 7 Weights of nodes = 1 2 6 4 2 0 3 Edges = (1, 2), (1, 3), (2, 4), (2, 5), (4, 6), (6, 7) Output : Maximum siz
11 min read
Disjoint Set meaning and definition in DSA
Disjoint Set is a data structure that keeps track of a set of elements partitioned into a number of disjoint subsets and it is used to efficiently solve problems that involve grouping elements into sets and performing operations on them. Characteristics of the Disjoint Set:It keeps a set partitioned into disjoint subsets.It allows the efficient uni
2 min read
Extended Disjoint Set Union on Trees
Prerequisites: DFS, Trees, DSUGiven a tree with of N nodes from value 1 to N and E edges and array arr[] which denotes number associated to each node. You are also given Q queries which contains 2 integers {V, F}. For each query, there is a subtree with vertex V, the task is to check if there exists count of numbers associated with each node in tha
15 min read
Test case generator for Tree using Disjoint-Set Union
In this article, we will generate test cases such that given set edges form a Tree. Below are the two conditions of the Tree: It should have one less edge than the number of vertices.There should be no cycle in it. Approach: The idea is to run a loop and add one edge each time that is generated randomly, and for adding each edge we will check wheth
11 min read
Disjoint Set Union (Randomized Algorithm)
A Disjoint set union is an algorithm that is used to manage a collection of disjoint sets. A disjoint set is a set in which the elements are not in any other set. Also, known as union-find or merge-find. The disjoint set union algorithm allows you to perform the following operations efficiently: Find: Determine which set a given element belongs to.
15+ min read
Queries to update Subarrays of a given Array using Disjoint Set
Given an array arr[] consisting of N integers, consisting only of 0's initially and queries Q[][] of the form {L, R, C}, the task for each query is to update the subarray [L, R] with value C. Print the final array generated after performing all the queries. Examples: Input: N = 5, Q = {{1, 4, 1}, {3, 5, 2}, {2, 4, 3}} Output: 1 3 3 3 2 Explanation:
10 min read
Job Sequencing Problem using Disjoint Set
Given a set of n jobs where each job i has a deadline di >=1 and profit pi>=0. Only one job can be scheduled at a time. Each job takes 1 unit of time to complete. We earn the profit if and only if the job is completed by its deadline. The task is to find the subset of jobs that maximizes profit. Examples: Input: Four Jobs with following deadl
15 min read