-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjava4
More file actions
49 lines (47 loc) · 892 Bytes
/
Copy pathjava4
File metadata and controls
49 lines (47 loc) · 892 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
47
48
49
import java.util.*;
class Main
{
public static void main(String[] args)
int dist[][]=new int[20][20];
int from[][]= new int[20][20];
int costmat[][]=new int[20][20];
int i,j,nodes;
Scanner s = new Scanner(System.in)
System.out.println("enter the number of nodes");
nodes=s.nextInt();
System.out.println("enter the cost matrix");
for(i=1;i<=nodes;i++)
{
for(j=1;j<=nodes;j++)
{
costmat[i][j]=s.nextInt();
costmat[i][i]=0;
dist[i][j]=costmat[i][j];
from[i][j]=j;
}
}
for(i=1;i<=nodes;i++)
{
for(j=1;j<=nodes;j++)
{
for(k=1;k<=nodes;k++)
{
if(dist[i][j]>dist[i][k]+dist[k][j];
{
dist[i][j]=dist[i][k]+dist[k][j];
from[i][j]=k;
}
}
}
}
for(i=1;i<=nodes;i++)
{
System.out.println("from router node:"+i)
System.out.println("destinode\t next-hop\t distance")
for(j=1;j<=nodes;j++)
{
System.out.println(j+"\t\t"+from[i][j]+"\t\t"+dist[i][j]);
}
}
}
}