Disjoint Sets: An Article
1. Disjoint sets and operations
2. Detecting a cycle
3. Graphical Representation
4. Array Representation
5. Weighted Union and Collapsing Find.
Disjoint Sets are useful for detecting a cycle in a nondirected graph. We can represent disjoint sets using graph and array and time efficient operations are weighted union and collapsing find.
Let's understand what disjoint sets are. You can represent each component as a set with S1 as {1, 2, 3, 4} and S2 as {5, 6, 7, 8} and the numbers are not common here, and the intersection of these two will not get anything which is ϕ.
So the first unit gets a disjoint set and we can see how join(4,8) appended with join(1,5) results in a circle.
Before to after:
We can form all the sets in the beginning and add all the edges one by one. We can try to find the cycles in the graph through the Disjoint Set Data Structure. I will include all the edges of a graph, and how can we take the help of disjoint sets of finding a cycle? We can consider each element as a set. We'll be going on taking edges and finding the sets of them one by them.
So we iteratively add all the edges and seeing if we find either of the numbers in the same set. If they are in the same set already, then we have a cycle detected. Kruskal's Algorithm uses the same way in order to find a spanning tree. We can also do parent-child relationships where in set{A,B} A is a parent of B. It really makes no difference which node you select as a parent.
Here's how we do things graphically: And we assign each set's parents accordingly based on the from-to pair of the set we got earlier as indicated by {A. B}.






Comments
Post a Comment