Type Here to Get Search Results !

Implement Dijkstra’s algorithm to compute the shortest path through a graph

Implement Dijkstra’s algorithm to compute the shortest path through a graph


Algorithm:


1. Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes.

2. Mark all nodes unvisited. Set the initial node as current. Create a set of the unvisited nodes called the unvisited set consisting of all the nodes.

3. For the current node, consider all of its unvisited neighbors and calculate their tentative distances. For example, if the current node A is marked with a distance of 6, and the edge connecting it with a neighbor B has length 2, then the distance to B (through A) will be 6 + 2 =

         8. If this distance is less than the previously recorded tentative distance of B, then overwrite that distance. Even though a neighbor has been examined, it is not marked as "visited" at this time, and it remains in the unvisited set.

4. When we are done considering all of the neighbors of the current node, mark the current node as visited and remove it from the unvisited set. A visited node will never be checked again.

5. If the destination node has been marked visited (when planning a route between two specific nodes) or if the smallest tentative distance among the nodes in the unvisited set is infinity (when planning a complete traversal; occurs when there is no connection between the initial node and remaining unvisited nodes), then stop. The algorithm has finished.

6. Select the unvisited node that is marked with the smallest tentative distance, and set it as the new "current node" then go back to step 3.


PROGRAM:

#include<stdio.h> 

#include<string.h>

 #include<math.h> 

main()

{

int u,v,num,i,j,l,k,s[10],min,cost[10][10],dist[10],path[10],n; 

clrscr();

printf("\n ENTER VERTECES:");

scanf("%d",&n);

printf("\n ENTER ADJECENCY MATRIX:\n");

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

{

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

scanf("%d",&cost[i][j]);

 


}


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

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

if(i==j)

 cost[i][j]=0;

else if(cost[i][j]==-1) cost[i][j]=30000;

printf("\nENTER SOURCE VERTEX:");

scanf("%d",&v); 

clrscr();

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

{

s[i]=0;

path[i]=v; 

dist[i]=cost[v][i];

}

dist[v]=0;

 for(num=2;num<=n;num++)

{

min=30000; u=0;

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

{

if(s[i]!=1) 

if(min>dist[i])

{

u=i;

 min=dist[i];

}

}

s[u]=1;

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

{ if(s[i]!=1) 

if(dist[i]>(min+cost[u][i]))

{

dist[i]=min+cost[u][i]; 

path[i]=u;

}

}

}

printf("\n");

printf("\nPATH MATRIX:\n"); 

printf("\nDISTANCE NODE PATH\n"); 

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

{ printf("\n %d",dist[i]); 

printf(" %d ",i);

j=i; do

{

printf(" --> %d ",path[j]); 

u=path[j];

j=u;

}while(u!=v);

}

getch();}


INPUT/OUTPUT:



ENTER VERTECES:8


ENTER ADJECENCY MATRIX:



0 2 -1 -1 -1 -1 6 -1

2 0 7 -1 2 -1 -1 -1

-1 7 0 3 -1 3 -1 -1

-1 -1 3 0 -1 -1 -1 2

-1 2 -1 -1  0 2 1 -1

-1 -1 3 -1  2 0 -1  2

6 -1 -1 -1  1 -1 0  4

-1 -1 -1 2 -1 2 4  0


ENTER SOURCE VERTEX:1


PATH MATRIX:


DISTANCE NODE PATH


0 1 --> 1

2 2 --> 1

9 3 --> 2 --> 1

10 4 --> 8 --> 6 --> 5 --> 2 --> 1

4 5 --> 2 --> 1

6 6 --> 5 --> 2 --> 1

5 7 --> 5 --> 2 --> 1

8 8 --> 6 --> 5 --> 2 --> 1


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Featured post

M

M