EPA: Floyd Warshall
This commit is contained in:
parent
0e153f547a
commit
6b28cbabbe
1 changed files with 44 additions and 0 deletions
44
floyd_warshall.c
Normal file
44
floyd_warshall.c
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define N 3
|
||||||
|
|
||||||
|
typedef int Mat[N*N];
|
||||||
|
|
||||||
|
void matiter(Mat C, int (*reduce)(int old, int ik, int kj)) {
|
||||||
|
for(unsigned int k = 0; k < N; k++) {
|
||||||
|
for(unsigned int i = 0; i < N; i++) {
|
||||||
|
for(unsigned int j = 0; j < N; j++) {
|
||||||
|
C[i*N+j] = reduce(C[i*N+j], C[i*N+k], C[k*N+j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int floyd(int old, int ik, int kj) {
|
||||||
|
int new = ik + kj;
|
||||||
|
return ( old < new ? old : new );
|
||||||
|
}
|
||||||
|
|
||||||
|
int warshall(int old, int ik, int kj) {
|
||||||
|
return (old ? old : ik && kj);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main( int argc, char *argv[] ) {
|
||||||
|
Mat C =
|
||||||
|
{
|
||||||
|
0 , 0 , 0 ,
|
||||||
|
1 , 0 , 0 ,
|
||||||
|
0 , 1 , 0
|
||||||
|
};
|
||||||
|
|
||||||
|
matiter(C, *warshall);
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < N; i++) {
|
||||||
|
for(unsigned int j = 0; j < N; j++) {
|
||||||
|
printf("%5d ", C[i*N+j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in a new issue