Path: blob/master/sage/graphs/modular_decomposition/src/random.c
4045 views
/******************************************************12Copyright 2004, 2010 Fabien de Montgolfier3[email protected]45This program is free software; you can redistribute it and/or6modify it under the terms of the GNU General Public License7as published by the Free Software Foundation; either version 28of the License, or (at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program; if not, write to the Free Software17Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.1819**********************************************************/2021#include <time.h>22#include "dm_english.h"2324#define NIV 2025#define VERBEUX 12627//extern noeud *decomposition_modulaire(int *,int);28extern void printarbre(noeud *);29/* ppm est la part par million d'arretes voulues dans le graphe */3031void compte(noeud *N, int level, int *C)32{33fils *F;34switch(N->type)35{36case SERIE: C[4*level]++; break;37case PARALLELE: C[4*level+1]++; break;38case PREMIER: C[4*level+2]++; break;39case FEUILLE: C[4*level+3]++; break;40}41if(N->type!=FEUILLE)42for(F=N->fils;F!=NULL;F=F->suiv)43compte(F->pointe, level+1, C);44}4546void test(int n, long int ppm, int *C)47{48/*genere un graphe aleatoire, l'affiche, le decompose*/49graphe G;5051int i,j;52adj *a;53noeud *R;545556srandom((unsigned)time(NULL));5758G.n=n;59G.G=(adj **)malloc(n*sizeof(adj *));6061for(i=0;i<n;i++)62G.G[i]=NULL;6364printf("Programme test : generation d'un graphe aleatoire de %i sommets\n",n);65for(i=0; i<n; i++)66{67if(VERBEUX)68{69printf("Ajacence de %i: ",i+1);70for(a=G.G[i]; a!=NULL; a=a->suiv)71printf("%i ",1+a->s);72}73for(j=i+1;j<n;j++)74{75if( (random()%1000000L) < ppm )76{77// ajoute j a l'adjacence de i78a=(adj *)malloc(sizeof(adj));79a->s=j;80a->suiv=G.G[i];81G.G[i]=a;82// et reciproquement83a=(adj *)malloc(sizeof(adj));84a->s=i;85a->suiv=G.G[j];86G.G[j]=a;87if(VERBEUX)88printf("%i ",j+1);89}90}91if(VERBEUX)92printf("\n");93}9495// appel de la fonction de decomposition96R = decomposition_modulaire(G);9798// affichage de l'arbre99if(VERBEUX)100printarbre(R);101102compte(R,0,C);103printf("Statistiques sur l'arbre de decomposition:\n");104if(C[0])105printf("La racine est Serie\n");106else if(C[1])107printf("La racine est Parrallele\n");108else109printf("La racine est Premier \n");110for(i=1 ; i<NIV ; i++)111{112printf("Niveau %i: %i modules (S-P-Pr= %i - %i - %i) et %i feuilles\n",i,113C[4*i]+C[4*i+1]+C[4*i+2], C[4*i], C[4*i+1], C[4*i+2],C[4*i+3]);114if(i<NIV-1 && C[4*i+4]+C[4*i+5]+C[4*i+6]+C[4*i+7]==0)115break;116}117printf("\n");118}119120int main(int narg, char **arg)121{122int n;123long ppm;124int C[4*NIV];int i;125126if(narg!=3)127{128printf("Decomposition modulaire de graphes \"aleatoires\" \n"129"Donnez en argument:\n"130"le nombre de sommets du graphe\n"131"puis la proportion d'aretes en en millioniemes \n"132"(generateur aleatoire tres primaire)\n"133"Exemple : %s 100 20000\n",arg[0]);134exit(0);135}136n=atoi(arg[1]);137ppm=atol(arg[2]);138for(i=0;i<4*NIV;i++) C[i]=0;139140test(n, ppm, C);141142return 0;143}144145146147148149150151