I'm trying to write an implementation of Kruskal's Algorithm using disjoint sets. I think I have it nearly working, but I cannot seem to make a piece of the code work right. The code needs to check to see if a node on the graph is already in the set it's trying to be added to; otherwise, you don't want to add it. Here is the code I'm using:
public static boolean difSets(int index1, int index2, ArrayList<Node> sets[], Node nodes[])
{
int setnum1 = 0;
int setnum2 = 0;
for(int i = 0; i < nodes.length; i++)
{
for(int j = 0; j < sets[i].size(); j++)
{
if(nodes[index1].getX() == sets[i].get(j).getX() && nodes[index1].getY() == sets[i].get(j).getY());
setnum1 = i;
if(nodes[index2].getX() == sets[i].get(j).getX() && nodes[index2].getY() == sets[i].get(j).getY());
setnum2 = i;
}
}
if(setnum1 == setnum2)
return false;
else
return true;
}
A little info: this method is determining whether or not the two nodes are already in the same set. The nodes array contains all of the points on the graph (Node is a class that just stores the x and y values and can retrieve them. Sets is an array of ArrayLists of nodes. At the start of the problem, every node will be in an ArrayList by itself; by the end, they should all be in the same ArrayList. Indexes 1 and 2 correspond to the node in the Nodes array.
Unfortunately, this code does not seem to be giving me the right output; I've been staring at it for over an hour and I can't figure out what the problem is, so I was hoping someone here could help me out.
Thanks in advance.