forked from infiniteoverflow/VTU-DAA-LAB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloyds.java
44 lines (37 loc) · 1.12 KB
/
Floyds.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
package com.programs;
import java.util.Scanner;
public class Floyds {
public static void floyd(int a[][],int n) {
int i,j,k;
int d[][] = new int[10][10];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
d[i][j] = a[i][j];
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]);
System.out.println("The distance matrix is:");
for(i=1;i<=n;i++) {
for (j = 1; j <= n; j++) {
System.out.print(d[i][j] + "\t");
}
System.out.println();
}
}
public static int min(int a,int b) {
return a>b?b:a;
}
public static void main(String[] args) {
int n,i,j;
int a[][] = new int[10][10];
Scanner in = new Scanner(System.in);
System.out.println("Enter the no. of nodes:");
n = in.nextInt();
System.out.println("Enter the cost adjacency matrix:");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j] = in.nextInt();
floyd(a,n);
}
}