not quite there yet but nearly ^^
WIP for the algorithm not to pass on previously used node
This commit is contained in:
@@ -6,19 +6,74 @@
|
||||
/* 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 */
|
||||
/* Updated: 2019/04/01 18:36:10 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
int in_use(t_ind **ret, int node)
|
||||
{
|
||||
t_ind *it;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (ret && ret[i])
|
||||
{
|
||||
it = ret[i];
|
||||
while (it)
|
||||
{
|
||||
if (it->index == node)
|
||||
return (1);
|
||||
it = it->next;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void reset_node(t_lmdata *data, 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;
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
|
||||
void mark_node(t_lmdata *data, int node)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -33,6 +88,12 @@ t_ind **resolve_path(t_lmdata *data, int start_ind, int end_ind, int nb_path)
|
||||
it = data->adj[j];
|
||||
while (j != start_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];
|
||||
@@ -45,19 +106,14 @@ t_ind **resolve_path(t_lmdata *data, int start_ind, int end_ind, int nb_path)
|
||||
if (it && it->weight == 2)
|
||||
{
|
||||
ft_printf("parent selected: %d\n", it->index);
|
||||
it->weight--;
|
||||
if (j != end_ind)
|
||||
reset_node(data, j);
|
||||
}
|
||||
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;
|
||||
j = (j == start_ind) ? -1 : it->index;
|
||||
}
|
||||
if (lst_indadd(&(ret[i]), j) == NULL)
|
||||
if (j != -1 && lst_indadd(&(ret[i]), j) == NULL)
|
||||
return (NULL); //protect this
|
||||
i++;
|
||||
i ++;
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
@@ -79,24 +135,29 @@ t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
|
||||
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++;
|
||||
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 < 8)
|
||||
while (i < 7)
|
||||
{
|
||||
ft_printf("===== adj of %d =====\n", i);
|
||||
it = data->adj[i];
|
||||
|
||||
Reference in New Issue
Block a user