lem_in/srcs/edmunds_karp.c
Tanguy MAZE c36f501b08 testing things
thinking of how to display the ants moving and optimise by moving at
least one on each path
2019-04-05 17:43:17 +02:00

106 lines
2.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* edmunds_karp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */
/* Updated: 2019/04/05 15:13:53 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)
{
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 s_ind, int node)
{
t_ind *it;
t_ind *it2;
it = data->adj[node];
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);
}
static t_ind **clean_ret(t_ind **ret)
{
tablst_inddel(ret);
return (NULL);
}
t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind
, int nb_path)
{
t_ind **ret;
int i;
int j;
ret = NULL;
i = 0;
if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*)
* (nb_path + 1))) != NULL)
{
while (i < nb_path && (j = e_ind) == e_ind)
{
while (j != s_ind)
{
if (lst_indadd(&(ret[i]), j) == NULL)
return (clean_ret(ret));
j = reset_weights(data, s_ind, j);
}
i++;
}
}
return (ret);
}
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
{
t_bfs tab[data->nb_nodes];
int nb_path;
nb_path = 0;
if (data && data->adj)
{
bfs(data, tab, start_ind, end_ind);
while (tab[end_ind].parent != -1)
{
nb_path++;
update_weights(data, tab, end_ind);
bfs(data, tab, start_ind, end_ind);
}
return (resolve_path(data, start_ind, end_ind, nb_path));
}
return (NULL);
}