-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDigraphGenerator.h
467 lines (427 loc) · 16.4 KB
/
DigraphGenerator.h
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#ifndef CH4_DIGRAPHGENERATOR_H
#define CH4_DIGRAPHGENERATOR_H
#include "../head/Digraph.h"
#include <set>
#include <random>
#include <algorithm>
std::random_device rd;
std::mt19937 g(rd());
using std::set;
using std::uniform_int_distribution;
using std::bernoulli_distribution;
using std::shuffle;
/**
* The {@code DigraphGenerator} class provides static methods for creating
* various digraphs, including Erdos-Renyi random digraphs, random DAGs,
* random rooted trees, random rooted DAGs, random tournaments, path digraphs,
* cycle digraphs, and the complete digraph.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/42digraph">Section 4.2</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
class DigraphGenerator {
public:
DigraphGenerator() = delete;
/**
* Returns a random simple digraph containing {@code V} vertices and {@code E} edges.
* @param V the number of vertices
* @param E the number of vertices
* @return a random simple digraph on {@code V} vertices, containing a total
* of {@code E} edges
* @throws IllegalArgumentException if no such simple digraph exists
*/
static Digraph *simple(int V, int E) {
if (E > (long) V * (V - 1)) throw runtime_error("Too many edges");
if (E < 0) throw runtime_error("Too few edges");
Digraph *G = new Digraph(V);
set<Edge> set1;
uniform_int_distribution<> dis(0, V - 1);
while (G->getE() < E) {
int v = dis(g);
int w = dis(g);
Edge e(v, w);
if ((v != w) && set1.find(e) == set1.end()) {
set1.insert(e);
G->addEdge(v, w);
}
}
return G;
}
/**
* Returns a random simple digraph on {@code V} vertices, with an
* edge between any two vertices with probability {@code p}. This is sometimes
* referred to as the Erdos-Renyi random digraph model.
* This implementations takes time propotional to V^2 (even if {@code p} is small).
* @param V the number of vertices
* @param p the probability of choosing an edge
* @return a random simple digraph on {@code V} vertices, with an edge between
* any two vertices with probability {@code p}
* @throws IllegalArgumentException if probability is not between 0 and 1
*/
static Digraph *simple(int V, double p) {
if (p < 0.0 || p > 1.0)
throw runtime_error("Probability must be between 0 and 1");
Digraph *G = new Digraph(V);
bernoulli_distribution dis(p);
for (int v = 0; v < V; v++)
for (int w = 0; w < V; w++)
if (v != w)
if (dis(g))
G->addEdge(v, w);
return G;
}
/**
* Returns the complete digraph on {@code V} vertices.
* @param V the number of vertices
* @return the complete digraph on {@code V} vertices
*/
static Digraph *complete(int V) {
return simple(V, V * (V - 1));
}
/**
* Returns a random simple DAG containing {@code V} vertices and {@code E} edges.
* Note: it is not uniformly selected at random among all such DAGs.
* @param V the number of vertices
* @param E the number of vertices
* @return a random simple DAG on {@code V} vertices, containing a total
* of {@code E} edges
* @throws IllegalArgumentException if no such simple DAG exists
*/
static Digraph *dag(int V, int E) {
if (E > (long) V * (V - 1) / 2) throw runtime_error("Too many edges");
if (E < 0) throw runtime_error("Too few edges");
Digraph *G = new Digraph(V);
set<Edge> set1;
uniform_int_distribution<> dis(0, V - 1);
vector<int> vertices(V);
for (int i = 0; i < V; i++)
vertices[i] = i;
shuffle(vertices.begin(), vertices.end(), g);
while (G->getE() < E) {
int v = dis(g);
int w = dis(g);
Edge e(v, w);
if ((v < w) && set1.find(e) == set1.end()) {
set1.insert(e);
G->addEdge(vertices[v], vertices[w]);
}
}
return G;
}
// tournament
/**
* Returns a random tournament digraph on {@code V} vertices. A tournament digraph
* is a DAG in which for every two vertices, there is one directed edge.
* A tournament is an oriented complete graph.
* @param V the number of vertices
* @return a random tournament digraph on {@code V} vertices
*/
static Digraph *tournament(int V) {
Digraph *G = new Digraph(V);
bernoulli_distribution dis(0.5);
for (int v = 0; v < G->getV(); v++) {
for (int w = v + 1; w < G->getV(); w++) {
if (dis(g)) G->addEdge(v, w);
else G->addEdge(w, v);
}
}
return G;
}
/**
* Returns a random rooted-in DAG on {@code V} vertices and {@code E} edges.
* A rooted in-tree is a DAG in which there is a single vertex
* reachable from every other vertex.
* The DAG returned is not chosen uniformly at random among all such DAGs.
* @param V the number of vertices
* @param E the number of edges
* @return a random rooted-in DAG on {@code V} vertices and {@code E} edges
*/
static Digraph *rootedInDAG(int V, int E) {
if (E > (long) V * (V - 1) / 2) throw runtime_error("Too many edges");
if (E < V - 1) throw runtime_error("Too few edges");
Digraph *G = new Digraph(V);
set<Edge> set1;
// fix a topological order
vector<int> vertices(V);
for (int i = 0; i < V; i++)
vertices[i] = i;
shuffle(vertices.begin(), vertices.end(), g);
// one edge pointing from each vertex, other than the root = vertices[V-1]
for (int v = 0; v < V - 1; v++) {
uniform_int_distribution<> dis(v + 1, V - 1);
int w = dis(g);
Edge e(v, w);
set1.insert(e);
G->addEdge(vertices[v], vertices[w]);
}
uniform_int_distribution<> dis(0, V - 1);
while (G->getE() < E) {
int v = dis(g);
int w = dis(g);
Edge e(v, w);
if ((v < w) && set1.find(e) == set1.end()) {
set1.insert(e);
G->addEdge(vertices[v], vertices[w]);
}
}
return G;
}
/**
* Returns a random rooted-out DAG on {@code V} vertices and {@code E} edges.
* A rooted out-tree is a DAG in which every vertex is reachable from a
* single vertex.
* The DAG returned is not chosen uniformly at random among all such DAGs.
* @param V the number of vertices
* @param E the number of edges
* @return a random rooted-out DAG on {@code V} vertices and {@code E} edges
*/
static Digraph *rootedOutDAG(int V, int E) {
if (E > (long) V * (V - 1) / 2) throw runtime_error("Too many edges");
if (E < V - 1) throw runtime_error("Too few edges");
Digraph *G = new Digraph(V);
set<Edge> set1;
// fix a topological order
vector<int> vertices(V);
for (int i = 0; i < V; i++)
vertices[i] = i;
shuffle(vertices.begin(), vertices.end(), g);
// one edge pointing from each vertex, other than the root = vertices[V-1]
for (int v = 0; v < V - 1; v++) {
uniform_int_distribution<> dis(v + 1, V - 1);
int w = dis(g);
Edge e(w, v);
set1.insert(e);
G->addEdge(vertices[w], vertices[v]);
}
uniform_int_distribution<> dis(0, V - 1);
while (G->getE() < E) {
int v = dis(g);
int w = dis(g);
Edge e(w, v);
if ((v < w) && set1.find(e) == set1.end()) {
set1.insert(e);
G->addEdge(vertices[w], vertices[v]);
}
}
return G;
}
/**
* Returns a random rooted-in tree on {@code V} vertices.
* A rooted in-tree is an oriented tree in which there is a single vertex
* reachable from every other vertex.
* The tree returned is not chosen uniformly at random among all such trees.
* @param V the number of vertices
* @return a random rooted-in tree on {@code V} vertices
*/
static Digraph *rootedInTree(int V) {
return rootedInDAG(V, V - 1);
}
/**
* Returns a random rooted-out tree on {@code V} vertices. A rooted out-tree
* is an oriented tree in which each vertex is reachable from a single vertex.
* It is also known as a <em>arborescence</em> or <em>branching</em>.
* The tree returned is not chosen uniformly at random among all such trees.
* @param V the number of vertices
* @return a random rooted-out tree on {@code V} vertices
*/
static Digraph *rootedOutTree(int V) {
return rootedOutDAG(V, V - 1);
}
/**
* Returns a path digraph on {@code V} vertices.
* @param V the number of vertices in the path
* @return a digraph that is a directed path on {@code V} vertices
*/
static Digraph *path(int V) {
Digraph *G = new Digraph(V);
vector<int> vertices(V);
for (int i = 0; i < V; i++)
vertices[i] = i;
shuffle(vertices.begin(), vertices.end(), g);
for (int i = 0; i < V - 1; i++) {
G->addEdge(vertices[i], vertices[i + 1]);
}
return G;
}
/**
* Returns a complete binary tree digraph on {@code V} vertices.
* @param V the number of vertices in the binary tree
* @return a digraph that is a complete binary tree on {@code V} vertices
*/
static Digraph *binaryTree(int V) {
Digraph *G = new Digraph(V);
vector<int> vertices(V);
for (int i = 0; i < V; i++)
vertices[i] = i;
shuffle(vertices.begin(), vertices.end(), g);
for (int i = 1; i < V; i++) {
G->addEdge(vertices[i], vertices[(i - 1) / 2]);
}
return G;
}
/**
* Returns a cycle digraph on {@code V} vertices.
* @param V the number of vertices in the cycle
* @return a digraph that is a directed cycle on {@code V} vertices
*/
static Digraph *cycle(int V) {
Digraph *G = new Digraph(V);
vector<int> vertices(V);
for (int i = 0; i < V; i++)
vertices[i] = i;
shuffle(vertices.begin(), vertices.end(), g);
for (int i = 0; i < V - 1; i++) {
G->addEdge(vertices[i], vertices[i + 1]);
}
G->addEdge(vertices[V - 1], vertices[0]);
return G;
}
/**
* Returns an Eulerian cycle digraph on {@code V} vertices.
*
* @param V the number of vertices in the cycle
* @param E the number of edges in the cycle
* @return a digraph that is a directed Eulerian cycle on {@code V} vertices
* and {@code E} edges
* @throws IllegalArgumentException if either {@code V <= 0} or {@code E <= 0}
*/
static Digraph *eulerianCycle(int V, int E) {
if (E <= 0)
throw runtime_error("An Eulerian cycle must have at least one edge");
if (V <= 0)
throw runtime_error("An Eulerian cycle must have at least one vertex");
Digraph *G = new Digraph(V);
uniform_int_distribution<> dis(0, V - 1);
vector<int> vertices(E);
for (int i = 0; i < E; i++)
vertices[i] = dis(g);
for (int i = 0; i < E - 1; i++) {
G->addEdge(vertices[i], vertices[i + 1]);
}
G->addEdge(vertices[E - 1], vertices[0]);
return G;
}
/**
* Returns an Eulerian path digraph on {@code V} vertices.
*
* @param V the number of vertices in the path
* @param E the number of edges in the path
* @return a digraph that is a directed Eulerian path on {@code V} vertices
* and {@code E} edges
* @throws IllegalArgumentException if either {@code V <= 0} or {@code E < 0}
*/
static Digraph *eulerianPath(int V, int E) {
if (E < 0)
throw runtime_error("negative number of edges");
if (V <= 0)
throw runtime_error("An Eulerian path must have at least one vertex");
Digraph *G = new Digraph(V);
vector<int> vertices(E + 1);
uniform_int_distribution<> dis(0, V - 1);
for (int i = 0; i < E + 1; i++)
vertices[i] = dis(g);
for (int i = 0; i < E; i++) {
G->addEdge(vertices[i], vertices[i + 1]);
}
return G;
}
/**
* Returns a random simple digraph on {@code V} vertices, {@code E}
* edges and (at least) {@code c} strong components. The vertices are randomly
* assigned integer labels between {@code 0} and {@code c-1} (corresponding to
* strong components). Then, a strong component is creates among the vertices
* with the same label. Next, random edges (either between two vertices with
* the same labels or from a vetex with a smaller label to a vertex with a
* larger label). The number of components will be equal to the number of
* distinct labels that are assigned to vertices.
*
* @param V the number of vertices
* @param E the number of edges
* @param c the (maximum) number of strong components
* @return a random simple digraph on {@code V} vertices and
{@code E} edges, with (at most) {@code c} strong components
* @throws IllegalArgumentException if {@code c} is larger than {@code V}
*/
static Digraph *strong(int V, int E, int c) {
if (c >= V || c <= 0)
throw runtime_error("Number of components must be between 1 and V");
if (E <= 2 * (V - c))
throw runtime_error("Number of edges must be at least 2(V-c)");
if (E > (long) V * (V - 1) / 2)
throw runtime_error("Too many edges");
// the digraph
Digraph *G = new Digraph(V);
// edges added to G (to avoid duplicate edges)
set<Edge> set1;
vector<int> label(V);
uniform_int_distribution<> dis(0, c - 1);
for (int v = 0; v < V; v++)
label[v] = dis(g);
// make all vertices with label c a strong component by
// combining a rooted in-tree and a rooted out-tree
for (int i = 0; i < c; i++) {
// how many vertices in component c
int count = 0;
for (int v = 0; v < G->getV(); v++) {
if (label[v] == i) count++;
}
// if (count == 0) System.err.println("less than desired number of strong components");
vector<int> vertices(count);
int j = 0;
for (int v = 0; v < V; v++) {
if (label[v] == i) vertices[j++] = v;
}
shuffle(vertices.begin(), vertices.end(), g);
// rooted-in tree with root = vertices[count-1]
for (int v = 0; v < count - 1; v++) {
uniform_int_distribution<> dis(v + 1, count - 1);
int w = dis(g);
Edge e(w, v);
set1.insert(e);
G->addEdge(vertices[w], vertices[v]);
}
// rooted-out tree with root = vertices[count-1]
for (int v = 0; v < count - 1; v++) {
uniform_int_distribution<> dis(v + 1, count - 1);
int w = dis(g);
Edge e(v, w);
set1.insert(e);
G->addEdge(vertices[v], vertices[w]);
}
}
uniform_int_distribution<> dis2(0, V - 1);
while (G->getE() < E) {
int v = dis2(g);
int w = dis2(g);
Edge e(v, w);
if (set1.find(e) == set1.end() && v != w && label[v] <= label[w]) {
set1.insert(e);
G->addEdge(v, w);
}
}
return G;
}
public:
class Edge {
public:
Edge(int v_, int w_) : v(v_), w(w_) {}
friend bool operator<(const Edge &e1, const Edge &e2) {
if (e1.v < e2.v) return true;
if (e1.v > e2.v) return false;
if (e1.w < e2.w) return true;
if (e1.w > e2.w) return false;
return false;
}
friend bool operator==(const Edge &e1, const Edge &e2) {
if (e1.v == e2.v && e1.w == e2.w) return true;
else return false;
}
private:
int v, w;
};
private:
};
#endif //CH4_DIGRAPHGENERATOR_H