//programme for floyd warshall algroithms - all pair shortest path
// Complexity - O(V ^ 3) where V is total vertices
#include<stdio.h>
#include<conio.h>
//number of vertices in ours graphh
#define V 4
/*
* Define Indefinite as large as possible to represent vertices
* not connected to eachother
*/
#define INF 99999
// a function to print solution matrix of all pair shortest paths
void printSolution(int dist[][V]);
//solves the all- pair shortest path problem
void floydWarshell(int graph[][V]){
//dist[][] will be the output matrix tht will have shortest
//path
int dist[V][V], i, j, k;
//initial solution matrix same as graph matrix
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = graph[i][j];
for (k = 0; k < V; k++)
{
// Pick all vertices as source one by one
for (i = 0; i < V; i++)
{
// Pick all vertices as destination for the
// above picked source
for (j = 0; j < V; j++)
{
// If vertex k is on the shortest path from
// i to j, then update the value of dist[i][j]
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
// Print the shortest distance matrix
printSolution(dist);
}
void printSolution(int dist[][V])
{
printf("Following matrix shows the shortest distances"
" between every pair of vertices \n");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][j] == INF)
printf("%7s", "INF");
else
printf("%7d", dist[i][j]);
}
printf("\n");
}
}
int main()
{
/* create the following weighted graph
10
(0)------->(3)
| /|\
5 | |1
| |
\|/ |
(1)------->(2)
3
*/
int graph[V][V] =
{
{ 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 }
};
// Print the solution
floydWarshell(graph);
getchar();
return 0;
}
// Complexity - O(V ^ 3) where V is total vertices
#include<stdio.h>
#include<conio.h>
//number of vertices in ours graphh
#define V 4
/*
* Define Indefinite as large as possible to represent vertices
* not connected to eachother
*/
#define INF 99999
// a function to print solution matrix of all pair shortest paths
void printSolution(int dist[][V]);
//solves the all- pair shortest path problem
void floydWarshell(int graph[][V]){
//dist[][] will be the output matrix tht will have shortest
//path
int dist[V][V], i, j, k;
//initial solution matrix same as graph matrix
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = graph[i][j];
for (k = 0; k < V; k++)
{
// Pick all vertices as source one by one
for (i = 0; i < V; i++)
{
// Pick all vertices as destination for the
// above picked source
for (j = 0; j < V; j++)
{
// If vertex k is on the shortest path from
// i to j, then update the value of dist[i][j]
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
// Print the shortest distance matrix
printSolution(dist);
}
void printSolution(int dist[][V])
{
printf("Following matrix shows the shortest distances"
" between every pair of vertices \n");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][j] == INF)
printf("%7s", "INF");
else
printf("%7d", dist[i][j]);
}
printf("\n");
}
}
int main()
{
/* create the following weighted graph
10
(0)------->(3)
| /|\
5 | |1
| |
\|/ |
(1)------->(2)
3
*/
int graph[V][V] =
{
{ 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 }
};
// Print the solution
floydWarshell(graph);
getchar();
return 0;
}
No comments:
Post a Comment