lem_in/srcs/edmunds_karp.c
Tanguy MAZE ab578f6039 we got it !
functionning edmunds_karp
still need some testing for already used nodes
still need some memory protection
2019-03-31 19:49:42 +02:00

112 lines
3.0 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* edmunds_karp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */
/* Updated: 2019/03/31 19:42:22 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
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;
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 (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);
it->weight--;
}
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;
}
if (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)
{
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++;
i = bfs_tab[i].parent;
}
ft_printf("===== new bfs =====\n");
bfs(data, bfs_tab, start_ind, end_ind);
}
ft_printf("===== list of adj =====\n");
i = 0;
while (i < 8)
{
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));
}