we got it !

functionning edmunds_karp
still need some testing for already used nodes
still need some memory protection
This commit is contained in:
Tanguy MAZE
2019-03-31 19:49:42 +02:00
parent 7a67b49802
commit ab578f6039
6 changed files with 323 additions and 41 deletions

View File

@@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ # # By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# # # Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
# Updated: 2019/03/27 18:00:05 by tmaze ### ########.fr # # Updated: 2019/03/31 19:48:45 by tmaze ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#
@@ -37,7 +37,8 @@ INCDIR = includes libft/includes
# Source files # Source files
SRC = bfs.c \ SRC = bfs.c \
lst_ind.c lst_ind.c \
edmunds_karp.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)
@@ -47,6 +48,7 @@ LIBFILE = libft.a
# Prefixes # Prefixes
OBJP = $(addprefix $(OBJDIR)/, $(SRC:.c=.o)) OBJP = $(addprefix $(OBJDIR)/, $(SRC:.c=.o))
INCP = $(foreach dir, $(INCDIR), -I$(dir)) INCP = $(foreach dir, $(INCDIR), -I$(dir))
OBJT = $(OBJDIR)/test.o
# Default Rule # Default Rule
DRULE = all DRULE = all

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */ /* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */
/* Updated: 2019/03/27 17:44:40 by tmaze ### ########.fr */ /* Updated: 2019/03/31 17:58:52 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -27,6 +27,7 @@ typedef struct s_node
typedef struct s_ind typedef struct s_ind
{ {
int index; int index;
int weight;
struct s_ind *next; struct s_ind *next;
} t_ind; } t_ind;
@@ -38,10 +39,20 @@ typedef struct s_lmdata
t_ind **adj; t_ind **adj;
} t_lmdata; } t_lmdata;
typedef struct s_bfs
{
int parent;
char visited;
int queue;
} t_bfs;
void lm_initdata(t_lmdata *data); void lm_initdata(t_lmdata *data);
int lm_getparams(t_lmdata *data); int lm_getparams(t_lmdata *data);
t_ind *lst_indadd(t_ind **lst, int ind); t_ind *lst_indadd(t_ind **lst, int ind);
void lst_inddel(t_ind **lst); void lst_inddel(t_ind **lst);
void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind);
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind);
#endif #endif

View File

@@ -6,67 +6,78 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */ /* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */
/* Updated: 2019/03/27 17:06:33 by tmaze ### ########.fr */ /* Updated: 2019/03/31 18:23:21 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "lem_in.h" #include "lem_in.h"
static void bfs_addtoqueue(int *queue, int node, int nb_nodes) static void bfs_print(t_bfs *tab, int nb_nodes)
{ {
int i; int i;
i = 0; i = 0;
while (i < nb_nodes && (queue[i] == -1 || queue[i] != node)) ft_printf("===== bfs print =====\n");
while (i < nb_nodes)
{
ft_printf("index: %d\nparent: %d\nvisited: %d\nqueue: %d\n\n", i
, tab[i].parent, tab[i].visited != 0, tab[i].queue);
i++; i++;
if (i < nb_nodes && queue[i] == -1) }
queue[i] = node;
} }
static void bfs_checkadj(t_lmdata *data, int *queue, int *parent, int i) 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; t_ind *it;
it = data->adj[queue[i]]; it = data->adj[tab[i].queue];
while (it != NULL) while (it != NULL)
{ {
bfs_addtoqueue(queue, it->index, data->nb_nodes); if (tab[it->index].visited == 0 && it->weight > 0)
parent[it->index] = queue[i]; bfs_addtoqueue(tab, it->index, data->nb_nodes);
if (tab[it->index].visited == 0 && it->weight > 0)
tab[it->index].parent = tab[i].queue;
it = it->next; it = it->next;
} }
} }
static void bfs_init(int *queue, char *visited, int *parent, t_lmdata *data) static void bfs_init(t_bfs *tab, t_lmdata *data)
{ {
ft_bzero(queue, data->nb_nodes * sizeof(int)); int i;
ft_bzero(visited, data->nb_nodes);
ft_memset(parent, -1, data->nb_nodes * sizeof(int));
}
t_ind *bfs(t_lmdata *data, int start_ind, int end_ind)
{
int parent[data->nb_nodes];
char visited[data->nb_nodes];
int queue[data->nb_nodes];
int i;
t_ind *ret;
bfs_init(queue, visited, parent, data);
bfs_addtoqueue(queue, start_ind, data->nb_nodes);
i = 0; i = 0;
while (i < data->nb_nodes && queue[i] != -1 && parent[end_ind] == -1 while (i < data->nb_nodes)
&& (visited[queue[i]] = 1))
bfs_checkadj(data, queue, parent, i++);
ret = NULL;
i = end_ind;
while (i != -1)
{ {
if (lst_indadd(&ret, i) == NULL) tab[i].parent = -1;
{ tab[i].visited = 0;
lst_inddel(&ret); tab[i].queue = -1;
return (NULL); i++;
}
i = parent[i];
} }
return (ret); }
void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind)
{
int i;
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_print(tab, data->nb_nodes);
} }

111
srcs/edmunds_karp.c Normal file
View File

@@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* edmunds_karp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "lem_in.h"
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;
if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_path + 1)))
!= NULL)
{
ret[nb_path] = NULL;
while (i < nb_path)
{
j = end_ind;
ft_printf("===== start resolv %d =====\n", i);
it = data->adj[j];
while (j != start_ind)
{
if (lst_indadd(&(ret[i]), j) == NULL)
return (NULL); //protect this
it = data->adj[j];
ft_printf("===== parents of %d =====\n", j);
while (it && it->weight != 2)
{
ft_printf("index: %d\nweight: %d\n\n", it->index, it->weight);
it = it->next;
}
if (it && it->weight == 2)
{
ft_printf("parent selected: %d\n", it->index);
it->weight--;
}
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;
}
if (lst_indadd(&(ret[i]), j) == NULL)
return (NULL); //protect this
i++;
}
}
return (ret);
}
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
{
t_bfs bfs_tab[data->nb_nodes];
int i;
int nb_path;
t_ind *it;
nb_path = 0;
ft_printf("===== Init bfs =====\n");
bfs(data, bfs_tab, start_ind, end_ind);
while (bfs_tab[end_ind].parent != -1)
{
nb_path++;
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++;
i = bfs_tab[i].parent;
}
ft_printf("===== new bfs =====\n");
bfs(data, bfs_tab, start_ind, end_ind);
}
ft_printf("===== list of adj =====\n");
i = 0;
while (i < 8)
{
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++;
}
return (resolve_path(data, start_ind, end_ind, nb_path));
}

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */ /* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */
/* Updated: 2019/03/27 17:45:17 by tmaze ### ########.fr */ /* Updated: 2019/03/31 17:53:47 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -19,6 +19,7 @@ t_ind *lst_indadd(t_ind **lst, int ind)
if ((new = (t_ind*)ft_memalloc(sizeof(t_ind))) != NULL) if ((new = (t_ind*)ft_memalloc(sizeof(t_ind))) != NULL)
{ {
new->index = ind; new->index = ind;
new->weight = 1;
new->next = *lst; new->next = *lst;
*lst = new; *lst = new;
} }

146
srcs/test.c Normal file
View File

@@ -0,0 +1,146 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/28 11:37:06 by tmaze #+# #+# */
/* Updated: 2019/03/31 19:47:57 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
#define NB_NODES 8
int add_link(t_lmdata *data, int n1, int n2)
{
return (lst_indadd(&(data->adj[n1]), n2) && lst_indadd(&(data->adj[n2]), n1));
}
int main(void)
{
t_lmdata data;
t_ind **path;
t_ind *it;
int i;
data.nb_nodes = NB_NODES;
if ((data.adj = (t_ind**)ft_memalloc(sizeof(t_ind*) * NB_NODES)) == NULL)
return (1);
if (!add_link(&data, 0, 1) || !add_link(&data, 0, 2))
{
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
if (!add_link(&data, 1, 4))
{
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[1]));
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
if (!add_link(&data, 2, 3) || !add_link(&data, 2, 5))
{
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[3]));
lst_inddel(&(data.adj[2]));
lst_inddel(&(data.adj[1]));
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
if (!add_link(&data, 3, 6))
{
lst_inddel(&(data.adj[6]));
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[3]));
lst_inddel(&(data.adj[2]));
lst_inddel(&(data.adj[1]));
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
/* if (!add_link(&data, 4, 5)) */
/* { */
/* lst_inddel(&(data.adj[6])); */
/* lst_inddel(&(data.adj[5])); */
/* lst_inddel(&(data.adj[4])); */
/* lst_inddel(&(data.adj[3])); */
/* lst_inddel(&(data.adj[2])); */
/* lst_inddel(&(data.adj[1])); */
/* lst_inddel(&(data.adj[0])); */
/* ft_memdel((void**)&(data.adj)); */
/* return (1); */
/* } */
if (!add_link(&data, 5, 7))
{
lst_inddel(&(data.adj[7]));
lst_inddel(&(data.adj[6]));
lst_inddel(&(data.adj[5]));
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[3]));
lst_inddel(&(data.adj[2]));
lst_inddel(&(data.adj[1]));
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
if (!add_link(&data, 6, 7))
{
lst_inddel(&(data.adj[7]));
lst_inddel(&(data.adj[6]));
lst_inddel(&(data.adj[5]));
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[3]));
lst_inddel(&(data.adj[2]));
lst_inddel(&(data.adj[1]));
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (1);
}
ft_printf("===== list of adj =====\n");
i = 0;
while (i < 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++;
}
path = edmunds_karp(&data, 0, 7);
if (path != NULL)
{
i = 0;
while (path[i])
{
it = path[i];
while (it)
{
ft_printf(" %d -> ", it->index);
it = it->next;
}
ft_putchar('\n');
lst_inddel(&(path[i]));
i++;
}
}
ft_memdel((void**)&path);
lst_inddel(&(data.adj[7]));
lst_inddel(&(data.adj[6]));
lst_inddel(&(data.adj[5]));
lst_inddel(&(data.adj[4]));
lst_inddel(&(data.adj[3]));
lst_inddel(&(data.adj[2]));
lst_inddel(&(data.adj[1]));
lst_inddel(&(data.adj[0]));
ft_memdel((void**)&(data.adj));
return (0);
}