It finnaly kinda works

algorithm with no double nodes and nearly efficient enough
still some optimizations to find for --big-superposition maps
This commit is contained in:
Tanguy MAZE
2019-04-18 20:18:50 +02:00
commit 54be2278c0
137 changed files with 6194 additions and 0 deletions

129
srcs/edmonds_karp.c Normal file
View File

@@ -0,0 +1,129 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* edmonds_karp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/18 09:59:11 by tmaze #+# #+# */
/* Updated: 2019/04/18 19:39:47 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);
ft_printf("scores[0]: %d\nscores[1]: %d\n\n", scores[0], scores[1]);
i = -1;
while (ret[1][++i])
ft_printf("ret[%d]->weight: %d\n\n", i, ret[1][i]->weight);
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]);
}