From 15669b740201edad23201310d9c560ed74598c0a Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Mon, 1 Apr 2019 19:06:19 +0200 Subject: [PATCH] not quite there yet but nearly ^^ WIP for the algorithm not to pass on previously used node --- Makefile | 6 +-- srcs/bfs.c | 2 +- srcs/edmunds_karp.c | 107 ++++++++++++++++++++++++++++++++++---------- srcs/lst_ind.c | 4 +- srcs/test.c | 51 ++++++++++----------- 5 files changed, 114 insertions(+), 56 deletions(-) diff --git a/Makefile b/Makefile index 99fb4d8..d4caaa8 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: tmaze +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/03/27 16:51:02 by tmaze #+# #+# # -# Updated: 2019/03/31 19:48:45 by tmaze ### ########.fr # +# Updated: 2019/04/01 14:49:54 by tmaze ### ########.fr # # # #******************************************************************************# @@ -27,7 +27,7 @@ endif # Compilator CC = gcc -FLAGS = -Wall -Wextra -Werror -g -O0 -fsanitize=address +FLAGS = -Wall -Wextra -Werror -g # Folders LIBDIR = libft @@ -36,7 +36,7 @@ OBJDIR = objs INCDIR = includes libft/includes # Source files -SRC = bfs.c lem_in.c lm_parser.c lm_mem_utils.c lm_graph_utils.c \ +SRC = bfs.c lm_parser.c lm_mem_utils.c lm_graph_utils.c \ lst_ind.c \ edmunds_karp.c diff --git a/srcs/bfs.c b/srcs/bfs.c index 3fe3540..73af16d 100644 --- a/srcs/bfs.c +++ b/srcs/bfs.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */ -/* Updated: 2019/03/31 18:23:21 by tmaze ### ########.fr */ +/* Updated: 2019/04/01 17:52:57 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/edmunds_karp.c b/srcs/edmunds_karp.c index fc51f2c..e7c28b6 100644 --- a/srcs/edmunds_karp.c +++ b/srcs/edmunds_karp.c @@ -6,19 +6,74 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */ -/* Updated: 2019/03/31 19:42:22 by tmaze ### ########.fr */ +/* Updated: 2019/04/01 18:36:10 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "lem_in.h" +int in_use(t_ind **ret, int node) +{ + t_ind *it; + int i; + + i = 0; + while (ret && ret[i]) + { + it = ret[i]; + while (it) + { + if (it->index == node) + return (1); + it = it->next; + } + i++; + } + return (0); +} + +void reset_node(t_lmdata *data, int node) +{ + t_ind *it; + t_ind *it2; + + it = data->adj[node]; + while (it) + { + it->weight = 1; + it2 = data->adj[it->index]; + while (it2 && it2->index != node) + it2 = it2->next; + if (it2) + it2->weight = 1; + it = it->next; + } +} + +void mark_node(t_lmdata *data, int node) +{ + t_ind *it; + t_ind *it2; + + it = data->adj[node]; + while (it) + { + it->weight = 2; + it2 = data->adj[it->index]; + while (it2 && it2->index != node) + it2 = it2->next; + if (it2) + it2->weight = 0; + it = it->next; + } +} + t_ind **resolve_path(t_lmdata *data, int start_ind, int end_ind, int nb_path) { t_ind **ret; t_ind *it; int i; int j; - int k; ret = NULL; i = 0; @@ -33,6 +88,12 @@ t_ind **resolve_path(t_lmdata *data, int start_ind, int end_ind, int nb_path) it = data->adj[j]; while (j != start_ind) { + if (in_use(ret, j)) + { + lst_inddel(&(ret[i])); + j = -1; + break ; + } if (lst_indadd(&(ret[i]), j) == NULL) return (NULL); //protect this it = data->adj[j]; @@ -45,19 +106,14 @@ t_ind **resolve_path(t_lmdata *data, int start_ind, int end_ind, int nb_path) if (it && it->weight == 2) { ft_printf("parent selected: %d\n", it->index); - it->weight--; + if (j != end_ind) + reset_node(data, j); } - k = it->index; - it = data->adj[k]; - while (it && it->index != j) - it = it->next; - if (it && it->index == j) - it->weight++; - j = (j == start_ind) ? -1 : k; + j = (j == start_ind) ? -1 : it->index; } - if (lst_indadd(&(ret[i]), j) == NULL) + if (j != -1 && lst_indadd(&(ret[i]), j) == NULL) return (NULL); //protect this - i++; + i ++; } } return (ret); @@ -79,24 +135,29 @@ t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind) i = end_ind; while (i != -1) { - it = data->adj[bfs_tab[i].parent]; - while (it && it->index != i) - it = it->next; - if (it && it->index == i) - it->weight--; - it = data->adj[i]; - while (it && it->index != bfs_tab[i].parent) - it = it->next; - if (it && it->index == bfs_tab[i].parent) - it->weight++; + if (i != end_ind && i != start_ind) + mark_node(data, i); i = bfs_tab[i].parent; } + ft_printf("===== list of adj =====\n"); + i = 0; + while (i < data->nb_nodes) + { + ft_printf("===== adj of %d =====\n", i); + it = data->adj[i]; + while (it) + { + ft_printf("index: %d\nweight: %d\n\n", it->index, it->weight); + it = it->next; + } + i++; + } ft_printf("===== new bfs =====\n"); bfs(data, bfs_tab, start_ind, end_ind); } ft_printf("===== list of adj =====\n"); i = 0; - while (i < 8) + while (i < 7) { ft_printf("===== adj of %d =====\n", i); it = data->adj[i]; diff --git a/srcs/lst_ind.c b/srcs/lst_ind.c index 4a39afc..1fdce82 100644 --- a/srcs/lst_ind.c +++ b/srcs/lst_ind.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */ -/* Updated: 2019/03/31 17:53:47 by tmaze ### ########.fr */ +/* Updated: 2019/04/01 17:37:51 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ void lst_inddel(t_ind **lst) { t_ind *tmp; - while (*lst != NULL) + while (lst && *lst != NULL) { tmp = *lst; *lst = (*lst)->next; diff --git a/srcs/test.c b/srcs/test.c index 5de9ff6..58b589d 100644 --- a/srcs/test.c +++ b/srcs/test.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/28 11:37:06 by tmaze #+# #+# */ -/* Updated: 2019/03/31 19:47:57 by tmaze ### ########.fr */ +/* Updated: 2019/04/01 17:51:54 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,19 +31,29 @@ int main(void) return (1); if (!add_link(&data, 0, 1) || !add_link(&data, 0, 2)) { - lst_inddel(&(data.adj[0])); - ft_memdel((void**)&(data.adj)); - return (1); - } - if (!add_link(&data, 1, 4)) - { - lst_inddel(&(data.adj[4])); lst_inddel(&(data.adj[1])); lst_inddel(&(data.adj[0])); ft_memdel((void**)&(data.adj)); return (1); } - if (!add_link(&data, 2, 3) || !add_link(&data, 2, 5)) + if (!add_link(&data, 1, 3)) + { + lst_inddel(&(data.adj[2])); + lst_inddel(&(data.adj[1])); + lst_inddel(&(data.adj[0])); + ft_memdel((void**)&(data.adj)); + return (1); + } + if (!add_link(&data, 2, 3)) + { + lst_inddel(&(data.adj[3])); + lst_inddel(&(data.adj[2])); + lst_inddel(&(data.adj[1])); + lst_inddel(&(data.adj[0])); + ft_memdel((void**)&(data.adj)); + return (1); + } + if (!add_link(&data, 3, 4) || !add_link(&data, 3, 5)) { lst_inddel(&(data.adj[4])); lst_inddel(&(data.adj[3])); @@ -53,9 +63,9 @@ int main(void) ft_memdel((void**)&(data.adj)); return (1); } - if (!add_link(&data, 3, 6)) + if (!add_link(&data, 4, 6)) { - lst_inddel(&(data.adj[6])); + lst_inddel(&(data.adj[5])); lst_inddel(&(data.adj[4])); lst_inddel(&(data.adj[3])); lst_inddel(&(data.adj[2])); @@ -64,21 +74,8 @@ int main(void) ft_memdel((void**)&(data.adj)); return (1); } - /* if (!add_link(&data, 4, 5)) */ - /* { */ - /* lst_inddel(&(data.adj[6])); */ - /* lst_inddel(&(data.adj[5])); */ - /* lst_inddel(&(data.adj[4])); */ - /* lst_inddel(&(data.adj[3])); */ - /* lst_inddel(&(data.adj[2])); */ - /* lst_inddel(&(data.adj[1])); */ - /* lst_inddel(&(data.adj[0])); */ - /* ft_memdel((void**)&(data.adj)); */ - /* return (1); */ - /* } */ - if (!add_link(&data, 5, 7)) + if (!add_link(&data, 5, 6)) { - lst_inddel(&(data.adj[7])); lst_inddel(&(data.adj[6])); lst_inddel(&(data.adj[5])); lst_inddel(&(data.adj[4])); @@ -89,7 +86,7 @@ int main(void) ft_memdel((void**)&(data.adj)); return (1); } - if (!add_link(&data, 6, 7)) + if (!add_link(&data, 7, 4) || !add_link(&data, 7, 1)) { lst_inddel(&(data.adj[7])); lst_inddel(&(data.adj[6])); @@ -115,7 +112,7 @@ int main(void) } i++; } - path = edmunds_karp(&data, 0, 7); + path = edmunds_karp(&data, 0, 6); if (path != NULL) { i = 0;