/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* edmunds_karp.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */ /* 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; ret = NULL; i = 0; if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_path + 1))) != NULL) { ret[nb_path] = NULL; while (i < nb_path) { j = end_ind; ft_printf("===== start resolv %d =====\n", i); 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]; 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; } if (j != -1 && lst_indadd(&(ret[i]), j) == NULL) return (NULL); //protect this i ++; } } return (ret); } t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind) { t_bfs bfs_tab[data->nb_nodes]; int i; 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) { 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++; } return (resolve_path(data, start_ind, end_ind, nb_path)); }