-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloydWarshallAlgo.java
More file actions
207 lines (185 loc) · 6.03 KB
/
FloydWarshallAlgo.java
File metadata and controls
207 lines (185 loc) · 6.03 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* javac FloydWarshallAlgo.java
* java FloydWarshallAlgo
*/
import java.util.*;
import java.io.*;
import java.math.*;
class FloydWarshallAlgo{
public static void main(String[] args){
try{
int counter = 1;
while(counter < 4){
String file = "g" + counter + ".txt";
System.out.print("Graph " + counter + "\t");
long[][] W = read_file_and_populate(file);
FWAlgo_efficient(W);
//FWAlgo(W); // uncomment this line to get inefficient computation
counter++;
}
} catch(IOException e){
e.printStackTrace();
}
}
public static long[][] read_file_and_populate(String file_loc) throws IOException{
FileInputStream fil = new FileInputStream(file_loc);
BufferedReader br = new BufferedReader(new InputStreamReader(fil));
String element = br.readLine();
String[] lines = element.split("\\s+");
int n = Integer.parseInt(lines[0]) + 1;
long[][] W = new long[n][n];
// initialization for cases i = j and i != j
// index starts at 1 instead of 0
for(int i = 1; i < n; i++){
for(int j = 1; j < n; j++){
if(i == j){
W[i][j] = 0;
} else{
W[i][j] = (long) Integer.MAX_VALUE;
}
}
}
while( (element = br.readLine()) != null){
String[] line = element.split("\\s+");
int i = Integer.parseInt(line[0]);
int j = Integer.parseInt(line[1]);
long w = Long.parseLong(line[2]);
W[i][j] = w;
}
return W;
}
// Floyd-Warshall Algorithm (bottom-up approach)
// efficient approach: only use 2 columns to deal with k-1 and k
public static void FWAlgo_efficient(long[][] W){
int n = W.length;
long[][][] D = new long[n][n][2];
// initialization of 3d array D
for(int i = 1; i < n; i++){
for(int j = 1; j < n; j++){
if(i == j) D[i][j][0] = 0;
D[i][j][0] = W[i][j];
}
}
for(int k = 1; k < n; k++){
for(int i = 1; i < n; i++){
for(int j = 1; j < n; j++){
D[i][j][1] = Math.min(D[i][j][0], D[i][k][0] + D[k][j][0]);
}
}
// copy items in A[][1] to A[][][0]
for(int i = 1; i < n; i++){
for(int j = 1; j < n; j++){
D[i][j][0] = D[i][j][1];
}
}
}
boolean neg = detectNegativeCycle(D,false);
System.out.print("Negative Cycle: " + neg + "\t");
if(!neg ){
findShortestShortestPath(D, false);
}
System.out.println("");
}
public static boolean detectNegativeCycle(long[][][] D, boolean big){
// detect negative cycle
int n = D.length;
int k;
if(big){
k = n - 1;
} else{
k = 0;
}
for(int i = 1; i < n; i++){
if(D[i][i][k] < 0){
return true;
}
}
return false;
}
public static void findShortestShortestPath(long[][][] D, boolean big){
int n = D.length;
long min = Long.MAX_VALUE;
int k;
if(big){
k = n - 1;
} else{
k = 0;
}
for(int i = 1; i < n; i++){
for(int j = 1; j < n; j++){
min = Math.min(min, D[i][j][k]);
}
}
System.out.println("shortest shortest path = " + min);
}
public static void displayD(long[][][] D, int k){
int n = D.length;
System.out.println("D matrix: ");
for(int i = 1; i < n; i++ ){
for(int j = 1; j < n; j++){
System.out.print(D[i][j][k] + "\t\t");
}
System.out.println("");
}
System.out.println("--------------------------");
}
public static void displayPi(int[][][] Pi, int k){
int n = Pi.length;
System.out.println("Pi matrix: ");
for(int i = 1; i < n; i++ ){
for(int j = 1; j < n; j++){
System.out.print(Pi[i][j][k] + "\t\t");
}
System.out.println("");
}
System.out.println("--------------------------");
}
// Floyd-Warshall Algorithm (bottom-up approach)
// memory-inefficient approach below
public static void FWAlgo(long[][] W){
int n = W.length;
long[][][] D = new long[n][n][n];
/*
* predecessor matrix: pi[i][j][k] :
* predecessor of vertex j on a shortest path
* from vertex i with all intermediate vertices
* in the set {1,2...,k}
*/
int[][][] Pi = new int[n][n][n];
// initialization of 3d array D
for(int i = 1; i < n; i++){
for(int j = 1; j < n; j++){
if(i == j) D[i][j][0] = 0;
D[i][j][0] = W[i][j];
}
}
// initialization of Pi matrix
for(int i = 1; i < n; i++){
for(int j = 1; j < n; j++){
if(i == j) Pi[i][j][0] = -1;
else{
Pi[i][j][0] = i;
}
}
}
// compute D and Pi
for(int k = 1; k < n; k++){
for(int i = 1; i < n; i++){
for(int j = 1; j < n; j++){
D[i][j][k] = Math.min(D[i][j][k-1], D[i][k][k-1] + D[k][j][k-1]);
if(D[i][j][k-1] <= D[i][k][k-1] + D[k][j][k-1]){
Pi[i][j][k] = Pi[i][j][k-1];
} else{
Pi[i][j][k] = Pi[k][j][k-1];
}
}
}
}
boolean neg = detectNegativeCycle(D, true);
System.out.print("Negative Cycle: " + neg + "\t");
if(!neg ){
findShortestShortestPath(D,true);
}
System.out.println("");
}
}