-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFloyd.java
36 lines (34 loc) · 823 Bytes
/
Floyd.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
import java.util.Scanner;
public class Floyd {
public static int min(int a, int b) {
return a < b?a:b;
}
public static void floyd(int d[][], int n) {
int i, j , k;
for(k = 1;k <= n;k++) {
for(i = 1;i <=n;i++) {
for(j = 1;j <= n;j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
for(i = 1;i <=n;i++) {
for(j = 1;j <= n;j++)
System.out.print(d[i][j] + " ");
System.out.println();
}
}
public static void main(String args[]) {
int w[][] = new int[10][10];
int n, i, j;
System.out.println("enter the number of nodes");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("enter the cost matrix");
for(i = 1;i <= n;i++)
for(j = 1;j <= n;j++)
w[i][j] = in.nextInt();
System.out.println(" all shortest pair");
floyd(w, n);
}
}