Disjoint Set Data Structures

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:

Applications of Disjoint set Union:

Practice problems for DSU:

GeeksforGeeks Like Article -->

Please Login to comment.

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 = 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 represen

11 min read Dynamic Disjoint Set Data Structure for large range values

Prerequisites: Disjoint Set Data StructureSetUnordered_MapDisjoint 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 sim

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 . 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 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 , 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 = , , > Output: 1 3 3 3 2 Explanation:

10 min read Find the number of Islands using Disjoint Set

Given a boolean 2D matrix, find the number of islands.A group of connected 1s forms an island. For example, the below matrix contains 5 islands <1, 1, 0, 0, 0>, , <1, 0, 0, 1, 1>, , <1, 0, 1, 0, 1>A cell in the 2D matrix can be connected to 8 neighbours. This is a variation of the standard problem: “Counting the numbe

15+ min read Introduction to Disjoint Set (Union-Find Algorithm)

What is a Disjoint set data structure?Two sets are called disjoint sets if they don't have any element in common, the intersection of sets is a null set. A data structure that stores non overlapping or disjoint subset of elements is called disjoint set data structure. The disjoint set data structure supports following operations: Adding new sets to

15+ min read Number of connected components of a graph ( using Disjoint Set Union )

Given an undirected graph G with vertices numbered in the range [0, N] and an array Edges[][] consisting of M edges, the task is to find the total number of connected components in the graph using Disjoint Set Union algorithm. Examples: Input: N = 4, Edges[][] = <<1, 0>, , >Output: 2Explanation: There are only 2 connected components as 7 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 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

Does a Data Scientist/Machine Learning Engineer require in depth knowledge of Data Structures and Algorithms?

In today's world, data scientists and machine learning engineers play a crucial role in analyzing data and building intelligent systems. As technology continues to advance, the demand for these experts is growing rapidly. Real-world data problems are complex, requiring strong skills in handling data and creating efficient algorithms. In this articl

10 min read Data Structures and Algorithms | Set 36

Que - 1. The function shiftNode() which takes as input two linked lists- destination and source. It deletes front node from source and places it onto the front of destination. Choose the set of statements which replace X, Y, Z in given function. void shiftNode(struct node** destRoot, struct node** srcRoot) < // the front of source node struct node*

4 min read Data Structures and Algorithms | Set 37

Que - 1. For 8 keys and 6 slots in a hashing table with uniform hashing and chaining, what is the expected number of items that hash to a particular location. (A) 2.33 (B) 0.75 (C) 1.33 (D) 2 Solution: Probability that key1 ends up in slot 1 = 1/6 Probability that key2 ends up in slot 1 = 1/6 Probability that key3 ends up in slot x = 1/6 Probabilit

4 min read Find maximum number of edge disjoint paths between two vertices

Given a directed graph and two vertices in it, source 's' and destination 't', find out the maximum number of edge disjoint paths from s to t. Two paths are said edge disjoint if they don't share any edge. There can be maximum two edge disjoint paths from source 0 to destination 7 in the above graph. Two edge disjoint paths are highlighted below in

15+ min read Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph

Give a complete graph with N-vertices. The task is to find out the maximum number of edge-disjoint spanning tree possible.Edge-disjoint Spanning Tree is a spanning tree where no two trees in the set have an edge in common. Examples: Input : N = 4 Output : 2 Input : N = 5 Output : 2 The maximum number of possible Edge-Disjoint Spanning tree from a c

3 min read Maximal Disjoint Intervals

Given a set of N intervals, the task is to find the maximal set of mutually disjoint intervals. Two intervals [i, j] & [k, l] are said to be disjoint if they do not have any point in common. Examples: Input: intervals[][] = , , , > Output: [2, 3] [4, 6] [8, 9] Intervals sorted w.r.t. end points = <, ,

6 min read Find two disjoint good sets of vertices in a given graph

Given an undirected unweighted graph with N vertices and M edges. The task is to find two disjoint good sets of vertices. A set X is called good if for every edge UV in the graph at least one of the endpoint belongs to X(i.e, U or V or both U and V belong to X). If it is not possible to make such sets then print -1. Examples: Input : Output : 8 min read Count of all possible pairs of disjoint subsets of integers from 1 to N

Given an integer, N. Consider the set of first N natural numbers A = <1, 2, 3, …, N>. Let M and P be two non-empty subsets of A. The task is to count the number of unordered pairs of (M, P) such that M and P are disjoint sets. Note that the order of M and P doesn’t matter. Examples: Input: N = 3 Output: 6 Explanation: The unordered pairs are (,

6 min read Split array into K disjoint subarrays such that sum of each subarray is odd.

Given an array arr[] containing N elements, the task is to divide the array into K(1 ? K ? N) subarrays and such that the sum of elements of each subarray is odd. Print the starting index (1 based indexing) of each subarray after dividing the array and -1 if no such subarray exists.Note: For all subarrays S1, S2, S3, . SK: The intersection of S1

8 min read Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime

Given an array arr[] of size N, the task is to find the maximum sum of a subsequence formed by concatenating disjoint subarrays whose lengths are prime numbers. Examples: Input: arr[] = <10, 10, 7, 10, 10, 10>Output: 50Explanation:Subsequence with maximum sum is obtained by concatenating following two subarrays: <10, 10>of length 2, which is prime

14 min read Check if a Binary String can be split into disjoint subsequences which are equal to "010"

Given a binary string, S of size N, the task is to check if it is possible to partition the string into disjoint subsequences equal to "010". Examples: Input: S = "010100"Output: YesExplanation: Partitioning the string in the manner 010100 to generate two subsequences equal to "010". Input: S = "010000"Output: No Approach: The idea is based on the

8 min read Count of ways to select exactly K non-disjoint ranges from given N ranges

Given two arrays L[] and R[] of size N, and an integer K, the task is to find the number of ways to select exact K disjoint ranges formed by taking elements present at the same index from the array L[] and R[]. Examples: Input: N = 7, K = 3, L[] = <1, 3, 4, 6, 1, 5, 8>, R[] = Output: 9Explanation: The possible ways of selectin

10 min read Count of Disjoint Groups by grouping points that are at most K distance apart

Given a 2D array arr[] and value K, where each list in arr[] represents a point on Cartesian coordinates, the task is to group the given points if the distance between them is less than or equal to K and find the total number of disjoint groups. Note: If points 'a' and 'b' are in the same group and 'a', 'd' are in the same group then 'b' and 'd' ar

11 min read How to check if two given sets are disjoint?

Given two sets represented by two arrays, how to check if the given two sets are disjoint or not? It may be assumed that the given arrays have no duplicates. Input: set1[] = <12, 34, 11, 9, 3>set2[] = Output: Not Disjoint3 is common in two sets.Input: set1[] = <12, 34, 11, 9, 3>set2[] = Output: Yes, DisjointThere is no com

15+ min read Make two sets disjoint by removing minimum elements

Given two sets of integers as two arrays of size m and n. Find count of minimum numbers that should be removed from the sets so that both set become disjoint or don't contains any elements in common. We can remove elements from any set. We need to find minimum total elements to be removed. Examples : Input : set1[] = <20, 21, 22>set2[] =

9 min read Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method

Given a connected acyclic graph, a source vertex and a destination vertex, your task is to count the number of vertices between the given source and destination vertex by Disjoint Union Method . Examples: Input : 1 4 4 5 4 2 2 6 6 3 1 3 Output : 3 In the input 6 is the total number of vertices labeled from 1 to 6 and the next 5 lines are the connec

10 min read Count all ordered pairs (A, B) such that A and B are disjoint

Given an array arr[] of size n contains distinct elements, the task is to find the number of ordered pairs (A, B) that can be made where A and B are subsets of the given array arr[] and A ∩ B = Φ (i.e, Both A and B subset should be disjoint or no common elements between them). Example: Input: arr = <1, 2>Output: 9Explanation: The subsets of the arr