0% found this document useful (0 votes)
20 views2 pages

SPPP 80

The document describes an implementation of Dijkstra's algorithm to find the shortest path in a graph. It defines a GRAPH class with methods to read in the graph, run Dijkstra's algorithm to calculate distances from a source vertex, and output the resulting graph with distances.

Uploaded by

lsomesh448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

SPPP 80

The document describes an implementation of Dijkstra's algorithm to find the shortest path in a graph. It defines a GRAPH class with methods to read in the graph, run Dijkstra's algorithm to calculate distances from a source vertex, and output the resulting graph with distances.

Uploaded by

lsomesh448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Name : Mohini Vilas Narkhede

Roll No : 80
Practical no : 3.4
Title : Implementation Program Based on Graph Obtainning Shortest Path (Dijekstra).

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int COST[10][10]={
class GRAPH
{
int n,v,DIST[10];
int COST[10][10];
public:
GRAPH(int);
void READ_GRAPH_80();
void SSSP_80();
void SHOW_GRAPH_80();
int MIN(int,int);
};
GRAPH::GRAPH(int para)
{
n=para;
}
void GRAPH::READ_GRAPH_80()
{
/* cout<<endl<<"Enter cost adj matrix : \n";
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>COST[i][j];
*/
cout<<endl<<"Enter Source vertex:";
cin>>v;
}
void GRAPH::SSSP_80()
{
int u,w,S[10];
for(int i=1;i<=n;i++)
{
S[i]=0;
DIST[i]=COST[v][i];
}
DIST[v]=0;
S[v]=1;
for(int num=2;num<=n-1;num++)
{
//find u such that....
int min=999;
for(w=1;w<=n;w++)
{
if(S[w]==0 && DIST[w]<min)
{
min=DIST[w];
u=w;
}
}
S[u]=1;
//update DIST[]
for(w=1;w<=n;w++)
{
if(S[w]==0)
DIST[w]=MIN( DIST[w], (DIST[u]+COST[u][w]) );
}
}
}
int GRAPH::MIN(int x,int y)
{
if(x<y) return x; else return y;
}
void GRAPH::SHOW_GRAPH_80()
{
cout<<endl<<"cost adj Matrix is:\n";
for(int i=1;i<=n;i++)
{
cout<<endl;
for(int j=1;j<=n;j++)
cout<<COST[i][j]<<" ";
}
cout<<endl<<"Source\tDesti\t Distance ";
for(i=1;i<=n;i++)
cout<<endl<<v<<"\t"<<i<<"\t"<<DIST[i];
}

void main()
{
clrscr();
int n;
cout<<endl<<"Enter no of vertices : ";
cin>>n;
GRAPH obj(n);
obj.READ_GRAPH_80();
obj.SSSP_80();
obj.SHOW_GRAPH_80();
getch();
}

You might also like