-
Notifications
You must be signed in to change notification settings - Fork 49
/
Main.c
55 lines (49 loc) · 966 Bytes
/
Main.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
50
51
52
53
54
55
//Dijkastra algorithm
#include <stdio.h>
#define N 9999
int G[6][6]={
{0,4,5,N,N,N},
{4,0,11,9,7,N},
{5,11,0,N,3,N},
{N,9,N,0,13,2},
{N,7,3,13,0,6},
{N,N,N,2,6,0},
};
int visit[6];
int distance[6];
int shortestDistance();
void Dijkstra();
int main(){
Dijkstra();
return 0;
}
int shortestDistance(){
int min = -1;
int minimum = N;
for(int i = 0;i< 6 ;i++){
if(visit[i]==0 && distance[i]<minimum){
minimum = distance[i];
min = i;
}
}
return min;
}
void Dijkstra(){
for(int i=0;i<6;i++){
visit[i] = 0;
distance[i] = N ;
}
distance[0] = 0;
for(int i=0;i<5;i++){
int u = shortestDistance();
visit[u] =1;
for(int j=0;j<6;j++){
if(!visit[j] && G[u][j]!=N && distance[u]+G[u][j]<distance[j]){
distance[j] = distance[u]+G[u][j];
}
}
}
for(int i=0;i<6;i++ ){
printf("Distance of %d from source node : %d\n",i,distance[i]);
}
}