/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* edmunds_karp.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */ /* Updated: 2019/04/15 17:37:09 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "lem_in.h" static void update_weights(t_lmdata *data, t_bfs *tab, int end_ind) { t_ind *it; int i; i = end_ind; while (i != -1) { if (tab[i].parent != -1) { it = data->adj[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 != tab[i].parent) it = it->next; if (it && it->index == tab[i].parent) it->weight++; } i = tab[i].parent; } } /* static int reset_weights(t_lmdata *data, int node) */ /* { */ /* t_ind *it; */ /* t_ind *it2; */ /* it = data->adj[node]; */ /* it2 = NULL; */ /* while (it && it->weight != 2) */ /* it = it->next; */ /* if (it && it->weight == 2) */ /* it->weight = 1; */ /* if (it && it->index) */ /* it2 = data->adj[it->index]; */ /* while (it2 && it2->index != node) */ /* it2 = it2->next; */ /* if (it2 && it2->index == node) */ /* it2->weight = 1; */ /* return ((it != NULL) ? it->index : -1); */ /* } */ static t_path *clean_ret(t_path *ret) { int i; i = 0; while (ret[i].nodes) lst_inddel(&(ret[i++].nodes)); return (NULL); } static t_path *resolve_path(t_lmdata *data, int s_ind, int e_ind, t_bfs *tab) { t_path *ret; int i; int j; t_ind *it; i = 0; ret = NULL; if ((ret = (t_path*)ft_memalloc(sizeof(t_path) * (data->nb_paths_max))) != NULL) { it = data->adj[e_ind]; while (it && i < data->nb_paths_max && (j = e_ind) == e_ind) { if (it->weight == 2 && (j = it->index)) { if (lst_indadd(&(ret[i].nodes), e_ind) == NULL) return (clean_ret(ret)); while (j != s_ind) { if (lst_indadd(&(ret[i].nodes), j) == NULL) return (clean_ret(ret)); j = tab[j].parent; } i++; } it = it->next; } } return (ret); } t_path *edmunds_karp(t_lmdata *data, int start_ind, int end_ind) { t_bfs tab[data->nb_nodes]; t_path *ret[2]; int score[2]; ret[0] = NULL; ret[1] = NULL; score[0] = 0; score[1] = 0; ft_printf("nb paths max: %d\n", data->nb_paths_max); if (data && data->adj) { bfs(data, tab, start_ind, end_ind); while (tab[end_ind].parent != -1) { update_weights(data, tab, end_ind); if (tab[end_ind].parent != -1) ret[1] = resolve_path(data, start_ind, end_ind, tab); ft_printf("ret 0: %p\n", ret[0]); ft_printf("score 0: %d\nscore 1: %d\n", score[0], get_score(data, ret[1])); score[1] = get_score(data, ret[1]); if (ret[0] == NULL || score[0] > score[1]) { score[0] = score[1]; if (ret[0] != NULL) clean_ret(ret[0]); ret[0] = ret[1]; bfs(data, tab, start_ind, end_ind); } else if (score[0] <= score[1]) { clean_ret(ret[1]); break; } } return (ret[0]); } return (NULL); }