73 lines
2.1 KiB
C
73 lines
2.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* 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);
|
|
}
|