Merge branch 'tmaze'

merging algorithm to master of project
This commit is contained in:
Tanguy MAZE 2019-04-05 14:06:42 +02:00
commit 0a9d7b816d
7 changed files with 182 additions and 160 deletions

View File

@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
# Updated: 2019/03/31 19:48:45 by tmaze ### ########.fr #
# Updated: 2019/04/05 13:54:08 by tmaze ### ########.fr #
# #
#******************************************************************************#
@ -27,7 +27,7 @@ endif
# Compilator
CC = gcc
FLAGS = -Wall -Wextra -Werror -g -O0 -fsanitize=address
FLAGS = -Wall -Wextra -Werror -g
# Folders
LIBDIR = libft
@ -36,7 +36,15 @@ OBJDIR = objs
INCDIR = includes libft/includes
# Source files
SRC = lem_in.c lm_parser.c lm_mem_utils.c lm_graph_utils.c lm_check_errors.c \
SRC = lm_parser.c \
lm_mem_utils.c \
lm_graph_utils.c \
lm_check_errors.c \
bfs.c \
lst_ind.c \
edmunds_karp.c \
# lem_in.c \
OBJ = $(SRC:.c=.o)

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */
/* Updated: 2019/03/31 17:58:52 by tmaze ### ########.fr */
/* Updated: 2019/04/03 15:57:39 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -125,6 +125,7 @@ int lm_getparams(t_lmdata *data);
t_ind *lst_indadd(t_ind **lst, int ind);
void lst_inddel(t_ind **lst);
void tablst_inddel(t_ind **tab);
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);

View File

@ -6,26 +6,12 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */
/* Updated: 2019/03/31 18:23:21 by tmaze ### ########.fr */
/* Updated: 2019/04/05 13:24:22 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
static void bfs_print(t_bfs *tab, int nb_nodes)
{
int i;
i = 0;
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++;
}
}
static void bfs_addtoqueue(t_bfs *tab, int node, int nb_nodes)
{
int i;
@ -43,13 +29,26 @@ static void bfs_addtoqueue(t_bfs *tab, int node, int nb_nodes)
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 (tab[it->index].visited == 0 && it->weight == 2)
break ;
it = it->next;
}
it = data->adj[tab[i].queue];
while (it != NULL)
{
if (tab[it->index].visited == 0 && it->weight > 0)
if (tab[it->index].visited == 0 && ((!used && it->weight > 0)
|| (used && it->weight == 2)))
bfs_addtoqueue(tab, it->index, data->nb_nodes);
if (tab[it->index].visited == 0 && it->weight > 0)
if (tab[it->index].visited == 0 && ((!used && it->weight > 0)
|| (used && it->weight == 2)))
tab[it->index].parent = tab[i].queue;
it = it->next;
}
@ -69,7 +68,7 @@ static void bfs_init(t_bfs *tab, t_lmdata *data)
}
}
void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind)
void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind)
{
int i;
@ -79,5 +78,4 @@ void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind)
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);
}

View File

@ -6,106 +6,98 @@
/* 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 */
/* Updated: 2019/04/03 17:18:35 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
t_ind **resolve_path(t_lmdata *data, int start_ind, int end_ind, int nb_path)
static void update_weights(t_lmdata *data, t_bfs *tab, int end_ind)
{
t_ind **ret;
t_ind *it;
int i;
i = end_ind;
while (i != -1)
{
it = data->adj[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 != tab[i].parent)
it = it->next;
if (it && it->index == tab[i].parent)
it->weight++;
i = tab[i].parent;
}
}
static int reset_weights(t_lmdata *data, int s_ind, int node)
{
t_ind *it;
t_ind *it2;
it = data->adj[node];
while (it && it->weight != 2)
it = it->next;
if (it && it->weight == 2)
it->weight--;
it2 = data->adj[it->index];
while (it2 && it2->index != node)
it2 = it2->next;
if (it2 && it2->index == node)
it2->weight++;
return ((node == s_ind) ? -1 : it->index);
}
static t_ind **clean_ret(t_ind **ret)
{
tablst_inddel(ret);
return (NULL);
}
t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind
, int nb_path)
{
t_ind **ret;
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)
if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*)
* (nb_path + 1))) != NULL)
{
ret[nb_path] = NULL;
while (i < nb_path)
while (i < nb_path && (j = e_ind) == e_ind)
{
j = end_ind;
ft_printf("===== start resolv %d =====\n", i);
it = data->adj[j];
while (j != start_ind)
while (j != s_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;
return (clean_ret(ret));
j = reset_weights(data, s_ind, j);
}
if (lst_indadd(&(ret[i]), j) == NULL)
return (NULL); //protect this
if (j != -1 && lst_indadd(&(ret[i]), j) == NULL)
return (clean_ret(ret));
i++;
}
}
return (ret);
}
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
{
t_bfs bfs_tab[data->nb_nodes];
int i;
t_bfs tab[data->nb_nodes];
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)
bfs(data, tab, start_ind, end_ind);
while (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++;
update_weights(data, tab, end_ind);
bfs(data, tab, start_ind, end_ind);
}
return (resolve_path(data, start_ind, end_ind, nb_path));
}

View File

@ -6,7 +6,7 @@
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/25 06:31:05 by mndhlovu #+# #+# */
/* Updated: 2019/03/25 06:31:09 by mndhlovu ### ########.fr */
/* Updated: 2019/04/05 13:43:21 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,6 +30,9 @@ int main(int ac, char **av)
t_lmdata ldata;
t_holder holder;
int fd;
t_list *it;
t_node *start;
t_node *end;
if (ac == 2)
{
@ -41,9 +44,21 @@ int main(int ac, char **av)
{
//validation works
ft_printf("Success\n");
it = ldata.nodes;
while (it && ((t_node*)(it->content))->role != 's')
{
ft_printf("=== node ===\nindex: %d\nrole: %c\n\n", ((t_node*)(it->content))->ind, ((t_node*)(it->content))->role);
it = it->next;
}
start = it->content;
it = ldata.nodes;
while (it && ((t_node*)(it->content))->role != 'e')
it = it->next;
end = it->content;
edmunds_karp(&ldata, start->ind, end->ind);
}
else
ft_printf("Fail\n");
}
return (0);
}
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */
/* Updated: 2019/03/31 17:53:47 by tmaze ### ########.fr */
/* Updated: 2019/04/03 15:57:26 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,10 +30,20 @@ void lst_inddel(t_ind **lst)
{
t_ind *tmp;
while (*lst != NULL)
while (lst && *lst != NULL)
{
tmp = *lst;
*lst = (*lst)->next;
ft_memdel((void**)&tmp);
}
}
void tablst_inddel(t_ind **tab)
{
int i;
i = 0;
while (tab[i])
lst_inddel(&(tab[i++]));
ft_memdel((void**)&tab);
}

View File

@ -6,13 +6,13 @@
/* 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 */
/* Updated: 2019/04/05 13:59:02 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
#define NB_NODES 8
#define NB_NODES 4
int add_link(t_lmdata *data, int n1, int n2)
{
@ -29,34 +29,8 @@ int main(void)
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))
if (!add_link(&data, 0, 2) || !add_link(&data, 0, 1) || !add_link(&data, 0, 3))
{
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]));
@ -64,10 +38,61 @@ int main(void)
ft_memdel((void**)&(data.adj));
return (1);
}
/* if (!add_link(&data, 1, 4)) */
/* { */
/* 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, 2, 3) || !add_link(&data, 2, 5)) */
/* { */
/* 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[5])); */
/* 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[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, 6) || !add_link(&data, 5, 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])); */
@ -76,32 +101,6 @@ int main(void)
/* 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)
@ -115,7 +114,7 @@ int main(void)
}
i++;
}
path = edmunds_karp(&data, 0, 7);
path = edmunds_karp(&data, 0, 1);
if (path != NULL)
{
i = 0;
@ -128,15 +127,14 @@ int main(void)
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]));
tablst_inddel(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]));