spring cleanup =)
normalized all algo files ready for integration
This commit is contained in:
parent
5dd290bcf7
commit
7b5ef85940
38
srcs/bfs.c
38
srcs/bfs.c
@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
while (i < data->nb_nodes && tab[i].queue != node)
|
||||
i++;
|
||||
return (i < data->nb_nodes && tab[i].queue == node);
|
||||
j = 0;
|
||||
while (j < data->nb_nodes && tab[j].queue != -1
|
||||
&& tab[j].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)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int used;
|
||||
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)
|
||||
{
|
||||
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];
|
||||
while (it)
|
||||
{
|
||||
if (it->weight > 0 && !tab[it->index].visited && (!used || (used && it->weight == 2)))
|
||||
{
|
||||
j = 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (it->weight > 0 && !tab[it->index].visited
|
||||
&& (!used || (used && it->weight == 2)))
|
||||
bfs_add_to_queue(data, tab, it->index, i);
|
||||
it = it->next;
|
||||
}
|
||||
i++;
|
||||
|
@ -6,43 +6,50 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
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 *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))
|
||||
if (it->weight == 2)
|
||||
{
|
||||
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;
|
||||
go_upstream(data, &(ret[i]), s_ind, it);
|
||||
i++;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
t_ind *it;
|
||||
|
||||
i = 0;
|
||||
ft_putstr("=== all paths ===\n");
|
||||
while (ret[i])
|
||||
i = e_ind;
|
||||
while (i != s_ind)
|
||||
{
|
||||
it = ret[i];
|
||||
while (it)
|
||||
{
|
||||
ft_printf("%s-> ", get_node(data, it->index)->name);
|
||||
it = data->adj[i];
|
||||
while (it && it->index != tab[i].parent)
|
||||
it = it->next;
|
||||
}
|
||||
ft_putstr("\n\n");
|
||||
i++;
|
||||
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--;
|
||||
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;
|
||||
|
||||
it = data->adj[node];
|
||||
while (it)
|
||||
if (ret[0] == NULL || scores[1] < scores[0])
|
||||
{
|
||||
if (it->weight != 1)
|
||||
return (1);
|
||||
it = it->next;
|
||||
if (ret[0] != NULL)
|
||||
tablst_inddel(ret[0]);
|
||||
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_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;
|
||||
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++;
|
||||
i = 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;
|
||||
}
|
||||
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);
|
||||
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]);
|
||||
cmp_scores(ret, scores);
|
||||
bfs(data, tab, s_ind, e_ind);
|
||||
}
|
||||
return (ret[0]);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
if (paths[i]->weight > 0)
|
||||
{
|
||||
if (!add_ant(&ants, ant_c, i, paths))
|
||||
if (!add_ant(&ants, ant_c++, i, paths))
|
||||
del_ants(&ants);
|
||||
if (!ants)
|
||||
return (0);
|
||||
ant_c++;
|
||||
}
|
||||
print_ants(data, &ants, paths);
|
||||
i = -1;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
||||
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 nbants;
|
||||
@ -44,46 +74,20 @@ int get_score(t_lmdata *data, t_ind **ret, int nb_paths)
|
||||
int min;
|
||||
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;
|
||||
min = (ret[0]->weight > 0) * (get_nb_nodes(ret[0]) + ret[0]->weight - 1);
|
||||
}
|
||||
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)
|
||||
while (++i < nb_paths && nbants > 0)
|
||||
if (scores[i] == min)
|
||||
{
|
||||
if (scores[i] == min)
|
||||
{
|
||||
nbants--;
|
||||
ret[i]->weight++;
|
||||
scores[i] = (ret[i]->weight > 0) * (get_nb_nodes(ret[i]) + ret[i]->weight - 1);
|
||||
}
|
||||
i++;
|
||||
nbants--;
|
||||
ret[i]->weight++;
|
||||
scores[i] = (ret[i]->weight > 0) * (get_nb_nodes(ret[i])
|
||||
+ ret[i]->weight - 1);
|
||||
}
|
||||
i = -1;
|
||||
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];
|
||||
}
|
||||
min = get_min_score(ret, scores);
|
||||
}
|
||||
return (min);
|
||||
return (get_final_score(ret, scores));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user