forked from Dipak3007/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloyd_algo.java
More file actions
45 lines (40 loc) · 1.28 KB
/
floyd_algo.java
File metadata and controls
45 lines (40 loc) · 1.28 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
import java.util.Scanner;
public class FloydsClass {
static final int MAX = 20;
static int a[][];
static int n;
public static void main(String args[]) {
a = new int[MAX][MAX];
ReadMatrix();
Floyds();
PrintMatrix();
}
static void ReadMatrix() {
System.out.println("Enter the number of vertices\n");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
System.out.println("Enter the Cost Matrix (999 for infinity) \n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
a[i][j] = scanner.nextInt();
}
}
scanner.close();
}
static void Floyds() {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if ((a[i][k] + a[k][j]) < a[i][j])
a[i][j] = a[i][k] + a[k][j];
}
}
static void PrintMatrix() {
System.out.println("The All Pair Shortest Path Matrix is:\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
System.out.print(a[i][j] + "\t");
System.out.println("\n");
}
}
}