Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- INF 2 8 INF
- INF INF 3 4
- INF INF 0 7
- 5 INF INF 0
- #include<stdio.h>
- #define V 4
- #define INF 99999
- void path(int i, int j)
- {
- if (i==j)
- printf("%d",i);
- else
- if (t[i][j]==0)
- printf("No path found.n");
- else
- {
- path(i,t[i][j]);
- printf("%d",j);
- }
- }
- void floydWarshall (int graph[][V])
- {
- int dist[V][V], i, j, k, t[V][V];
- for (i = 0; i < V; i++)
- for (j = 0; j < V; j++)
- dist[i][j] = graph[i][j];
- for (i = 0; i < V; i++)
- for (j = 0; j < V; j++)
- {
- if(i==j || dist[i][j]==INF)
- t[i][j]=0;
- else
- t[i][j]=i+1;
- }
- for (k = 0; k < V; k++)
- {
- for (i = 0; i < V; i++)
- {
- for (j = 0; j < V; j++)
- {
- if (dist[i][k] + dist[k][j] < dist[i][j]){
- dist[i][j] = dist[i][k] + dist[k][j];
- t[i][j]=t[k][j];
- }
- }
- }
- }
- printf("nShortest paths matrix: n");
- pathMatrix(dist);
- printf("nPredecessor matrix: n");
- pathMatrix(t);
- //How to call path reconstruction in this function?
- for(i=0;i<V;i++)
- {
- for(j=0;j<V;j++)
- {
- if(i==j)
- {
- printf("nShortest cyclic path from %d. vertex: %d",i+1,dist[i][i]);
- }
- }
- }
- }
- void pathMatrix(int dist[][V])
- {
- 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");
- }
- }
- void predMatrix(int t[][V])
- {
- for (int i = 0; i < V; i++)
- {
- for (int j = 0; j < V; j++)
- {
- printf ("%7d", t[i][j]);
- }
- printf("n");
- }
- }
- int main()
- {
- int graph[V][V] = {{INF,2,8,INF},{INF,INF,3,4},{INF,INF,INF,7},{5,INF,INF,INF}};
- floydWarshall(graph);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement