let's go ! B)

added bfs WIP
This commit is contained in:
Tanguy MAZE
2019-03-27 18:04:14 +01:00
parent 4e87b2e587
commit 7a67b49802
6 changed files with 209 additions and 16 deletions

72
srcs/bfs.c Normal file
View File

@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bfs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */
/* Updated: 2019/03/27 17:06:33 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
static void bfs_addtoqueue(int *queue, int node, int nb_nodes)
{
int i;
i = 0;
while (i < nb_nodes && (queue[i] == -1 || queue[i] != node))
i++;
if (i < nb_nodes && queue[i] == -1)
queue[i] = node;
}
static void bfs_checkadj(t_lmdata *data, int *queue, int *parent, int i)
{
t_ind *it;
it = data->adj[queue[i]];
while (it != NULL)
{
bfs_addtoqueue(queue, it->index, data->nb_nodes);
parent[it->index] = queue[i];
it = it->next;
}
}
static void bfs_init(int *queue, char *visited, int *parent, t_lmdata *data)
{
ft_bzero(queue, data->nb_nodes * sizeof(int));
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;
while (i < data->nb_nodes && queue[i] != -1 && parent[end_ind] == -1
&& (visited[queue[i]] = 1))
bfs_checkadj(data, queue, parent, i++);
ret = NULL;
i = end_ind;
while (i != -1)
{
if (lst_indadd(&ret, i) == NULL)
{
lst_inddel(&ret);
return (NULL);
}
i = parent[i];
}
return (ret);
}

View File

@@ -1 +0,0 @@
#include "lem_in.h"

View File

@@ -1,28 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_path.c :+: :+: :+: */
/* lst_ind.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/25 16:15:08 by tmaze #+# #+# */
/* Updated: 2019/03/26 17:09:03 by tmaze ### ########.fr */
/* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */
/* Updated: 2019/03/27 17:45:17 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
int get_nb_nodes(t_lmdata *data)
t_ind *lst_indadd(t_ind **lst, int ind)
{
t_ind *new;
if ((new = (t_ind*)ft_memalloc(sizeof(t_ind))) != NULL)
{
new->index = ind;
new->next = *lst;
*lst = new;
}
return (new);
}
int get_path(t_lmdata *data, int nb_nodes)
void lst_inddel(t_ind **lst)
{
int parent[nb_nodes];
char visited[nb_nodes];
t_list *queue;
t_ind *tmp;
queue = NULL;
ft_bzero(visited);
while (*lst != NULL)
{
tmp = *lst;
*lst = (*lst)->next;
ft_memdel((void**)&tmp);
}
}