0

There are a set of elements S = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Here there are 10 elements (N = 10). An array arr to manage the connectivity of the elements. Arr[ ] that is indexed by elements of sets, which are of size N (as N elements in set), used to manage the operations of union and find.

https://www.hackerearth.com/practice/data-structures/disjoint-data-strutures/basics-of-disjoint-data-structures/tutorial/

What I cant understand is how are the elements selected to start with union operation? In this Link(mentioned above) the union has started with (2,1) , can I use any two elements? say (1,8)? I don't seem to get the same tree as explained in the image if I start with selecting different elements(other than Union(2,1)).

1 Answers1

0

You typically perform Union and Find operations as required by the task you are solving. For example, one way to test if an undirected graph contains a cycle is to consider its edges one by one. When considering an edge (u, v) we:

  1. Ask the question: are u and v already connected? That translates to two operations, Find(u) and Find(v). If Find(u) = Find(v), then the edge (u, v) is part of a cycle and the algorithm terminates with the answer "yes".
  2. Mark u and v (along with their respective sets) as connected, so that they are not connected again in the future. That translates to the operation Union(u, v).

At the end, if no cycles were found, the algorithm terminates with the answer "no".

Cătălin Frâncu
  • 1,179
  • 8
  • 15
  • @Cātālin Thanks for your response, I am just getting started with DSA in depth as I proceed towards my masters degree. I am currently following the Algorithms part 1 course by robert sedgewick on Coursera. The content and approach in the course often gets confusing for me as I am a non CS student ( In bachelor's). – Kshitij Zutshi Nov 14 '19 at 14:25