Disjoint Set

Graphs Minimum Spanning Tree Hard
  • Fun Fact: The union-find data structure, or the disjoint set, is specifically designed for efficient handling of the equivalence relation problem
  • It plays a fundamental part in some very popular real-world applications
  • For example, it is a key component in Kruskal's algorithm, which is a widely used algorithm to find the minimum spanning tree in a graph
  • The minimum spanning tree has plenty of utilities, from designing the efficient networking in telecom industry to powering the recommendation engines in apps like Netflix, where it's used to cluster users based on their viewing patterns
  • The rank and size heuristics are used to optimize the efficiency of the union-find operations, making these applications even more performant in solving real-world problems

Design a disjoint set (also called union-find) data structure that supports the following operations:


DisjointSet(int n) initializes the disjoint set with n elements.

void unionByRank(int u, int v) merges the sets containing u and v using the rank heuristic.

void unionBySize(int u, int v) merges the sets containing u and v using the size heuristic.

bool find(int u, int v) checks if the elements u and v are in the same set and returns true if they are, otherwise false.

Examples:

Input:

["DisjointSet", "unionByRank", "unionBySize", "find", "find"]

[[5], [0, 1], [2, 3], [0, 1], [0, 3]]


Output:

[null, null, null, true, false]


Explanation:

DisjointSet ds = new DisjointSet(5); // Initialize a disjoint set with 5 elements

ds.unionByRank(0, 1); // Merge sets containing 0 and 1 using rank

ds.unionBySize(2, 3); // Merge sets containing 2 and 3 using size

ds.find(0, 1); // Returns true as 0 and 1 are in the same set

ds.find(0, 3); // Returns false as 0 and 3 are not in the same set

Input:

["DisjointSet", "unionBySize", "unionBySize", "find", "find"]

[[3], [0, 1], [1, 2], [0, 2], [0, 1]]


Output:

[null, null, null, true, true]


Explanation:

DisjointSet ds = new DisjointSet(3); // Initialize a disjoint set with 3 elements

ds.unionBySize(0, 1); // Merge sets containing 0 and 1 using size

ds.unionBySize(1, 2); // Merge sets containing 1 and 2 using rank

ds.find(0, 2); // Returns true as 0 and 2 are in the same set

ds.find(0, 1); // Returns true as 0 and 1 are in the same set

Input:

["DisjointSet", "unionByRank", "unionBySize", "unionByRank", "find", "find"]

[[5], [0, 1], [3, 4], [1, 2], [0, 2], [1, 3]]

Constraints

  • 1 <= n <= 104
  • 0 <= u, v < n
  • At most 5 * 104 calls will be made to unionByRank, unionBySize, and find

Hints

  • When calling find(), update the parent pointer to the root, making future lookups O(1).
  • "Attach the smaller height tree to the larger height tree to keep the structure balanced. Attach the smaller set to the larger set to keep set sizes balanced."

Company Tags

Wayfair Alibaba Riot Games PayPal Walmart Ernst & Young Docker NVIDIA Medtronic Reddit Siemens Healthineers Etsy Rakuten Intel Salesforce HCL Technologies Target Broadcom Databricks Optum Red Hat Chewy Electronic Arts PwC Splunk Google Microsoft Amazon Meta Apple Netflix Adobe