-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIjkstra.C
49 lines (45 loc) · 1.06 KB
/
DIjkstra.C
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
#include<stdio.h>
#include<stdbool.h>
#define INF 999
#define V 5
int minDist(int dist[], bool sptSet[]) {
int min = INF, idx;
for(int v=0;v<V;v++)
if(sptSet[v]==false && dist[v]<min) {
min = dist[v]; idx = v;
}
return idx;
}
void print(int dist[])
{
printf("Vertex \t Distance\n");
for(int i=0;i<V;i++)
printf("%d \t %d\n",i,dist[i]);
}
void dijkstra(int graph[V][V], int s) {
int dist[V]; bool sptSet[V];
for(int v=0;v<V;v++)
{
dist[v]=INF; sptSet[v]=false;
}
dist[s]=0;
for(int i=0;i<V-1;i++)
{
int u = minDist(dist,sptSet);
sptSet[u]=true;
for(int j=0;j<V;j++)
if(!sptSet[j] && graph[u][j] && dist[u]!=INF && dist[u]+graph[u][j]<dist[j])
dist[j] = dist[u]+graph[u][j];
}
print(dist);
}
int main()
{
int graph[V][V];
printf("enter adjacency matrix\n");
for(int i=0;i<V;i++)
for(int j=0;j<V;j++)
scanf("%d",&graph[i][j]);
dijkstra(graph,0);
return 0;
}