Line | Hits | Source |
---|---|---|
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) | |
37 | 20 | { |
38 | 20 | if (numVertices <= 0) { |
39 | 0 | throw new IllegalArgumentException("A positive # of vertices must be specified."); |
40 | } | |
41 | 20 | mNumVertices = numVertices; |
42 | 20 | if (p < 0 || p > 1) { |
43 | 0 | throw new IllegalArgumentException("p must be between 0 and 1."); |
44 | } | |
45 | 20 | mEdgeConnectionProbability = p; |
46 | 20 | mRandom = new Random(); |
47 | 20 | } |
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() { | |
54 | 20 | UndirectedGraph g = new UndirectedSparseGraph(); |
55 | 20 | GraphUtils.addVertices(g,mNumVertices); |
56 | 20 | Object[] v_array = g.getVertices().toArray(); |
57 | ||
58 | 2000 | for (int i = 0; i < mNumVertices-1; i++) |
59 | { | |
60 | 1980 | Vertex v_i = (Vertex) v_array[i]; |
61 | 100980 | for (int j = i+1; j < mNumVertices; j++) |
62 | { | |
63 | 99000 | Vertex v_j = (Vertex) v_array[j]; |
64 | 99000 | if (mRandom.nextDouble() < mEdgeConnectionProbability) |
65 | 10860 | g.addEdge(new UndirectedSparseEdge(v_i, v_j)); |
66 | } | |
67 | } | |
68 | 20 | return g; |
69 | } | |
70 | ||
71 | public void setSeed(long seed) { | |
72 | 20 | mRandom.setSeed(seed); |
73 | 20 | } |
74 | } | |
75 | ||
76 | ||
77 | ||
78 | ||
79 | ||
80 | ||
81 | ||
82 | ||
83 | ||
84 | ||
85 |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |