-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathFloydWarshall.java
39 lines (32 loc) · 1.01 KB
/
FloydWarshall.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Arrays;
public class FloydWarshall {
static final int INF = 10000;
public static int[][] solve(int graph[][]) {
int result[][] = new int[graph.length][graph.length];
for (int i = 0; i < graph.length; i++)
for (int j = 0; j < graph.length; j++)
result[i][j] = graph[i][j];
for (int k = 0; k < graph.length; k++)
{
for (int i = 0; i < graph.length; i++)
{
for (int j = 0; j < graph.length; j++)
{
result[i][j] = Math.min(result[i][j], result[i][k] + result[k][j]);
}
}
}
return result;
}
public static void main(String args[])
{
int graph[][] =
{{0, 3, INF, INF, 2},
{INF, 0, 3, INF, 4},
{1, INF, 0, 1, 3},
{INF, 2, INF, 0, INF},
{4, 5, INF, INF, 0}};
int result[][] = solve(graph);
System.out.println(Arrays.deepToString(result));
}
}