0

I'm currently working on implementing the Floyd Warshall algorithm from pseudocode to java. I felt like I had it correct, but anytime I ran it on a graph, I was getting an output of large negative number and I feel that it was probably the way I implemented my algorithm. After looking I realized I may have missed something. I haven't implemented the k-1 part of the pseudocode and I'm not sure how to implement this. Im hoping this will correct the issue.

Here it the code i have written.

public static int[][] floyd(int n, int[][] W, int[][] P, int[][] D) {
        D = W;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                P[i][j] = 0;
        for (int k = 0; k < n; k++)
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++) {
                    if ((D[i][j] == Integer.MAX_VALUE) && (D[i][k] + D[k][j] == Integer.MAX_VALUE)) { // add
                        continue;
                    } else if (D[i][j] <= D[i][k] + D[k][j]) {
                        D[i][j] = D[i][j];
                    } else {
                        D[i][j] = D[i][k] + D[k][j];

                    }
                }
        return D;
    }

And here is the pseudocode im writing based off of.

Floyd-Warshall(W)
n = W.rows
D(0) = W
for (k = 1 to n)
    for ( i = 1 to n )
        for ( j = 1 to n )
            if (dij(k-1) == INF) && (dik(k-1) + dkj(k-1)) == INF)
                continue
        else if (dij(k-1) ≤ dik(k-1) + dkj(k-1))
            dij(k) = dij(k-1) // add
        else
            dij(k) = dik(k-1) + dkj(k-1) // add
return (D(n))

EDIT

