forked from Aerin2805/Hacktoberfest2022-DSA-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijsktra.c
92 lines (73 loc) · 1.73 KB
/
Dijsktra.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
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
82
83
84
85
86
87
88
89
90
91
92
#include<stdio.h>
#define INF 999
#define MAX 10
void dijsktra(int Graph[MAX][MAX],int n,int start){
int cost[MAX][MAX],distance[MAX],pred[MAX],visited[MAX],i,j,count,mindist,nextnode;
// creating cost matrix
for ( i = 0; i < n; i++)
{
for(j = 0; j<n ; j++){
if (Graph[i][j]==0)
{
cost[i][j]=INF;
}
else{
cost[i][j]= Graph[i][j];
}
}
}
for ( i = 0; i < n; i++)
{
distance[i] = cost[start][i];
pred[i] = start;
visited[i] = 0;
}
distance[start] = 0;
visited[start] = 1;
count = 1;
while (count < n-1)
{
mindist = INF;
for ( i = 0; i < n; i++)
{
if (distance[i] < mindist && !visited[i])
{
mindist = distance[i];
nextnode = i;
}
}
visited[nextnode] = 1;
for ( i = 0; i < n; i++)
{
if(!visited[i]){
if (mindist + cost[nextnode][i] < distance[i])
{
distance[i] = mindist + cost[nextnode][i];
pred[i] = nextnode;
}
}
}
count++;
}
// for printing the array
for ( i = 0; i < n; i++)
{
if(i != start){
printf("\nDistance from source to %d is : %d",i,distance[i]);
}
}
}
int main(){
int Graph[MAX][MAX] = {
{0,7,9,0,0,0},
{7,0,10,15,0,0},
{9,10,0,11,0,2},
{0,15,11,0,6,0},
{0,0,0,6,0,9},
{14,0,2,0,9,0}
};
int n = 6;
int start = 0;
dijsktra(Graph,n,start);
return 0;
}