-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstras.java
More file actions
81 lines (62 loc) · 1.36 KB
/
dijkstras.java
File metadata and controls
81 lines (62 loc) · 1.36 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.Scanner;
public class dijkstras {
void dijk(int am[][],int src ,int n)
{
//boolean[] vis = new boolean[30];
int dist[]=new int[30];
int vis[]=new int[40];
for(int i=0;i<n;i++)
{ dist[i]=am[src][i];
vis[i]=0;
}
vis[src]=1;
for(int i=1;i<n;i++)
{
int unvis=0;
int min=999;
for(int j=0;j<n;j++)
{
if((vis[j]==0)&&(dist[j]<min))
{
unvis=j;
min=dist[j];
}
}
vis[unvis]=1;
for(int v=0;v<n;v++)
{
if(dist[unvis]+am[unvis][v]<dist[v])
{
dist[v]=dist[unvis]+am[unvis][v];
}
}
}
for(int i=0;i<n;i++)
System.out.println("to vertex"+(i+1)+" is "+dist[i]);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int src,n,i,j;
System.out.println("enter the number of vertices");
n=sc.nextInt();
System.out.println("enter the adjecency matrix");
int am[][]=new int[30][30];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
am[i][j]=sc.nextInt();
}
System.out.println("the entered adjecency matrix is");
for(i=0;i<n;i++) {
for(j=0;j<n;j++)
System.out.print(am[i][j]+"\t");
System.out.println("");
}
System.out.println("enter the source vertex");
src=sc.nextInt();
System.out.println("the shortest path from sourcr vertex to all other vertices are\n");
dijkstras ob =new dijkstras();
ob.dijk(am,src,n);
}
}