FIND-SET(x) – returns the representative or a pointer to the representative of the set that contains element x.
In algorithms find-set(x) is used in disjoint data structures. I don’t understand the use of this function. Suppose I have a graph with 4 vertices, a,b,c,d and the weights given as a-b=4, b-c=5, c-d=6 … How does find-set(u)!=find-set(v) (where u,v are any vertices of the graph) help me define the occurrence of a cycle in the graph!?? Find is defined as:
function Find(x)
if x.parent == x
return x
else
return Find(x.parent)
Find-Set(x)is used in disjoint data structures most frequently to implement Minimum Spanning Trees (MST).If
uandvare in the same set, it means that they are already connected. If they are already connected, then adding another connection between them creates a cycle.We can begin analyzing this by saying that all vertices are within their own distinct, single-element sets. When we establish a connection between two vertices e.g.
a-b, then we putaandbwithin the same set. Consequently, we need to determine how they’re in the same set; thus, we useFind-Set(x)to determine if they are within the same set (in your case, it’s a disjoint forest implementation).The example you provide does not have a cycle, so I’ll add another edge:
Example
Initially, assume we have a set of vertices
a,b,candd. We want to determine the minimum spanning tree such that there is the minimum number of edges possible to connect all three vertices to the same forest.Edges available now:
(a, b),(c, d),(b, c),(d, c)(extra edge which is a cycle!)This assumes you know the definitions of vertex, edge and forest which are all fundamental graph terms
Since
aandbare currently distinct sets, we can combine them through an algorithm such asMerge-Set(a, b)which places them within the same set:A=(a, b)while there are stillcanddthat must be connected. Note here thatAis the name of the setWe can see that
(c, d)is also possible; so, we can merge them:B=(c, d)and we also have(a, b).Bis the name of this disjoint setNow we can merge
A=(a, b)andB=(c, d)by knowing that we have edge(b, c). Since there are multiple elements within the set, we first determine whether the edge is necessary throughFind-Set(x). IfFind-Set(b) == Find-Set(c)then we know we have a cycle i.e. ifA == B. Fortunately, we do not since(b, c)does not occur within either setA=(a, b)orB=(c, d). Now we merge them as usual and arrive at our MST which is a set:A=(a, b, c, d)(note that B has been deleted!).Recall our extra edge which is to be a cycle now. If we attempt to add
(d, c), we see that the set whichdandcreside in are the same i.e.Find-Set(d) == Find-Set(c)orA == Asincedandcare both in setA. Consequently, we can determine that this edge creates a cycle!