Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can be the algorithm to find all disjoint sets from a set of sets?

I have 'n' sets (n<10). Each set may have lets say, 1000 elements. I want to find all the disjoint sets for these sets. Say, for eg, I have sets

A = {2,5,6,7}, B = {5,1} and C = {5,7}. 

Then the output would be {{5}, {2,6}, {1}, {7}}. What can be the algorithm for this? I thought about finding pairwise disjoint sets and then using these new (disjoint)sets to again find disjoint sets from the sets which are left. But this will not scale well. Hope this helps: Diagram Example

like image 815
aceBox Avatar asked Jan 09 '16 11:01

aceBox


People also ask

What is a disjoint set?

In other words, a disjoint set is a group of sets where no item can be in more than one set. It is also called a union–find data structure as it supports union and find operation on subsets.

What is a disjoint-set data structure?

Then it is advisable to use Disjoint-Set data structure to pose an efficient solution. Disjoint Set Union is sometimes also referred to as Union-Find because of its two important operations - \phi ϕ i.e. NULL. A Disjoint-Set data structure that keeps records of a set of elements partitioned into several non-overlapping (Disjoint) subsets.

What is disjoint set Union (DSU)?

This article discusses the data structure Disjoint Set Unionor DSU. Often it is also called Union Findbecause of its two main operations. This data structure provides the following capabilities. We are given several elements, each of which is a separate set.

How to implement a disjoint set using union–find in Python?

Following is the C++, Java, and Python implementation of union–find that uses a hash table to implement a disjoint set: System. out. print ( ds. Find ( i) + " "); ds. Union ( 4, 3); // 4 and 3 are in the same set ds. Union ( 2, 1); // 1 and 2 are in the same set ds. Union ( 1, 3); // 1, 2, 3, 4 are in the same set return self.


1 Answers

You can consider your problem as a boolean two entry map, elements being the rows, sets being the columns and the boolean is the answer of the question is the element included in the set. For instance your example would be:

t A B C
2 1 0 0
5 1 1 1
6 1 0 0
7 1 0 1 
1 0 1 0

Then create a key for each element describing the differents sets it is in and register this element in a map.

For instance if we consider the key creation function as something like this:

int keyFunction(bool Xa, bool Xb, bool Xc) {
  int key =0;
  if (Xa) {key+=4;}
  if (Xb) {key+=2;}
  if (Xc) {key+=1;}
  return key;
}

We can then create the map:

Key ElementsQueue
0   []
1   []
2   [1]
3   []
4   [2,6]
5   [7]
6   []
7   [5]

and return the elements of this map.

like image 129
88877 Avatar answered Oct 23 '22 00:10

88877