0

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.

gmaster
  • 692
  • 1
  • 10
  • 27
  • Probably I would refactor this to use http://docs.oracle.com/javase/6/docs/api/java/util/Set.html – Luis Feb 08 '13 at 08:57
  • From your code I understand that the length of the nodes array is always the same as the one of the sets array. What happens when you start merging sets, shouldn't that decrease the size of the sets array? – Bogdan Feb 08 '13 at 09:09
  • I just left null sets in the spaces rather than shrink it. The loops just ignored it because .size() returned 0. In retrospect, this was probably a way over complicated implementation of disjoint sets. – gmaster Feb 12 '13 at 03:39

1 Answers1

0

Solved it. One of the many things in java that simply makes no sense to me.

public static boolean difSets(int index1, int index2, ArrayList<Node> sets[], Node nodes[])
{
    int setnum1 = 0;
    int setnum2 = 0;
    int x1 = nodes[index1].getX();
    int y1 = nodes[index1].getY();
    int x2 = nodes[index2].getX();
    int y2 = nodes[index2].getY();
    for(int i = 0; i < nodes.length; i++)
    {
        for(int j = 0; j < sets[i].size(); j++)
        {
            int x3 = sets[i].get(j).getX();
            int y3 = sets[i].get(j).getY();
            if(x1 == x3 && y1 == y3)
                setnum1 = i;
            if(x2 == x3 && y2 == y3)
                setnum2 = i;
        }
    }
    if(setnum1 == setnum2)
        return false;
    else
        return true;
}

That should in effect be exactly the same as what I had before. Nevertheless...

gmaster
  • 692
  • 1
  • 10
  • 27
  • I don't know what your `Node` class looks like, but I'm guessing `getX()` and `getY()` return `Integer`s. If this is the case, then the `==` in lines 9 and 11 test reference equality rather than value equality. In that case, you could have used the `equals` method instead, or, as you did in this solution, unboxed at least one value in each comparison to `int`. – Jakob Jun 15 '17 at 21:46