Heres all of my code thats relevant with some corrections


        int V = g.nodeList.size();
        int W[][] = new int[V][V];
        int P[][] = new int[V][V];

        // Adding wights to graph
        for (int i = 0; i < V; i++) {
            for (int j = 0; j < V; j++) {
                W[j][i] = Integer.parseInt(g.edgeList.get((j * V) + i).label);
                if (W[j][i] == 0) {
                    W[i][j] = Integer.MAX_VALUE;
                }
            }
        }

        System.out.println("Matrix to find the shortest path of.");
        printMatrix(W, V, g);
        System.out.println("Shortest Path Matrix.");
        printMatrix(floyd(V, W, P), V, g);

        System.out.println("Path Matrix");
        printPredMatrix(P, V, g);

    }

    public static int[][] floyd(int n, int[][] W, int[][] P) {
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                P[i][j] = 0;
        for (int k = 0; k < n; k++)
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++) {
                    if ((W[i][j] == Integer.MAX_VALUE) && (W[i][k] + W[k][j] == Integer.MAX_VALUE)) { // add
                        continue;
                    } else if (W[i][j] <= W[i][k] + W[k][j]) {
                        W[i][j] = W[i][j];
                    } else {
                        W[i][j] = W[i][k] + W[k][j];

                    }
                }
        return W;
    }

    public static int min(int i, int j) {
        if (i > j) {
            return j;
        }
        return i;
    }


    public static void printMatrix(int[][] Matrix, int V, Graph g) {
        System.out.print("\n\t");
        for (int j = 0; j < V; j++) {
            System.out.print(g.nodeList.get(j).name + "\t");
        }
        System.out.println();
        for (int j = 0; j < 35; j++) {
            System.out.print("-");
        }
        System.out.println();
        for (int i = 0; i < V; i++) {
            System.out.print(g.nodeList.get(i).name + " |\t");
            for (int j = 0; j < V; j++) {
                if ((Matrix[i][j] == Integer.MAX_VALUE)||(Matrix[i][j] == Integer.MIN_VALUE)) {
                    System.out.print("~");
                } else {
                    System.out.print(Matrix[i][j]);
                }

                System.out.print("\t");
            }
            System.out.println("\n");
        }
        System.out.println("\n");
    }

    public static void printPredMatrix(int[][] Matrix, int V, Graph g) {
        System.out.print("\n\t");
        for (int j = 0; j < V; j++) {
            System.out.print(g.nodeList.get(j).name + "\t");
        }
        System.out.println();
        for (int j = 0; j < V * 3; j++) {
            System.out.print("-");
        }
        System.out.println();
        for (int i = 0; i < V; i++) {
            System.out.print(g.nodeList.get(i).name + " |\t");
            for (int j = 0; j < V; j++) {
                System.out.print(Matrix[i][j]);
                System.out.print("\t");
            }
            System.out.println("\n");
        }
        System.out.println("\n");
    }

}```
  • 1
    What's your input? – nicomp Dec 01 '19 at 20:08
  • How are D and W being initialized? And why are you doing D = W, if you are passing both as parameters? – krionz Dec 01 '19 at 20:15
  • a 2d array of the weights of the edges – ryan williams Dec 01 '19 at 20:16
  • Those are initilized outside of the floyd module i can add more code iff needed – ryan williams Dec 01 '19 at 20:18
  • If vertice i doesn't have an edge to vertice j what value are you using? – krionz Dec 01 '19 at 20:19
  • But what arguments do you pass onto your method? – FailingCoder Dec 01 '19 at 20:19
  • I think that (D[i][k] + D[k][j] == Integer.MAX_VALUE) should be (D[i][k] == Integer.MAX_VALUE || D[k][j] == Integer.MAX_VALUE) – krionz Dec 01 '19 at 20:20
  • *... a specific problem or error, and the shortest code necessary to reproduce the problem.* likely including your input and output because they are probably necessary to produce your error. – FailingCoder Dec 01 '19 at 20:20
  • @krionz `Integer.MAX_VALUE + 1 == Integer.MIN_VALUE` – FailingCoder Dec 01 '19 at 20:22
  • @KnowNoTrend What do you mean by that? I think that it will end up computing the wrong value for the shortest path. – krionz Dec 01 '19 at 20:24
  • @krionz It would be called Integer overflow. There is no int higher than `Integer.MAX_VALUE`. – FailingCoder Dec 01 '19 at 20:26
  • @KnowNoTrend of course, thank you. – krionz Dec 01 '19 at 20:27
  • Maybe im not explaining it right. I have added the rest of the code above. The program im working off of is a class program that im stuck on. He gave us the code that takes in an adjacency matrix and adds its nodes and edges to a graph. I just ned to do the floyd warshall algorithm on it but am stuck. @KnowNoTrend – ryan williams Dec 01 '19 at 20:28
  • `g.edgeList.get((j * V) + i).label` What is g? What values does it contain? What does the `edgeList.get()` method return? – FailingCoder Dec 01 '19 at 20:29
  • @KnowNoTrend g is the graph class im referencing. edgelist is an arraylist within the graph class that stores the edges. and edgelist.get should return a specific edge – ryan williams Dec 01 '19 at 20:32
  • But what is in the arraylist within the graph class that stores the edges? What does edgelist.get return? – FailingCoder Dec 01 '19 at 20:33
  • @KnowNoTrend dpends on what i call. the edge has the head node, tail node and label which is the weight of the edge – ryan williams Dec 01 '19 at 20:41
  • Then what do you call? What are the head node, the tail node and the label? Do you see where this conversation is going? Every time you give me a vague answer, I will ask an even more specific question until you give me a specific input and output. – FailingCoder Dec 01 '19 at 20:42
  • I'm not trying to be vague. I guess I just don't understand what you're asking exactly. If I call the lable it should return the weight of the edge. In the code above, when I add it to an 2d array it seems to add it correctly, but when it runs the Floyd module, it's not correct. Idk what I'm doing wrong @KnowNoTrend – ryan williams Dec 01 '19 at 20:51
  • "dpends on what i call..." **Then what do you call?** – FailingCoder Dec 01 '19 at 20:52
  • Well now il calling the label to get the weight. Are you suggesting I need to call something else ? Sorry if I'm not follow, I've been working on it for the last 8 hours an am a little brain dead at this point – ryan williams Dec 01 '19 at 20:57

0 Answers0