001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
|
package algs41;
import java.util.Arrays;
import algs13.Stack;
import algs24.MinPQ;
import stdlib.*;
public class GraphGenerator {
/**
* Create a graph from input stream.
*/
public static Graph fromIn (In in) {
Graph G = new Graph (in.readInt());
int E = in.readInt();
for (int i = 0; i < E; i++) {
int v = in.readInt();
int w = in.readInt();
G.addEdge(v, w);
}
return G;
}
public static Graph copy (Graph G) {
Graph R = new Graph (G.V());
for (int v = 0; v < G.V(); v++) {
// reverse so that adjacency list is in same order as original
Stack<Integer> reverse = new Stack<Integer>();
for (int w : G.adj(v)) {
reverse.push(w);
}
for (int w : reverse) {
R.addEdge (v, w);
}
}
return R;
}
public static Graph complete (int V) {
return simple (V, V * (V - 1) / 2);
}
public static Graph simple (int V, int E) {
if (V < 0 || E < 0) throw new IllegalArgumentException ();
if (E > V * (V - 1) / 2) throw new IllegalArgumentException ("Number of edges must be less than V*(V-1)/2");
Graph G = new Graph (V);
newEdge: while (E > 0) {
int v = StdRandom.uniform (V);
int w = StdRandom.uniform (V);
if (v == w) continue;
for (int w2 : G.adj (v))
if (w == w2) continue newEdge;
G.addEdge (v, w);
E--;
}
return G;
}
public static Graph simpleConnected (int V, int E) {
if (V < 0 || E < 0) throw new IllegalArgumentException ();
if (E > V * (V - 1) / 2) throw new IllegalArgumentException ("Number of edges must be less than V*(V-1)/2");
Graph G = spanningTree (V);
newEdge: while (G.E () < E) {
int v = StdRandom.uniform (V);
int w = StdRandom.uniform (V);
if (v == w) continue;
for (int w2 : G.adj (v))
if (w == w2) continue newEdge;
G.addEdge (v, w);
E--;
}
return G;
}
public static Graph connected (int V, int E) {
if (V < 0 || E < 0) throw new IllegalArgumentException ();
Graph G = spanningTree (V);
while (G.E () < E) {
int v = StdRandom.uniform (V);
int w = StdRandom.uniform (V);
G.addEdge (v, w);
}
return G;
}
public static Graph random (int V, int E) {
if (V < 0 || E < 0) throw new IllegalArgumentException ();
Graph G = new Graph (V);
while (G.E () < E) {
int v = StdRandom.uniform (V);
int w = StdRandom.uniform (V);
G.addEdge (v, w);
}
return G;
}
public static Graph spanningTree (int V) {
if (V < 1) throw new IllegalArgumentException ();
int[] vertices = new int[V];
for (int i = 0; i < V; i++)
vertices[i] = i;
StdRandom.shuffle (vertices);
Graph G = new Graph (V);
for (int i = 1; i < V; i++) {
int v = vertices[StdRandom.uniform (i)];
int w = vertices[i];
G.addEdge (v, w);
}
return G;
}
public static Graph connected(int V, int E, int c) {
if (c >= V || c <= 0)
throw new IllegalArgumentException("Number of components must be between 1 and V");
if (E <= (V-c))
throw new IllegalArgumentException("Number of edges must be at least (V-c)");
if (E > V * (V - 1) / 2)
throw new IllegalArgumentException("Too many edges");
int[] label = new int[V];
for (int v = 0; v < V; v++) {
label[v] = StdRandom.uniform(c);
}
// The following hack ensures that each color appears at least once
{
Arrays.sort (label);
label[0] = 0;
for (int v = 1; v < V; v++) {
if (label[v]-label[v-1] > 1 || V-v == c-label[v-1]-1)
label[v] = label[v-1]+1;
}
StdRandom.shuffle (label);
}
// make all vertices with label c a connected component
Graph G = new Graph(V);
for (int i = 0; i < c; i++) {
// how many vertices in component c
int count = 0;
for (int v = 0; v < V; v++) {
if (label[v] == i) count++;
}
int[] vertices = new int[count];
{
int j = 0;
for (int v = 0; v < V; v++)
if (label[v] == i) vertices[j++] = v;
}
StdRandom.shuffle(vertices);
for (int j = 1; j < count; j++) {
int v = vertices[StdRandom.uniform (j)];
int w = vertices[j];
G.addEdge (v, w);
}
}
while (G.E() < E) {
int v = StdRandom.uniform(V);
int w = StdRandom.uniform(V);
if (v != w && label[v] == label[w]) {
G.addEdge(v, w);
}
}
return G;
}
/**
* Returns a random simple bipartite graph on {@code V1} and {@code V2} vertices,
* containing each possible edge with probability {@code p}.
* @param V1 the number of vertices in one partition
* @param V2 the number of vertices in the other partition
* @param p the probability that the graph contains an edge with one endpoint in either side
* @return a random simple bipartite graph on {@code V1} and {@code V2} vertices,
* containing each possible edge with probability {@code p}
* @throws IllegalArgumentException if probability is not between 0 and 1
*/
public static Graph bipartite(int V1, int V2, double p) {
if (p < 0.0 || p > 1.0)
throw new IllegalArgumentException("Probability must be between 0 and 1");
int[] vertices = new int[V1 + V2];
for (int i = 0; i < V1 + V2; i++)
vertices[i] = i;
StdRandom.shuffle(vertices);
Graph G = new Graph(V1 + V2);
for (int i = 0; i < V1; i++)
for (int j = 0; j < V2; j++)
if (StdRandom.bernoulli(p))
G.addEdge(vertices[i], vertices[V1+j]);
return G;
}
/**
* Returns a path graph on {@code V} vertices.
* @param V the number of vertices in the path
* @return a path graph on {@code V} vertices
*/
public static Graph path(int V) {
Graph G = new Graph(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 graph on {@code V} vertices.
* @param V the number of vertices in the binary tree
* @return a complete binary tree graph on {@code V} vertices
*/
public static Graph binaryTree(int V) {
Graph G = new Graph(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 graph on {@code V} vertices.
* @param V the number of vertices in the cycle
* @return a cycle graph on {@code V} vertices
*/
public static Graph cycle(int V) {
Graph G = new Graph(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;
}
/**
* Returns an Eulerian cycle graph on {@code V} vertices.
*
* @param V the number of vertices in the cycle
* @param E the number of edges in the cycle
* @return a graph that is an Eulerian cycle on {@code V} vertices
* and {@code E} edges
* @throws IllegalArgumentException if either {@code V <= 0} or {@code E <= 0}
*/
public static Graph eulerianCycle(int V, int E) {
if (E <= 0)
throw new IllegalArgumentException("An Eulerian cycle must have at least one edge");
if (V <= 0)
throw new IllegalArgumentException("An Eulerian cycle must have at least one vertex");
Graph G = new Graph(V);
int[] vertices = new int[E];
for (int i = 0; i < E; i++)
vertices[i] = StdRandom.uniform(V);
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 graph on {@code V} vertices.
*
* @param V the number of vertices in the path
* @param E the number of edges in the path
* @return a graph that is an Eulerian path on {@code V} vertices
* and {@code E} edges
* @throws IllegalArgumentException if either {@code V <= 0} or {@code E < 0}
*/
public static Graph eulerianPath(int V, int E) {
if (E < 0)
throw new IllegalArgumentException("negative number of edges");
if (V <= 0)
throw new IllegalArgumentException("An Eulerian path must have at least one vertex");
Graph G = new Graph(V);
int[] vertices = new int[E+1];
for (int i = 0; i < E+1; i++)
vertices[i] = StdRandom.uniform(V);
for (int i = 0; i < E; i++) {
G.addEdge(vertices[i], vertices[i+1]);
}
return G;
}
/**
* Returns a wheel graph on {@code V} vertices.
* @param V the number of vertices in the wheel
* @return a wheel graph on {@code V} vertices: a single vertex connected to
* every vertex in a cycle on {@code V-1} vertices
*/
public static Graph wheel(int V) {
if (V <= 1) throw new IllegalArgumentException("Number of vertices must be at least 2");
Graph G = new Graph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++)
vertices[i] = i;
StdRandom.shuffle(vertices);
// simple cycle on V-1 vertices
for (int i = 1; i < V-1; i++) {
G.addEdge(vertices[i], vertices[i+1]);
}
G.addEdge(vertices[V-1], vertices[1]);
// connect vertices[0] to every vertex on cycle
for (int i = 1; i < V; i++) {
G.addEdge(vertices[0], vertices[i]);
}
return G;
}
/**
* Returns a star graph on {@code V} vertices.
* @param V the number of vertices in the star
* @return a star graph on {@code V} vertices: a single vertex connected to
* every other vertex
*/
public static Graph star(int V) {
if (V <= 0) throw new IllegalArgumentException("Number of vertices must be at least 1");
Graph G = new Graph(V);
int[] vertices = new int[V];
for (int i = 0; i < V; i++)
vertices[i] = i;
StdRandom.shuffle(vertices);
// connect vertices[0] to every other vertex
for (int i = 1; i < V; i++) {
G.addEdge(vertices[0], vertices[i]);
}
return G;
}
/**
* Returns a uniformly random {@code k}-regular graph on {@code V} vertices
* (not necessarily simple). The graph is simple with probability only about e^(-k^2/4),
* which is tiny when k = 14.
*
* @param V the number of vertices in the graph
* @param k degree of each vertex
* @return a uniformly random {@code k}-regular graph on {@code V} vertices.
*/
public static Graph regular(int V, int k) {
if (V*k % 2 != 0) throw new IllegalArgumentException("Number of vertices * k must be even");
Graph G = new Graph(V);
// create k copies of each vertex
int[] vertices = new int[V*k];
for (int v = 0; v < V; v++) {
for (int j = 0; j < k; j++) {
vertices[v + V*j] = v;
}
}
// pick a random perfect matching
StdRandom.shuffle(vertices);
for (int i = 0; i < V*k/2; i++) {
G.addEdge(vertices[2*i], vertices[2*i + 1]);
}
return G;
}
// http://www.proofwiki.org/wiki/Labeled_Tree_from_Prüfer_Sequence
// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.36.6484&rep=rep1&type=pdf
/**
* Returns a uniformly random tree on {@code V} vertices.
* This algorithm uses a Prufer sequence and takes time proportional to <em>V log V</em>.
* @param V the number of vertices in the tree
* @return a uniformly random tree on {@code V} vertices
*/
public static Graph tree(int V) {
Graph G = new Graph(V);
// special case
if (V == 1) return G;
// Cayley's theorem: there are V^(V-2) labeled trees on V vertices
// Prufer sequence: sequence of V-2 values between 0 and V-1
// Prufer's proof of Cayley's theorem: Prufer sequences are in 1-1
// with labeled trees on V vertices
int[] prufer = new int[V-2];
for (int i = 0; i < V-2; i++)
prufer[i] = StdRandom.uniform(V);
// degree of vertex v = 1 + number of times it appers in Prufer sequence
int[] degree = new int[V];
for (int v = 0; v < V; v++)
degree[v] = 1;
for (int i = 0; i < V-2; i++)
degree[prufer[i]]++;
// pq contains all vertices of degree 1
MinPQ<Integer> pq = new MinPQ<Integer>();
for (int v = 0; v < V; v++)
if (degree[v] == 1) pq.insert(v);
// repeatedly delMin() degree 1 vertex that has the minimum index
for (int i = 0; i < V-2; i++) {
int v = pq.delMin();
G.addEdge(v, prufer[i]);
degree[v]--;
degree[prufer[i]]--;
if (degree[prufer[i]] == 1) pq.insert(prufer[i]);
}
G.addEdge(pq.delMin(), pq.delMin());
return G;
}
public static void print (Graph G, String filename) {
if (G == null) return;
System.out.println (filename);
System.out.println (G);
System.out.println ();
G.toGraphviz (filename + ".png");
}
public static void main (String[] args) {
//StdRandom.setSeed (10);
args = new String[] { "6", "10", "3" };
int V = Integer.parseInt (args[0]);
int E = Integer.parseInt (args[1]);
int c = Integer.parseInt (args[2]);
for (int i= 5; i>0; i--) {
print (GraphGenerator.random (V, E), "random-" + V + "-" + E);
print (GraphGenerator.random (V, E), "random-" + V + "-" + E);
print (GraphGenerator.simple (V, E), "simple-" + V + "-" + E);
print (GraphGenerator.complete (V), "complete-" + V);
print (GraphGenerator.spanningTree (V), "spanningTree-" + V);
print (GraphGenerator.simpleConnected (V, E), "simpleConnected-" + V + "-" + E);
print (GraphGenerator.connected (V, E), "connected-" + V + "-" + E);
print (GraphGenerator.path (V), "path-" + V);
print (GraphGenerator.binaryTree (V), "binaryTree-" + V);
print (GraphGenerator.cycle (V), "cycle-" + V);
if (E <= (V - c)) E = (V - c) + 1;
print (GraphGenerator.connected (V, E, c), "connected-" + V + "-" + E + "-" + c);
print (GraphGenerator.eulerianCycle (V, E), "eulerian-" + V + "-" + E);
}
}
}
|