int main(){ Graphe g = initGraphe(3); ajouterArc(0,1,0.5f,g); ajouterArc(0,2,0.6f,g); afficherGraphe(g); deleteGraphe(g); return 0; }
t_graphe *parcours_largeur(t_graphe* graphe, int s) { int *visite; int *visite2; int *names; t_graphe *T; t_ch_ch_int *ttmp; t_ch_int *tmp; T = init(GO); visite = get_visite(graphe->taille); visite2 = get_visite(graphe->taille); names = get_numbers(graphe); visite[get_index(s, names, graphe->taille)] = 2; for (ttmp = graphe->graphe; ttmp != NULL; ttmp = ttmp->next) { if (visite[get_index(ttmp->number, names, graphe->taille)] == 2) { visite[get_index(ttmp->number, names, graphe->taille)] = 1; for (tmp = ttmp->liste; tmp != NULL; tmp = tmp->next) if (visite[get_index(tmp->number, names, graphe->taille)] == 0) { ajouterArc(T, ttmp->name, tmp->name, tmp->data); visite[get_index(tmp->number, names, graphe->taille)] = 2; } ttmp = graphe->graphe; } } return (T); }
t_graphe *initRand(int type, int n) { int i; int j; int new_arc; t_graphe *graphe; new_arc = 0; graphe = init(type); srand(time(0)*getpid()); new_arc = random(); for (i = 0; i < n; i++) for (j = 0; j < (n-1); j++) { new_arc = 1 + rand(); if (new_arc < n) ajouterArc(graphe, i, j, NULL); } return (graphe); }
t_graphe *parcours_profondeur(t_graphe* graphe, char *n_s) { int *visite; int *names; t_pile *pile; int s; int u; int v; int i; int succ; int size; t_ch_int *tmp; t_ch_ch_int *ttmp; t_graphe *T; void *data; u = 0; v = 0; data = 0; succ = 0; size = taille(graphe); visite = xmalloc(sizeof(*visite)*size); names = xmalloc(sizeof(*names)*size); pile = xmalloc(sizeof(*pile)); pile->pile = xmalloc(sizeof(*pile->pile)*size); pile->taille_pile = 0; T = init(GO); empiler(name_to_number(graphe, n_s), pile); for (i = 0, ttmp = graphe->graphe; i < size; i++, ttmp = ttmp->next) names[i] = ttmp->number; inserer(get_index(s, names, size), visite); while (pile->taille_pile != 0) { u = sommet(pile); succ = 0; for (ttmp = graphe->graphe; ttmp != NULL; ttmp = ttmp->next) if (ttmp->number == u) break; for (tmp = ttmp->liste; tmp != NULL; tmp = tmp->next) if (!visite[get_index(tmp->number, names, size)]) succ++; if (succ != 0) { for (ttmp = graphe->graphe; ttmp != NULL; ttmp = ttmp->next) if (ttmp->number == u) break; for (tmp = ttmp->liste; tmp != NULL; tmp = tmp->next) if (!visite[get_index(tmp->number, names, size)]) { v = tmp->number; data = tmp->data; break; } ajouterArc(T, number_to_name(graphe, u), number_to_name(graphe, v), data); empiler(v,pile); inserer(get_index(v, names, size), visite); } else depiler(pile); } return (T); }