-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloydwarshall.cpp
More file actions
60 lines (54 loc) · 1.09 KB
/
floydwarshall.cpp
File metadata and controls
60 lines (54 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<stdio.h>
#define N 10
int cost[N][N];
int minimum(int a,int b){
return(a>b?b:a);
}
void floydwarshall(int n){
int w[n+1][n][n];
int i,j,k;
/*Initialize first weight array as infinity*/
for(i=0;i<n;i++){
for(j=0;j<n;j++){
w[0][i][j] = 1000;
}
}
/*k=0 step initialize directly connected edges*/
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(cost[i][j]!=1000){
w[0][i][j] = cost[i][j];
}
}
}
/*run n times n^2 updates*/
printf("All pair shortest path is:\n");
for(k=1;k<=n;k++){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
w[k][i][j] = minimum(w[k-1][i][j],w[k-1][i][k-1]+w[k-1][k-1][j]);
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d ",w[k][i][j]);
}
printf("\n");
}
printf("\n");
}
}
int main(){
int n,i=0,j=0,s=0;
printf("Enter the no of vertices:");
scanf("%d",&n);
//printf("%d",INT_MAX);
printf("\nEnter the cost matrix:");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&cost[i][j]);
}
}
floydwarshall(n);
return 0;
}