-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFroydWarshall.java
More file actions
36 lines (27 loc) · 918 Bytes
/
Copy pathFroydWarshall.java
File metadata and controls
36 lines (27 loc) · 918 Bytes
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
package algorithm.module;
public class FroydWarshall {
static final int INF = Integer.MAX_VALUE;
int[][] distance;
public void froydWarshall(int[][] graph) {
int n = graph.length;
distance = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
distance[i][j] = graph[i][j];
}
}
for (int i = 0; i < n; i++) {
System.arraycopy(graph[i], 0, distance[i], 0, n);
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int newDistance = distance[i][k] + distance[k][j];
if (newDistance < distance[i][j]) {
distance[i][j] = newDistance;
}
}
}
}
}
}