0

I've been trying for a week to get path finding to work for a game i'm working on. I'm using this implementation of Floyd Warshall's algorithm. I believe I've managed to narrow down the problem to be within this loop:

for(int k = 0; k < nodes.length; k++)
    for(int i = 0; i < nodes.length; i++)
        for(int j = 0; j < nodes.length; j++)
            if(D[i][k] != Integer.MAX_VALUE
            && D[k][j] != Integer.MAX_VALUE
            && D[i][k] + D[k][j] < D[i][j]){
                D[i][j] = D[i][k]+D[k][j];
                P[i][j] = nodes[k];
            }

I've been testing with 4 nodes were every node connects to all other nodes and all connections have a weight of 1. Here's the values of all relevant variables before the loop.

for(Node n:nodes)System.out.println(n.pos.toString() + " " + n.index);

Output:

[x: 4, y: 4] 0
[x: 4, y: 5] 1
[x: 5, y: 4] 2
[x: 5, y: 5] 3

This is as expected

for(Edge e:edgeList)
    System.out.println(e.from.pos.toString() + " " + e.to.pos.toString());

Output:

[x: 4, y: 4] [x: 5, y: 4]
[x: 4, y: 4] [x: 4, y: 5]
[x: 4, y: 4] [x: 5, y: 5]
[x: 4, y: 5] [x: 4, y: 4]
[x: 4, y: 5] [x: 5, y: 4]
[x: 4, y: 5] [x: 5, y: 5]
[x: 5, y: 4] [x: 4, y: 4]
[x: 5, y: 4] [x: 4, y: 5]
[x: 5, y: 4] [x: 5, y: 5]
[x: 5, y: 5] [x: 4, y: 4]
[x: 5, y: 5] [x: 5, y: 4]
[x: 5, y: 5] [x: 4, y: 5]

This is also as expected.

for(int[] ia:D){
    for(int a:ia)System.out.print(a + " ");
    System.out.print("\n");
}

Output:

2147483647 1 1 1 
1 2147483647 1 1 
1 1 2147483647 1 
1 1 1 2147483647 

This is also as expected.

P = new Node[nodes.length][nodes.length];
for(int k = 0; k < nodes.length; k++)
    for(i = 0; i < nodes.length; i++)
        for(int j = 0; j < nodes.length; j++)
            if(D[i][k] != Integer.MAX_VALUE
            && D[k][j] != Integer.MAX_VALUE
            && D[i][k]+D[k][j] < D[i][j]){
                D[i][j] = D[i][k]+D[k][j];
                P[i][j] = nodes[k];
                System.out.println(nodes[k].pos.toString() + " " + k);
            }

Output:

[x: 4, y: 4] 0
[x: 4, y: 4] 0
[x: 4, y: 4] 0
[x: 4, y: 5] 1

This is where i think the problem is, i expected the output to contain all the different nodes, only the two nodes found here work in the getShortestPath function. I believe this would be the correct output of the loop and the order should be irrelevant as far as i understand.

[x: 4, y: 4] 0
[x: 4, y: 5] 1
[x: 5, y: 4] 2
[x: 5, y: 5] 3

I have no idea what exactly causes the problem in the loop or if I've misunderstood something about the algorithm.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Luka
  • 341
  • 2
  • 11
  • 1
    If you show us the output you are expecting, that would help a lot. To say "it isn't as expected" doesn't help much as there are many possible "other" outcomes. – Bohemian Sep 25 '12 at 01:08

1 Answers1

1

From the result:

[x: 4, y: 4] 0
[x: 4, y: 4] 0
[x: 4, y: 4] 0
[x: 4, y: 5] 1

I can almost guess what happened. You may have initialized all D[i][j] to 1 but D[i][i] to Integer.MAX_VALUE. So the first 3 update was D[i][i] = D[i][0] + D[i][0] with k = 0, i = 1..3. But D[0][0] can be updated only be d[0][1] + d[1][0].

Your implementation of floyd-warshall looks totally ok. Maybe you should check your bug in the some other place like the recovery of shortest path route which is more faulty to have bug.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
chyx
  • 501
  • 2
  • 10
  • Unfortunately the initializeWeight function from the implementation is exactly the same as what i got, so this is not whats wrong, I've added the values of the D variable to my question now. Thanks for the try. – Luka Sep 25 '12 at 10:58