-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.java
64 lines (50 loc) · 1.24 KB
/
Graph.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.lang.Math;
import java.util.Arrays;
class Graph {
static final int INF = Integer.MAX_VALUE;
private int[][] weights;
private int[][] predecessor;
public Graph (int[][] weights) {
this.weights = weights;
}
public int[][] getPredecessor () {
return this.predecessor;
}
public int[][] floydWarshall() {
int n = weights.length;
int[][] prev_D = weights;
int[][] next_D = new int[n][n];
int[][] prev_PI = new int [n][n];
int[][] next_PI = new int [n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i == j || weights[i][j] == INF)
prev_PI[i][j] = -1;
else
prev_PI[i][j] = i + 1;
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
int x = prev_D[i][k];
int y = prev_D[k][j];
if (x == INF || y == INF) {
next_D[i][j] = prev_D[i][j];
next_PI[i][j] = prev_PI[i][j];
}
else if (x < INF && y < INF) {
if (prev_D[i][j] <= x + y) {
next_D[i][j] = prev_D[i][j];
next_PI[i][j] = prev_PI[i][j];
} else {
next_D[i][j] = x + y;
next_PI[i][j] = prev_PI[k][j];
}
}
}
prev_D = next_D;
prev_PI = next_PI;
}
this.predecessor = next_PI;
return next_D;
}
}