Exp No 10
Exp No 10
AIM:
To write a C++ program to implementShortest Path Algorithm using Dijkstra
Algorithm.
ALGORITHM :
1. Initialization of all nodes with distance "infinite"; initialization of the starting node
with 0.
2. Marking of the distance of the starting node as permanent, all other distances as
temporarily.
3. Setting of starting node as active. Calculation of the temporary distances of all
neighbor nodes of the active node by summing up its distance with the weights of
the edges.
4. If such a calculated distance of a node is smaller as the current one, update the
distance and set the current node as antecessor.
5. This step is also called update and is Dijkstra's central idea.
6. Setting of the node with the minimal temporary distance as active.
7. Mark its distance as permanent. Repeating of steps 4 to 7 until there aren't any
nodes left with a permanent distance, which neighbors still have temporary
distance
PROGRAM:
#include<iostream
>
#include<stdio.h>
using namespace
std; #define
INFINITY 9999
#define max 5
void dijkstra(int G[max]
[max],intn,intstartnode); int main() {
int G[max][max]={{0,1,0,3,10},{1,0,5,0,0},{0,5,0,2,1},{3,0,2,0,6},
{10,0,1,6,0}}; int n=5; int u=0;dijkstra(G,n,u); return 0; } void
dijkstra(int G[max][max],intn,intstartnode) { int cost[max]
[max],distance[max],pred[max]; int
visited[max],count,mindistance,nextnode,i,j; 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];
for(i=0;i<n;i++)
{ distance[i]=cost[startn
ode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1; count=1;
while(count<n-1)
{ mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!
visited[i])
{ mindistance=distance[i];
nextnode=i;
}
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++;
}
for(i=0;i<n;i++) if(i!
=startnode) {
cout<<"\nDistance of node"<<i<<"="<<distance[i];
cout<<"\nPath="<<i;
j=i; do {
j=pred[j];
cout<<"<-"<<j;
}while(j!=startnode);
}
}
OUTPUT :
gedit short1.cpp
novice@it002:~$ g++ short1.cpp
novice@it002:~$ ./a.out
Distance of node1=1
Path=1<-0
Distance of node2=5
Path=2<-3<-0
Distance of node3=3
Path=3<-0
Distance of node4=6
Path=4<-2<-3<-0
RESULT:
Thus the C++ program Shortest Path Algorithm using Dijkstras algorithm was written,
executed and verified successfully.
10 B: BELLMANFORD ALGORITHM
AIM:
To write a C++ program to implement Shortest Path Algorithm using Bellman ford
Algorithm.
ALGORITHM :
#include<iostream>
#include<stdio.h>
#define MAX 10
using namespace
std; typedefstruct
edge
{ int
src;
intdes
t;
intwt;
}edge
;
void bellman_ford(intnv,edge e[],intsrc_graph,int ne)
{
intu,v,weight,i,j=0;
int dis[MAX];
{ dis[i]
=999;
}
{ u=e[j
].src;
v=e[j].dest;
weight=e[j].wt;
{ u=e[j
].src;
v=e[j].des
t;
weight=e[j].wt;
/* if you enter no of vertices: 5 then vertices will be 1,2,3,4,5. so while giving input
enter source and destination vertex accordingly */
printf("Enter the source vertex of the graph: ");
cin>>src_graph;
for(inti=0;i<ne;i++)
{
cout<<"\nFor edge
"<<i+1<<"=>"; cout<<"\
nEnter source vertex :";
cin>>e[i].src; cout<<"Enter
destination vertex :";
cin>>e[i].dest;
cout<<"Enter weight :";
cin>>e[i].wt;
}
bellman_ford(nv,e,src_graph,ne);
return 0;
}
OUTPUT :
gedit short2.cpp
novice@it002:~$ g++
short2.cpp
novice@it002:~$ ./a.out
Enter the number of
vertices: 3
Enter the source vertex of the graph: 1
RESULT:
Thus the C++ program Shortest Path Algorithm using Bellman ford algorithm was written,
executed and verified successfully.