Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/graphs/modular_decomposition/src/random.c
4045 views
1
/******************************************************
2
3
Copyright 2004, 2010 Fabien de Montgolfier
4
[email protected]
5
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License
8
as published by the Free Software Foundation; either version 2
9
of the License, or (at your option) any later version.
10
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20
**********************************************************/
21
22
#include <time.h>
23
#include "dm_english.h"
24
25
#define NIV 20
26
#define VERBEUX 1
27
28
//extern noeud *decomposition_modulaire(int *,int);
29
extern void printarbre(noeud *);
30
/* ppm est la part par million d'arretes voulues dans le graphe */
31
32
void compte(noeud *N, int level, int *C)
33
{
34
fils *F;
35
switch(N->type)
36
{
37
case SERIE: C[4*level]++; break;
38
case PARALLELE: C[4*level+1]++; break;
39
case PREMIER: C[4*level+2]++; break;
40
case FEUILLE: C[4*level+3]++; break;
41
}
42
if(N->type!=FEUILLE)
43
for(F=N->fils;F!=NULL;F=F->suiv)
44
compte(F->pointe, level+1, C);
45
}
46
47
void test(int n, long int ppm, int *C)
48
{
49
/*genere un graphe aleatoire, l'affiche, le decompose*/
50
graphe G;
51
52
int i,j;
53
adj *a;
54
noeud *R;
55
56
57
srandom((unsigned)time(NULL));
58
59
G.n=n;
60
G.G=(adj **)malloc(n*sizeof(adj *));
61
62
for(i=0;i<n;i++)
63
G.G[i]=NULL;
64
65
printf("Programme test : generation d'un graphe aleatoire de %i sommets\n",n);
66
for(i=0; i<n; i++)
67
{
68
if(VERBEUX)
69
{
70
printf("Ajacence de %i: ",i+1);
71
for(a=G.G[i]; a!=NULL; a=a->suiv)
72
printf("%i ",1+a->s);
73
}
74
for(j=i+1;j<n;j++)
75
{
76
if( (random()%1000000L) < ppm )
77
{
78
// ajoute j a l'adjacence de i
79
a=(adj *)malloc(sizeof(adj));
80
a->s=j;
81
a->suiv=G.G[i];
82
G.G[i]=a;
83
// et reciproquement
84
a=(adj *)malloc(sizeof(adj));
85
a->s=i;
86
a->suiv=G.G[j];
87
G.G[j]=a;
88
if(VERBEUX)
89
printf("%i ",j+1);
90
}
91
}
92
if(VERBEUX)
93
printf("\n");
94
}
95
96
// appel de la fonction de decomposition
97
R = decomposition_modulaire(G);
98
99
// affichage de l'arbre
100
if(VERBEUX)
101
printarbre(R);
102
103
compte(R,0,C);
104
printf("Statistiques sur l'arbre de decomposition:\n");
105
if(C[0])
106
printf("La racine est Serie\n");
107
else if(C[1])
108
printf("La racine est Parrallele\n");
109
else
110
printf("La racine est Premier \n");
111
for(i=1 ; i<NIV ; i++)
112
{
113
printf("Niveau %i: %i modules (S-P-Pr= %i - %i - %i) et %i feuilles\n",i,
114
C[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]);
115
if(i<NIV-1 && C[4*i+4]+C[4*i+5]+C[4*i+6]+C[4*i+7]==0)
116
break;
117
}
118
printf("\n");
119
}
120
121
int main(int narg, char **arg)
122
{
123
int n;
124
long ppm;
125
int C[4*NIV];int i;
126
127
if(narg!=3)
128
{
129
printf("Decomposition modulaire de graphes \"aleatoires\" \n"
130
"Donnez en argument:\n"
131
"le nombre de sommets du graphe\n"
132
"puis la proportion d'aretes en en millioniemes \n"
133
"(generateur aleatoire tres primaire)\n"
134
"Exemple : %s 100 20000\n",arg[0]);
135
exit(0);
136
}
137
n=atoi(arg[1]);
138
ppm=atol(arg[2]);
139
for(i=0;i<4*NIV;i++) C[i]=0;
140
141
test(n, ppm, C);
142
143
return 0;
144
}
145
146
147
148
149
150
151