spring cleanup =)

normalized all algo files
ready for integration
This commit is contained in:
Tanguy MAZE 2019-04-21 16:55:12 +02:00
parent 5dd290bcf7
commit 7b5ef85940
4 changed files with 118 additions and 129 deletions

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/18 09:25:05 by tmaze #+# #+# */ /* Created: 2019/04/18 09:25:05 by tmaze #+# #+# */
/* Updated: 2019/04/20 18:04:48 by tmaze ### ########.fr */ /* Updated: 2019/04/21 14:42:08 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,20 +26,24 @@ void bfs_init(t_lmdata *data, t_bfs *tab)
} }
} }
int is_in_queue(t_lmdata *data, t_bfs *tab, int node) void bfs_add_to_queue(t_lmdata *data, t_bfs *tab, int node, int i)
{ {
int i; int j;
i = 0; j = 0;
while (i < data->nb_nodes && tab[i].queue != node) while (j < data->nb_nodes && tab[j].queue != -1
i++; && tab[j].queue != node)
return (i < data->nb_nodes && tab[i].queue == node); j++;
if (j < data->nb_nodes && tab[j].queue == -1)
{
tab[j].queue = node;
tab[node].parent = tab[i].queue;
}
} }
void bfs(t_lmdata *data, t_bfs *tab, int s_ind, int e_ind) void bfs(t_lmdata *data, t_bfs *tab, int s_ind, int e_ind)
{ {
int i; int i;
int j;
int used; int used;
t_ind *it; t_ind *it;
@ -49,21 +53,15 @@ void bfs(t_lmdata *data, t_bfs *tab, int s_ind, int e_ind)
while (i < data->nb_nodes && tab[i].queue != -1 && tab[e_ind].parent == -1) while (i < data->nb_nodes && tab[i].queue != -1 && tab[e_ind].parent == -1)
{ {
tab[tab[i].queue].visited = 1; tab[tab[i].queue].visited = 1;
used = (tab[tab[i].queue].old_visited && get_node_path(data->adj[tab[tab[i].queue].parent], tab[i].queue)->weight == 1); used = (tab[tab[i].queue].old_visited
&& get_node_path(data->adj[tab[tab[i].queue].parent]
, tab[i].queue)->weight == 1);
it = data->adj[tab[i].queue]; it = data->adj[tab[i].queue];
while (it) while (it)
{ {
if (it->weight > 0 && !tab[it->index].visited && (!used || (used && it->weight == 2))) if (it->weight > 0 && !tab[it->index].visited
{ && (!used || (used && it->weight == 2)))
j = 0; bfs_add_to_queue(data, tab, it->index, i);
while (j < data->nb_nodes && tab[j].queue != -1 && tab[j].queue != it->index)
j++;
if (j < data->nb_nodes && tab[j].queue == -1)
{
tab[j].queue = it->index;
tab[it->index].parent = tab[i].queue;
}
}
it = it->next; it = it->next;
} }
i++; i++;

View File

@ -6,35 +6,23 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/18 09:59:11 by tmaze #+# #+# */ /* Created: 2019/04/18 09:59:11 by tmaze #+# #+# */
/* Updated: 2019/04/20 18:58:38 by tmaze ### ########.fr */ /* Updated: 2019/04/21 16:11:56 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "lem_in.h" #include "lem_in.h"
t_ind **resolve_paths(t_lmdata *data, int nb_paths, int s_ind, int e_ind) static void go_upstream(t_lmdata *data, t_ind **path, int s_ind, t_ind *it)
{ {
t_ind **ret;
t_ind *it;
t_ind *it2;
int i;
int j;
int nb_nodes; int nb_nodes;
int j;
t_ind *it2;
if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_paths + 1))) != NULL) nb_nodes = 1;
{
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; j = it->index;
while (j != s_ind) while (j != s_ind)
{ {
lst_indadd(&(ret[i]), j); lst_indadd(path, j);
nb_nodes++; nb_nodes++;
it2 = data->adj[j]; it2 = data->adj[j];
while (it2 && it2->weight != 2) while (it2 && it2->weight != 2)
@ -42,7 +30,26 @@ t_ind **resolve_paths(t_lmdata *data, int nb_paths, int s_ind, int e_ind)
if (it2 && it2->weight == 2) if (it2 && it2->weight == 2)
j = it2->index; j = it2->index;
} }
ret[i]->weight = nb_nodes; (*path)->weight = nb_nodes;
}
static t_ind **resolve_paths(t_lmdata *data, int nb_paths, int s_ind
, int e_ind)
{
t_ind **ret;
t_ind *it;
int i;
if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_paths + 1))) != NULL)
{
i = 0;
it = data->adj[e_ind];
while (it && i < nb_paths)
{
if (it->weight == 2)
{
lst_indadd(&(ret[i]), e_ind);
go_upstream(data, &(ret[i]), s_ind, it);
i++; i++;
} }
it = it->next; it = it->next;
@ -51,59 +58,11 @@ t_ind **resolve_paths(t_lmdata *data, int nb_paths, int s_ind, int e_ind)
return (ret); return (ret);
} }
void print_paths(t_lmdata *data, t_ind **ret) static void update_weights(t_lmdata *data, t_bfs *tab, int s_ind, int e_ind)
{ {
int i; int i;
t_ind *it; 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++;
}
}
int check_if_visited(t_lmdata *data, int node)
{
t_ind *it;
it = data->adj[node];
while (it)
{
if (it->weight != 1)
return (1);
it = it->next;
}
return (0);
}
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; i = e_ind;
while (i != s_ind) while (i != s_ind)
{ {
@ -117,11 +76,16 @@ t_ind **edmonds_karp(t_lmdata *data, int s_ind, int e_ind)
it = it->next; it = it->next;
if (it) if (it)
it->weight--; it->weight--;
tab[i].old_visited = check_if_visited(data, i); it = data->adj[i];
while (it && it->weight == 1)
it = it->next;
tab[i].old_visited = (it && it->weight != 1);
i = tab[i].parent; i = tab[i].parent;
} }
ret[1] = resolve_paths(data, nb_paths, s_ind, e_ind); }
scores[1] = get_score(data, ret[1], nb_paths);
static void cmp_scores(t_ind ***ret, int *scores)
{
if (ret[0] == NULL || scores[1] < scores[0]) if (ret[0] == NULL || scores[1] < scores[0])
{ {
if (ret[0] != NULL) if (ret[0] != NULL)
@ -131,6 +95,30 @@ t_ind **edmonds_karp(t_lmdata *data, int s_ind, int e_ind)
} }
else else
tablst_inddel(ret[1]); tablst_inddel(ret[1]);
}
t_ind **edmonds_karp(t_lmdata *data, int s_ind, int e_ind)
{
t_bfs tab[data->nb_nodes];
t_ind **ret[2];
int nb_paths;
int scores[2];
int i;
ret[0] = NULL;
scores[0] = 0;
nb_paths = 0;
i = 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++;
update_weights(data, tab, s_ind, e_ind);
ret[1] = resolve_paths(data, nb_paths, s_ind, e_ind);
scores[1] = get_score(data, ret[1], nb_paths);
cmp_scores(ret, scores);
bfs(data, tab, s_ind, e_ind); bfs(data, tab, s_ind, e_ind);
} }
return (ret[0]); return (ret[0]);

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/06 11:37:56 by tmaze #+# #+# */ /* Created: 2019/04/06 11:37:56 by tmaze #+# #+# */
/* Updated: 2019/04/20 19:05:32 by tmaze ### ########.fr */ /* Updated: 2019/04/21 16:14:45 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -78,11 +78,10 @@ int push_ants(t_lmdata *data, t_ind **paths, int nb_paths)
while (++i < nb_paths) while (++i < nb_paths)
if (paths[i]->weight > 0) if (paths[i]->weight > 0)
{ {
if (!add_ant(&ants, ant_c, i, paths)) if (!add_ant(&ants, ant_c++, i, paths))
del_ants(&ants); del_ants(&ants);
if (!ants) if (!ants)
return (0); return (0);
ant_c++;
} }
print_ants(data, &ants, paths); print_ants(data, &ants, paths);
i = -1; i = -1;

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/18 18:05:06 by tmaze #+# #+# */ /* Created: 2019/04/18 18:05:06 by tmaze #+# #+# */
/* Updated: 2019/04/19 12:52:45 by tmaze ### ########.fr */ /* Updated: 2019/04/21 16:53:11 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,6 +37,36 @@ static int get_nb_nodes(t_ind *lst)
return (i); return (i);
} }
static int get_min_score(t_ind **ret, int *scores)
{
int i;
int min;
i = -1;
min = FT_INT_MAX;
while (ret[++i])
if (min > scores[i])
min = scores[i];
return (min);
}
static int get_final_score(t_ind **ret, int *scores)
{
int i;
int min;
i = -1;
min = 0;
while (ret[++i])
{
scores[i] = (ret[i]->weight > 0) * (get_nb_nodes(ret[i])
+ ret[i]->weight - 1);
if (min < scores[i])
min = scores[i];
}
return (min);
}
int get_score(t_lmdata *data, t_ind **ret, int nb_paths) int get_score(t_lmdata *data, t_ind **ret, int nb_paths)
{ {
int nbants; int nbants;
@ -44,46 +74,20 @@ int get_score(t_lmdata *data, t_ind **ret, int nb_paths)
int min; int min;
int scores[nb_paths]; int scores[nb_paths];
if (nb_paths == 1)
{
ret[0]->weight = data->nbants;
min = (ret[0]->weight > 0) * (get_nb_nodes(ret[0]) + ret[0]->weight - 1);
}
else
{
min = FT_INT_MAX;
i = -1;
nbants = data->nbants; nbants = data->nbants;
init_scores(ret, scores); init_scores(ret, scores);
while (ret[++i]) min = get_min_score(ret, scores);
if (min > scores[i]) while (nbants > 0 && (i = -1))
min = scores[i];
while (nbants > 0 && !(i = 0))
{
while (i < nb_paths && nbants > 0)
{ {
while (++i < nb_paths && nbants > 0)
if (scores[i] == min) if (scores[i] == min)
{ {
nbants--; nbants--;
ret[i]->weight++; ret[i]->weight++;
scores[i] = (ret[i]->weight > 0) * (get_nb_nodes(ret[i]) + ret[i]->weight - 1); scores[i] = (ret[i]->weight > 0) * (get_nb_nodes(ret[i])
+ ret[i]->weight - 1);
} }
i++; min = get_min_score(ret, scores);
} }
i = -1; return (get_final_score(ret, scores));
min = FT_INT_MAX;
while (ret[++i])
if (min > scores[i])
min = scores[i];
}
i = -1;
min = 0;
while (ret[++i])
{
scores[i] = (ret[i]->weight > 0) * (get_nb_nodes(ret[i]) + ret[i]->weight - 1);
if (min < scores[i])
min = scores[i];
}
}
return (min);
} }