-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloyd.java
More file actions
48 lines (34 loc) · 865 Bytes
/
Copy pathfloyd.java
File metadata and controls
48 lines (34 loc) · 865 Bytes
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
import java.util.Scanner;
public class floyd {
void fld(int w[][], int n) {
int i,j,k;
for( i=1; i<=n; i++)
for( j=1; j<=n; j++)
for( k=1; k<=n; k++)
w[i][j] = Math.min(w[i][j], w[i][k] + w[k][j]);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertex");
n = sc.nextInt();
int w[][] = new int[n+1][n+1];
System.out.println("Enter the weighted matrix");
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++) {
w[i][j] = sc.nextInt();
}
floyd fl = new floyd();
fl.fld(w, n);
System.out.println("the shortest path matrix is");
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++)
{
System.out.print(w[i][j] +" ");
}
System.out.println();
}
sc.close();
}
}