1 /*
2 * Copyright (c) 2003, The JUNG Authors
3 * All rights reserved.
4 *
5 * This software is open-source under the BSD license; see either "license.txt"
6 * or https://github.com/jrtom/jung/blob/master/LICENSE for a description.
7 */
8 /*
9 * Created on Jul 2, 2003
10 *
11 */
12 package edu.uci.ics.jung.algorithms.generators.random;
13
14 import java.util.Map;
15 import java.util.Set;
16
17 import com.google.common.base.Supplier;
18
19 import edu.uci.ics.jung.graph.Graph;
20 import edu.uci.ics.jung.graph.util.EdgeType;
21
22 /**
23 * Generates a mixed-mode random graph (with random edge weights) based on the output of
24 * <code>BarabasiAlbertGenerator</code>.
25 * Primarily intended for providing a heterogeneous sample graph for visualization testing, etc.
26 */
27 public class MixedRandomGraphGenerator {
28
29 /**
30 * Returns a random mixed-mode graph. Starts with a randomly generated
31 * Barabasi-Albert (preferential attachment) generator
32 * (4 initial vertices, 3 edges added at each step, and num_vertices - 4 evolution steps).
33 * Then takes the resultant graph, replaces random undirected edges with directed
34 * edges, and assigns random weights to each edge.
35 * @param <V> the vertex type
36 * @param <E> the edge type
37 * @param graphFactory factory for graphs of the appropriate type
38 * @param vertexFactory factory for vertices of the appropriate type
39 * @param edgeFactory factory for edges of the appropriate type
40 * @param edge_weights storage for the edge weights that this generator creates
41 * @param num_vertices number of vertices to generate
42 * @param seedVertices storage for the seed vertices that this generator creates
43 * @return the generated graph
44 */
45 public static <V,E> Graph<V,E> generateMixedRandomGraph(
46 Supplier<Graph<V,E>> graphFactory,
47 Supplier<V> vertexFactory,
48 Supplier<E> edgeFactory,
49 Map<E,Number> edge_weights,
50 int num_vertices, Set<V> seedVertices)
51 {
52 int seed = (int)(Math.random() * 10000);
53 BarabasiAlbertGenerator<V,E> bag =
54 new BarabasiAlbertGenerator<V,E>(graphFactory, vertexFactory, edgeFactory,
55 4, 3, //false, parallel,
56 seed, seedVertices);
57 bag.evolveGraph(num_vertices - 4);
58 Graph<V, E> ug = bag.get();
59
60 Graph<V, E> g = graphFactory.get();
61 for(V v : ug.getVertices()) {
62 g.addVertex(v);
63 }
64
65 // randomly replace some of the edges by directed edges to
66 // get a mixed-mode graph, add random weights
67
68 for(E e : ug.getEdges()) {
69 V v1 = ug.getEndpoints(e).getFirst();
70 V v2 = ug.getEndpoints(e).getSecond();
71
72 E me = edgeFactory.get();
73 g.addEdge(me, v1, v2, Math.random() < .5 ? EdgeType.DIRECTED : EdgeType.UNDIRECTED);
74 edge_weights.put(me, Math.random());
75 }
76
77 return g;
78 }
79
80 }