/
DigraphGenerator.java
333 lines (302 loc) · 11.6 KB
/
DigraphGenerator.java
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
/*************************************************************************
* Compilation: javac DigraphGenerator.java
* Execution: java DigraphGenerator V E
* Dependencies: Digraph.java
*
* A digraph generator.
*
*************************************************************************/
/**
* The <tt>DigraphGenerator</tt> 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="http://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
*/
public class DigraphGenerator {
private static final class Edge implements Comparable<Edge> {
private int v;
private int w;
private Edge(int v, int w) {
this.v = v;
this.w = w;
}
public int compareTo(Edge that) {
if (this.v < that.v) return -1;
if (this.v > that.v) return +1;
if (this.w < that.w) return -1;
if (this.w > that.w) return +1;
return 0;
}
}
/**
* Returns a random simple digraph containing <tt>V</tt> vertices and <tt>E</tt> edges.
* @param V the number of vertices
* @param E the number of vertices
* @return a random simple digraph on <tt>V</tt> vertices, containing a total
* of <tt>E</tt> edges
* @throws IllegalArgumentException if no such simple digraph exists
*/
public static Digraph simple(int V, int E) {
if (E > (long) V*(V-1)) throw new IllegalArgumentException("Too many edges");
if (E < 0) throw new IllegalArgumentException("Too few edges");
Digraph G = new Digraph(V);
SET<Edge> set = new SET<Edge>();
while (G.E() < E) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
Edge e = new Edge(v, w);
if ((v != w) && !set.contains(e)) {
set.add(e);
G.addEdge(v, w);
}
}
return G;
}
/**
* Returns a random simple digraph on <tt>V</tt> vertices, with an
* edge between any two vertices with probability <tt>p</tt>. This is sometimes
* referred to as the Erdos-Renyi random digraph model.
* @param V the number of vertices
* @param p the probability of choosing an edge
* @return a random simple digraph on <tt>V</tt> vertices, with an edge between
* any two vertices with probability <tt>p</tt>
* @throws IllegalArgumentException if probability is not between 0 and 1
*/
public static Digraph simple(int V, double p) {
if (p < 0.0 || p > 1.0)
throw new IllegalArgumentException("Probability must be between 0 and 1");
Digraph G = new Digraph(V);
for (int v = 0; v < V; v++)
for (int w = 0; w < V; w++)
if (v != w)
if (StdRandom.bernoulli(p))
G.addEdge(v, w);
return G;
}
/**
* Returns the complete digraph on <tt>V</tt> vertices.
* @param V the number of vertices
* @return the complete digraph on <tt>V</tt> vertices
*/
public static Digraph complete(int V) {
return simple(V, V*(V-1));
}
/**
* Returns a random simple DAG containing <tt>V</tt> vertices and <tt>E</tt> 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 <tt>V</tt> vertices, containing a total
* of <tt>E</tt> edges
* @throws IllegalArgumentException if no such simple DAG exists
*/
public static Digraph dag(int V, int E) {
if (E > (long) V*(V-1) / 2) throw new IllegalArgumentException("Too many edges");
if (E < 0) throw new IllegalArgumentException("Too few edges");
Digraph G = new Digraph(V);
SET<Edge> set = new SET<Edge>();
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
while (G.E() < E) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
Edge e = new Edge(v, w);
if ((v < w) && !set.contains(e)) {
set.add(e);
G.addEdge(vertices[v], vertices[w]);
}
}
return G;
}
// tournament
/**
* Returns a random tournament digraph on <tt>V</tt> vertices. A tournament digraph
* is a DAG in which for every two vertices <tt>v != w<tt>, there is either
* a directed edge from <tt>v</tt> to <tt>w</tt> or a directed edge
* from <tt>w</tt> to <tt>v</tt>.
* @param V the number of vertices
* @return a random tournament digraph on <tt>V</tt> vertices
*/
public static Digraph tournament(int V) {
return dag(V, V*(V-1)/2);
}
/**
* Returns a random rooted-in DAG on <tt>V</tt> vertices and <tt>E</tt> edges.
* 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 <tt>V</tt> vertices and <tt>E</tt> edges
*/
public static Digraph rootedInDAG(int V, int E) {
if (E > (long) V*(V-1) / 2) throw new IllegalArgumentException("Too many edges");
if (E < V-1) throw new IllegalArgumentException("Too few edges");
Digraph G = new Digraph(V);
SET<Edge> set = new SET<Edge>();
// fix a topological order
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
// one edge pointing from each vertex, other than the root = vertices[V-1]
for (int v = 0; v < V-1; v++) {
int w = StdRandom.uniform(v+1, V);
Edge e = new Edge(v, w);
set.add(e);
G.addEdge(vertices[v], vertices[w]);
}
while (G.E() < E) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
Edge e = new Edge(v, w);
if ((v < w) && !set.contains(e)) {
set.add(e);
G.addEdge(vertices[v], vertices[w]);
}
}
return G;
}
/**
* Returns a random rooted-out DAG on <tt>V</tt> vertices and <tt>E</tt> edges.
* 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 <tt>V</tt> vertices and <tt>E</tt> edges
*/
public static Digraph rootedOutDAG(int V, int E) {
if (E > (long) V*(V-1) / 2) throw new IllegalArgumentException("Too many edges");
if (E < V-1) throw new IllegalArgumentException("Too few edges");
Digraph G = new Digraph(V);
SET<Edge> set = new SET<Edge>();
// fix a topological order
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
// one edge pointing from each vertex, other than the root = vertices[V-1]
for (int v = 0; v < V-1; v++) {
int w = StdRandom.uniform(v+1, V);
Edge e = new Edge(v, w);
set.add(e);
G.addEdge(vertices[w], vertices[v]);
}
while (G.E() < E) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
Edge e = new Edge(v, w);
if ((v < w) && !set.contains(e)) {
set.add(e);
G.addEdge(vertices[w], vertices[v]);
}
}
return G;
}
/**
* Returns a random rooted-in tree on <tt>V</tt> vertices.
* 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 <tt>V</tt> vertices
*/
public static Digraph rootedInTree(int V) {
return rootedInDAG(V, V-1);
}
/**
* Returns a random rooted-out tree on <tt>V</tt> vertices.
* 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 <tt>V</tt> vertices
*/
public static Digraph rootedOutTree(int V) {
return rootedOutDAG(V, V-1);
}
/**
* Returns a path digraph on <tt>V</tt> vertices.
* @param V the number of vertices in the path
* @return a digraph that is a directed path on <tt>V</tt> vertices
*/
public static Digraph path(int V) {
Digraph G = new Digraph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
for (int i = 0; i < V-1; i++) {
G.addEdge(vertices[i], vertices[i+1]);
}
return G;
}
/**
* Returns a complete binary tree digraph on <tt>V</tt> vertices.
* @param V the number of vertices in the binary tree
* @return a digraph that is a complete binary tree on <tt>V</tt> vertices
*/
public static Digraph binaryTree(int V) {
Digraph G = new Digraph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
for (int i = 1; i < V; i++) {
G.addEdge(vertices[i], vertices[(i-1)/2]);
}
return G;
}
/**
* Returns a cycle digraph on <tt>V</tt> vertices.
* @param V the number of vertices in the cycle
* @return a digraph that is a directed cycle on <tt>V</tt> vertices
*/
public static Digraph cycle(int V) {
Digraph G = new Digraph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++) vertices[i] = i;
StdRandom.shuffle(vertices);
for (int i = 0; i < V-1; i++) {
G.addEdge(vertices[i], vertices[i+1]);
}
G.addEdge(vertices[V-1], vertices[0]);
return G;
}
/**
* Unit tests the <tt>DigraphGenerator</tt> library.
*/
public static void main(String[] args) {
int V = Integer.parseInt(args[0]);
int E = Integer.parseInt(args[1]);
System.out.println("complete graph");
System.out.println(complete(V));
System.out.println();
System.out.println("simple");
System.out.println(simple(V, E));
System.out.println();
System.out.println("path");
System.out.println(path(V));
System.out.println();
System.out.println("cycle");
System.out.println(cycle(V));
System.out.println();
System.out.println("binary tree");
System.out.println(binaryTree(V));
System.out.println();
System.out.println("tournament");
System.out.println(tournament(V));
System.out.println();
System.out.println("DAG");
System.out.println(dag(V, E));
System.out.println();
System.out.println("rooted-in DAG");
System.out.println(rootedInDAG(V, E));
System.out.println();
System.out.println("rooted-out DAG");
System.out.println(rootedOutDAG(V, E));
System.out.println();
System.out.println("rooted-in tree");
System.out.println(rootedInTree(V));
System.out.println();
System.out.println("rooted-out DAG");
System.out.println(rootedOutTree(V));
System.out.println();
}
}