Coverage details for edu.uci.ics.jung.random.generators.ErdosRenyiGenerator

LineHitsSource
1 /*
2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
3 * of California
4 * All rights reserved.
5 *
6 * This software is open-source under the BSD license; see either
7 * "license.txt" or
8 * http://jung.sourceforge.net/license.txt for a description.
9 */
10 package edu.uci.ics.jung.random.generators;
11  
12 import java.util.Random;
13  
14 import edu.uci.ics.jung.graph.ArchetypeGraph;
15 import edu.uci.ics.jung.graph.UndirectedGraph;
16 import edu.uci.ics.jung.graph.Vertex;
17 import edu.uci.ics.jung.graph.impl.UndirectedSparseEdge;
18 import edu.uci.ics.jung.graph.impl.UndirectedSparseGraph;
19 import edu.uci.ics.jung.utils.GraphUtils;
20  
21 /**
22  * Random Generator of Erdos-Renyi "binomial model"
23  * @author William Giordano, Scott White, Joshua O'Madadhain
24  */
25 public class ErdosRenyiGenerator implements GraphGenerator
26 {
27     private int mNumVertices;
28     private double mEdgeConnectionProbability;
29     private Random mRandom;
30  
31     /**
32      *
33      * @param numVertices number of vertices graph should have
34      * @param p Connection's probability between 2 vertices
35      */
36     public ErdosRenyiGenerator(int numVertices,double p)
3720    {
3820        if (numVertices <= 0) {
390            throw new IllegalArgumentException("A positive # of vertices must be specified.");
40         }
4120        mNumVertices = numVertices;
4220        if (p < 0 || p > 1) {
430            throw new IllegalArgumentException("p must be between 0 and 1.");
44         }
4520        mEdgeConnectionProbability = p;
4620        mRandom = new Random();
4720    }
48  
49     /**
50      * Returns a graph in which each pair of vertices is connected by
51      * an undirected edge with the probability specified by the constructor.
52      */
53     public ArchetypeGraph generateGraph() {
5420        UndirectedGraph g = new UndirectedSparseGraph();
5520        GraphUtils.addVertices(g,mNumVertices);
5620        Object[] v_array = g.getVertices().toArray();
57  
582000        for (int i = 0; i < mNumVertices-1; i++)
59         {
601980            Vertex v_i = (Vertex) v_array[i];
61100980            for (int j = i+1; j < mNumVertices; j++)
62             {
6399000                Vertex v_j = (Vertex) v_array[j];
6499000                if (mRandom.nextDouble() < mEdgeConnectionProbability)
6510860                    g.addEdge(new UndirectedSparseEdge(v_i, v_j));
66             }
67         }
6820        return g;
69     }
70  
71     public void setSeed(long seed) {
7220        mRandom.setSeed(seed);
7320    }
74 }
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.