Search code examples
javaclasstypesedges

Type compatibility issues in Java


I am trying to implement Kruskal's algorithm for a minimum spanning tree in java. I am struggling with type incompatibilities. Throughout the program I have several for loops that look like this:

for (Edge f : G.edges()) {
            int x = f.either(), y = f.other(x);
            if (!uf.connected(x, y)) {
                if (f.weight() < e.weight()) {
                    System.err.println("Edge " + f + " violates cut optimality conditions");
                    return false;
                }
            }
        }

All of the for loops I have look similar to this with the for either having

Edge f : G.edges()

or

Edge e : G.edges()

inside the parentheses. In this case, Edge is another class within the project Edge.java, and edges is a method within this class:

for (Edge e : edges()) {

        // all edges in MST except e
        uf = new UF(G.V());
        for (Edge f : mst) {
            int x = f.either(), y = f.other(x);
            if (f != e) uf.union(x, y);
        }

When this code is put into NetBeans, the lines containing the for loop produce an type error, "incompatible types: com.sun.javafx.geom.Edge cannot be converted to kruskal.Edge" NetBeans recommends fixing this by changing the types of my f and e variables to edges. Doing this would make my code look like this:

for (com.sun.javafx.geom.Edge e : G.edges()) {
        int v = e.either(), w = e.other(v);
        if (!uf.connected(v, w)) {
            System.err.println("Not a spanning forest");
            return false;
        }
    }

The only problem with this solution is, when I implement it, it fails to recognize my either() and other() methods that are implemented in my Edge.java file. Here is a sample of my other() method:

public int other(int vertex) {
    if      (vertex == v) return w;
    else if (vertex == w) return v;
    else throw new IllegalArgumentException("Illegal endpoint");
}

I believe there is a simple solution to this problem that I am unable to think of. What would be the best way to go about fixing my type issue while still being able to access my methods from the Edge class?


Solution

  • Remove all occurences of "com.sun.javafx.geom.Edge"

    And in the import statement use only the kruskal.Edge