-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloyds.java
More file actions
63 lines (53 loc) · 1.38 KB
/
Floyds.java
File metadata and controls
63 lines (53 loc) · 1.38 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
package hi;
import java.util.*;
public class Hi {
public static int min(int a,int b)
{
if(a>b)
{
return b;
}
return a;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n;
System.out.println("Enter the number of vertices");
n=scan.nextInt();
int adj[][]=new int[n][n];
System.out.println("Enter the adjascency matrix");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
adj[i][j]=scan.nextInt();
}
}
System.out.println("Entered adjascency matrix");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(adj[i][j]+" ");
}System.out.println();
}
for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
adj[i][j]=min( adj[i][j], (adj[i][k]+adj[k][j]));
}
}
}
System.out.println("The final matrix is");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(adj[i][j]+" ");
}System.out.println();
}
}
}