void parcoursEnLargeurCouleurs(tGraphe graphe, tNumeroSommet sommetDepart, tTabCouleurs tableauCouleurs) { tFileSommets fileSommets; // variable contenant les numéros des sommets défilés tNumeroSommet x; // variable contenant les voisins de x tNumeroSommet y; // variable contenant les noms des sommets affichés tNomSommet nomSommet; // indice de boucle int i; grapheRecupNomSommet(graphe,sommetDepart,nomSommet); fileSommets = fileSommetsAlloue(); // on colorie le sommet de départ en vert et tous les autres sommets en bleu for (i = 0 ; i < grapheNbSommets(graphe) ; i++) { tableauCouleurs[i] = BLEU; } for (i = 0 ; i < grapheNbSommets(graphe) ; i++) { if (i == sommetDepart) { tableauCouleurs[i] = VERT; // on affiche le graphe au départ (quand tous les sommets) sont bleus et le sommet de départ est vert graphe2visuCouleurs(graphe,"visuCouleurs",tableauCouleurs); } } // on enfile le sommet de départ fileSommetsEnfile(fileSommets,sommetDepart); while (!fileSommetsEstVide(fileSommets)) { // on défile le dernier sommet enfilé x = fileSommetsDefile(fileSommets); // on parcourt la liste des voisins de x for (i = 0 ; i < grapheNbVoisinsSommet(graphe,x) ; i++) { // on récupère le numéro du i-ème voisin de x y = grapheVoisinSommetNumero(graphe,x,i); // s'il est bleu, on le colorie en vert et on l'enfile if (tableauCouleurs[y] == BLEU) { tableauCouleurs[y] = VERT; // on affiche le graphe car il y a eu un changement de couleur graphe2visuCouleurs(graphe,"visuCouleurs",tableauCouleurs); fileSommetsEnfile(fileSommets,y); } } // on colorie x en rouge et on affiche le nom de sommet correspondant tableauCouleurs[x] = ROUGE; // on affiche le graphe car il y a eu un changement de couleur graphe2visuCouleurs(graphe,"visuCouleurs",tableauCouleurs); grapheRecupNomSommet(graphe,x,nomSommet); } printf("\n"); // on libère l'espace mémoire occupé par le graphe car on n'en a plus besoin grapheLibere(graphe); }
/* * Parcours en parcours_en_profondeur */ void parcours_en_profondeur(tGraphe graphe, tNumeroSommet sommet, char *outfile){ tTabCouleurs couleurs; tPileSommets pile; tNumeroSommet xSommet; int i[MAX_SOMMETS]; int nSommet = grapheNbSommets(graphe); int entrer; /* On Initialise les couleurs et l'indice i*/ init_parcours(graphe, couleurs, i, nSommet); /* On dépile tout en affichant */ pile = pileSommetsAlloue(); /* On colorie le promier sommet en vert et on l'enpile */ couleurs[sommet] = VERT; pileSommetsEmpile(pile, sommet); while (!pileSommetsEstVide(pile)) { graphe_visu_color(graphe, couleurs, outfile); xSommet = pileSommetsTete(pile); traitement_pile(pile, xSommet, i, graphe, couleurs); scanf("%d", &entrer); } graphe_visu_color(graphe, couleurs, outfile); pileSommetsLibere(pile); printf("resultat du parcours en profondeur: ---------------------------------------\n"); print_i(graphe, i, nSommet); }
// // paramètres : // // graphe = graphe orienté // s = sommet de départ // result = tableau de sommets à une distance inférieure ou égale à 2 du sommet s (result[i]=1 si distance(s,i)<=2, -1 sinon) // void distance_inf_2(tGraphe graphe, tNumeroSommet s, int result[]) { // nombre de sommets du graphe unsigned int n = grapheNbSommets(graphe); int nSuccesseur_de_s = grapheNbSuccesseursSommet(graphe,s); int nSuccesseur_de_y; tNumeroSommet x, y; tNumeroSommet tab[n]; // variable de parcours unsigned int i; /* Pour eviter des erreurs */ for(i =0; i< n; i++){ tab[n] =-1; } /* boucle principale */ for (i=0; i<nSuccesseur_de_s; i++){ // y est le nom du successeur courant x = grapheSuccesseurSommetNumero(graphe, x, i); tab[x] = 1; result[x] = 1; nSuccesseur_de_y = grapheNbSuccesseursSommet(graphe,x); for(int j =0; j< nSuccesseur_de_y; j++){ y = grapheSuccesseurSommetNumero(graphe, x, i); //Si on ne la pas deja traité if(tab[y] == -1){ tab[y] = 1; result[y] = 2; } } } }
void grapheChangeType(tGraphe graphe, int oriente) { if (grapheNbSommets(graphe)>0) halt("On ne peut changer le type d'un graphe que s'il est vide"); if ( (oriente!=0) && (oriente!=1) ) halt("Orientation incorrecte"); graphe->estOriente = oriente; }
// // paramètres : // // graphe = graphe orienté // s = sommet de départ // result = tableau de sommets accessibles depuis s (result[i]=1 si i est accessible depuis s, -1 sinon) // void parcours_largeur(tGraphe graphe, tNumeroSommet s, int result[]) { // nombre de sommets du graphe unsigned int n = grapheNbSommets(graphe); // le tableau de couleurs tCouleur tabCouleurs[n]; // la file tFileSommets file; // sommets x,y tNumeroSommet x, y; // variable de parcours unsigned int i; /* initialisation */ // colorier tous les sommets en bleu for (i=0; i<n; i++) { tabCouleurs[i] = BLEU; } // initialiser la file file = fileSommetsAlloue(); // colorier s en vert tabCouleurs[s] = VERT; // enfiler s fileSommetsEnfile(file, s); /* boucle principale */ // tant que la file n'est pas vide while (!fileSommetsEstVide(file)) { // x = tête de file x = fileSommetsDefile(file); // parcours de tous les successeurs for (i=0; i<grapheNbSuccesseursSommet(graphe,x); i++) { // y est le nom du successeur courant y = grapheSuccesseurSommetNumero(graphe, x, i); if (tabCouleurs[y] == BLEU) { // colorier y en vert et l’enfiler tabCouleurs[y] = VERT; fileSommetsEnfile(file, y); } } tabCouleurs[x] = ROUGE; } // libérer la file fileSommetsLibere(file); // tous les sommets en rouge sont accessibles for (i=0; i<n; i++) { if (tabCouleurs[i] == ROUGE) result[i] = 1; else result[i] = -1; } }
void affiche_parcours_largeur(tGraphe graphe, tNumeroSommet num_sommet, int first) { tNumeroSommet current_voisin; tTabCouleurs tab_couleurs; int i, nb_sommets, voisins; char *argv[3]; argv[0] = "evince"; argv[1] = output; argv[2] = NULL; /* char *argv[3]; argv[0] = "evince"; argv[1] = output; argv[2] = NULL; */ /* DFS (graphe G, sommet s) { Marquer(s); POUR CHAQUE élément s_fils de Voisins(s) FAIRE SI NonMarqué(s_fils) ALORS DFS(G,s_fils); FIN-SI FIN-POUR } */ graphe2visu(graphe, output, tab_couleurs); switch(fork()){ case 0 : if (execvp(argv[0], argv) == -1) halt("Error evince"); exit(EXIT_SUCCESS); } if(first){ nb_sommets = grapheNbSommets(graphe); if(verbose) printf("Nb sommets : %d\n", nb_sommets); for(i = 0 ; i < nb_sommets; i++){ tab_couleurs[i] = BLEU; } } tab_couleurs[num_sommet] = ROUGE; if(verbose) printf("Sommet %d marqué ROUGE \n", num_sommet); voisins = grapheNbVoisinsSommet(graphe, num_sommet); if(verbose) printf("Nombre de voisins du sommet %d : %d\n", num_sommet, voisins); for(i = 0 ; i<voisins ; i++){ current_voisin = grapheVoisinSommetNumero(graphe, num_sommet, i); if(verbose) printf("Voisin courant : %d\n", current_voisin); sleep(2); if(tab_couleurs[current_voisin] == BLEU) affiche_parcours_largeur(graphe, current_voisin, 0); } }
void graphe2visuCouleurs(tGraphe graphe, char *outfile, tTabCouleurs tabCouleurs) { FILE *fic; char commande[80]; char dotfile[80]; /* le fichier dot pour cr´eer le ps */ int ret, i, j; char isdigraph, *isdigraph2, *color; tNomSommet actuel, prochain, sommet; /* on va cr´eer un fichier pour graphviz, dans le fichier "outfile".dot */ strcpy(dotfile, outfile); strcat(dotfile, ".dot"); fic = fopen(dotfile, "w"); if (fic==NULL) halt ("Ouverture du fichier %s en ´ecriture impossible\n", dotfile); isdigraph = '-'; isdigraph2 = "graph {"; if(grapheEstOriente(graphe)) { isdigraph = '>'; isdigraph2 = "digraph {"; } fprintf(fic, "%s\n", isdigraph2 ); for(i = 0; i<=grapheNbSommets(graphe) - 1; i++) { grapheRecupNomSommet(graphe, i, sommet); if(tabCouleurs[i] == ROUGE) { color = "red"; }else if(tabCouleurs[i] == BLEU) { color = "blue"; }else if(tabCouleurs[i] == VERT) { color = "green"; } fprintf(fic, "%s [color=%s]\n", sommet, color); } for(i = 0; i <= grapheNbArcs(graphe) - 1; i++) { grapheRecupNomSommet(graphe, grapheRecupArcNumero(graphe, i).orig, actuel); grapheRecupNomSommet(graphe, grapheRecupArcNumero(graphe, i).dest, prochain); fprintf(fic, " %s -%c %s\n", actuel, isdigraph, prochain); } fprintf(fic, "}\n"); fclose(fic); sprintf(commande, "dot -Tps %s -o %s", dotfile, outfile); ret = system(commande); if (WEXITSTATUS(ret)) halt("La commande suivante a ´echou´e\n%s\n", commande); }
void graphe_visu_color(tGraphe graphe,tTabCouleurs tab_couleur, char *outfile) { FILE *fic; char commande[80]; char dotfile[80]; /* le fichier dot pour cr´eer le ps */ char psfile[80]; int ret; int nArcs = grapheNbArcs(graphe); int nSommets = grapheNbSommets (graphe); int estOriente = grapheEstOriente(graphe); char *delimiter; tNomSommet orig ; tNomSommet dest; static char* char_colors[3]; tArc arc; char_colors[0] = "red"; char_colors[1] = "blue"; char_colors[2] = "green"; /* on va créer un fichier pour graphviz, dans le fichier "outfile".dot */ strcpy(dotfile, outfile); strcpy(psfile, outfile); strcat(dotfile, ".dot"); strcat(psfile, ".ps"); fic = fopen(dotfile, "w"); if (fic==NULL) { halt ("Ouverture du fichier %s en ´ecriture impossible\n", dotfile); } estOriente ? fprintf(fic, "digraph {\n"): fprintf(fic, "graph {\n"); delimiter = estOriente ? "->" : "--"; /* Ecrit tous les sommets du graphe */ for (int i = 0; i < nSommets; i++) { grapheRecupNomSommet(graphe, i, orig); fprintf(fic, "%s [color=%s];\n", orig, char_colors[tab_couleur[i]]); } /* Ajoute les liaisons du graphe */ for(int i = 0; i< nArcs; i++){ arc = grapheRecupArcNumero(graphe, i); grapheRecupNomSommet(graphe, arc.orig, orig); grapheRecupNomSommet(graphe, arc.dest, dest); fprintf(fic, "%s %s %s;\n", orig,delimiter, dest); } /* On ferme le graphe */ fprintf(fic, "}\n"); fclose(fic); sprintf(commande, "dot -Tps %s -o %s ", dotfile, psfile); ret = system(commande); if (WEXITSTATUS(ret)){ halt("La commande suivante a ´echou´e\n%s\n", commande); } }
// // paramètres : // // graphe = graphe orienté // s = sommet de départ // k = distance minimale // result = tableau de sommets à une distance strictement égale à k du sommet s (result[i]=1 si distance(s,i)==k, -1 sinon) // void distance_eq_k(tGraphe graphe, tNumeroSommet s, unsigned int k, int result[]) { // A MODIFIER ET COMPLETER // ... // pour le moment, on se contente de tout mettre à FAUX for (unsigned int i=0; i<grapheNbSommets(graphe); i++) { result[i] = -1; } }
void graphe2visu(tGraphe graphe, char *outfile) { FILE *fic; char commande[80]; char dotfile[80]; /* le fichier dot pour creer le ps */ int ret,nbsommets; nbsommets = grapheNbSommets(graphe); int i,j; tNomSommet nomSommet; tNomSommet nomVoisin; /* on va creer un fichier pour graphviz, dans le fichier "outfile".dot */ strcpy(dotfile, outfile); strcat(dotfile, ".dot"); fic = fopen(dotfile, "w"); if (fic==NULL) halt ("Ouverture du fichier %s en ecriture impossible\n", dotfile); fprintf(fic, "graph {\n"); /*tester tous les sommets*/ for (i=0; i<nbsommets; i++){ /*nbvoisins =grapheNbVoisinsSommet(graphe,i); */ /*tester les voisins du sommet*/ for(j=0; j <nbsommets;j++){ if (grapheExisteArcEntre(graphe,i,j)){ grapheRecupNomSommet(graphe, i, nomSommet); grapheRecupNomSommet(graphe,j, nomVoisin); fprintf(fic, " %s -- %s\n", nomSommet, nomVoisin); } } }/*mettre ; ?????????,*/ fprintf(fic, "}"); fclose(fic); sprintf(commande, "dot -Tps %s -o %s", dotfile, outfile); ret = system(commande); if (WEXITSTATUS(ret)) halt("La commande suivante a echouee\n%s\n", commande); }
void parcoursLargeur(tGraphe graphe, tNomSommet nom, tTabCouleurs tabCouleurs) { tFileSommets file; tNumeroSommet s, x, y; char * fichier; int i, cpt; cpt=0; fichier=malloc(sizeof(char[20])); s = grapheChercheSommetParNom(graphe, nom); file = fileSommetsAlloue(); for(i = 0; i <= grapheNbSommets(graphe) - 1; i++) { if(i != s) tabCouleurs[i] = BLEU; } tabCouleurs[s] = VERT; graphe2visuCouleurs(graphe, "img/visu0.ps", tabCouleurs); fileSommetsEnfile(file, s); while(!fileSommetsEstVide(file)) { x = fileSommetsDefile(file); for(i = 0; i <= grapheNbVoisinsSommet(graphe, x) - 1; i++) { y = grapheVoisinSommetNumero(graphe, x, i); if(tabCouleurs[y] == BLEU) { tabCouleurs[y] = VERT; fileSommetsEnfile(file, y); } } tabCouleurs[x] = ROUGE; cpt++; sprintf(fichier, "img/visu%d.ps",cpt ); graphe2visuCouleurs(graphe, fichier, tabCouleurs); } }
/* fait le parcours en largeur vu en cours et affiche la liste des sommets dans l'ordre de parcours */ void parcoursEnLargeur(tGraphe graphe, tNumeroSommet sommetDepart) { // le tableau de couleurs et la file de sommets tTabCouleurs tableauCouleurs; tFileSommets fileSommets; // variable contenant les numéros des sommets défilés tNumeroSommet x; // variable contenant les voisins de x tNumeroSommet y; // variable contenant les noms des sommets affichés tNomSommet nomSommet; // indice de boucle int i; grapheRecupNomSommet(graphe,sommetDepart,nomSommet); printf("Parcours en largeur avec %s comme sommet de départ :",nomSommet); fileSommets = fileSommetsAlloue(); // on colorie le sommet de départ en vert et tous les autres sommets en bleu for (i = 0 ; i < grapheNbSommets(graphe) ; i++) { if (i == sommetDepart) tableauCouleurs[i] = VERT; else tableauCouleurs[i] = BLEU; } // on enfile le sommet de départ fileSommetsEnfile(fileSommets,sommetDepart); while (!fileSommetsEstVide(fileSommets)) { // on défile le dernier sommet enfilé x = fileSommetsDefile(fileSommets); // on parcourt la liste des voisins de x for (i = 0 ; i < grapheNbVoisinsSommet(graphe,x) ; i++) { // on récupère le numéro du i-ème voisin de x y = grapheVoisinSommetNumero(graphe,x,i); // s'il est bleu, on le colorie en vert et on l'enfile if (tableauCouleurs[y] == BLEU) { tableauCouleurs[y] = VERT; fileSommetsEnfile(fileSommets,y); } } // on colorie x en rouge et on affiche le nom de sommet correspondant tableauCouleurs[x] = ROUGE; grapheRecupNomSommet(graphe,x,nomSommet); printf(" %s",nomSommet); } printf("\n"); }
void parcour_Profondeur(tGraphe g,char* sommet){ int i,nbVoisin; tNumeroSommet sommetDepart,suivant,x; tNomSommet nom; tTabCouleurs coul; tPileSommets file; file= pileSommetsAlloue(); sommetDepart=grapheChercheSommetParNom(g,sommet); // n opérations pour la boucle for (i=0;i<grapheNbSommets(g);i++) coul[i]=1; coul[sommetDepart] = 2; printf("\n\n\n\ndebut de l'empilade\n"); pileSommetsEmpile(file,sommetDepart); while(!pileSommetsEstVide(file)) { x=pileSommetsDepile(file); nbVoisin=grapheNbVoisinsSommet(g,x); for(i=0;i<nbVoisin;i++) { suivant=grapheVoisinSommetNumero(g,x,i); if(coul[suivant]==1) { coul[suivant]=2; pileSommetsEmpile(file,suivant); } coul[x]=0; grapheRecupNomSommet(g,x,nom); printf("%s est trouvé\n",nom); } } }
void parcourLarg(tGraphe graphe, tNomSommet som) { int nb_sommets; tNumeroSommet current; //tNomSommet destination; nb_sommets = grapheNbSommets(graphe); numerosParcourus = (tNumeroSommet*)malloc(nb_sommets*sizeof(tNumeroSommet)); numerosEnfiles = (tNumeroSommet*)malloc(nb_sommets*sizeof(tNumeroSommet)); current = grapheChercheSommetParNom(graphe, som); enfiler(current); while(!fileEstVide()){ int i, nb_successeurs; tNomSommet sommet; current = numerosEnfiles[enfilesIndex++]; grapheRecupNomSommet (graphe, current, sommet); printf(" %s ",sommet); if (grapheEstOriente(graphe)) { nb_successeurs = grapheNbSuccesseursSommet(graphe,current); for ( i = 0 ; i < nb_successeurs ; i++ ){ enfiler(grapheSuccesseurSommetNumero(graphe,current,i)); } } else { nb_successeurs = grapheNbVoisinsSommet(graphe,current); for ( i = 0 ; i < nb_successeurs ; i++ ){ enfiler(grapheVoisinSommetNumero(graphe,current,i)); } } } printf("\n"); free(numerosParcourus); free(numerosEnfiles); }
/* Affichage */ void grapheAffiche(tGraphe graphe) { tNomSommet nom; tNumeroSommet i, j, successeur, predecesseur; tValeurArc val; tArc arc; /* Affiche la liste des sommets et leur nom */ printf("Type de l'arbre : "); if (grapheEstOriente(graphe)) printf("orientee"); else printf("non orientee"); printf("\n\n"); printf("Nombre de sommets : %d\n\n", grapheNbSommets(graphe)); printf("Liste des sommets avec leurs numeros:\n"); for(i=0; i < grapheNbSommets(graphe);i++) { grapheRecupNomSommet(graphe, i, nom); printf("- %d : %s\n", i, nom); } printf("\n"); /* Matrice d'adjacence */ printf("Matrice d'adjacence :\n"); printf("%2s", ""); for(i=0; i < grapheNbSommets(graphe);i++) { printf("%7d", i); } printf("\n"); for(i=0; i < grapheNbSommets(graphe);i++) { printf("%3d ", i); for(j=0; j < grapheNbSommets(graphe);j++) { if (grapheExisteArcEntre(graphe,i,j)) { val = grapheRecupValeurArc(graphe,i,j); printf("%6.2f ", val);} else printf(" "); } printf("\n"); } printf("\n"); /* Affiche les voisins, successeurs, prédecesseurs */ if (grapheEstOriente(graphe)) { /* affichage des successeurs */ printf("Liste des successeurs :\n"); for(i=0; i < grapheNbSommets(graphe);i++) { grapheRecupNomSommet(graphe, i, nom); printf("%s : ", nom); for(j=0; j<grapheNbSuccesseursSommet(graphe, i); j++) { successeur = grapheSuccesseurSommetNumero(graphe, i, j); grapheRecupNomSommet(graphe, successeur, nom); printf("%s=%.2f ", nom, grapheRecupValeurArc(graphe, i, successeur)); } printf("\n"); } /* affichage des prédécesseurs */ printf("\nListe des predecesseurs :\n"); for(i=0; i < grapheNbSommets(graphe);i++) { grapheRecupNomSommet(graphe, i, nom); printf("%s : ", nom); for(j=0; j<grapheNbPredecesseursSommet(graphe, i); j++) { predecesseur = graphePredecesseurSommetNumero(graphe, i, j); grapheRecupNomSommet(graphe, predecesseur, nom); printf("%s=%.2f ", nom, grapheRecupValeurArc(graphe, predecesseur, i)); } printf("\n"); } } else { /* le graphe n'est pas orienté */ printf("Liste des voisins :\n"); for(i=0; i < grapheNbSommets(graphe);i++) { grapheRecupNomSommet(graphe, i, nom); printf("%s : ", nom); for(j=0; j<grapheNbVoisinsSommet(graphe, i); j++) { successeur = grapheVoisinSommetNumero(graphe, i, j); grapheRecupNomSommet(graphe, successeur, nom); printf("%s=%.2f ", nom, grapheRecupValeurArc(graphe, i, successeur)); } printf("\n"); } } printf("\n"); /* Affiche la liste des arcs */ printf("Liste des arcs :\n"); for(i=0; i < grapheNbArcs(graphe); i++) { arc = grapheRecupArcNumero(graphe, i); grapheRecupNomSommet(graphe, arc.orig, nom); printf("%s", nom); if (grapheEstOriente(graphe)) printf(" -> "); else printf(" -- "); grapheRecupNomSommet(graphe, arc.dest, nom); printf("%s ",nom); printf(" = %.2f\n", arc.val); } }
int main(int argc, char *argv[]) { tGraphe graphe; graphe = grapheAlloue(); grapheChargeFichier(graphe, argv[1]); int nbsommet=grapheNbSommets(graphe); tNumeroSommet i=0; tNumeroSommet j=0; tNumeroSommet k=0; tNomSommet nom; tNumeroSommet maxvoisin; while (i< nbsommet){ if(grapheNbVoisinsSommet (graphe, i)==0){ grapheRecupNomSommet(graphe,i,nom); printf("Sommet sans voisin : %s\n",nom); } i++; } while (j< nbsommet){ if (maxvoisin <= grapheNbVoisinsSommet(graphe, j)){ maxvoisin = grapheNbVoisinsSommet(graphe, j); } j++; } while (k<nbsommet){ if (maxvoisin == grapheNbVoisinsSommet(graphe, k) ){ grapheRecupNomSommet(graphe,k,nom); printf("Sommet avec voisin max : %s\n",nom); } k++; } /*Q7 display graphique*/ grapheLibere(graphe); exit(EXIT_SUCCESS); }
/* programme principal */ int main(int argc, char *argv[]) { // graphe tGraphe graphe; // nom du ficher d'entree char *fic; // usage if (argc!=2) halt("Usage : %s <fichier_graphe>\n", argv[0]); // lecture du nom de fichier fic = argv[1]; // chargement du graphe graphe = grapheAlloue(); grapheChargeFichier(graphe, fic); // le nombre de sommets du graphe unsigned int n = grapheNbSommets(graphe); // initialisation du sommet de départ à 0 (le premier sommet du graphe) tNumeroSommet depart = 0; tNomSommet nom; grapheRecupNomSommet(graphe, depart, nom); printf("Sommet de depart : %s\n", nom); // tableau de résultat des différentes fonctionnalités int result[MAX_SOMMETS]; // valeur de k unsigned int k = 3; /*** parcours en largeur ***/ printf("*** Parcours largeur:\t"); // appel procédure parcours_largeur(graphe, depart, result); // affichage for (unsigned int i=0; i<n; i++) { if (result[i] == 1) { // nom du sommet grapheRecupNomSommet(graphe, i, nom); printf("%s ", nom); } } printf("\n"); /*** distance <= 2 ***/ printf("*** Distance <= 2:\t"); // appel procédure distance_inf_2(graphe, depart, result); // affichage for (unsigned int i=0; i<n; i++) { if (result[i] == 1) { // nom du sommet grapheRecupNomSommet(graphe, i, nom); printf("%s ", nom); } } printf("\n"); /*** distance == 2 ***/ printf("*** Distance == 2:\t"); // appel procédure distance_eq_2(graphe, depart, result); // affichage for (unsigned int i=0; i<n; i++) { if (result[i] == 1) { // nom du sommet grapheRecupNomSommet(graphe, i, nom); printf("%s ", nom); } } printf("\n"); /*** distance == k ***/ printf("*** Distance == k (%d):\t", k); // appel procédure distance_eq_k(graphe, depart, k, result); // affichage for (unsigned int i=0; i<n; i++) { if (result[i] == 1) { // nom du sommet grapheRecupNomSommet(graphe, i, nom); printf("%s ", nom); } } printf("\n"); exit(EXIT_SUCCESS); }
/* affiche le graphe avec les sommets colorés */ void graphe2visuCouleurs(tGraphe graphe, char *outfile, tTabCouleurs tabCouleurs) { // le canal d'écriture FILE *fic; // la commande de transformation du fichier .dot en fichier .ps char commande[80]; // le fichier .dot servant à créer le .ps char dotfile[80]; // permettra d'exécuter la commande int ret; // on va créer un fichier pour graphviz, dans le fichier "outfile".dot strcpy(dotfile, outfile); strcat(dotfile, ".dot"); fic = fopen(dotfile, "w"); // on déclenche une exception si le fichier n'est pas valide if (fic == NULL) { halt ("Ouverture du fichier %s en écriture impossible\n", dotfile); } // on vérifie l'orientation du graphe et on fait l'affichage correspondant switch (grapheEstOriente(graphe)) { case 1 : fprintf(fic,"digraph {\n"); break; default : fprintf(fic,"graph {\n"); break; } tNomSommet nomSommet; // écrit dans le fichier la couleur des sommets à cette étape for (int i = 0; i < grapheNbSommets(graphe); i++) { grapheRecupNomSommet(graphe, i, nomSommet); fprintf(fic,"%s [color=%s];\n",nomSommet,enumToString(tabCouleurs[i])); } printf("\n"); // variables qui vont contenir les noms de sommets origine et destination, et leurs numéros, et enfin l'arc qui les relie tNumeroSommet numSommetOrig; tNumeroSommet numSommetDest; tNomSommet nomSommetOrig; tNomSommet nomSommetDest; tArc arc; int i; // on parcourt la liste des arcs for (i = 0 ; i < grapheNbArcs(graphe) ; i++) { // on récupère le numéro de l'arc ainsi que le numéro du sommet origine et celui du sommet destination arc = grapheRecupArcNumero(graphe,i); numSommetOrig = arc.orig; numSommetDest = arc.dest; // on récupère les noms des sommets et on les affiche selon l'orientation du graphe grapheRecupNomSommet(graphe,numSommetOrig,nomSommetOrig); grapheRecupNomSommet(graphe,numSommetDest,nomSommetDest); switch (grapheEstOriente(graphe)) { case 1 : fprintf(fic,"%s -> %s\n",nomSommetOrig,nomSommetDest); break; default : fprintf(fic,"%s -- %s\n",nomSommetOrig,nomSommetDest); break; } } // on cherche les sommets qui n'ont pas de voisins et on les affiche for (i = 0 ; i < grapheNbSommets(graphe) ; i++) { if (grapheNbVoisinsSommet(graphe,i) == 0) { grapheRecupNomSommet(graphe,i,nomSommetOrig); fprintf(fic,"%s\n",nomSommetOrig); } } fprintf(fic,"}"); // on ferme le canal d'écriture et on met à jour la commande fclose(fic); sprintf(commande, "dot -Tps %s -o %s.ps", dotfile, outfile); // on exécute la commande ret = system(commande); if (WEXITSTATUS(ret)) { halt("La commande suivante a échoué\n%s\n", commande); } /*sprintf(commande2,"evince %s.ps",outfile); printf("%s\n",commande2); ret = system(commande2);*/ timer(2); }
int solutionLab(tGraphe graphe, tNomSommet entree, tNomSommet sortie, tTabCouleurs tabCouleurs) { int i, pasVoisinBleu; tNumeroSommet numE, numS, x, y, varChemin; tNomSommet nomVarChemin, nom1,nom2; tPileSommets pile, pileChemin; pile = pileSommetsAlloue(); pileChemin = pileSommetsAlloue(); pasVoisinBleu = 1 == 1; for(i = 0; i < grapheNbSommets(graphe); i++) { tabCouleurs[i] = BLEU; } numE = grapheChercheSommetParNom(graphe, entree); numS = grapheChercheSommetParNom(graphe, sortie); tabCouleurs[numE] = VERT; pileSommetsEmpile(pile, numE); while(!pileSommetsEstVide(pile)&& pileSommetsTete(pile) != numS) { x = pileSommetsTete(pile); pasVoisinBleu = 1 == 1; for(i = 0; i < grapheNbVoisinsSommet(graphe, x);i++) { y = grapheVoisinSommetNumero(graphe, x, i); if(tabCouleurs[y] == BLEU) { tabCouleurs[y] = VERT; pileSommetsEmpile(pile, y); pasVoisinBleu = pasVoisinBleu && 0 == 1; } } if(pasVoisinBleu) { x = pileSommetsDepile(pile); tabCouleurs[x] = ROUGE; } } if(!pileSommetsEstVide(pile)&& pileSommetsTete(pile) == numS) { while(!pileSommetsEstVide(pile)) { varChemin = pileSommetsDepile(pile); pileSommetsEmpile(pileChemin, varChemin); if(!pileSommetsEstVide(pile)) { while(!estVoisin(graphe, pileSommetsTete(pile), varChemin)) { pileSommetsDepile(pile); } } } while(!pileSommetsEstVide(pileChemin)) { grapheRecupNomSommet(graphe, pileSommetsDepile(pileChemin), nomVarChemin); printf("%s\n", nomVarChemin ); } return 0 == 0; } return 0 == 1; }
void graphe2visu(tGraphe graphe, char *outfile, tTabCouleurs tabCouleurs) { FILE *fic; char commande[80]; char dotfile[80]; /* le fichier dot pour créer le ps */ int ret; int i; int is_oriente = 0; int nb_arcs; int nb_sommets; tNomSommet nom_sommet1; tNomSommet nom_sommet2; tNomSommet nom_sommet3; tArc arc; /* on va cr´eer un fichier pour graphviz, dans le fichier "outfile".dot */ strcpy(dotfile, outfile); strcat(dotfile, ".dot"); fic = fopen(dotfile, "w"); if (fic==NULL) halt ("Ouverture du fichier %s en ´ecriture impossible\n", dotfile); is_oriente = grapheEstOriente(graphe); nb_arcs = grapheNbArcs(graphe); nb_sommets = grapheNbSommets(graphe); if(is_oriente) fprintf(fic, "digraph {\n"); else fprintf(fic, "graph {\n"); for(i = 0; i < nb_sommets; i++){ grapheRecupNomSommet(graphe, i, nom_sommet3); if(tabCouleurs[i] == VERT) fprintf(fic, "%s [color=green];\n", nom_sommet3); if(tabCouleurs[i] == BLEU) fprintf(fic, "%s [color=blue];\n", nom_sommet3); if(tabCouleurs[i] == ROUGE) fprintf(fic, "%s [color=red];\n", nom_sommet3); } for(i = 0; i < nb_arcs; i++){ arc = grapheRecupArcNumero(graphe, i); grapheRecupNomSommet(graphe, arc.orig, nom_sommet1); grapheRecupNomSommet(graphe, arc.dest, nom_sommet2); if(is_oriente) fprintf(fic," %s -> %s\n",nom_sommet1, nom_sommet2); else fprintf(fic," %s -- %s\n", nom_sommet1, nom_sommet2); } fprintf(fic, "}\n"); /**/ fclose(fic); sprintf(commande, "dot -Tps %s -o %s", dotfile, outfile); ret = system(commande); if (ret == -1) halt("La commande suivante a echouée : %s\n", commande); }
void graphe2visu (tGraphe graphe, char* outfile) { FILE *fic; /* le fichier .dot dan lequel on va ecrire*/ char commande[80]; /* la commande appeller par sprintf plus bas */ char dotfile[80]; /* le fichier dot pour creer le ps */ int nb_sommet, i, j, nb_successeur, ret; tNomSommet origine; tNomSommet destination; /* on va creer un fichier pour graphviz, dans le fichier "outfile".dot */ strcpy(dotfile, outfile); strcat(dotfile, ".dot"); /* on ouvre le fichier .dot en ecriture */ fic = fopen(dotfile, "w"); if (fic==NULL) halt("Ouverture du fichier %s en ecriture impossible\n", dotfile); /* on recupere le nombre de sommet du graphe*/ nb_sommet = grapheNbSommets(graphe); if (grapheEstOriente(graphe)) { fprintf(fic, "digraph {\n"); /* digraph pour oriente */ for (i=0; i<nb_sommet;i++) { /* on recupere le nb de successeur du sommet*/ nb_successeur = grapheNbSuccesseursSommet(graphe,i); for (j=0;j<nb_successeur;j++) { /* on recupere le nom du sommet i et on le stock dans la variable origine */ grapheRecupNomSommet(graphe,i,origine); /* on recupere le nom du successeur j du sommet i et on le stock dans la variable destination*/ grapheRecupNomSommet(graphe,grapheSuccesseurSommetNumero(graphe,i,j),destination); /* -> pour oriente*/ fprintf(fic, "\t%s -> %s;\n", origine, destination); } } fprintf(fic, "}"); } else { fprintf(fic, "graph {\n"); /* graph pour non oriente */ for (i=0; i<nb_sommet;i++) { /* on recupere le nb de voisin du sommet*/ nb_successeur = grapheNbVoisinsSommet(graphe,i); for (j=0;j<nb_successeur;j++) { /* on recupere le nom du sommet i et on le stock dans la variable origine */ grapheRecupNomSommet(graphe,i,origine); /* on recupere le nom du voisin j du sommet i et on le stock dans la variable destination*/ grapheRecupNomSommet(graphe,grapheVoisinSommetNumero(graphe,i,j),destination); /* -- pour non oriente*/ if (strcmp(origine, destination) < 0) fprintf(fic, "\t%s -- %s;\n", origine, destination); } } fprintf(fic, "}"); } fclose(fic); /* on stock dans commande la commande qui permet a partir d'un .dot de creer un .ps*/ sprintf(commande, "dot -Tps %s -o %s.ps", dotfile, outfile); /* on appelle la fonction stocker dans commande */ ret = system(commande); if (WEXITSTATUS(ret)) halt("La commande suivante a echoue \n%s\n", commande); }
/*calcule les tableaux d et pred*/ void plus_courte_chaine(tGraphe graphe, tNomSommet s) { /*creation de file et on la libere*/ tFileSommets file; file = fileSommetsAlloue(); fileSommetsLibere(file); int d[MAX_SOMMETS]; tNumeroSommet pred [MAX_SOMMETS]; tCouleur tTabCouleurs[MAX_SOMMETS]; tNomSommet nomSommet; int nbSommets = grapheNbSommets(graphe); /*colorier en bleu tous les sommets sauf s*/ while(i<nbSommets){ grapheRecupNomSommet(graphe,i,nomSommet); if (nomSommet != s){ tTabCouleurs[i] = 1; } else { tTabCouleurs[i] = 2; } i++; } /*d(s) =0 et pred(s) est indéfini*/ d[s] = 0; pred[s]=-1; /*colorier s en vert et enfiler*/ tNumeroSommet indexSommetS = grapheChercheSommetParNom(graphe,s); tTabCouleurs[indexSommetS] = 2; printf("Couleur du sommet S avant enfilage : %s\n",couleur_sommet(tTabCouleurs[indexSommetS])); fileSommetsEnfile(file,indexSommetS); while (!fileSommetsEstVide(file)){ tNumeroSommet x; x = fileSommetsDefile(file); if(grapheNbVoisinsSommet(graphe,x) != 0){ for (int j = 0; j < grapheNbVoisinsSommet(graphe,x);j++){ tNumeroSommet y; y = grapheVoisinSommetNumero(graphe,x,j); if (tTabCouleurs[y] == 1){ tTabCouleurs[y] = 2; fileSommetsEnfile(file, y); } } } /*faire d(y)=d(x)+1 et pred(y) := x*/ /*colorier x en rouge*/ tTabCouleurs[x] = 0; } /*affichage du plus court chemin*/ }
void affiche_parcours_largeur(tGraphe graphe, tNumeroSommet num_sommet) { tFileSommets file; tNomSommet nom_sommet; tNumeroSommet current_voisin; tTabCouleurs tab_couleurs; int i, nb_sommets, voisins; char *argv[3]; argv[0] = "evince"; argv[1] = output; argv[2] = NULL; switch(fork()) { case -1: perror("fork"); exit(EXIT_FAILURE); case 0 : execvp(argv[0], argv); halt("Error evince"); exit(EXIT_FAILURE); default: file = fileSommetsAlloue(); nb_sommets = grapheNbSommets(graphe); if(verbose) printf("Nb sommets : %d\n", nb_sommets); for(i = 0 ; i < nb_sommets; i++) { tab_couleurs[i] = BLEU; } fileSommetsEnfile(file, num_sommet); tab_couleurs[num_sommet] = VERT; grapheRecupNomSommet(graphe, num_sommet, nom_sommet); printf("Sommet %s empilé\n", nom_sommet); graphe2visu(graphe, output, tab_couleurs); sleep(2); while(!fileSommetsEstVide(file)) { voisins = grapheNbVoisinsSommet(graphe, num_sommet); if(verbose) printf("Nb voisins : %d\n", voisins); for(i = 0; i < voisins; i++) { current_voisin = grapheVoisinSommetNumero(graphe, num_sommet, i); if(tab_couleurs[current_voisin] == BLEU ) { fileSommetsEnfile(file, current_voisin); tab_couleurs[current_voisin] = VERT; grapheRecupNomSommet(graphe, current_voisin, nom_sommet); printf("Sommet %s empilé\n", nom_sommet); graphe2visu(graphe, output, tab_couleurs); sleep(2); } if(verbose) printf("voisin num %d\n", current_voisin); } tab_couleurs[num_sommet] = ROUGE; num_sommet = fileSommetsDefile(file); grapheRecupNomSommet(graphe, num_sommet, nom_sommet); if(verbose) printf("Sommet %s depilé\n", nom_sommet); graphe2visu(graphe, output, tab_couleurs); sleep(2); } fileSommetsLibere(file); wait(NULL); } }