diff --git a/graphe/bin/exe b/graphe/bin/exe new file mode 100755 index 0000000..7578396 Binary files /dev/null and b/graphe/bin/exe differ diff --git a/graphe/graphe.txt b/graphe/graphe.txt new file mode 100644 index 0000000..0b92373 --- /dev/null +++ b/graphe/graphe.txt @@ -0,0 +1,12 @@ +digraph family +{ +6[label="S6"] +8[label="S8"] +9[label="S9"] +15[label="S15"] +23[label="S23"] +6->15 +9->8 +9->15 +23->15 +} diff --git a/graphe/obj/graphe.o b/graphe/obj/graphe.o new file mode 100644 index 0000000..f17ea47 Binary files /dev/null and b/graphe/obj/graphe.o differ diff --git a/graphe/obj/liste.o b/graphe/obj/liste.o new file mode 100644 index 0000000..862b8d2 Binary files /dev/null and b/graphe/obj/liste.o differ diff --git a/graphe/obj/main.o b/graphe/obj/main.o new file mode 100644 index 0000000..f90438e Binary files /dev/null and b/graphe/obj/main.o differ diff --git a/graphe/src/graphe.c b/graphe/src/graphe.c index 97f1a43..f3737f7 100644 --- a/graphe/src/graphe.c +++ b/graphe/src/graphe.c @@ -1,144 +1,117 @@ #include "graphe.h" -Graphe graphNouve(void) +Graphe graphNouv() { return NULL; } -Graphe adjs(Graphe g, int s) +Graphe adjst(Graphe g, int x) { - if(vide(g)) - { - return adjt(g, s); - } - if(x < g->s) - { - return adjt(g, s); - } - if(x == g -> s) - { - return g; - } - g -> suiv = adjs(g -> suiv, s); - return g; -} - -Graphe adjt(Graphe g, int x) -{ - Maillong *m; + MaillonG *m; - m = (Maillong *)malloc(sizeof(Maillong)); + m = (MaillonG*)malloc(sizeof(MaillonG)); if(m == NULL) { - printf("Probleme malloc adjt"); + printf("probleme malloc MaillonG\n"); exit(1); } - m -> s = x; - m -> suiv = g; - m -> succ = listeNouv(); + m->v = x; + m->suiv = g; + m->l = listenouv(); + return m; } -Liste listeNouv(void) +Graphe adjs(Graphe g, int x) { - return NULL; + if(videGraphe(g)) + return adjst(g, x); + if(x < g->v) + return adjst(g, x); + if(x == g->v) + return g; + g->suiv = adjs(g->suiv, x); + return g; } -bool vide(Graphe g) +Graphe adja(Graphe g, int x, int y) { - return g == NULL; + Graphe m; + + m = g; + if(!exs(g, x) || !exs(g, y)) + return g; + while(g->v != x) + g = g->suiv; + g->l = insList(g->l, y); + return m; } -bool exist(Graphe g, int x) +Graphe sups1(Graphe g, int x) { - if(vide(g)) - { - return false; - } - if(x < g -> s) - { - return false; - } - if(x == g -> s) + Graphe m; + + if(g->v == x) { - return true; + m = g; + g = g->suiv; + free(m); + return g; } - return exist(g -> suiv, x); + g->suiv = sups1(g->suiv, x); + return g; } -Graphe adja(Graphe g, int x, int y) +Graphe sups(Graphe g, int x) { - Maillon *aux = g; - - if(!exist(g, x) || !exist(g, y)) - { + if(!exs(g, x) || de(g, x) != 0 || di(g, x) != 0) return g; - } - while(g -> s != x) - { - g = g -> suiv; - } - g -> succ = inserer(g -> succ, y); - return aux; + return sups1(g, x); } Graphe supa(Graphe g, int x, int y) { - Maillon *aux = g; + Graphe m; + m = g; if(!exa(g, x, y)) - { return g; - } - while(g -> s != x) - { - g = g -> suiv; - } - g -> succ = supprimer(g -> succ, y); - return aux; + while(g->v != x) + g = g->suiv; + g->l = supList(g->l, y); + return m; } -Graphe sups(Graphe g, int x, int y) +bool exs(Graphe g, int s) { - if(di(g, x) != 0 || de(g, x) != 0) - { - return g; - } - return sups1(Graphe g, int x); + if(videGraphe(g)) + return false; + if(s < g->v) + return false; + if(s == g->v) + return true; + return exs(g->suiv, s); } -Graphe sups1(Graphe g, int x) + +bool exa(Graphe g, int x, int y) { - if(vide(g)) - { - return g; - } - if(x < g -> s) - { - return g; - } - if(x == g -> s) - { - return supst(g); - } - g -> suiv = sups1(g -> suiv, x); - return g; + if(!exs(g, x) || !exs(g, y)) + return false; + while(g->v != x) + g = g->suiv; + return rechList(g->l, y); } + int de(Graphe g, int x) { - if(vide(g)) - { - return 0; - } - if(!exs(g,x)) - { - return 0; - } - while(g -> v != x) + while(!videGraphe(g)) { - g = g -> suiv; + if(g->v == x) + return lgList(g->l); + g = g->suiv; } - return longueur(g -> succ); + return 0; } int di(Graphe g, int x) @@ -146,57 +119,87 @@ int di(Graphe g, int x) int cpt; cpt = 0; - while(!vide(g)) + while(!videGraphe(g)) { - if(appartient(g -> succ, x)) - { + if(rechList(g->l, x)) cpt++; - } - g = g -> suiv; + g = g->suiv; } return cpt; } -Graphe supst(Graphe g) +Liste esg(Graphe g/*, int x*/) { - Maillon *m; - - m = g; - g = g -> suiv; - free(m); - return g; + Liste res = listenouv(); + if(videGraphe(g)) + return res; + while(g != NULL) + { + res = insList(res, g->v); + g = g->suiv; + } + return res; } -void affichage(Graphe g) +bool videGraphe(Graphe g) { - FILE *flot; - Graphe aux = g; + return g == NULL; +} + +Liste esuc(Graphe g, int x) +{ + if(!exs(g, x)) + return listenouv(); + while(g->v != x) + g = g->suiv; + return g->l; +} + +Liste parcoursProfondeur(Graphe g, Liste e, Liste l) +{ + int k; Liste l1; + if(videList(e)) + return l; + k = tList(e); + if(rechList(l, k)) + l1 = l; + else + l1 = parcoursProfondeur(g, esuc(g, k), insList(l, k)); + return parcoursProfondeur(g, supList(e, k), l1); +} + +void afficherGraphe(Graphe g) +{ + FILE *flot; + Graphe m; + Liste aux; + + m = g; flot = fopen("graphe.txt", "w"); - if(!flot) + if(flot == NULL) { - printf("probleme ouverture graphe.txt"); + printf("probleme ouverture fichier affichage graphe\n"); exit(1); } - fprintf(flot, "digraph family\n"); - while(!vide(g)) + fprintf(flot, "digraph family\n{\n"); + while(!videGraphe(g)) { - fprintf(flot, "%d[label=\"%d\"]\n"g -> s, g -> s); - g = g -> suiv; + fprintf(flot, "%d[label=\"S%d\"]\n", g->v, g->v); + g = g->suiv; } - while(!vide(aux)) + while(!videGraphe(m)) { - while(l1 != NULL) + aux = m->l; + while(aux != NULL) { - fprintf(flot, "%d->%d\n", aux -> s, l1 -> v); - l1 = l1 -> suiv; + fprintf(flot, "%d->%d\n", m->v, aux->v); + aux = aux->suiv; } - aux = aux -> suiv; + m = m->suiv; } fprintf(flot, "}\n"); fclose(flot); system("dotty graphe.txt"); } - -/*Liste fct(Graphe g, Liste e, Liste l)*/ diff --git a/graphe/src/graphe.h b/graphe/src/graphe.h index ba8cfa1..df8ea39 100644 --- a/graphe/src/graphe.h +++ b/graphe/src/graphe.h @@ -1,31 +1,29 @@ -#include -#include #include +#include +#include +#include +#include "liste.h" -typedef struct maillong -{ - struct maillong *suiv; - int s; - Liste succ; -} Maillong, *Graphe; - -typedef struct maillonl +typedef struct maillonG { - int v; - struct maillonl *suiv; -} Maillonl, *Liste; + int v; + struct maillonG *suiv; + Liste l; +} MaillonG, *Graphe; -Graphe adjs(Graphe g, int s); -Graphe graphNouv(void); -Graphe adjt(Graphe g, int x); -bool vide(Graphe g); -bool exist(Graphe g, int x); +Graphe graphNouv(); +Graphe adjst(Graphe g, int x); +Graphe adjs(Graphe g, int x); Graphe adja(Graphe g, int x, int y); -Graphe supa(Graphe g, int x, int y); -Graphe sups(Graphe g, int x, int y); +Graphe sups1(Graphe g, int x); Graphe sups(Graphe g, int x); +Graphe supa(Graphe g, int x, int y); +bool exs(Graphe g, int s); +bool exa(Graphe g, int x, int y); int de(Graphe g, int x); int di(Graphe g, int x); -Graphe supst(Graphe g); -void affichage(Graphe g); - +Liste esg(Graphe g/*, int x*/); +bool videGraphe(Graphe g); +Liste esuc(Graphe g, int x); +Liste parcoursProfondeur(Graphe g, Liste e, Liste l); +void afficherGraphe(Graphe g); diff --git a/graphe/src/graph.c b/graphe/src/liste.c similarity index 88% rename from graphe/src/graph.c rename to graphe/src/liste.c index c88ced2..a324406 100644 --- a/graphe/src/graph.c +++ b/graphe/src/liste.c @@ -1,4 +1,4 @@ -#include "graph.h" +#include "liste.h" Liste listenouv(void) { @@ -18,7 +18,7 @@ Liste insTList(Liste l, int x) maill = (Maillon*)malloc(sizeof(Maillon)); if(maill == NULL) { - printf("Pb malloc\n"); + printf("Probleme malloc Maillon Liste\n"); exit(1); } maill->v = x; @@ -44,7 +44,7 @@ Liste supTList(Liste l) if(videList(l)) { - printf("Opération interdite\n"); + printf("Opération interdite suppression tete liste\n"); exit(1); } aux = l; @@ -69,7 +69,7 @@ int tList(Liste l) { if(videList(l)) { - printf("Opération interdite\n"); + printf("Opération interdite tListe\n"); exit(1); } return l->v; diff --git a/graphe/src/graph.h b/graphe/src/liste.h similarity index 83% rename from graphe/src/graph.h rename to graphe/src/liste.h index 5a20f54..1dc9c80 100644 --- a/graphe/src/graph.h +++ b/graphe/src/liste.h @@ -3,22 +3,12 @@ #include #include - typedef struct maillon { int v; struct maillon *suiv; } Maillon, *Liste; - -typedef struct maillonG -{ - int v; - struct maillonG *suiv; - Liste l; -} MaillonG, *Graphe; - - Liste listenouv (void); bool videList (Liste l); Liste insTList (Liste l, int x); diff --git a/graphe/src/main.c b/graphe/src/main.c new file mode 100644 index 0000000..927b48d --- /dev/null +++ b/graphe/src/main.c @@ -0,0 +1,79 @@ +#include "graphe.h" + +void testList() +{ + Liste l = listenouv(); + + l = insList(l, 3); + l = insList(l, 5); + l = insList(l, 1); + l = insList(l, 9); + l = insList(l, 20); + l = insList(l, 7); + afficherList(l); + l = supList(l, 1); + l = supList(l, 4); + l = supList(l, 9); + afficherList(l); + printf("Valeur en tete %d => 3\n", tList(l)); + if(rechList(l, 1)) + printf("Rech marche pas\n"); + else + printf("Rech marche\n"); + if(rechList(l, 20)) + printf("Rech marche\n"); + else + printf("Rech marche pas\n"); + printf("lgList %d => 4\n", lgList(l)); +} + +void testGraphe() +{ + Graphe g = graphNouv(); + + g = adjs(g, 3); + g = adjs(g, 6); + // g = adjs(g, 3); marche pas (normal) + g = adjs(g, 9); + g = adjs(g, 8); + g = adjs(g, 15); + g = adjs(g, 23); + if(exs(g, 3)) + printf("exs marche\n"); + else + printf("exs marche pas \n"); + if(exs(g, 415)) + printf("exs marche pas\n"); + else + printf("exs marche\n"); + g = adja(g, 3, 6); + g = adja(g, 6, 15); + g = adja(g, 9, 8); + g = adja(g, 9, 6); + g = adja(g, 9, 15); + g = adja(g, 23, 15); + // Test existe + // if(exa(g, 3, 5)) + // printf("exa marche pas\n"); + // else + // printf("exa marche\n"); + // if(exa(g, 3, 6)) + // printf("exa marche\n"); + // else + // printf("exa marche pas\n"); + afficherGraphe(g); + g = supa(g, 9, 6); + afficherGraphe(g); + // g = sups(g, 15); fonctionne pas (normal) + g = supa(g, 3, 6); + g = sups(g, 3); + afficherGraphe(g); + afficherList(esg(g)); +} + +int main(void) +{ + //testList(); + testGraphe(); + return 0; +}