-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloydWarshall.java
More file actions
67 lines (59 loc) · 2.04 KB
/
FloydWarshall.java
File metadata and controls
67 lines (59 loc) · 2.04 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
import java.util.Scanner;
public class FloydWarshall {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices:");
int n = scanner.nextInt();
System.out.println("Enter the number of edges:");
int e = scanner.nextInt();
int[][] p = new int[10][10];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
p[i][j] = 999;
}
}
for (int i = 1; i <= e; i++) {
System.out.println("Enter the end vertices of edge" + i + " with its weight");
int u = scanner.nextInt();
int v = scanner.nextInt();
int w = scanner.nextInt();
p[u][v] = w;
}
System.out.println("Matrix of input data:");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(p[i][j] + "\t");
}
System.out.println();
}
floyds(p, n);
System.out.println("Transitive closure:");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(p[i][j] + "\t");
}
System.out.println();
}
System.out.println("The shortest paths are:");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j) {
System.out.println("<" + i + "," + j + ">=" + p[i][j]);
}
}
}
}
public static void floyds(int[][] p, int n) {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) {
p[i][j] = 0;
} else {
p[i][j] = Math.min(p[i][j], p[i][k] + p[k][j]);
}
}
}
}
}
}