Skip to content

Commit 2e79f22

Browse files
committed
graph: GraphGenerator
1 parent 6a3a5fd commit 2e79f22

File tree

1 file changed

+160
-1
lines changed

1 file changed

+160
-1
lines changed

Algorithm_full_features/graph/GraphGenerator.java

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,163 @@ public static Graph eulerianPath(int V, int E) {
204204
}
205205

206206
// Returns a wheel on `V` vertices
207-
}
207+
public static Graph wheel(int V) {
208+
if(V <= 1)
209+
throw new IllegalArgumentException("Number of vertices must be at least 2");
210+
Graph G = new Graph(V);
211+
int[] vertices = new int[V];
212+
for(int i = 0; i < V; i++)
213+
vertices[i] = i;
214+
StdRandom.shuffle(vertices);
215+
216+
for(int i = 0; i < V - 1; i++)
217+
G.addEdge(vertices[i], vertices[i + 1]);
218+
219+
G.addEdge(vertices[V - 1], vertices[1]);
220+
221+
for(int i = 1; i < V; i++)
222+
G.addEdge(vertices[0], vertices[i]);
223+
224+
return G;
225+
}
226+
227+
// Returns a star graph on `V` vertices
228+
public static Graph star(int V) {
229+
if(V <= 0)
230+
throw new IllegalArgumentException("Number of vertices must be at least one");
231+
Graph G = new Graph(V);
232+
int[] vertices = new int(V);
233+
for(int i = 0; i < V; i++)
234+
vertices[i] = i;
235+
StdRandom.shuffle(vertices);
236+
237+
for(int i = 1; i < V; i++)
238+
G.addEdge(vertices[0], vertices[i]);
239+
240+
return G;
241+
}
242+
243+
// Returns a uniformly random `k-regular` graph on `V` vertices(not necessarily
244+
// simple). The graph is simple with probability only about e^(-k^2/4);, which is
245+
// tiny when k = 14.
246+
public static Graph regular(int V, int k) {
247+
if(V * k % 2 != 0)
248+
throw new IllegalArgumentException("Number of vertices * k must be even");
249+
Graph G = new Graph(V);
250+
int[] vertices = new int[V * k];
251+
for(int v = 0; v < V; v++)
252+
for(int j = 0; j < k; j++)
253+
vertices[v + V * j] = v;
254+
255+
// pick a random perfect matching
256+
StdRandom.shuffle(vertices);
257+
for(int i = 0; i < V * k / 2; i++)
258+
G.addEdge(vertices[2 * i], vertices[2 * i + 1]);
259+
260+
return G;
261+
}
262+
263+
// Returns a uniformly random tree on `V` vertices.
264+
public static Graph tree(int V) {
265+
Graph G = new Graph(V);
266+
// special case
267+
if(V == 1)
268+
return G;
269+
// Cayley's theorem: there are V^(V - 2) labeled trees on V vertices
270+
// Prufer sequence: sequence of V - 2 values between 0 and V - 1
271+
// Prufer's proof of Cayley's theorem: Prufer sequences are in 1-1
272+
// with labeled trees on V vertices.
273+
int [] prufer = new int[V - 2];
274+
for(int i = 0; i < V - 2; i++)
275+
prufer[i] = StdRandom.uniform(V);
276+
277+
// degree of vertex v = 1 + number of times it appears in Prufer sequence
278+
int[] degree = new int[V];
279+
for(int v = 0; v < V; v++)
280+
degree[v] = 1;
281+
for(int i = 0; i < V - 2; i++)
282+
degree[prufer[i]]++;
283+
284+
// pq contains all vertices of degree 1
285+
MinPQ<Integer> pq = new MinPQ<Integer>();
286+
for(int v = 0; v < V; v++) {
287+
if(degree[v] == 1)
288+
pq.insert(v);
289+
}
290+
291+
// repeatedly delMin() degree 1 vertex that has the minimum index
292+
for(int i = 0; i< V - 2; i++) {
293+
int v = pq.delMin();
294+
G.addEdge(v, prufer[i]);
295+
degree[v]--;
296+
degree[prufer[i]]--;
297+
if(degree[prufer[i]] == 1)
298+
pq.insert(prufer[i]);
299+
}
300+
G.addEdge(pq.delMin(), pq.delMin());
301+
302+
return G;
303+
}
304+
305+
// test
306+
public static void main(String[] args) {
307+
int V = Integer.parseInt(args[0]);
308+
int E = Integer.parseInt(args[1]);
309+
int V1 = V / 2;
310+
int V2 = V - V1;
311+
312+
StdOut.println("complete graph");
313+
StdOut.println(complete(V));
314+
StdOut.println();
315+
316+
StdOut.println("simple");
317+
StdOut.println(simple(V, E));
318+
StdOut.println();
319+
320+
StdOut.println("Erdos-Renyi");
321+
double p = (double)E / (V * (V - 1) / 2.0);
322+
StdOut.println(simple(V, p));
323+
StdOut.println();
324+
325+
StdOut.println("complete bipartite");
326+
StdOut.println(completeBipartite(V1, V2));
327+
StdOut.println();
328+
329+
StdOut.println("bipartite");
330+
StdOut.println(bipartite(V1, V2, E));
331+
StdOut.println();
332+
333+
StdOut.println("Erods Renyi bipartite");
334+
double q = (double)E / (V1 * V2);
335+
StdOut.println(bipartite(V1, V2, q));
336+
StdOut.println();
337+
338+
StdOut.println("path");
339+
StdOut.println(path(V));
340+
StdOut.println();
341+
342+
StdOut.println("cycle");
343+
StdOut.println(cycle(V));
344+
StdOut.println();
345+
346+
StdOut.println("binary tree");
347+
StdOut.println(binaryTree(V));
348+
StdOut.println();
349+
350+
StdOut.println("tree");
351+
StdOut.println(tree(V));
352+
StdOut.println();
353+
354+
StdOut.println("4-regular");
355+
StdOut.println(regular(V, 4));
356+
StdOut.println();
357+
358+
StdOut.println("star");
359+
StdOut.println(star(V));
360+
StdOut.println();
361+
362+
StdOut.println("wheel");
363+
StdOut.println(wheel(V));
364+
StdOut.println();
365+
}
366+
}

0 commit comments

Comments
 (0)