aaaaaaand it's done !

algorithm finished and normed
more testing might be needed
This commit is contained in:
Tanguy MAZE
2019-04-03 17:20:29 +02:00
parent 15669b7402
commit ea0eda89d0
5 changed files with 92 additions and 152 deletions

View File

@@ -6,167 +6,98 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */
/* Updated: 2019/04/01 18:36:10 by tmaze ### ########.fr */
/* Updated: 2019/04/03 17:18:35 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
int in_use(t_ind **ret, int node)
static void update_weights(t_lmdata *data, t_bfs *tab, int end_ind)
{
t_ind *it;
int i;
i = 0;
while (ret && ret[i])
i = end_ind;
while (i != -1)
{
it = ret[i];
while (it)
{
if (it->index == node)
return (1);
it = data->adj[tab[i].parent];
while (it && it->index != i)
it = it->next;
}
i++;
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;
}
return (0);
}
void reset_node(t_lmdata *data, int node)
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 = 1;
it2 = data->adj[it->index];
while (it2 && it2->index != node)
it2 = it2->next;
if (it2)
it2->weight = 1;
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);
}
void mark_node(t_lmdata *data, int node)
static t_ind **clean_ret(t_ind **ret)
{
t_ind *it;
t_ind *it2;
it = data->adj[node];
while (it)
{
it->weight = 2;
it2 = data->adj[it->index];
while (it2 && it2->index != node)
it2 = it2->next;
if (it2)
it2->weight = 0;
it = it->next;
}
tablst_inddel(ret);
return (NULL);
}
t_ind **resolve_path(t_lmdata *data, int start_ind, int end_ind, int nb_path)
t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind
, int nb_path)
{
t_ind **ret;
t_ind *it;
int i;
int j;
ret = NULL;
i = 0;
if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_path + 1)))
!= NULL)
if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*)
* (nb_path + 1))) != NULL)
{
ret[nb_path] = NULL;
while (i < nb_path)
while (i < nb_path && (j = e_ind) == e_ind)
{
j = end_ind;
ft_printf("===== start resolv %d =====\n", i);
it = data->adj[j];
while (j != start_ind)
while (j != s_ind)
{
if (in_use(ret, j))
{
lst_inddel(&(ret[i]));
j = -1;
break ;
}
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);
if (j != end_ind)
reset_node(data, j);
}
j = (j == start_ind) ? -1 : it->index;
return (clean_ret(ret));
j = reset_weights(data, s_ind, j);
}
if (j != -1 && lst_indadd(&(ret[i]), j) == NULL)
return (NULL); //protect this
i ++;
return (clean_ret(ret));
i++;
}
}
return (ret);
}
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
{
t_bfs bfs_tab[data->nb_nodes];
int i;
t_bfs tab[data->nb_nodes];
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)
bfs(data, tab, start_ind, end_ind);
while (tab[end_ind].parent != -1)
{
nb_path++;
i = end_ind;
while (i != -1)
{
if (i != end_ind && i != start_ind)
mark_node(data, i);
i = bfs_tab[i].parent;
}
ft_printf("===== list of adj =====\n");
i = 0;
while (i < data->nb_nodes)
{
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++;
}
ft_printf("===== new bfs =====\n");
bfs(data, bfs_tab, start_ind, end_ind);
}
ft_printf("===== list of adj =====\n");
i = 0;
while (i < 7)
{
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++;
update_weights(data, tab, end_ind);
bfs(data, tab, start_ind, end_ind);
}
return (resolve_path(data, start_ind, end_ind, nb_path));
}