From 15669b740201edad23201310d9c560ed74598c0a Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Mon, 1 Apr 2019 19:06:19 +0200 Subject: [PATCH 1/2] 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; From ea0eda89d04a783f095b981bd459e117a73ed293 Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Wed, 3 Apr 2019 17:20:29 +0200 Subject: [PATCH 2/2] aaaaaaand it's done ! algorithm finished and normed more testing might be needed --- includes/lem_in.h | 3 +- srcs/bfs.c | 35 +++++----- srcs/edmunds_karp.c | 155 ++++++++++++-------------------------------- srcs/lst_ind.c | 12 +++- srcs/test.c | 39 ++++++----- 5 files changed, 92 insertions(+), 152 deletions(-) diff --git a/includes/lem_in.h b/includes/lem_in.h index 2440833..4363cba 100644 --- a/includes/lem_in.h +++ b/includes/lem_in.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */ -/* Updated: 2019/03/31 17:58:52 by tmaze ### ########.fr */ +/* Updated: 2019/04/03 15:57:39 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -112,6 +112,7 @@ int lm_getparams(t_lmdata *data); t_ind *lst_indadd(t_ind **lst, int ind); void lst_inddel(t_ind **lst); +void tablst_inddel(t_ind **tab); void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind); t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind); diff --git a/srcs/bfs.c b/srcs/bfs.c index 73af16d..eaf3a70 100644 --- a/srcs/bfs.c +++ b/srcs/bfs.c @@ -6,26 +6,12 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */ -/* Updated: 2019/04/01 17:52:57 by tmaze ### ########.fr */ +/* Updated: 2019/04/03 17:19:07 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "lem_in.h" -static void bfs_print(t_bfs *tab, int nb_nodes) -{ - int i; - - i = 0; - ft_printf("===== bfs print =====\n"); - while (i < nb_nodes) - { - ft_printf("index: %d\nparent: %d\nvisited: %d\nqueue: %d\n\n", i - , tab[i].parent, tab[i].visited != 0, tab[i].queue); - i++; - } -} - static void bfs_addtoqueue(t_bfs *tab, int node, int nb_nodes) { int i; @@ -43,13 +29,26 @@ static void bfs_addtoqueue(t_bfs *tab, int node, int nb_nodes) static void bfs_checkadj(t_lmdata *data, t_bfs *tab, int i) { t_ind *it; + char used; + it = data->adj[tab[i].queue]; + used = 0; + while (it != NULL) + { + if (tab[it->index].visited == 0 && it->weight == 2) + used = 1; + if (tab[it->index].visited == 0 && it->weight == 2) + break ; + it = it->next; + } it = data->adj[tab[i].queue]; while (it != NULL) { - if (tab[it->index].visited == 0 && it->weight > 0) + if (tab[it->index].visited == 0 && ((!used && it->weight > 0) + || (used && it->weight == 2))) bfs_addtoqueue(tab, it->index, data->nb_nodes); - if (tab[it->index].visited == 0 && it->weight > 0) + if (tab[it->index].visited == 0 && ((!used && it->weight > 0) + || (used && it->weight == 2))) tab[it->index].parent = tab[i].queue; it = it->next; } @@ -69,7 +68,7 @@ static void bfs_init(t_bfs *tab, t_lmdata *data) } } -void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind) +void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind) { int i; diff --git a/srcs/edmunds_karp.c b/srcs/edmunds_karp.c index e7c28b6..de41131 100644 --- a/srcs/edmunds_karp.c +++ b/srcs/edmunds_karp.c @@ -6,167 +6,98 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */ -/* Updated: 2019/04/01 18:36:10 by tmaze ### ########.fr */ +/* Updated: 2019/04/03 17:18:35 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "lem_in.h" -int in_use(t_ind **ret, int node) +static void update_weights(t_lmdata *data, t_bfs *tab, int end_ind) { t_ind *it; int i; - i = 0; - while (ret && ret[i]) + i = end_ind; + while (i != -1) { - it = ret[i]; - while (it) - { - if (it->index == node) - return (1); + it = data->adj[tab[i].parent]; + while (it && it->index != i) it = it->next; - } - i++; + if (it && it->index == i) + it->weight--; + it = data->adj[i]; + while (it && it->index != tab[i].parent) + it = it->next; + if (it && it->index == tab[i].parent) + it->weight++; + i = tab[i].parent; } - return (0); } -void reset_node(t_lmdata *data, int node) +static int reset_weights(t_lmdata *data, int s_ind, 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; + while (it && it->weight != 2) it = it->next; - } + if (it && it->weight == 2) + it->weight--; + it2 = data->adj[it->index]; + while (it2 && it2->index != node) + it2 = it2->next; + if (it2 && it2->index == node) + it2->weight++; + return ((node == s_ind) ? -1 : it->index); } -void mark_node(t_lmdata *data, int node) +static t_ind **clean_ret(t_ind **ret) { - 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; - } + tablst_inddel(ret); + return (NULL); } -t_ind **resolve_path(t_lmdata *data, int start_ind, int end_ind, int nb_path) +t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind + , int nb_path) { t_ind **ret; - t_ind *it; int i; int j; ret = NULL; i = 0; - if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_path + 1))) - != NULL) + if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*) + * (nb_path + 1))) != NULL) { - ret[nb_path] = NULL; - while (i < nb_path) + while (i < nb_path && (j = e_ind) == e_ind) { - j = end_ind; - ft_printf("===== start resolv %d =====\n", i); - it = data->adj[j]; - while (j != start_ind) + while (j != s_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]; - ft_printf("===== parents of %d =====\n", j); - while (it && it->weight != 2) - { - ft_printf("index: %d\nweight: %d\n\n", it->index, it->weight); - it = it->next; - } - if (it && it->weight == 2) - { - ft_printf("parent selected: %d\n", it->index); - if (j != end_ind) - reset_node(data, j); - } - j = (j == start_ind) ? -1 : it->index; + return (clean_ret(ret)); + j = reset_weights(data, s_ind, j); } if (j != -1 && lst_indadd(&(ret[i]), j) == NULL) - return (NULL); //protect this - i ++; + return (clean_ret(ret)); + i++; } } return (ret); } -t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind) +t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind) { - t_bfs bfs_tab[data->nb_nodes]; - int i; + t_bfs tab[data->nb_nodes]; int nb_path; - t_ind *it; nb_path = 0; - ft_printf("===== Init bfs =====\n"); - bfs(data, bfs_tab, start_ind, end_ind); - while (bfs_tab[end_ind].parent != -1) + bfs(data, tab, start_ind, end_ind); + while (tab[end_ind].parent != -1) { nb_path++; - i = end_ind; - while (i != -1) - { - 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 < 7) - { - 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++; + update_weights(data, tab, end_ind); + bfs(data, tab, start_ind, end_ind); } return (resolve_path(data, start_ind, end_ind, nb_path)); } diff --git a/srcs/lst_ind.c b/srcs/lst_ind.c index 1fdce82..39a0f03 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/04/01 17:37:51 by tmaze ### ########.fr */ +/* Updated: 2019/04/03 15:57:26 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,3 +37,13 @@ void lst_inddel(t_ind **lst) ft_memdel((void**)&tmp); } } + +void tablst_inddel(t_ind **tab) +{ + int i; + + i = 0; + while (tab[i]) + lst_inddel(&(tab[i++])); + ft_memdel((void**)&tab); +} diff --git a/srcs/test.c b/srcs/test.c index 58b589d..9eb7988 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/04/01 17:51:54 by tmaze ### ########.fr */ +/* Updated: 2019/04/03 15:58:22 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ int main(void) ft_memdel((void**)&(data.adj)); return (1); } - if (!add_link(&data, 1, 3)) + if (!add_link(&data, 1, 4)) { lst_inddel(&(data.adj[2])); lst_inddel(&(data.adj[1])); @@ -44,7 +44,7 @@ int main(void) ft_memdel((void**)&(data.adj)); return (1); } - if (!add_link(&data, 2, 3)) + if (!add_link(&data, 2, 3) || !add_link(&data, 2, 5)) { lst_inddel(&(data.adj[3])); lst_inddel(&(data.adj[2])); @@ -53,20 +53,9 @@ int main(void) 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])); - 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, 4, 6)) + if (!add_link(&data, 3, 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])); @@ -74,7 +63,18 @@ int main(void) ft_memdel((void**)&(data.adj)); return (1); } - if (!add_link(&data, 5, 6)) + if (!add_link(&data, 4, 5)) + { + lst_inddel(&(data.adj[6])); + lst_inddel(&(data.adj[5])); + 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, 6) || !add_link(&data, 5, 7)) { lst_inddel(&(data.adj[6])); lst_inddel(&(data.adj[5])); @@ -86,7 +86,7 @@ int main(void) ft_memdel((void**)&(data.adj)); return (1); } - if (!add_link(&data, 7, 4) || !add_link(&data, 7, 1)) + if (!add_link(&data, 6, 7)) { lst_inddel(&(data.adj[7])); lst_inddel(&(data.adj[6])); @@ -112,7 +112,7 @@ int main(void) } i++; } - path = edmunds_karp(&data, 0, 6); + path = edmunds_karp(&data, 0, 7); if (path != NULL) { i = 0; @@ -125,11 +125,10 @@ int main(void) it = it->next; } ft_putchar('\n'); - lst_inddel(&(path[i])); i++; } } - ft_memdel((void**)&path); + tablst_inddel(path); lst_inddel(&(data.adj[7])); lst_inddel(&(data.adj[6])); lst_inddel(&(data.adj[5]));