-
Notifications
You must be signed in to change notification settings - Fork 0
/
All pairs shortest path.c
94 lines (86 loc) · 1.68 KB
/
All pairs shortest path.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
int min(int a,int b){
return (a<b)?a:b ;
}
void copy(int c[50][50],int a[50][50],int n){
int i,j,k;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
a[i][j] = c[i][j];
}
}
}
void accept(int c[50][50],int D[50][50],int n){
int i,j,k;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&c[i][j]);
}
}
//generating D0
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(c[i][j] == 0 || c[i][j] == 999)
D[i][j] = -1;
else
D[i][j] = i+1;
}
}//D0 generated
}
void all_pairs_shortest_path(int A[50][50],int D[50][50],int n){
int i,j,k;
for(k=0;k<n;k++){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if ( A[i][j] > (A[i][k]+A[k][j]) )
D[i][j] = k+1;
A[i][j] = min( A[i][j] , (A[i][k]+A[k][j]) ) ;
}
}
}
}
void path(int v1,int v2,int D[50][50],int n){
if( v1 == D[v1-1][v2-1])
return;
printf("%d-",D[v1-1][v2-1]);
path(D[v1-1][v2-1],v2,D,n);
}
void find_path(int A[50][50],int D[50][50],int n){
int v1,v2,ch;
while(1){
printf("\n1:Continue 2:Break\n");
scanf("%d",&ch);
if(ch == 2) break;
printf("Enter 2 vertices to find path");
scanf("%d%d",&v1,&v2);
printf("Length=%d\n",A[v1-1][v2-1]);
printf("Path:%d-",v1);
path(v1,v2,D,n);
printf("%d\n",v2);
}
}
void display(int mat[50][50],int n){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d\t",mat[i][j]);
}
printf("\n");
}
}
int main(){
int cost[50][50],D[50][50],A[50][50];
int i,j,k,n;
printf("Enter no of vertices ");
scanf("%d",&n);
printf("Enter cost adjacency matrix:\n");
accept(cost,D,n);
copy(cost,A,n);
all_pairs_shortest_path(A,D,n);
printf("A^n Matrix is:\n");
display(A,n);
printf("D^n Matrix is:\n");
display(D,n);
find_path(A,D,n);
return 0;
}