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);
  }
}
示例#2
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);
}
示例#3
0
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);
}
示例#4
0
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);
}
/*
* Affiche les indices trouver
*/
void print_i(tGraphe graphe, int i[MAX_SOMMETS], int nSommet){
  tNomSommet nomSommet;

  for (size_t y = 0; y < nSommet; y++) {
    grapheRecupNomSommet(graphe, y, nomSommet);
    printf("%s: %d\n", nomSommet, i[y]);
  }
}
示例#6
0
文件: graphe.c 项目: LoicFabre/ARO
void grapheAjouteArc(tGraphe graphe, tArc arc) {
  tNumeroSommet orig, dest;
  tNomSommet nom_orig, nom_dest;
  
  orig = arc.orig;
  dest = arc.dest;

  /* On teste si l'arc existe déjà ou pas */
  if (grapheExisteArcEntre(graphe, orig, dest)) {
    grapheRecupNomSommet(graphe, orig, nom_orig);
    grapheRecupNomSommet(graphe, dest, nom_dest);
    halt("Un arc existe deja  entre les sommets %s (numero %d) "
	 "et %s (numero %d)\n",
	 nom_orig, orig, nom_dest, dest);
  }


  /* la matrice d'adjacence */
  graphe->matriceAdjacence[orig][dest] = arc.val;
  if (!grapheEstOriente(graphe)) {
    graphe->matriceAdjacence[dest][orig] = arc.val;
  }

  if (grapheEstOriente(graphe)) {
    /* ajout dans les successeurs */
    tableauEntiersAjoute(& (graphe->tableSuccesseurs[orig]), dest);
    /* ajout dans les prédécesseurs */
    tableauEntiersAjoute(& (graphe->tablePredecesseurs[dest]), orig);
  } 

  /* Même si l'arbre est orienté, on remplit la table des voisins.
     Toutefois, si l'arc est orienté, on vérifie que l'arc
     inverse n'existe pas déjà , car dans ce cas les voisins sont
     déjà placés. */
  
  if ( ! ( grapheEstOriente(graphe) && 
	   grapheExisteArcEntre(graphe,dest,orig) ) ) {
      tableauEntiersAjoute(& (graphe->tableVoisins[orig]), dest);
      /* si l'arc va du sommet vers lui même, on n'ajoute pas de voisin */
      if (orig!=dest)
	tableauEntiersAjoute(& (graphe->tableVoisins[dest]), orig);
    }
  /* Mise à jour de la table des arcs */
  tableauArcsAjoute(& (graphe->tableArcs), arc);
}
示例#7
0
/* 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");
}
/*
* Boucle principal
*/
void traitement_pile(tPileSommets pile, tNumeroSommet xSommet, int i[MAX_SOMMETS], tGraphe graphe, tTabCouleurs couleurs){
  tNumeroSommet iSucc;
    tNomSommet nomSommet;
      tNomSommet nomSommet2;
  int trouver = 0;
  int nSucc =0;
  int j =0;
  i[xSommet] = i[xSommet] + 1;
  nSucc = grapheNbSuccesseursSommet(graphe, xSommet);
  while(i[xSommet] <= nSucc && trouver == 0){
    iSucc = grapheSuccesseurSommetNumero(graphe, xSommet, j);
      grapheRecupNomSommet(graphe, iSucc, nomSommet);
      grapheRecupNomSommet(graphe, xSommet, nomSommet2);
    printf("sommet x %s\n", nomSommet2);
      printf("succ du sommet x %s\n", nomSommet);
    printf("sommet %d\n", i[xSommet]);
    printf("couleur i %d\n",couleurs[iSucc] );
    printf("BLEU = %d\n", BLEU);
    printf("-----------------------------\n");
    if(couleurs[iSucc] == BLEU){
      trouver = 1;
    }
    else{
      i[xSommet] = i[xSommet] +1;
      j++;
    }
  }
  if(trouver == 1){
    couleurs[iSucc] = VERT;
    printf("empile \n");
    pileSommetsEmpile(pile, iSucc);
  }
  else{
    couleurs[xSommet] = ROUGE;
    printf("depile \n");
    pileSommetsDepile(pile);
  }
}
示例#9
0
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);
	}
    }  
}
示例#10
0
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);
}
示例#11
0
/* 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);
}
示例#12
0
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;
}
示例#13
0
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);
  
  
}
示例#14
0
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);
}
示例#15
0
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);
}
示例#16
0
/* 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);
}
示例#17
0
/*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*/


}
示例#18
0
文件: graphe.c 项目: LoicFabre/ARO
/* 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);
  }
  
}
示例#19
0
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);
    }



}