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

DAA Practical

The document discusses two algorithms: Floyd-Warshall for all pairs shortest path and Bellman-Ford for single source shortest path. It provides the problem statements, C code implementations of the algorithms on sample graphs, and outputs the shortest paths.

Uploaded by

Souvik Pal
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)
38 views4 pages

DAA Practical

The document discusses two algorithms: Floyd-Warshall for all pairs shortest path and Bellman-Ford for single source shortest path. It provides the problem statements, C code implementations of the algorithms on sample graphs, and outputs the shortest paths.

Uploaded by

Souvik Pal
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/ 4

DAA Practical

Problem Statement:
Write a C program to implement all pair shortest path problem (use the graph given given
below) using Floyd-Warshall Algorithm.

Program Code:
#include <stdio.h> printMatrix(matrix);
#define nV 4 }
#define INF 999
void printMatrix(int matrix[][nV]) {
void printMatrix(int matrix[][nV]); for (int i = 0; i < nV; i++) {
for (int j = 0; j < nV; j++) {
void floydWarshall(int graph[][nV]) { if (matrix[i][j] == INF)
int matrix[nV][nV], i, j, k; printf("%4s", "INF");
else
for (i = 0; i < nV; i++) printf("%4d", matrix[i][j]);
for (j = 0; j < nV; j++) }
matrix[i][j] = graph[i][j]; printf("\n");
}
for (k = 0; k < nV; k++) { }
for (i = 0; i < nV; i++) {
for (j = 0; j < nV; j++) { int main() {
if (matrix[i][k] + matrix[k][j] < matrix[i] int graph[nV][nV] = {{0, 3, INF, 7},
[j])
{8, 0, 2, INF},
matrix[i][j] = matrix[i][k] + matrix[k][j];
{5, INF, 0, 1},
}
{2, INF, INF, 0}};
}
floydWarshall(graph);
}
}

Output:

Problem Statement:
 Write a C program to implement single source shortest path problem (use the graph given given
below) using Bellman-Ford Algorithm.

Program Code:
#include <stdio.h> printf("Enter source: ");
#include <stdlib.h> scanf("%d",&S);
int Bellman_Ford(int G[20][20] , int V, int E, distance[S-1]=0 ;
int edge[20][2])
for(i=0;i<V-1;i++)
{
{
int
for(k=0;k<E;k++)
i,u,v,k,distance[20],parent[20],S,flag=1;
{
for(i=0;i<V;i++)
u = edge[k][0] , v = edge[k][1] ;
distance[i] = 1000 , parent[i] = -1 ;
if(distance[u]+G[u][v] < distance[v]) distance[v] = distance[u] + G[u][v] ,
parent[v]=u ;
} if(G[i][j]!=0)
} edge[k][0]=i,edge[k++][1]=j;
for(k=0;k<E;k++)
{
u = edge[k][0] , v = edge[k][1] ; }
if(distance[u]+G[u][v] < distance[v]) if(Bellman_Ford(G,V,k,edge))
flag = 0 ; printf("\nNo negative weight cycle\n");
} else printf("\nNegative weight cycle
exists\n");
if(flag)
return 0;
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("Enter no. of vertices: ");
scanf("%d",&V);
printf("Enter the adjency matrix of the
graph:\n");
for(i=0;i<V;i++)
for(j=0;j<V;j++)
{
scanf("%d",&G[i][j]);
Output:

You might also like