MCPcopy Create free account
hub / github.com/Seogeurim/CS-study / FloydWarshall

Class FloydWarshall

contents/algorithm/code/FloydWarshallTest.java:18–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16}
17
18class FloydWarshall {
19 private int N;
20 private int[][] D;
21 private final static int INF = Integer.MAX_VALUE;
22
23 public FloydWarshall(int N) {
24 this.N = N;
25 D = new int[N+1][N+1];
26 initDistance();
27 }
28
29 private void initDistance() {
30 for (int i = 1; i <= N; i++) {
31 for (int j = 1; j <= N; j++) {
32 if (i != j) D[i][j] = INF;
33 }
34 }
35 }
36
37 public void setWeight(int i, int j, int w) {
38 D[i][j] = w;
39 }
40
41 public void getShortestDistance() {
42 /* Floyd-Warshall */
43 for (int k = 1; k <= N; k++){
44 for (int i = 1; i <= N; i++) {
45 for (int j = 1; j <= N; j++) {
46 if (D[i][k] == INF || D[k][j] == INF) continue;
47 D[i][j] = Math.min(D[i][j], D[i][k] + D[k][j]);
48 }
49 }
50 }
51
52 printDistance();
53 }
54
55 private void printDistance() {
56 for (int i = 1; i <= N; i++) {
57 for (int j = 1; j <= N; j++) {
58 if (D[i][j] == INF) System.out.print("∞ ");
59 else System.out.print(D[i][j] + " ");
60 }
61 System.out.println();
62 }
63 }
64}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected