0% found this document useful (0 votes)
60 views4 pages

Programs: / Implementation of Dijkstra'S Algorithm

This document contains code to implement Dijkstra's algorithm for finding the shortest path between nodes in a graph. It defines constants, a node label structure, and a shortpath function that takes a graph adjacency matrix and source and target nodes as input. The function initializes all nodes as tentative, makes the source permanent, then iteratively relaxes edges and marks the closest tentative node as permanent until reaching the target. It returns the path as a list of nodes and the total distance. The code provides a sample run on a 4 node graph to find the shortest path from node 1 to 4.

Uploaded by

xorand
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views4 pages

Programs: / Implementation of Dijkstra'S Algorithm

This document contains code to implement Dijkstra's algorithm for finding the shortest path between nodes in a graph. It defines constants, a node label structure, and a shortpath function that takes a graph adjacency matrix and source and target nodes as input. The function initializes all nodes as tentative, makes the source permanent, then iteratively relaxes edges and marks the closest tentative node as permanent until reaching the target. It returns the path as a list of nodes and the total distance. The code provides a sample run on a 4 node graph to find the shortest path from node 1 to 4.

Uploaded by

xorand
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAMS:

/*IMPLEMENTATION OF DIJKSTRA’S ALGORITHM*/


#include<iostream.h>
#include<conio.h>
#include<limits.h>
#define MAXNODE 10 //Max Number of Nodes in a Graph
#define PERMANENT 1
#define TENTATIVE 2
#define infinity INT_MAX

typedef struct NODELABEL


{
int predecessor;
int length; //Optimal Distance from source
int label; //Tentative Or Permanent
}NODELABEL;

//ShortPath

int shortpath(a,n,s,t,path,dist)
int a[MAXNODE][MAXNODE],n,s,t,path[MAXNODE],*dist;
{
NODELABEL state[MAXNODE];
int i,k,min,count;
int rpath[MAXNODE];
*dist=0;
//intiliaze all Nodes as Tentative Nodes
for(i=1;i<=n;i++)
{
state[i].predecessor=0;
state[i].length=infinity;
state[i].label=TENTATIVE;
}
//Make Source Node As Permanent
state[s].predecessor=0;
state[s].length=0;
state[s].label=PERMANENT;

//Start From Source Node


k=s;
do
{
//Check All Path From Kth Node & Find Their Distance From Kth Node
for(i=1;i<=n;i++)
{
if(a[k][i]>0&&state[i].label==TENTATIVE)
{
if(state[k].length+a[k][i]<state[i].length)
{
state[i].predecessor=k;
state[i].length=state[k].length+a[k][i];
}
}
}

//Find Tentative With Smaller Cost


min=infinity;
k=0;
for(i=1;i<=n;i++)
{
if(state[i].label==TENTATIVE&&state[i].length<min)
{
min=state[i].length;
k=i;
}
}
//Is Source Or Sink Is Isolated
if(k==0)
return(0);
state[k].label=PERMANENT;
}while(k!=t);
k=t;
count=0;
do
{
count=count+1;
rpath[count]=k;
k=state[k].predecessor;
}while(k!=0);
//Reverse Node In Reverse direction
for(i=1;i<=count;i++)
path[i]=rpath[count-i+1];
for(i=1;i<count;i++)
*dist+=a[path[i]][path[i+1]];
return(count);
}

void main()
{
int a[MAXNODE][MAXNODE],i,j;
int path[MAXNODE];
int from,to,dist,count,n;
clrscr();
cout<<"\n\tIMPLEMENTATION OF DIJKSTRA ALGORITHM \n\n";
cout<<"\n\n ENTER NUMBER OF NODES IN THE GRAPH:";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\n ENTER NODE "<<i<<" CONECTIVITY:";
for(j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
cout<<"ENTER FROM TO WHERE:";
cin>>from;
cin>>to;
count=shortpath(a,n,from,to,path,&dist);
if(dist)
{
cout<<"\n SHORTEST PATH IS :\t";
cout<<path[1];
for(i=2;i<=count;i++)
cout<<"->"<<path[i];
cout<<"\n THE MINIMUM DISTANCE = "<< dist<<endl;
}
else
cout<<"\n PATH DOES NOT EXIST \n";
getch();
}
OUTPUT:

IMPLEMENTATION OF DIJKSTRA ALGORITHM

ENTER NUMBER OF NODES IN THE GRAPH: 4

ENTER NODE 1 CONECTIVITY:


0
1
2
0

ENTER NODE 2 CONECTIVITY:


0
0
0
1

ENTER NODE 3 CONECTIVITY:


0
0
0
0

ENTER NODE 4 CONECTIVITY:


0
0
2
0
ENTER FROM TO WHERE:
1
4

SHORTEST PATH IS: 1->2->4

THE MINIMUM DISTANCE = 2

You might also like