I have implemented Dijkstra's Algorithm in C, using arrays instead of any structs.
Here is my implementation:
#include <stdio.h>
#include <stdlib.h>
int G[20][20], distance[20], inSet[20], q[20], parent[20];
void print(int V)
{
int i ;
for (i = 0; i < V; i++)
{
printf("i = %d parent = %d distance from source = %d\n",
i + 1,
parent[i],
distance[i]) ;
}
}
int Q(int V)
{
int sum = 0, i;
for (i = 0; i < V; i++)
{
sum += q[i] ;
}
return sum ;
}
int extractMin(int V)
{
int i, idx, min = 1000;
for (i = 0; i < V; i++)
{
if (distance[i] <= min && inSet[i] == 0)
{
min = distance[i], idx = i;
}
}
q[idx] = 0 ;
return idx ;
}
void dijkstra(int S, int V)
{
int u, i, check_empty = Q(V);
while (check_empty > 0)
{
u = extractMin(V);
inSet[u] = 1;
q[u] = 0;
for (i = 0; i < V; i++)
{
if (G[u][i] > 0)
{
if (distance[u] + G[u][i] < distance[i])
{
distance[i] = distance[u] + G[u][i], parent[i] = u + 1;
}
}
}
check_empty = Q(V);
}
print(V);
}
int main()
{
int V, i, j, S;
printf("Enter no. of vertices: ");
scanf("%d", &V);
printf("Enter graph in matrix form:\n");
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
scanf("%d", &G[i][j]);
}
}
for (i = 0; i < V; i++)
{
distance[i] = 1000, inSet[i] = 0, q[i] = 1, parent[i] = -1;
}
printf("Enter the source vertex: ");
scanf("%d", &S);
distance[S - 1] = 0 ;
dijkstra(S, V);
return 0;
}
It's just a simple code, and I have given a bound size of 20 for now, just for simplicity.
My concerns:
Is my code efficient enough? Or is there another more simple way to implement Dijkstra?
Is it ok to use arrays instead of structs for graph implementation? Does it have any performance issues? I have seen a lot of codes on the internet which use structs such as
struct Graph
, orstruct edge
.Is using global variables better than local variables in my implementation? The variables I declared global in my code are the ones which are used in 2 or more functions, and I would have to send them as parameters if not global.
If I make the code more adaptive by increasing the array sizes to, say 105, would my code be efficient enough? Or do I need any modifications?