void plus_courte_chaine (tGraphe graphe, tNomSommet s, int d[], tNumeroSommet pred[]) { int i; tTabCouleurs tabCouleurs; tFileSommets file; tNumeroSommet numSommetx, numSommety; file = fileSommetsAlloue(); for(i=0; i<graphe->nbSommets; i++){ tabCouleurs[i]=BLEU; } while(!(fileSommetsEstVide(file))){ fileSommetsDefile(file); } numSommetx = grapheChercheSommetParNom(graphe,s); numSommety = grapheChercheSommetParNom(graphe,s); d[numSommetx] = 0; tabCouleurs[numSommetx] = VERT; fileSommetsEnfile(file, numSommetx); while(!(fileSommetsEstVide(file))){ numSommetx = fileSommetsDefile(file); for(i=0; i<grapheNbVoisinsSommet(graphe,numSommetx); i++){ numSommety = grapheVoisinSommetNumero(graphe,numSommetx,i); if(tabCouleurs[numSommety]==BLEU){ tabCouleurs[numSommety]=VERT; fileSommetsEnfile(file, numSommety); d[numSommety] = d[numSommetx] + 1; printf("Incr d[%i]\n", numSommety); pred[numSommety] = numSommetx; } tabCouleurs[numSommetx]=ROUGE; } } fileSommetsLibere(file); }
/* Modification */ void grapheAjouteSommet(tGraphe graphe, tNomSommet sommet) { if (graphe->nbSommets==MAX_SOMMETS) { halt("Nombre de sommet maximum (=%d) atteint", MAX_SOMMETS); }; if (grapheChercheSommetParNom(graphe, sommet)>=0) halt("Sommet deja existant %s", sommet); strcpy(graphe->nomSommets[graphe->nbSommets], sommet); graphe->nbSommets ++ ; }
static void grapheAjouteArcNoms(tGraphe graphe, tNomSommet orig, tNomSommet dest, tValeurArc val) { tNumeroSommet i_orig, i_dest; tArc arc; i_orig = grapheChercheSommetParNom(graphe, orig); if ((i_orig==-1)) halt("Sommet inexistant : %s\n", orig); i_dest = grapheChercheSommetParNom(graphe, dest); if ((i_dest==-1)) halt("Sommet inexistant : %s\n", dest); arc.orig = i_orig; arc.dest = i_dest; arc.val = val; grapheAjouteArc(graphe, arc); }
int main(int argc, char *argv[]){ tGraphe graphe; int d[MAX_SOMMETS]; tNumeroSommet pred[MAX_SOMMETS], numSommet; graphe = grapheAlloue(); grapheChargeFichier(graphe, argv[1]); plus_courte_chaine(graphe, argv[2],d,pred); numSommet = grapheChercheSommetParNom(graphe,argv[2]); printf("%d [%d]\n",d[numSommet], numSommet); for(int i=0; i<grapheNbVoisinsSommet(graphe,numSommet); i++){ printf("d[%d] = %d \n",i, d[i]); } grapheLibere(graphe); }
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); } }
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); }
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; }
/*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*/ }