-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.cpp
261 lines (237 loc) · 5.86 KB
/
generate.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "com.h"
#include "generate.h"
template<typename Container>
bool exist_in(const Container& c,const typename Container::value_type& v){
return (c.end() != std::find(c.begin(),c.end(),v) );
}
void ba(int size,int new_add,std::map<int,std::vector<int> >& adjs){
if(adjs.empty()){
std::cerr<<"empty graph "<<std::endl; std::exit(1);
}
while(adjs.size()<size){
std::cout<<adjs.size()<<std::endl;
int m0=adjs.size();
std::vector<int> new_adj;//id=adj.size()=m0
int sum=0;
for(int k=0;k<adjs.size();k++){
sum+=adjs[k].size();
}
std::vector<double> p(adjs.size());
for(int k=0;k<adjs.size();k++){
p[k]=double(adjs[k].size())/double(sum);
}
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> u(0.0,1.0);
for(int m=0;m<new_add;m++){
double rand=u(mt);
int k=0;
std::vector<double>::iterator itr=p.begin();
for(itr=p.begin();itr!=p.end();++itr){
rand-=*itr;
if(rand<=0){
break;
}
k++;
}
adjs[k].push_back(m0);
new_adj.push_back(k);
p[k]=0.0;
}
p.clear();
adjs[m0]=new_adj;
}
}
void complete(int size,std::map<int,std::vector<int> >& adjs){
for(int i=0;i<size;i++){
std::vector<int> adj;//id=i
for(int j=0;j<i;j++){
adj.push_back(j);
}
for(int j=i+1;j<size;j++){
adj.push_back(j);
}
adjs[i]=adj;
}
}
void er(int size, double p,std::vector<std::pair<int,int> >& edges){
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> u(0.0,1.0);
for(int i=0;i<size;i++){
for(int j=0;j<i;j++){
if(u(mt)<p){
std::pair<int,int> edge;
edge.first=i;
edge.second=j;
edges.push_back(edge);
}
}
for(int j=i+1;j<size;j++){
if(u(mt)<p){
std::pair<int,int> edge;
edge.first=i;
edge.second=j;
edges.push_back(edge);
}
}
}
}
void ws(int size,int mean_degree,double beta,std::map<int,std::vector<int> >& adjs){
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> u(0.0,1.0);
//construct regular ring lattice
for(int i=0;i<size;i++){
std::vector<int> adj;//adj[i]
for(int j=i-int(mean_degree/2);j<i;j++){
int j_mod_size=j;
if(j<0){
j_mod_size+=size;
}
adj.push_back(j_mod_size);
}
for(int j=i+1;j<=i+int(mean_degree/2);j++){
int j_mod_size=j;
if(j>=size){
j_mod_size-=size;
}
adj.push_back(j_mod_size);
}
adjs[i]=adj;
}
for(int i=0;i<size;i++){
for(int j=0;j<adjs.at(i).size();j++){
double rand;
int rewire;
rand=u(mt);
if(rand<beta){
do{
rewire=int(u(mt)*size);
}while(rewire==i);
if(!exist_in(adjs.at(i),rewire)){
int k=adjs.at(i)[j];
adjs.at(i)[j]=rewire;
auto itr=std::find(adjs.at(k).begin(),adjs.at(k).end(),i);
//int index=std::distance(adj[k].begin(),itr);
adjs.at(k).erase(itr);
adjs.at(rewire).push_back(i);
}
}
}
}
}
/*
void ws(int size,int mean_degree,double beta,std::vector<std::pair<int,int> >& edges){
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> u(0.0,1.0);
//construct regular ring lattice
for(int i=0;i<size;i++){
for(int j=i-int(mean_degree/2);j<i;j++){
std::pair<int,int> edge;
int j_mod_size=j;
if(j<0){
j_mod_size+=size;
}
edge.first=i;
edge.second=j_mod_size;
edges.push_back(edge);
}
for(int j=i+1;j<=i+int(mean_degree/2);j++){
std::pair<int,int> edge;
int j_mod_size=j;
if(j>=size){
j_mod_size-=size;
}
edge.first=i;
edge.second=j_mod_size;
edges.push_back(edge);
}
}
for(auto edge: edges){
if(u(mt)<beta){
int random=int(u(mt)*size);
edge.second=random;
}
}
}
*/
//edge list/adjacency list converter
void edge_to_adj(std::vector<std::pair<int,int> > edges,std::map<int,std::vector<int> >& adjs,bool directed){
for(auto edge: edges){
adjs[edge.first].push_back(edge.second);
if(directed==false){
adjs[edge.second].push_back(edge.first);
}
}
}
void adj_to_edge(std::map<int,std::vector<int> > adjs,std::vector<std::pair<int,int> >& edges){
for(auto adj: adjs){
for(auto itr=adj.second.begin();itr!=adj.second.end();itr++){
std::pair<int,int> pair;
pair.first=adj.first;
pair.second=*itr;
edges.push_back(pair);
}
}
}
//symmetric list for undirected graph
void symmetrize(std::map<int,std::vector<int> >& adjs){
for(int i=0;i<adjs.size();i++){
for(int j=0;j<adjs.at(i).size();j++){
int k=adjs.at(i)[j];
if(!exist_in(adjs.at(k),i)){
adjs[k].push_back(i);
}
}
}
}
void adjs_to_adj(std::map<int,std::vector<int> > adjs, std::vector<std::vector<int> >& adj){
std::vector<std::vector<int> > adj0(adjs.size());
for(auto node=adjs.begin();node!=adjs.end();++node){
adj0[node->first]=node->second;
}
adj=adj0;
}
void adj_to_adjs(std::vector<std::vector<int> > adj,std::map<int,std::vector<int> >& adjs){
for(int i=0;i<adj.size();i++){
adjs[i]=adj[i];
}
}
void simplify(std::vector<std::pair<int,int> >& edges){
std::vector<std::pair<int,int> > edges_simplified;
std::vector<std::pair<int,int> >::iterator itr;
for(itr=edges.begin();itr!=edges.end();++itr){
std::pair<int,int> pair;
if(itr->first==itr->second || (edges_simplified.end() != std::find(edges_simplified.begin(),edges_simplified.end(),*itr)) ){
continue;
}
pair.first=itr->first;
pair.second=itr->second;
edges_simplified.push_back(pair);
}
edges.clear();
edges=edges_simplified;
}
void cout_edges(std::vector<std::pair<int,int> > edges){
for(auto edge: edges){
std::cout<<edge.first<<" "<<edge.second<<std::endl;
}
}
void cout_adjs(std::map<int,std::vector<int> > adjs){
for(auto itr=adjs.begin();itr!=adjs.end();itr++){
std::cout<<itr->first<<" ";
for(auto nbd: itr->second){
std::cout<<nbd<<" ";
}
std::cout<<std::endl;
}
}
std::map<int,int> degree_dist(std::map<int,std::vector<int> > adjs){
std::map<int,int> degree_dist;
for(auto node: adjs){
degree_dist[node.second.size()]+=1;
}
return degree_dist;
}