# Dijkstra’s Algorithm in C

Here you will find out about Dijkstra’s calculation in C.

Dijkstra calculation is likewise called single source briefest way calculation. It depends on the insatiable method.

The calculation keeps up a rundown visited[ ] of vertices, whose most limited good ways from the source is as of now known.

On the off chance that visited, rises to 1, at that point the briefest separation

of vertex I am as of now known. At first, visited[i] is set apart as, for source vertex.

At each progression, we mark visited[v] as 1. Vertex v is a vertex at most limited good

ways from the source vertex. At each progression of the calculation, most limited separation of every vertex is put away in a cluster distance[ ].

Dijkstra’s Algorithm

Make cost framework C[ ][ ] from nearness network adj[ ][ ]. C[i][j] is the expense of going from vertex I to vertex j.

On the off chance that there is no edge between vertices I and j, at that point C[i][j] is interminability.

Cluster visited[ ] is introduced to zero.

for(i=0;i<n;i++)

visited[i]=0;

In the event that the vertex 0 is the source vertex, at that point visited is set apart as 1.

Make the separation framework, by putting away the expense of vertices from vertex no. 0 to n-1 from the source vertex 0.

for(i=1;i<n;i++)

distance[i]=cost[i];

At first, separation of source vertex is taken as 0. for example distance=0;

for(i=1;i<n;i++)

– Choose a vertex w, with the end goal that distance[w] is least and visited[w] is 0. Imprint visited[w] as 1.

– Recalculate the most brief separation of outstanding vertices from the source.

– Only, the vertices not set apart as 1 in cluster visited[ ] ought to be considered for recalculation of separation. for example for every vertex v

if(visited[v]==0)

distance[v]=min(distance[v],

distance[w]+cost[w][v])

Time Complexity

The program contains two settled circles every one of which has a multifaceted nature of O(n).

n is number of vertices. So the intricacy of calculation is O(n2).

Program for Dijkstra’s Algorithm in C

C Program on Dijkstra Algorithm for Finding Minimum Distance of Vertices from a Given Source in a Graph

``````#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10

void dijkstra(int G[MAX][MAX],int n,int startnode);

int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");

for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);

printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);

return 0;
}

void dijkstra(int G[MAX][MAX],int n,int startnode)
{

int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;

//pred[] stores the predecessor of each node
//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];

//initialize pred[],distance[] and visited[]
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}

distance[startnode]=0;
visited[startnode]=1;
count=1;

while(count<n-1)
{
mindistance=INFINITY;

//nextnode gives the node at minimum distance
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}

//check if a better path exists through nextnode
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}

//print the path and distance of each node
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);

j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}``````

Output

Remark beneath in the event that you discovered something incorrectly in over Dijkstra calculation in C.

error: Alert: Content is protected!!