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,43 +6,50 @@
/* 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)
{
int nb_nodes;
int j;
t_ind *it2;
nb_nodes = 1;
j = it->index;
while (j != s_ind)
{
lst_indadd(path, j);
nb_nodes++;
it2 = data->adj[j];
while (it2 && it2->weight != 2)
it2 = it2->next;
if (it2 && it2->weight == 2)
j = it2->index;
}
(*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 **ret;
t_ind *it; t_ind *it;
t_ind *it2;
int i; int i;
int j;
int nb_nodes;
if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_paths + 1))) != NULL) if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_paths + 1))) != NULL)
{ {
i = 0; i = 0;
it = data->adj[e_ind]; it = data->adj[e_ind];
j = e_ind;
while (it && i < nb_paths) while (it && i < nb_paths)
{ {
if (it->weight == 2 && (nb_nodes = 1)) if (it->weight == 2)
{ {
lst_indadd(&(ret[i]), e_ind); lst_indadd(&(ret[i]), e_ind);
j = it->index; go_upstream(data, &(ret[i]), s_ind, it);
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++; i++;
} }
it = it->next; it = it->next;
@ -51,86 +58,67 @@ 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; i = e_ind;
ft_putstr("=== all paths ===\n"); while (i != s_ind)
while (ret[i])
{ {
it = ret[i]; it = data->adj[i];
while (it) while (it && it->index != tab[i].parent)
{
ft_printf("%s-> ", get_node(data, it->index)->name);
it = it->next; it = it->next;
} if (it->index == tab[i].parent)
ft_putstr("\n\n"); it->weight++;
i++; it = data->adj[tab[i].parent];
while (it && it->index != i)
it = it->next;
if (it)
it->weight--;
it = data->adj[i];
while (it && it->weight == 1)
it = it->next;
tab[i].old_visited = (it && it->weight != 1);
i = tab[i].parent;
} }
} }
int check_if_visited(t_lmdata *data, int node) static void cmp_scores(t_ind ***ret, int *scores)
{ {
t_ind *it; if (ret[0] == NULL || scores[1] < scores[0])
it = data->adj[node];
while (it)
{ {
if (it->weight != 1) if (ret[0] != NULL)
return (1); tablst_inddel(ret[0]);
it = it->next; ret[0] = ret[1];
scores[0] = scores[1];
} }
return (0); else
tablst_inddel(ret[1]);
} }
t_ind **edmonds_karp(t_lmdata *data, int s_ind, int e_ind) t_ind **edmonds_karp(t_lmdata *data, int s_ind, int e_ind)
{ {
t_bfs tab[data->nb_nodes]; t_bfs tab[data->nb_nodes];
t_ind **ret[2]; t_ind **ret[2];
t_ind *it;
int nb_paths; int nb_paths;
int scores[2]; int scores[2];
int i; int i;
ret[0] = NULL; ret[0] = NULL;
scores[0] = 0; scores[0] = 0;
i = 0;
nb_paths = 0; nb_paths = 0;
i = 0;
while (i < data->nb_nodes) while (i < data->nb_nodes)
tab[i++].old_visited = 0; tab[i++].old_visited = 0;
bfs(data, tab, s_ind, e_ind); bfs(data, tab, s_ind, e_ind);
while (tab[e_ind].parent != -1) while (tab[e_ind].parent != -1)
{ {
nb_paths++; nb_paths++;
i = e_ind; update_weights(data, tab, s_ind, e_ind);
while (i != s_ind)
{
it = data->adj[i];
while (it && it->index != tab[i].parent)
it = it->next;
if (it->index == tab[i].parent)
it->weight++;
it = data->adj[tab[i].parent];
while (it && it->index != i)
it = it->next;
if (it)
it->weight--;
tab[i].old_visited = check_if_visited(data, i);
i = tab[i].parent;
}
ret[1] = resolve_paths(data, nb_paths, s_ind, e_ind); ret[1] = resolve_paths(data, nb_paths, s_ind, e_ind);
scores[1] = get_score(data, ret[1], nb_paths); scores[1] = get_score(data, ret[1], nb_paths);
if (ret[0] == NULL || scores[1] < scores[0]) cmp_scores(ret, scores);
{
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); 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) nbants = data->nbants;
init_scores(ret, scores);
min = get_min_score(ret, scores);
while (nbants > 0 && (i = -1))
{ {
ret[0]->weight = data->nbants; while (++i < nb_paths && nbants > 0)
min = (ret[0]->weight > 0) * (get_nb_nodes(ret[0]) + ret[0]->weight - 1); if (scores[i] == min)
}
else
{
min = FT_INT_MAX;
i = -1;
nbants = data->nbants;
init_scores(ret, scores);
while (ret[++i])
if (min > scores[i])
min = scores[i];
while (nbants > 0 && !(i = 0))
{
while (i < nb_paths && nbants > 0)
{ {
if (scores[i] == min) nbants--;
{ ret[i]->weight++;
nbants--; scores[i] = (ret[i]->weight > 0) * (get_nb_nodes(ret[i])
ret[i]->weight++; + ret[i]->weight - 1);
scores[i] = (ret[i]->weight > 0) * (get_nb_nodes(ret[i]) + ret[i]->weight - 1);
}
i++;
} }
i = -1; min = get_min_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); return (get_final_score(ret, scores));
} }