-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.hpp
355 lines (303 loc) · 10.5 KB
/
generator.hpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#pragma once
#include <iostream>
#include <vector>
#include <stack>
#include <set>
#include <random>
#include "graph.hpp"
#include "UnionFind.hpp"
class GraphGenerator
{
private:
static std::default_random_engine engine;
static std::uniform_real_distribution<double> uniform;
static bool is_connected(const Graph &g)
{
std::vector<bool> visited(g.num_vertices(), false);
std::stack<vertex_t> stack;
stack.push(0);
while (!stack.empty())
{
vertex_t v = stack.top();
visited[v] = true;
stack.pop();
for (auto &e : g.edges_from(v))
{
if (!visited[e.to])
{
visited[e.to] = true;
stack.push(e.to);
}
}
}
for (bool v : visited)
{
if (!v)
{
return false;
}
}
return true;
}
public:
/*
* Generates a random graph with n vertices.
* The weights of the edges are generated randomly as numbers between 0 and 1.
* Every edge has probability p of appearing in the graph.
*/
static Graph make_random_graph(int n = -1, double p = 0.2)
{
const int MAX_N = 1e5;
if (n < 0)
n = (int)(uniform(engine) * MAX_N);
if (n > MAX_N)
throw std::runtime_error("graph too big");
Graph g(n);
for (vertex_t v = 0; v < n; ++v)
{
for (vertex_t u = 0; u < n; ++u)
{
if (u == v)
continue;
if (uniform(engine) < p)
g.add_edge(u, v, uniform(engine));
}
}
return g;
}
/*
* Same as make_random_graph, but ensures that the graph is connected.
*/
static Graph make_random_connected_graph(int n = -1, double p = 0.2)
{
Graph g = make_random_graph(n, p);
n = g.num_vertices();
// One edge every for every 10 000 possible
size_t edges_per_iter = (n * n) / 10000 + 1;
// Initialize Union-Find data structure
UnionFind uf(n);
// Add edges until the graph is connected
while (uf.find(0) != uf.find(n - 1))
{
vertex_t v = (vertex_t)(uniform(engine) * n);
vertex_t u = (vertex_t)(uniform(engine) * n);
if (v == u || g.has_edge(v, u))
{
continue;
}
g.add_edge(v, u, uniform(engine));
uf.unite(v, u);
}
return g;
}
/*
* some porucentage of nodes is very poorly connected, i.e. has very little edges, up to max_degree (or a bit more for the sake of being connected)
*/
static Graph make_random_sparse_graph(int n = -1, double p = 0.2, int max_degree = -1, double p_sparse = 0.03)
{
const int MAX_N = 1e5;
if (n < 0)
n = (int)(uniform(engine) * MAX_N);
if (max_degree < 0 || max_degree > n)
throw std::runtime_error("invalid degree");
if (n > MAX_N)
throw std::runtime_error("graph too big");
int sparse_count = (int)(n * p_sparse);
std::unordered_set<int> sparse_nodes;
while (sparse_nodes.size() < sparse_count)
{
int rand_node = (int)(uniform(engine) * n);
sparse_nodes.insert(rand_node);
}
Graph g(n);
std::vector<int> degrees(n, 0);
UnionFind uf(n);
for (vertex_t v = 0; v < n; ++v)
{
for (vertex_t u = 0; u < n; ++u)
{
if (u == v || degrees[v] >= max_degree || degrees[u] >= max_degree)
continue;
bool is_sparse_node = (sparse_nodes.find(v) != sparse_nodes.end()) || (sparse_nodes.find(u) != sparse_nodes.end());
int sparse_degree = is_sparse_node ? max_degree / 2 : max_degree;
if (uniform(engine) < p && degrees[v] < sparse_degree && degrees[u] < sparse_degree)
{
g.add_edge(u, v, uniform(engine));
degrees[v]++;
degrees[u]++;
uf.unite(v, u);
}
}
}
// Add edges until the graph is connected
while (uf.find(0) != uf.find(n - 1))
{
vertex_t v = (vertex_t)(uniform(engine) * n);
vertex_t u = (vertex_t)(uniform(engine) * n);
if (v == u || g.has_edge(v, u) || degrees[v] >= max_degree || degrees[u] >= max_degree)
continue;
g.add_edge(v, u, uniform(engine));
degrees[v]++;
degrees[u]++;
uf.unite(v, u);
}
return g;
}
/*
* some porucentage of nodes is very well connected, i.e. has many edges, at least min_degree
*/
static Graph make_random_dense_graph(int n = -1, double p = 0.5, int min_degree = -1, double p_dense = 0.01)
{
Graph g = make_random_graph(n, p);
n = g.num_vertices();
// Subset of vertices which should have high degree
int dense_node_count = (int)(n * p_dense); // 1% of the nodes will be dense
std::vector<vertex_t> dense_nodes(dense_node_count);
for (int i = 0; i < dense_node_count; i++)
{
dense_nodes[i] = i;
}
for (vertex_t v : dense_nodes)
{
while (g.edges_from(v).size() < min_degree)
{
// Add edges from v to other random vertices until its degree is 'min_degree'
while (true)
{
vertex_t u = (vertex_t)(uniform(engine) * n);
if (v != u && !g.has_edge(v, u))
{
g.add_edge(v, u, uniform(engine));
break;
}
}
}
}
UnionFind uf(n);
// Now, we have a dense graph but it may not be connected. Let's connect it using a similar method as before.
// One edge every for every 10 000 possible
size_t edges_per_iter = (n * n) / 10000 + 1;
while (uf.find(0) != uf.find(n - 1))
{
size_t edges_added = 0;
while (edges_added < edges_per_iter)
{
vertex_t v = (vertex_t)(uniform(engine) * n);
vertex_t u = (vertex_t)(uniform(engine) * n);
if (v == u || g.has_edge(v, u))
{
continue;
}
g.add_edge(v, u, uniform(engine));
edges_added++;
uf.unite(v, u);
}
}
return g;
}
Graph make_random_city(int n = -1, double p = 0.2, int max_degree = -1, int min_degree = -1, double p_dense = 0.1, double p_sparse = 0.2)
{
// define type of nodes and their density
int sparse_node_count = (int)(p_sparse * n);
int dense_node_count = (int)(p_dense * n);
int regular_node_count = n - (dense_node_count + sparse_node_count);
// Initialize city graph and degrees
Graph city(n);
std::vector<int> degrees(n, 0);
// initialize vertices for each type of node
std::vector<vertex_t> dense_nodes(dense_node_count);
std::vector<vertex_t> sparse_nodes(sparse_node_count);
std::vector<vertex_t> regular_nodes(regular_node_count);
// Initialize union-find structure
UnionFind uf(n);
// Initialize these vectors with the corresponding indices
for (int i = 0; i < dense_node_count; i++)
{
dense_nodes[i] = i;
}
for (int i = 0; i < sparse_node_count; i++)
{
sparse_nodes[i] = dense_node_count + i;
}
for (int i = 0; i < regular_node_count; i++)
{
regular_nodes[i] = dense_node_count + sparse_node_count + i;
}
// handle dense nodes
for (vertex_t v : dense_nodes)
{
while (degrees[v] < min_degree)
{
// Add edges from v to other random vertices until its degree is 'min_degree'
while (true)
{
vertex_t u = (vertex_t)(uniform(engine) * n);
if (v != u && !city.has_edge(v, u))
{
city.add_edge(v, u, uniform(engine));
degrees[v]++;
degrees[u]++;
uf.unite(v, u);
break;
}
}
}
}
// handle sparse nodes
for (vertex_t v : sparse_nodes)
{
while (degrees[v] < max_degree)
{
// Add edges from v to other random vertices until its degree is 'max_degree'
while (true)
{
vertex_t u = (vertex_t)(uniform(engine) * n);
if (v != u && !city.has_edge(v, u))
{
city.add_edge(v, u, uniform(engine));
degrees[v]++;
degrees[u]++;
uf.unite(v, u);
break;
}
}
}
}
// handle regular nodes
for (vertex_t v : regular_nodes)
{
// Add edges from v to other random vertices based on probability 'p'
for (vertex_t u = 0; u < n; ++u)
{
if (v != u && !city.has_edge(v, u) && uniform(engine) < p)
{
city.add_edge(v, u, uniform(engine));
degrees[v]++;
degrees[u]++;
uf.unite(v, u);
}
}
}
// Ensure connectivity of the graph
size_t edges_per_iter = (n * n) / 10000 + 1;
while (uf.find(0) != uf.find(n - 1))
{
size_t edges_added = 0;
while (edges_added < edges_per_iter)
{
vertex_t v = (vertex_t)(uniform(engine) * n);
vertex_t u = (vertex_t)(uniform(engine) * n);
if (v == u || city.has_edge(v, u))
{
continue;
}
city.add_edge(v, u, uniform(engine));
edges_added++;
uf.unite(v, u);
}
}
return city;
}
};
std::default_random_engine GraphGenerator::engine;
std::uniform_real_distribution<double> GraphGenerator::uniform(0, 1);