Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "merci.h"
- graphs initialisation_graphe()
- {
- graphs graphique;
- int tmps;
- printf("\n Souhaitez vous que je graphs soit un graphs oriente ? \n");
- printf("tapez 1 pour oui \n");
- printf("Tapez 2 pour Non \n");
- scanf("%d",&tmps);
- graphique.type = tmps;
- do
- {
- printf("Combien de noeuds possede le graphique ? ( Max 25) : ");
- scanf("%d",&tmps);
- }while(tmps >25 || tmps < 0 );
- graphique.degres = tmps;
- graphique.sommet = malloc(sizeof(char)*tmps);
- for (int i = 0 ; i < tmps ; i++)
- {
- fflush(stdin);
- printf("Entrez la valeur du sommet n %d : ",i);
- scanf("%c",&graphique.sommet[i]);
- printf(" La valeur du sommets vaut %c \n",graphique.sommet[i]);
- }
- printf("combien de lien possede le graph ? :");
- scanf("%d",&tmps);
- graphique.nombreLiens = tmps;
- graphique.liens = malloc(sizeof(char)*tmps);
- for(int i = 0 ; i < tmps ;i ++)
- {
- graphique.liens[i] = malloc(sizeof(char)*3);
- }
- int j = 0 ;
- for(int i = 0 ; i< tmps ;i++)
- {
- printf("point de depart du lien : ");
- fflush(stdin);
- scanf(" %c",&graphique.liens[i][j]);
- printf("point d'arrive du lien : ");
- fflush(stdin);
- scanf(" %c",&graphique.liens[i][j+1]);
- printf("poids du lien : ");
- fflush(stdin);
- scanf(" %c",&graphique.liens[i][j+2]);
- }
- return graphique;
- }
- int isInGraph(graphs graphique, char som)
- {
- for(int i = 0; i <graphique.degres; i++)
- {
- if(graphique.sommet[i] == som)
- {
- return 1;
- }
- }
- return 0;
- }
- char* voisin_niveau_1(graphs graphiques, char sommets)
- {
- char *voisin;
- voisin = malloc(sizeof(char)*graphiques.degres);
- int nbvoisin = 0;
- do
- {
- printf("\n de quel sommet voulez vous connaitre les voisin : ");
- fflush(stdin);
- scanf(" %c",&sommets);
- }while(isInGraph(graphiques,sommets) == 0);
- printf(" les voisin du sommets %c sont : ",sommets);
- for(int i = 0; i < graphiques.nombreLiens;i++)
- {
- if(graphiques.liens[i][0] == sommets && graphiques.type == 1)
- {
- printf(" %c",graphiques.liens[i][1]);
- voisin[nbvoisin] = graphiques.liens[i][1];
- nbvoisin+=1;
- }
- if(graphiques.liens[i][0] == sommets && graphiques.type == 2)
- {
- printf(" %c",graphiques.liens[i][1]);
- voisin[nbvoisin] = graphiques.liens[i][1];
- nbvoisin+=1;
- }
- if(graphiques.liens[i][1] == sommets && graphiques.type == 2)
- {
- printf(" %c, ",graphiques.liens[i][0]);
- voisin[nbvoisin] = graphiques.liens[i][0];
- nbvoisin+=1;
- }
- }
- printf("\n");
- return voisin;
- }
- int parcours_profondeur(graphs graphique,char sommet,int vis)
- {
- char* visit = malloc(sizeof(char)*graphique.degres);
- char* voisinage = malloc(sizeof(char)*graphique.degres);
- printf("%c",sommet);
- voisinage = voisin_niveau_1(graphique,sommet);
- for (int i = 0; i < strlen(voisinage);i++)
- {
- if(strchr(visit,voisinage[i]) == NULL)
- {
- visit[vis] = voisinage[i];
- parcours_profondeur(graphique,voisinage[i],vis+1);
- }
- else
- {
- return 0;
- }
- }
- }
- int parcours_largeur(graphs graphique, char sommet,int vis){
- char * visit = malloc(sizeof(char)*graphique.degres);
- char* voisinage = malloc(sizeof(char)*graphique.degres);
- printf("%c",sommet);
- //voisinage = ;
- }
- int main()
- {
- while(1)
- {
- int choix = 0;
- graphs graphset;
- char sommets;
- printf("[1]Initialisation du graphe\n");
- printf("[2]Determiner les voisins directs d'un sommet\n");
- printf("[3]Parcours en profondeur d'un graph oriente \n");
- printf("[4]Parcours en profondeur d'un graph oriente (en cours) \n");
- printf("[5]Quitter le programme\n");
- scanf("%d", &choix);
- switch(choix)
- {
- case 1:
- graphset = initialisation_graphe();
- break;
- case 2:
- do
- {
- printf("\n De quel sommet voulez-vous connaitre les voisin :\n ");
- fflush(stdin);
- scanf(" %c",&sommets);
- }while(isInGraph(graphset,sommets) == 0);
- voisin_niveau_1(graphset,sommets);
- break;
- case 3 :
- parcours_profondeur(graphset,graphset.sommet[0],0);
- printf("\n");
- break;
- case 4 :
- parcours_largeur(graphset,graphset.sommet[0],0);
- break;
- case 5 :
- return 1;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement