3 Fevrier
This commit is contained in:
parent
f7b8416cf3
commit
f2be2729c2
165
Graphes/TP1:CodageDeGraphes/Exercice1.c
Normal file
165
Graphes/TP1:CodageDeGraphes/Exercice1.c
Normal file
@ -0,0 +1,165 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <graph.h>
|
||||
|
||||
struct graphe{
|
||||
int ordre; /*l'ordre du graphe*/
|
||||
int** adj; /*la matrice d'adjacence, donnée par un double tableau dynamique*/
|
||||
int oriente; /*vaut 0 si le graphe est non orienté, 1 sinon*/
|
||||
};
|
||||
|
||||
typedef struct graphe graphe;
|
||||
|
||||
graphe creergraphe(int ord,int or){
|
||||
int i;
|
||||
int j;
|
||||
graphe NouvGraphe;
|
||||
NouvGraphe.ordre=ord;
|
||||
NouvGraphe.oriente=or;
|
||||
NouvGraphe.adj=calloc(ord,sizeof(int*));
|
||||
for (i=0;i<ord;i++){
|
||||
NouvGraphe.adj[i]=calloc(ord,sizeof(int));
|
||||
} for (i=0;i<ord;i++){
|
||||
for (j=0;j<ord;j++){
|
||||
NouvGraphe.adj[i][j]=0;
|
||||
}
|
||||
}
|
||||
return NouvGraphe;
|
||||
}
|
||||
|
||||
void ajoutArete(graphe g,int v,int w){
|
||||
if (g.oriente==0){
|
||||
g.adj[v][w]=1;
|
||||
g.adj[w][v]=1;
|
||||
} else{
|
||||
g.adj[v][w]=1;
|
||||
}
|
||||
}
|
||||
|
||||
void voisinage(graphe g,int s){
|
||||
int i;
|
||||
int voisin;
|
||||
if(g.oriente==0){
|
||||
printf("Les voisins du sommet %d sont:\n",s);
|
||||
for(i=0;i<g.ordre;i++){
|
||||
if(g.adj[i][s]==1){
|
||||
printf("%d\n",i);
|
||||
voisin=1;
|
||||
}
|
||||
} if(voisin!=1){
|
||||
printf("Il n'a pas de voisin.\n");
|
||||
}
|
||||
} else{
|
||||
printf("Les voisins entrants du sommet %d sont:\n",s);
|
||||
for(i=0;i<g.ordre;i++){
|
||||
if(g.adj[i][s]==1){
|
||||
printf("%d\n",i);
|
||||
voisin=1;
|
||||
}
|
||||
} if(voisin!=1){
|
||||
printf("Il n'en a pas.\n");
|
||||
} voisin=0;
|
||||
printf("Les voisins sortants du sommet %d sont:\n",s);
|
||||
for(i=0;i<g.ordre;i++){
|
||||
if(g.adj[s][i]==1){
|
||||
printf("%d\n",i);
|
||||
voisin=1;
|
||||
}
|
||||
} if(voisin!=1){
|
||||
printf("Il n'en a pas.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void nbrVoisin(graphe g,int s){
|
||||
int i;
|
||||
int nbrVoisin=0;
|
||||
int nbrVoisinEntrant=0;
|
||||
int nbrVoisinSortant=0;
|
||||
if(g.oriente==0){
|
||||
for(i=0;i<g.ordre;i++){
|
||||
if(g.adj[i][s]==1){
|
||||
nbrVoisin++;
|
||||
}
|
||||
} printf("Le sommet %d possede %d voisin(s)\n",s,nbrVoisin);
|
||||
} else{
|
||||
for(i=0;i<g.ordre;i++){
|
||||
if(g.adj[i][s]==1){
|
||||
nbrVoisinEntrant++;
|
||||
}
|
||||
} printf("Le sommet %d possede %d voisin(s) entrant(s)\n",s,nbrVoisinEntrant);
|
||||
for(i=0;i<g.ordre;i++){
|
||||
if(g.adj[s][i]==1){
|
||||
nbrVoisinSortant++;
|
||||
}
|
||||
} printf("Le sommet %d possede %d voisin(s) sortant(s)\n",s,nbrVoisinSortant);
|
||||
}
|
||||
}
|
||||
|
||||
void visuelGraphe(graphe g){
|
||||
#define M_PI 3.14159265358979323846
|
||||
int taille=1000;
|
||||
int origine=taille/2;
|
||||
int distance=4*origine/5;
|
||||
int tailleVert=taille/20;
|
||||
|
||||
InitialiserGraphique();
|
||||
CreerFenetre(10,10,taille,taille);
|
||||
|
||||
int i,j;
|
||||
int x,y;
|
||||
char* nV=malloc(2);
|
||||
*nV='0';
|
||||
*(nV+1)='\0';
|
||||
int* cX=calloc(g.ordre,sizeof(int));
|
||||
int* cY=calloc(g.ordre,sizeof(int));
|
||||
for(i=0;i<g.ordre;i++){
|
||||
x=(int) origine+distance*cos(2*M_PI*i/g.ordre);
|
||||
y=(int) origine+distance*sin(2*M_PI*i/g.ordre);
|
||||
cX[i]=x+tailleVert/2;
|
||||
cY[i]=y+tailleVert/2;
|
||||
RemplirArc(x,y,tailleVert,tailleVert,0,360);
|
||||
EcrireTexte(x,y,nV,2);
|
||||
(*nV)++;
|
||||
|
||||
}
|
||||
|
||||
for(i=0;i<g.ordre;i++){
|
||||
for(j=0;j<g.ordre;j++){
|
||||
if(g.adj[i][j]!=0){
|
||||
DessinerSegment(cX[i],cY[i],cX[j],cY[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Touche();
|
||||
FermerGraphique();
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
graphe Graphe;
|
||||
Graphe=creergraphe(7,0);
|
||||
ajoutArete(Graphe,0,1); /*0=France,1=Belgique*/
|
||||
ajoutArete(Graphe,0,2); /*2=Luxembourg*/
|
||||
ajoutArete(Graphe,0,3); /*3=Allemagne*/
|
||||
ajoutArete(Graphe,0,4); /*4=Suisse*/
|
||||
ajoutArete(Graphe,0,5); /*5=Italie*/
|
||||
ajoutArete(Graphe,0,6); /*6=Espagne*/
|
||||
ajoutArete(Graphe,1,2);
|
||||
ajoutArete(Graphe,1,3);
|
||||
ajoutArete(Graphe,1,4);
|
||||
ajoutArete(Graphe,2,3);
|
||||
ajoutArete(Graphe,2,4);
|
||||
ajoutArete(Graphe,3,4);
|
||||
voisinage(Graphe,0);
|
||||
nbrVoisin(Graphe,0);
|
||||
voisinage(Graphe,4);
|
||||
nbrVoisin(Graphe,4);
|
||||
voisinage(Graphe,5);
|
||||
nbrVoisin(Graphe,5);
|
||||
visuelGraphe(Graphe);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user