0% found this document useful (0 votes)
12 views7 pages

Exp No 10

Uploaded by

papu varsha
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)
12 views7 pages

Exp No 10

Uploaded by

papu varsha
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/ 7

DATE Ex.

NO: 10: Shortest Path Algorithms (Dijkstra's algorithm,


Bellman Ford Algorithm)

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 :

1. Start with the weighted graph


2. Choose the starting vertex and assign infinity path values to all other vertex
3. Visit each edge and relax the path distance if they are inaccurate
4. We need to do this V times because in the worst case the vertex path length might
need to be readjusted V times
5. Notice how the vertex at the top right corner had its path length adjusted
After all vertices have their path lengths we check if a negative cycle is present
PROGRAM:

#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];

/* initializing array 'dis' with 999. 999 denotes infinite distance */


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

{ dis[i]
=999;
}

/* distance of source vertex from source vertex is o */


dis[src_graph]=0;

/* relaxing all the edges nv - 1 times */


for(i=0;i<nv-1;i++)
{
for(j=0;j<ne;j++)

{ u=e[j
].src;
v=e[j].dest;
weight=e[j].wt;

if(dis[u]!=999 && dis[u]+weight < dis[v])


{
dis[v]=dis[u]+weight;
}
}
}

/* checking if negative cycle is present */


for(j=0;j<ne;j++)

{ u=e[j
].src;
v=e[j].des
t;
weight=e[j].wt;

if(dis[u]+weight < dis[v])


{
cout<<"\n\nNEGATIVE CYCLE PRESENT..!!\n";
return;
}
}

cout<<"\nVertex"<<" Distance from source";


for(i=1;i<=nv;i++)
{
cout<<"\n"<<i<<"\t"<<dis[i];
}}
int
main()
{
intnv,ne,src_graph;
edge e[MAX];

cout<<"Enter the number of vertices: ";


cin>>nv;

/* 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;

cout<<"\nEnter no. of edges: ";


cin>>ne;

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

Enter no. of edges: 3

For edge 1=>


Enter source vertex :1
Enter destination vertex :2
Enter weight :3

For edge 2=>


Enter source vertex :1
Enter destination vertex :3
Enter weight :3

For edge 3=>


Enter source vertex :2
Enter destination vertex :4
Enter weight :7

Vertex Distance from source

RESULT:
Thus the C++ program Shortest Path Algorithm using Bellman ford algorithm was written,
executed and verified successfully.

You might also like