-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.cpp
More file actions
161 lines (128 loc) · 4.22 KB
/
Network.cpp
File metadata and controls
161 lines (128 loc) · 4.22 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "Allvars.h"
#include "Prototypes.h"
void makeCIJ_degreefixed(void){
int i,j,row,column;
int trash,Newindex;
int warning;
gsl_ran_shuffle(rand_generator,Netwk.edges[1],Netwk.Nedges,sizeof(int));
for(i=0;i<Netwk.Nedges;i++){
row = Netwk.edges[0][i];
column = Netwk.edges[1][i];
if(Netwk.CIJ[row*Nc+column]==1 or row==column){
warning = 1;
// printf("row: %d column: %d i:%d\n",row,column, i );
while(1){
Newindex = (int)ceil(Netwk.Nedges*gsl_rng_uniform(rand_generator));
// Se verifica si el par de conexiones que resultan
// en la permutacion no existe
if ( !( Netwk.CIJ[ Netwk.edges[0][i]*Nc + Netwk.edges[1][Newindex] ] or
Netwk.CIJ[ Netwk.edges[0][Newindex]*Nc + Netwk.edges[1][i] ] ) )
{
// printf("i: %d Ni:%d , %d %d %d %d\n",i,Newindex,Netwk.edges[0][i],Netwk.edges[1][Newindex],Netwk.edges[0][Newindex],Netwk.edges[1][i] );
if( !( Netwk.edges[0][i] == Netwk.edges[1][Newindex] or
Netwk.edges[0][Newindex] == Netwk.edges[1][i] ) )
{
// printf("No Son iguales\n");
//Se crea la nueva conexion
Netwk.CIJ[ Netwk.edges[0][i]*Nc + Netwk.edges[1][Newindex] ] = 1;
// Si la conexion resultante del indice permutado ya existia
// y se habia creado, se elimina dicha conexion.
if(Newindex<i){
//Se elimina la antigua conexion
Netwk.CIJ[ Netwk.edges[0][Newindex]*Nc + Netwk.edges[1][Newindex] ] = 0;
// Se crea la otra conexion
Netwk.CIJ[ Netwk.edges[0][Newindex]*Nc + Netwk.edges[1][i] ];
}
// Se reescribe el vector de conexiones
trash = Netwk.edges[1][i];
Netwk.edges[1][i] = Netwk.edges[1][Newindex];
Netwk.edges[1][Newindex] = trash;
break;
}
}
warning = warning + 1;
if(warning>2*Netwk.Nedges*Netwk.Nedges){
printf(" ### Iteration limit was reached ###\n");
gsl_ran_shuffle(rand_generator,Netwk.edges[1],Netwk.Nedges,sizeof(int));
warning=1;
i=0;
printf(" ===== The program has been terminated ====\n");
exit(0);
}
}
}
else{
Netwk.CIJ[row*Nc+column]=1;
}
}
printf("*** Matrix was created with exit ***\n");
// Saving Matrix in an output file
SaveMatrix("AdjacencyMatrix.txt");
printf("*** Matrix was saved in file 'AdjacencyMatrix.txt' \n");
//Saving Edges in an ouput file
SaveEdges("Edges.txt");
printf("*** Table of edges was saved in file 'Edges.txt' \n");
}
void MakeCIJ_Fast(void){
int i,j,row,column;
// Permuting out-edge vector
gsl_ran_shuffle(rand_generator,Netwk.edges[1],Netwk.Nedges,sizeof(int));
//Creating Adjacency Matrix
for(i=0;i<Netwk.Nedges;i++){
row = Netwk.edges[0][i];
column = Netwk.edges[1][i];
Netwk.CIJ[row*Nc+column]+=1;
}
//---------------------------------------------------
//Eliminating autoloops and repeated edges
//---------------------------------------------------
for(i=0;i<Netwk.Nnodes;i++){
// Autoloops
if(Netwk.CIJ[i*Nc+i]>0){
Nloops_deleted = Netwk.CIJ[i*Nc+i] + Nloops_deleted;
Netwk.CIJ[i*Nc+i]=0;
}
for(j=0;j<Netwk.Nnodes;j++){
// repeated edges
if(Netwk.CIJ[i*Nc+j]>1){
Nrepeated_deleted = Netwk.CIJ[i*Nc+j] + Nrepeated_deleted - 1;
Netwk.CIJ[i*Nc+j] = 1;
}
}
}
printf("*** Matrix was created with exit***\n");
// Saving Matrix in an output file
SaveMatrix("./data/AdjacencyMatrix0.txt");
printf("*** Matrix was saved in file './data/AdjacencyMatrix0.txt' \n");
//Saving Edges in an ouput file
SaveEdges("./data/Edges.txt");
printf("*** Table of edges was saved in file 'Edges.txt' \n");
}
void SaveMatrix(char filename[]){
int i,j;
FILE *Thefile;
Thefile = fopen(filename,"w");
for(i=0;i<Netwk.Nnodes;i++){
for(j=0;j<Netwk.Nnodes;j++){
fprintf(Thefile,"%d ",Netwk.CIJ[i*Nc+j]);
}
fprintf(Thefile,"\n");
}
fclose(Thefile);
}
void SaveEdges(char filename[]){
int i,j;
FILE *Ofile;
Ofile = fopen(filename,"w");
for(i=0;i<Netwk.Nnodes;i++){
for(j=0;j<Netwk.Nnodes;j++){
if(Netwk.CIJ[i*Nc+j]==1){
fprintf(Ofile,"%d %d\n",i,j);
}
}
}
fclose(Ofile);
}