-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
66 lines (55 loc) · 1.58 KB
/
Main.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
65
66
import java.io.*;
class Main {
public static void main (String[] args) {
String fileName = "graph.txt";
int[][] W = readInput(fileName);
Graph graph = new Graph(W);
int[][] paths_weights = graph.floydWarshall();
int[][] predecessors = graph.getPredecessor();
print("Floyd-Marshall all-pairs shortest-path's weights: ", paths_weights, Graph.INF);
print("\nFloyd-Marshall all-pairs shortest-path predecessors: ", predecessors, -1);
}
static int[][] readInput (String fileName) {
int[][] W = null;
try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
int n = Integer.parseInt(br.readLine());
W = new int[n][n];
for (int i = 0; i < n; i++) {
String[] row = br.readLine().split("\\s+");
for (int j = 0; j < n; j++) {
String x = row[j];
if (x.equals("INF")) {
W[i][j] = Graph.INF;
} else {
W[i][j] = Integer.parseInt(x);
}
}
}
} catch (FileNotFoundException e) {
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
}
return W;
}
static void print(String title, int[][] matrix, int comparator) {
int n = matrix.length;
System.out.print(title + "\n\t");
for (int i = 1; i <= n; i++) System.out.print("[," + i + "]" + "\t");
System.out.println("");
for (int i = 0; i < n; i++) {
System.out.print("[" + (i + 1) + ",]\t");
for (int j = 0; j < n; j++) {
int x = matrix[i][j];
System.out.print(
(x == comparator ?
(comparator == -1 ? "nil" : "INF")
:
x
) + "\t"
);
}
System.out.println("");
}
}
}