From 6b28cbabbe0484a942b8465cb98ce75257a481f3 Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Sun, 21 Feb 2016 16:52:20 +0100 Subject: [PATCH] EPA: Floyd Warshall --- floyd_warshall.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 floyd_warshall.c diff --git a/floyd_warshall.c b/floyd_warshall.c new file mode 100644 index 0000000..4efab70 --- /dev/null +++ b/floyd_warshall.c @@ -0,0 +1,44 @@ +#include + +#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; +}