1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct { //graphe représenté par une matrice d'adjacence
int nombre_sommet;
int ** matrice_adjacence;
int ** temps;
} graphe;
void affiche_graphe(graphe G){
int i,j;
printf("Graphe avec %d sommets \n",G.nombre_sommet);
for(i = 0; i<G.nombre_sommet; i++){
printf("Voisins de %d: ",i);
for(j = 0; j < G.nombre_sommet; j++){
if(G.matrice_adjacence[i][j]) printf("%d ",j);
}
printf("\n");
}
}
graphe init_graphe(int n){//créé un graphe dont tous les sommets sont isolés
graphe G ; int i;
G.nombre_sommet=n;
G.matrice_adjacence=malloc(sizeof(int*));
for(i=0; i<n; i++) G.matrice_adjacence[i]=calloc(n,sizeof(int));
return G;
}
graphe alea_graphe(int n, float p){
graphe G=init_graphe(n);
for(int i=0; i<n-2; i++){
for(int j=0; j<n-1; j++){
if(rand()%100 <p*100)
{G.matrice_adjacence[i][j]=1;
G.matrice_adjacence[j][i]=1;
int t=rand()%30;
G.temps[i][j]=t;
G.temps[j][i]=t;
}
}
}
return G;
}
int main(){
srand(time(NULL));
graphe L;
L=alea_graphe(10, 0.2);
affiche(L);
} |
Partager