View Javadoc
1   /*
2   * Copyright (c) 2003, The JUNG Authors
3   *
4   * All rights reserved.
5   *
6   * This software is open-source under the BSD license; see either
7   * "license.txt" or
8   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9   */
10  package edu.uci.ics.jung.algorithms.generators.random;
11  
12  import java.util.ArrayList;
13  import java.util.List;
14  import java.util.Random;
15  
16  import com.google.common.base.Supplier;
17  
18  import edu.uci.ics.jung.algorithms.generators.GraphGenerator;
19  import edu.uci.ics.jung.graph.Graph;
20  import edu.uci.ics.jung.graph.UndirectedGraph;
21  
22  /**
23   * Generates a random graph using the Erdos-Renyi binomial model
24   * (each pair of vertices is connected with probability p).
25   * 
26   *  @author William Giordano, Scott White, Joshua O'Madadhain
27   */
28  public class ErdosRenyiGenerator<V,E> implements GraphGenerator<V,E> {
29      private int mNumVertices;
30      private double mEdgeConnectionProbability;
31      private Random mRandom;
32      Supplier<UndirectedGraph<V,E>> graphFactory;
33      Supplier<V> vertexFactory;
34      Supplier<E> edgeFactory;
35  
36      /**
37       *
38       * @param graphFactory factory for graphs of the appropriate type
39       * @param vertexFactory factory for vertices of the appropriate type
40       * @param edgeFactory factory for edges of the appropriate type
41       * @param numVertices number of vertices graph should have
42       * @param p Connection's probability between 2 vertices
43       */
44  	public ErdosRenyiGenerator(Supplier<UndirectedGraph<V,E>> graphFactory,
45  			Supplier<V> vertexFactory, Supplier<E> edgeFactory,
46  			int numVertices,double p)
47      {
48          if (numVertices <= 0) {
49              throw new IllegalArgumentException("A positive # of vertices must be specified.");
50          }
51          mNumVertices = numVertices;
52          if (p < 0 || p > 1) {
53              throw new IllegalArgumentException("p must be between 0 and 1.");
54          }
55          this.graphFactory = graphFactory;
56          this.vertexFactory = vertexFactory;
57          this.edgeFactory = edgeFactory;
58          mEdgeConnectionProbability = p;
59          mRandom = new Random();
60  	}
61  
62      /**
63       * Returns a graph in which each pair of vertices is connected by 
64       * an undirected edge with the probability specified by the constructor.
65       */
66  	public Graph<V,E> get() {
67          UndirectedGraph<V,E> g = graphFactory.get();
68          for(int i=0; i<mNumVertices; i++) {
69          	g.addVertex(vertexFactory.get());
70          }
71          List<V> list = new ArrayList<V>(g.getVertices());
72  
73  		for (int i = 0; i < mNumVertices-1; i++) {
74              V v_i = list.get(i);
75  			for (int j = i+1; j < mNumVertices; j++) {
76                  V v_j = list.get(j);
77  				if (mRandom.nextDouble() < mEdgeConnectionProbability) {
78  					g.addEdge(edgeFactory.get(), v_i, v_j);
79  				}
80  			}
81  		}
82          return g;
83      }
84  
85      /**
86       * Sets the seed of the internal random number generator to {@code seed}.
87       * Enables consistent behavior.
88       * 
89       * @param seed the seed to use for the internal random number generator
90       */
91      public void setSeed(long seed) {
92          mRandom.setSeed(seed);
93      }
94  }
95  
96  
97  
98  
99  
100 
101 
102 
103 
104 
105