-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra's algorithm.c
103 lines (85 loc) · 1.59 KB
/
dijkstra's algorithm.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
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <process.h>
#include <conio.h>
void dijkstra(int n,int cost[10][10],int source, int dest, int d[],int p[],int s[])
{
int i,j,u,v,min;
for (i=0; i < n; i++)
{
d[i] = cost[source][i];
s[i] = 0;
p[i] = source;
}
s[source] = 1;
for(i=1; i < n; i++)
{
min = 9999;
u = -1;
for (j=0; j < n; j++)
{
if ( s[j] == 0 )
{
if ( d[j] < min )
{
min = d[j];
u = j;
}
}
}
if (u == -1) return;
s[u] = 1;
if ( u == dest) return;
for ( v = 0; v < n; v++ )
{
if ( s[v] == 0 )
{
if ( d[u] + cost[u][v] < d[v] )
{
d[v] = d[u] + cost[u][v];
p[v] = u;
}
}
}
}
}
void print_path(int source, int destination, int d[], int p[])
{
int i;
i = destination;
while (i != source)
{
printf("%d <- ",i);
i = p[i];
}
printf("%d = %d\n",i,d[destination]);
printf("\n\n");
}
void main()
{
int n,i,j,cost[10][10],a[10][10],d[10],p[10],s[10];
printf("Enter the no. of nodes\n");
scanf("%d",&n);
printf("Enter the cost adjacency matrix\n");
for(i=0; i < n ; i++)
{
for (j=0; j < n; j++)
{
scanf("%d",&cost[i][j]);
}
}
for (i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
dijkstra(n,cost,i,j,d,p,s);
if (d[j] == 9999)
printf("%d is not reachable from %d \n",j, i);
else
{
printf("The shortest path is shown below\n");
print_path(i, j, d, p);
}
}
getch();
}
}