150 lines
3.4 KiB
C
150 lines
3.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* edmunds_karp.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */
|
|
/* Updated: 2019/04/13 14:55:19 by tmaze ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "lem_in.h"
|
|
|
|
static void update_weights(t_lmdata *data, t_bfs *tab, int end_ind)
|
|
{
|
|
t_ind *it;
|
|
int i;
|
|
|
|
i = end_ind;
|
|
while (i != -1)
|
|
{
|
|
if (tab[i].parent != -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 get_score(t_lmdata *data, t_ind **ret)
|
|
{
|
|
int tot;
|
|
int score;
|
|
int nb_paths;
|
|
int i;
|
|
t_ind *it;
|
|
|
|
i = 0;
|
|
tot = 0;
|
|
score = 0;
|
|
while (ret[i])
|
|
i++;
|
|
nb_paths = i;
|
|
i = 0;
|
|
while (ret[i] && (it = ret[i]))
|
|
{
|
|
while (it && tot++)
|
|
it = it->next;
|
|
tot *= (data->nbants / nb_paths);
|
|
if (score == 0 || tot > score)
|
|
score = tot;
|
|
i++;
|
|
}
|
|
return (score);
|
|
}
|
|
|
|
/* 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 = 1; */
|
|
/* it2 = data->adj[it->index]; */
|
|
/* while (it2 && it2->index != node) */
|
|
/* it2 = it2->next; */
|
|
/* if (it2 && it2->index == node) */
|
|
/* it2->weight = 1; */
|
|
/* return ((!it && node == s_ind) ? -1 : it->index); */
|
|
/* } */
|
|
|
|
static t_ind **clean_ret(t_ind **ret)
|
|
{
|
|
tablst_inddel(ret);
|
|
return (NULL);
|
|
}
|
|
|
|
static t_ind **resolve_path(int s_ind, int e_ind
|
|
, int nb_path, t_bfs *tab)
|
|
{
|
|
t_ind **ret;
|
|
int i;
|
|
int j;
|
|
|
|
i = 0;
|
|
ret = NULL;
|
|
if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_path + 1))) != NULL)
|
|
while (i < nb_path && (j = e_ind) == e_ind)
|
|
{
|
|
while (j != s_ind)
|
|
{
|
|
if (lst_indadd(&(ret[i]), j) == NULL)
|
|
return (clean_ret(ret));
|
|
j = tab[j].parent;
|
|
}
|
|
i++;
|
|
}
|
|
return (ret);
|
|
}
|
|
|
|
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind
|
|
, int nb_paths)
|
|
{
|
|
t_bfs tab[data->nb_nodes];
|
|
t_ind **ret[2];
|
|
int score[2];
|
|
|
|
ret[0] = NULL;
|
|
ret[1] = NULL;
|
|
score[0] = 0;
|
|
score[1] = 0;
|
|
if (data && data->adj)
|
|
{
|
|
bfs(data, tab, start_ind, end_ind);
|
|
while (tab[end_ind].parent != -1)
|
|
{
|
|
update_weights(data, tab, end_ind);
|
|
bfs(data, tab, start_ind, end_ind);
|
|
ret[1] = resolve_path(start_ind, end_ind, nb_paths, tab);
|
|
if (ret[0] == NULL || (score[1] = get_score(data, ret[1])) < score[0])
|
|
{
|
|
score[0] = score[1];
|
|
if (ret[0] != NULL)
|
|
clean_ret(ret[0]);
|
|
ret[0] = ret[1];
|
|
}
|
|
else if (score[1] >= score[0])
|
|
{
|
|
clean_ret(ret[1]);
|
|
break;
|
|
}
|
|
}
|
|
return (ret[0]);
|
|
}
|
|
return (NULL);
|
|
}
|