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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With