I have implemented union-find algorithm inspired by this code with this API:
void unify(int p, int q)
int find(int p)
boolean connected(int p, int q)
int clusterSize(int p)
Then I detect clusters with a nested loop like this:
for i = (0) to (data size)
for j = (i+1) to (data size)
if ( "i" and "j" meet this condition )
unify(i,j)
end for
end for
To actually store each cluster/group in a separate array
, currently I loop over all my data elements and call find
like this:
for i = 0 to data-size
k = find(i);
// ... Store data element "i" in cluster "k" (array "k")
end-for
I have a feeling like, the last for loop
to store data elements in different groups/clusters might not be necessary. I wonder if I'm on right track?