added finished algorithm
merged algorithm from other personnal repo
This commit is contained in:
97
srcs/bfs.c
97
srcs/bfs.c
@@ -5,80 +5,65 @@
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/14 11:39:52 by tmaze ### ########.fr */
|
||||
/* Created: 2019/04/18 09:25:05 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/21 14:42:08 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
static void bfs_addtoqueue(t_bfs *tab, int node, int nb_nodes)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (tab[node].visited == 0)
|
||||
{
|
||||
while (i < nb_nodes && tab[i].queue != -1 && tab[i].queue != node)
|
||||
i++;
|
||||
if (i < nb_nodes && tab[i].queue == -1)
|
||||
tab[i].queue = node;
|
||||
}
|
||||
}
|
||||
|
||||
static void bfs_checkadj(t_lmdata *data, t_bfs *tab, int i)
|
||||
{
|
||||
t_ind *it;
|
||||
char used;
|
||||
|
||||
it = data->adj[tab[i].queue];
|
||||
used = 0;
|
||||
while (it != NULL)
|
||||
{
|
||||
if (tab[it->index].visited == 0 && it->weight == 2)
|
||||
used = 1;
|
||||
if (used)
|
||||
break ;
|
||||
it = it->next;
|
||||
}
|
||||
it = data->adj[tab[i].queue];
|
||||
while (it != NULL)
|
||||
{
|
||||
if (tab[it->index].visited == 0 && ((!used && it->weight == 1)
|
||||
|| (used && it->weight == 2)))
|
||||
bfs_addtoqueue(tab, it->index, data->nb_nodes);
|
||||
if (tab[it->index].visited == 0 && ((!used && it->weight == 1)
|
||||
|| (used && it->weight == 2)))
|
||||
tab[it->index].parent = tab[i].queue;
|
||||
if (it->weight == 2)
|
||||
break ;
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
|
||||
static void bfs_init(t_bfs *tab, t_lmdata *data)
|
||||
void bfs_init(t_lmdata *data, t_bfs *tab)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < data->nb_nodes)
|
||||
{
|
||||
tab[i].parent = -1;
|
||||
tab[i].visited = 0;
|
||||
tab[i].queue = -1;
|
||||
tab[i].visited = 0;
|
||||
tab[i].parent = -1;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind)
|
||||
void bfs_add_to_queue(t_lmdata *data, t_bfs *tab, int node, int i)
|
||||
{
|
||||
int j;
|
||||
|
||||
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 used;
|
||||
t_ind *it;
|
||||
|
||||
ft_printf("New bfs\n");
|
||||
bfs_init(tab, data);
|
||||
bfs_addtoqueue(tab, start_ind, data->nb_nodes);
|
||||
i = 0;
|
||||
while (i < data->nb_nodes && tab[i].queue != -1
|
||||
&& tab[end_ind].parent == -1 && (tab[tab[i].queue].visited = 1))
|
||||
bfs_checkadj(data, tab, i++);
|
||||
bfs_init(data, tab);
|
||||
tab[i].queue = s_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);
|
||||
it = data->adj[tab[i].queue];
|
||||
while (it)
|
||||
{
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
125
srcs/edmonds_karp.c
Normal file
125
srcs/edmonds_karp.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* edmonds_karp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/18 09:59:11 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/21 16:11:56 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
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;
|
||||
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++;
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
static void update_weights(t_lmdata *data, t_bfs *tab, int s_ind, int e_ind)
|
||||
{
|
||||
int i;
|
||||
t_ind *it;
|
||||
|
||||
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--;
|
||||
it = data->adj[i];
|
||||
while (it && it->weight == 1)
|
||||
it = it->next;
|
||||
tab[i].old_visited = (it && it->weight != 1);
|
||||
i = tab[i].parent;
|
||||
}
|
||||
}
|
||||
|
||||
static void cmp_scores(t_ind ***ret, int *scores)
|
||||
{
|
||||
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]);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
return (ret[0]);
|
||||
}
|
@@ -6,7 +6,7 @@
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:31:05 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/14 12:22:28 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/22 10:02:05 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -67,7 +67,7 @@ static int lem_in(t_syntax *synt, t_holder *holder,
|
||||
lm_clear_unv(holder);
|
||||
get_nb_paths_max(lmdata, get_node_role(lmdata, 's')->ind
|
||||
, get_node_role(lmdata, 'e')->ind);
|
||||
if ((*ret = edmunds_karp(lmdata, get_node_role(lmdata, 's')->ind
|
||||
if ((*ret = edmonds_karp(lmdata, get_node_role(lmdata, 's')->ind
|
||||
, get_node_role(lmdata, 'e')->ind)) == NULL)
|
||||
return (0);
|
||||
if (!push_ants(lmdata, *ret, get_nb_paths(*ret)))
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/06 11:37:56 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/12 12:06:51 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/21 16:14:45 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -37,8 +37,7 @@ static void clean_ants(t_ants **ants)
|
||||
}
|
||||
}
|
||||
|
||||
static void print_ants(t_lmdata *data, t_ants **ants, char *has_start
|
||||
, t_ind **paths)
|
||||
static void print_ants(t_lmdata *data, t_ants **ants, t_ind **paths)
|
||||
{
|
||||
t_ants *it;
|
||||
|
||||
@@ -56,43 +55,36 @@ static void print_ants(t_lmdata *data, t_ants **ants, char *has_start
|
||||
ft_putchar(' ');
|
||||
else
|
||||
ft_putchar('\n');
|
||||
has_start[it->nb_path] = 0;
|
||||
it = it->next;
|
||||
}
|
||||
clean_ants(ants);
|
||||
}
|
||||
|
||||
static void init_ants(t_ants **ants, int *ant_c, char *has_start, int nb_paths)
|
||||
{
|
||||
*ants = NULL;
|
||||
ft_bzero(has_start, nb_paths);
|
||||
*ant_c = 2;
|
||||
has_start[0] = 1;
|
||||
}
|
||||
|
||||
int push_ants(t_lmdata *data, t_ind **paths, int nb_paths)
|
||||
{
|
||||
t_ants *ants;
|
||||
int i;
|
||||
int ant_c;
|
||||
char has_start[nb_paths];
|
||||
int i;
|
||||
|
||||
init_ants(&ants, &ant_c, has_start, nb_paths);
|
||||
if (add_ant(&ants, 1, 0, paths) == NULL)
|
||||
ants = NULL;
|
||||
i = 0;
|
||||
ant_c = 2;
|
||||
while (paths[i] && paths[i]->weight == 0)
|
||||
i++;
|
||||
if (add_ant(&ants, 1, i, paths) == NULL)
|
||||
return (0);
|
||||
while ((ants || ant_c <= data->nbants) && (i = -1))
|
||||
while (ants || ant_c <= data->nbants)
|
||||
{
|
||||
while (++i < nb_paths)
|
||||
if (ant_c <= data->nbants && !has_start[i])
|
||||
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++;
|
||||
has_start[i] = 1;
|
||||
}
|
||||
print_ants(data, &ants, has_start, paths);
|
||||
print_ants(data, &ants, paths);
|
||||
i = -1;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/10 11:23:36 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/10 11:24:01 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/18 19:04:49 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -33,6 +33,7 @@ t_ants *add_ant(t_ants **ants, int nb_ant, int nb_path, t_ind **paths)
|
||||
it = it->next;
|
||||
it->next = new;
|
||||
}
|
||||
paths[nb_path]->weight--;
|
||||
}
|
||||
return (new);
|
||||
}
|
||||
|
93
srcs/score_utils.c
Normal file
93
srcs/score_utils.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* score_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/18 18:05:06 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/21 16:53:11 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
static void init_scores(t_ind **ret, int *scores)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (ret[i])
|
||||
{
|
||||
scores[i] = ret[i]->weight;
|
||||
ret[i]->weight = 0;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static int get_nb_nodes(t_ind *lst)
|
||||
{
|
||||
t_ind *it;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
it = lst;
|
||||
while (it && (++i))
|
||||
it = it->next;
|
||||
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;
|
||||
int i;
|
||||
int min;
|
||||
int scores[nb_paths];
|
||||
|
||||
nbants = data->nbants;
|
||||
init_scores(ret, scores);
|
||||
min = get_min_score(ret, scores);
|
||||
while (nbants > 0 && (i = -1))
|
||||
{
|
||||
while (++i < nb_paths && nbants > 0)
|
||||
if (scores[i] == min)
|
||||
{
|
||||
nbants--;
|
||||
ret[i]->weight++;
|
||||
scores[i] = (ret[i]->weight > 0) * (get_nb_nodes(ret[i])
|
||||
+ ret[i]->weight - 1);
|
||||
}
|
||||
min = get_min_score(ret, scores);
|
||||
}
|
||||
return (get_final_score(ret, scores));
|
||||
}
|
Reference in New Issue
Block a user