Bellman Ford Algorithm: Title
Bellman Ford Algorithm: Title
Bellman Ford Algorithm: Title
Title:
C program to find the shortest path across vertices of a given graph using Bellman-Ford
Algorithm.
Objectives:
Find the shortest path between any two pair of vertices along with intermediate
vertices using Bellman-Ford algorithm.
Know the time complexity of the program.
Problem Statement:
The program is to find the shortest path. We are given a weighted, directed graph G = (V, E)
with source and weight function W: E-> R, with possibly negative weight edges. We have to
find the shortest path from given source vertex and destination vertex.
Algorithm:
1. Start
2. A graph with source vertex is taken as input.
3. The cost (distance[20]) of each vertex is initialised to 1000 and the
predecessor(parent[20]) to -1.
4. The cost of source is initialised to 0.
5. Here, comes the iteration of created matrix of distances that is V-1 times where V is
no. of vertices.
(a) If distance[v] > distance[u] + weight of edge uv
Then distance[v] = weight of edge uv + distance[u]
(b) Else distance[v] = distance[v]
6. After this iteration again the same process is tested and if the iteration is done more
than V-1 times implies there is a negative cycle and is impossible to print the shortest
path as output.
Program(code) in C:
#include <stdio.h>
#include <stdlib.h>
int bellman_ford(int g[20][20],int V,int e,int edge[20][2])
{
int i,u,v,k,distance[20],parent[20],s,flag=1;
for(i=0;i<V;i++)
distance[i]=1000,parent[i]=-1;
printf("enter source: ");
scanf("%d",&s);
distance[s-1]=0;
for(i=0;i<V-1;i++)
{
for(k=0;k<e;k++)
{
u=edge[k][0],v=edge[k][1];
if(distance[u]+g[u][v]<distance[v])
{
distance[v]=distance[u]+g[u][v];
parent[v]=u;
}
}
}
for(k=0;k<e;k++)
{
u=edge[k][0],v=edge[k][1];
if(distance[u]+g[u][v]<distance[v])
flag=0;
}
if(flag)
for(i=0;i<V;i++)
printf("vertex %d-> cost=%d parent =%d\n",i+1,distance[i],parent[i]+1);
return flag;
}
int main()
{
int v,edge[20][2],g[20][20],i,j,k=0;
printf("bellman ford\n");
printf("enter no. of vertices:");
scanf("%d",&v);
printf("enter graph in matrix form:\n");
for(i=0;i<v;i++)
for(j=0;j<v;j++)
{
scanf("%d",&g[i][j]);
if(g[i][j]!=0)
edge[k][0]=i,edge[k++][1]=j;
}
if(bellman_ford(g,v,k,edge))
printf("\n no negative weight cycle\n");
else printf("\n negative weight cycle exists\n");
return 0;
}
Test case - 1:
bellman ford
enter no. of vertices:5
enter graph in matrix form:
0 2 1000 1 1000
1000 0 3 1000 1000
1000 1000 0 1000 1
1000 -2 1000 0 1000
1000 1000 1000 1 0
enter source: 1
vertex 1-> cost=0 parent =0
vertex 2-> cost=-1 parent =4
vertex 3-> cost=2 parent =2
vertex 4-> cost=1 parent =1
vertex 5-> cost=3 parent =3
bellman ford
Enter no. of vertices: 5
Enter graph in matrix form:
0 2 1000 1 1000
1000 0 3 1000 1000
1000 1000 0 1000 1
1000 -2 1000 0 1000
1000 1000 1000 1 0
Enter source: 3
Vertex 1 -> cost = 1000 parent = 0
Vertex 2 -> cost = 0 parent = 4
Vertex 3 -> cost = 0 parent = 0
Vertex 4 -> cost = 2 parent = 5
Vertex 5 -> cost = 1 parent = 3
bellman ford
enter no. of vertices:5
enter graph in matrix form:
0 2 1000 1 1000
1000 0 3 1000 1000
1000 1000 0 1000 1
1000 -2 1000 0 1000
1000 1000 1000 1 0
enter source: 4
vertex 1-> cost=998 parent =2
vertex 2-> cost=-2 parent =4
vertex 3-> cost=1 parent =2
vertex 4-> cost=0 parent =0
vertex 5-> cost=2 parent =3
bellman ford
enter no. of vertices:5
enter graph in matrix form:
0 2 1000 1 1000
1000 0 3 1000 1000
1000 1000 0 1000 1
1000 -2 1000 0 1000
1000 1000 1000 1 0
enter source: 5
vertex 1-> cost=999 parent =2
vertex 2-> cost=-1 parent =4
vertex 3-> cost=2 parent =2
vertex 4-> cost=1 parent =5
vertex 5-> cost=0 parent =0
Test case -2 :
bellman ford
Enter source: 1
----------------------------------------------
Conclusion: Simulating the above C program helped me understand the bellman ford
algorithm and its complexity.
DIJKSTRA’S ALGORITHM
Algorithm:
1. Dijkstra functions is defined with parameters graph, no. of edges and source.
3. While(Q!= empty)
Parent[i] = u + 1
Program in C:
#include <stdio.h>
#include <stdlib.h>
int G[20][20] , distance[20] , inSet[20] , q[20] , parent[20] ;
void print( int V )
{
int i ;
for ( i = 0 ; i < V ; i++ )
printf("i = %d parent = %d distance from source = %d \n", i + 1 , parent[i] ,
distance[i] ) ;
}
int Q( int V )
{
int sum = 0 , i ;
for( i = 0 ; i < V ; i++ )
sum += q[i] ;
return sum ;
}
int extractMin( int V )
{
int i , idx , min = 1000 ;
for ( i = 0 ; i < V ; i++ )
{
if ( distance[i] <= min && inSet[i] == 0 )
min = distance[i] , idx = i ;
}
q[idx] = 0 ;
return idx ;
}
void dijkstra ( int S , int V )
{
int u , i , check_empty = Q(V);
inSet[u] = 1 ;
q[u] = 0 ;
for( i = 0 ; i < V ; i++ )
{
if( G[u][i] > 0 )
{
if( distance[u] + G[u][i] < distance[i] )
distance[i] = distance[u] + G[u][i] , parent[i] = u + 1 ;
}
}
check_empty = Q(V);
}
print(V);
}
int main()
{
int V , i , j , S;
printf ( "Enter no. of vertices: " ) ;
scanf ( "%d" , &V ) ;