diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..3d01742f8ff2e9d49639b1d780ca650bca9adade --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +CC = gcc +CFLAGS = -Wall -Wextra -fsanitize=address -fsanitize=leak + +FOLDER = . +SOURCES = $(wildcard $(FOLDER)/*.c) +OBJECTS = $(SOURCES:.c=.o) + +TARGET = all + +$(TARGET) : $(OBJECTS) + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) + +.PHONY: clean + +clean: + rm -f $(TARGET) $(OBJECTS) + +rebuild: clean all diff --git a/ex1.c b/ex1.c new file mode 100644 index 0000000000000000000000000000000000000000..80a820b71e9ee834c001019b8ea70f4828c1c975 --- /dev/null +++ b/ex1.c @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <stdlib.h> + +#define N 5 +#define inf 100 + +void print_matrice(int** w, int n) { + for(int i = 0; i < n; i++) { + for(int j = 0; j < n; j++) { + if(w[i][j] == inf) { + printf(" inf"); + } else { + printf("%4d", w[i][j]); + } + } + printf("\n"); + } + printf("\n"); +} + +int** floyd_warshall(int** w, int n) { + for(int k = 0; k < n; k++) { + for(int i = 0; i < n; i++) { + for(int j = 0; j < n; j++) { + if(w[i][j] > w[i][k] + w[k][j]) { + w[i][j] = w[i][k] + w[k][j]; + } + } + } + print_matrice(w, n); + } + return w; +} + +int** init_matrice(int n) { + int** w = malloc(n * sizeof(int*)); + for(int i = 0; i < n; i++) { + w[i] = malloc(n * sizeof(int)); + } + return w; +} + +void free_matrice(int** w, int n) { + for(int i = 0; i < n; i++) { + free(w[i]); + } + free(w); +} + +int main() { + + int data[N][N] = { + {0, 3, 8, inf, -4}, + {inf, 0, inf, 1, 7}, + {inf, 4, 0, inf, inf}, + {2, inf, -5, 0, inf}, + {inf, inf, inf, 6, 0} + }; + + // int data[N][N] = { + // {0, 2, 4, inf, 3}, + // {2, 0, 8, inf, 1}, + // {6, 2, 0, 4, 3}, + // {1, inf, inf, 0, 5}, + // {inf, inf, inf, 1, 0} + // }; + + int** mat = init_matrice(N); + + for(int i = 0; i < N; i++) { + for(int j = 0; j < N; j++) { + mat[i][j] = data[i][j]; + } + } + print_matrice(mat, N); + floyd_warshall(mat, N); + free_matrice(mat, N); + + return 0; +} \ No newline at end of file