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