0

I have a personal coding question. I'm trying to code in java a Floyd-Warshall algorithm, with a predecessor matrix. My goal is to return both the matrix array and predMatrix which I don't know how to do, and also when I ran it with just returning one array the matrix one, I get a indexoutOfboundsException: -1 at java.util.ArrayList.elementData(Unknown Source) at java.util.ArrayList.get(Unknown Source)

public int[][] floydWarshall(Graph g) {     
    ArrayList<Edge> n = g.getEdgeList();

    int[][] matrix = new int[n.size()][n.size()];
    int[][] predMatrix = new int[0][0];
    for(int k = 0; k <= n.size(); k++) {
        for(int i = 0; i < n.size(); i++) {
            for(int j = 0; j < n.size(); j++) {
                String label = n.get(k - 1).getLabel();
                String label2 = n.get(i).getLabel();
                String label3 = n.get(j).getLabel();
                //String predessor = n.get(k).getTail().getName();
                int kDistance = Integer.parseInt(label);
                int iDistance = Integer.parseInt(label2);
                int jDistance = Integer.parseInt(label3);
                if((matrix[iDistance][jDistance] == Integer.MAX_VALUE) &&
                        (matrix[iDistance][kDistance] + matrix[kDistance][jDistance] == Integer.MAX_VALUE)) {
                        continue;
                    //matrix[iDistance][jDistance] = kDistance;
                } else if(matrix[iDistance][jDistance] <= matrix[iDistance][kDistance] + matrix[kDistance][jDistance])
                    matrix[iDistance][jDistance] = matrix[iDistance][jDistance];
                    predMatrix[iDistance][jDistance] = predMatrix[iDistance][jDistance];
                }else {
                    matrix[iDistance][jDistance] = matrix[iDistance][kDistance] + matrix[kDistance][jDistance];
                    predMatrix[iDistance][jDistance] = predMatrix[kDistance][jDistance];

                }
            }
        }
    }
    return matrix;
}
  • When `k = 0`, why did you not expect `n.get(k - 1)` to throw that error? --- Since you use `k - 1` and `k <= n.size()`, perhaps you meant `int i = 1`? Though, since you otherwise don't use `k` for anything, why not a normal 0-based loop like the `i` and `j` loops? – Andreas Dec 01 '19 at 06:23
  • *"My goal is to return both the matrix array and predMatrix"* Change return type to `int[][][]` and use `return new int[][][] { matrix, predMatrix };`, then caller would do `int[][][] result = floydWarshall(g); int[][] matrix = result[0], predMatrix = result[1];` – Andreas Dec 01 '19 at 06:27

1 Answers1

0

The IndexOutOfBoundsException occurs because you set k = 0 in your for loop and then call n.get(k - 1) before changing the value of k. The index value -1 is out of bounds in the ArrayList so naturally you get an exception.

Java does not allow you to return multiple values at the same time. One method for returning multiple values is to create an object with your values as fields, and return that instead (explanation here).

Tumblewood
  • 73
  • 2
  • 7