we got it !
functionning edmunds_karp still need some testing for already used nodes still need some memory protection
This commit is contained in:
85
srcs/bfs.c
85
srcs/bfs.c
@@ -6,67 +6,78 @@
|
||||
/* 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 */
|
||||
/* Updated: 2019/03/31 18:23:21 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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;
|
||||
|
||||
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++;
|
||||
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;
|
||||
|
||||
it = data->adj[queue[i]];
|
||||
it = data->adj[tab[i].queue];
|
||||
while (it != NULL)
|
||||
{
|
||||
bfs_addtoqueue(queue, it->index, data->nb_nodes);
|
||||
parent[it->index] = queue[i];
|
||||
if (tab[it->index].visited == 0 && it->weight > 0)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
ft_bzero(visited, data->nb_nodes);
|
||||
ft_memset(parent, -1, data->nb_nodes * sizeof(int));
|
||||
}
|
||||
int i;
|
||||
|
||||
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)
|
||||
while (i < data->nb_nodes)
|
||||
{
|
||||
if (lst_indadd(&ret, i) == NULL)
|
||||
{
|
||||
lst_inddel(&ret);
|
||||
return (NULL);
|
||||
}
|
||||
i = parent[i];
|
||||
tab[i].parent = -1;
|
||||
tab[i].visited = 0;
|
||||
tab[i].queue = -1;
|
||||
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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user