-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloydWarshall_APSS.java
63 lines (61 loc) · 1.98 KB
/
FloydWarshall_APSS.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
import java.util.Scanner;
public class FloydWarshall_APSS {
static void printArray(int arr[][], int n){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
private static void getAdjacenyMatrix(int arr[][], int n, Scanner s) {
System.out.println("Enter the adjacency matrix: ");
System.out.println("Note: Enter 999 if path does not exist.");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = s.nextInt();
}
}
}
static void initialisePathMatrix(int p[][], int d[][], int n, Scanner s){
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if(i==j || d[i][j]==999)
p[i][j] = -1;
else if(d[i][j]!=999 && i!=j)
p[i][j] = i+1;
}
}
}
static void apss(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of vertices: ");
int n = sc.nextInt();
int d0[][] = new int[n][n];
int p0[][] = new int[n][n];
getAdjacenyMatrix(d0, n, sc);
initialisePathMatrix(p0, d0, n, sc);
System.out.println("D0: ");
printArray(d0, n);
System.out.println("P0: ");
printArray(p0, n);
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if((d0[i][k]+d0[k][j])<d0[i][j]){
d0[i][j] = d0[i][k] + d0[k][j];
p0[i][j] = k+1;
}
}
}
System.out.println("D"+(k+1)+": ");
printArray(d0, n);
System.out.println("P"+(k+1)+": ");
printArray(p0, n);
System.out.println();
}
}
public static void main(String[] args) {
apss();
}
}