Floyd-Warshall Algorithm

If you want to find the length of node and shorthest path in a graph we can use Floyd-Warshall Algorithm

`
int [][] map = new int[n][n];
for (int i = 0; i < n; i++) {
Arrays.fill(map[i], Integer.MAX_VALUE/2);
}
for (int i = 0; i < n; i++) {
map[i][i] = 0;
}
for (int i = 0; i < a.length; i++) {
map[a[i]-1][b[i]-1] = len[i];
map[b[i]-1][a[i]-1] = len[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int j2 = 0; j2 < n; j2++) {
map[j][j2] = Math.min(map[j][j2], map[j][i] + map[i][j2]);
}
}

    }

`

You can see the explanation in here : http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm

 
0
Kudos
 
0
Kudos

Now read this

Facebook Hacker Cup Qualification Round

Last weekend i’m participating on Facebook Hacker Cup, and the first stage is qualification round. If you managed to answer one of the problem you can pass to next round. In Qualification round there is 3 problems : Cooking Books... Continue →