fuckinlem_in/srcs/edmonds_karp.c
Tanguy MAZE 1f24922155 Works a bit less but on in a better way
changed rules of queue addition
still problems on map4 with 2 merged paths
2019-04-19 13:26:13 +02:00

126 lines
2.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* edmonds_karp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/18 09:59:11 by tmaze #+# #+# */
/* Updated: 2019/04/19 12:53:52 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
t_ind **resolve_paths(t_lmdata *data, int nb_paths, int s_ind, int e_ind)
{
t_ind **ret;
t_ind *it;
t_ind *it2;
int i;
int j;
int nb_nodes;
if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_paths + 1))) != NULL)
{
i = 0;
it = data->adj[e_ind];
j = e_ind;
while (it && i < nb_paths)
{
if (it->weight == 2 && (nb_nodes = 1))
{
lst_indadd(&(ret[i]), e_ind);
j = it->index;
while (j != s_ind)
{
lst_indadd(&(ret[i]), j);
nb_nodes++;
it2 = data->adj[j];
while (it2 && it2->weight != 2)
it2 = it2->next;
if (it2 && it2->weight == 2)
j = it2->index;
}
ret[i]->weight = nb_nodes;
i++;
}
it = it->next;
}
}
return (ret);
}
void print_paths(t_lmdata *data, t_ind **ret)
{
int i;
t_ind *it;
i = 0;
ft_putstr("=== all paths ===\n");
while (ret[i])
{
it = ret[i];
while (it)
{
ft_printf("%s-> ", get_node(data, it->index)->name);
it = it->next;
}
ft_putstr("\n\n");
i++;
}
}
t_ind **edmonds_karp(t_lmdata *data, int s_ind, int e_ind)
{
t_bfs tab[data->nb_nodes];
t_ind **ret[2];
t_ind *it;
int nb_paths;
int scores[2];
int i;
ret[0] = NULL;
scores[0] = 0;
i = 0;
nb_paths = 0;
while (i < data->nb_nodes)
tab[i++].old_visited = 0;
bfs(data, tab, s_ind, e_ind);
while (tab[e_ind].parent != -1)
{
nb_paths++;
i = e_ind;
while (i != s_ind)
{
tab[i].old_visited = 0;
it = data->adj[i];
while (it && it->index != tab[i].parent)
it = it->next;
if (it->index == tab[i].parent)
it->weight++;
if (i != e_ind && it->weight == 2)
tab[i].old_visited = 1;
it = data->adj[tab[i].parent];
while (it && it->index != i)
it = it->next;
if (it)
it->weight--;
i = tab[i].parent;
}
ret[1] = resolve_paths(data, nb_paths, s_ind, e_ind);
scores[1] = get_score(data, ret[1], nb_paths);
if (ret[0] == NULL || scores[1] < scores[0])
{
if (ret[0] != NULL)
tablst_inddel(ret[0]);
ret[0] = ret[1];
scores[0] = scores[1];
}
else
tablst_inddel(ret[1]);
bfs(data, tab, s_ind, e_ind);
}
return (ret[0]);
}