added finished algorithm

merged algorithm from other personnal repo
This commit is contained in:
Tanguy MAZE
2019-04-22 10:13:09 +02:00
parent c6339a00e9
commit a8b2e31701
128 changed files with 5020 additions and 86 deletions

View File

@@ -5,80 +5,65 @@
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */
/* Updated: 2019/04/14 11:39:52 by tmaze ### ########.fr */
/* Created: 2019/04/18 09:25:05 by tmaze #+# #+# */
/* Updated: 2019/04/21 14:42:08 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
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;
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 (used)
break ;
it = it->next;
}
it = data->adj[tab[i].queue];
while (it != NULL)
{
if (tab[it->index].visited == 0 && ((!used && it->weight == 1)
|| (used && it->weight == 2)))
bfs_addtoqueue(tab, it->index, data->nb_nodes);
if (tab[it->index].visited == 0 && ((!used && it->weight == 1)
|| (used && it->weight == 2)))
tab[it->index].parent = tab[i].queue;
if (it->weight == 2)
break ;
it = it->next;
}
}
static void bfs_init(t_bfs *tab, t_lmdata *data)
void bfs_init(t_lmdata *data, t_bfs *tab)
{
int i;
i = 0;
while (i < data->nb_nodes)
{
tab[i].parent = -1;
tab[i].visited = 0;
tab[i].queue = -1;
tab[i].visited = 0;
tab[i].parent = -1;
i++;
}
}
void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind)
void bfs_add_to_queue(t_lmdata *data, t_bfs *tab, int node, int i)
{
int j;
j = 0;
while (j < data->nb_nodes && tab[j].queue != -1
&& tab[j].queue != node)
j++;
if (j < data->nb_nodes && tab[j].queue == -1)
{
tab[j].queue = node;
tab[node].parent = tab[i].queue;
}
}
void bfs(t_lmdata *data, t_bfs *tab, int s_ind, int e_ind)
{
int i;
int used;
t_ind *it;
ft_printf("New bfs\n");
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_init(data, tab);
tab[i].queue = s_ind;
while (i < data->nb_nodes && tab[i].queue != -1 && tab[e_ind].parent == -1)
{
tab[tab[i].queue].visited = 1;
used = (tab[tab[i].queue].old_visited
&& get_node_path(data->adj[tab[tab[i].queue].parent]
, tab[i].queue)->weight == 1);
it = data->adj[tab[i].queue];
while (it)
{
if (it->weight > 0 && !tab[it->index].visited
&& (!used || (used && it->weight == 2)))
bfs_add_to_queue(data, tab, it->index, i);
it = it->next;
}
i++;
}
}