-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path10.a.Floyd'sAlgorithm.java
More file actions
39 lines (32 loc) · 1.08 KB
/
10.a.Floyd'sAlgorithm.java
File metadata and controls
39 lines (32 loc) · 1.08 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
/*
10.a. Write Java programs to Implement All-Pairs Shortest Paths problem using Floyd's algorithm.
*/
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Number of Vertices");
int n = scanner.nextInt();
int[][][] D = new int[n + 1][n][n];
System.out.println("Enter Distance Matrix");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
D[0][i][j] = scanner.nextInt();
}
}
for (int k = 1; k <= n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
D[k][i][j] = Math.min(D[k - 1][i][j], D[k - 1][i][k - 1] + D[k - 1][k - 1][j]);
}
}
}
System.out.println("Shortest Distance Matrix");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(D[n][i][j] + " ");
}
System.out.println();
}
}
}