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

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 import java.util.Random;
12  
13 import edu.uci.ics.jung.graph.ArchetypeGraph;
14 import edu.uci.ics.jung.graph.UndirectedGraph;
15 import edu.uci.ics.jung.graph.Vertex;
16 import edu.uci.ics.jung.graph.decorators.Indexer;
17 import edu.uci.ics.jung.graph.impl.SparseVertex;
18 import edu.uci.ics.jung.graph.impl.UndirectedSparseEdge;
19 import edu.uci.ics.jung.graph.impl.UndirectedSparseGraph;
20 /**
21  * Simple graph generator where |V| vertices are generated and |E| random edges
22  * chosen pairwise uniformly
23  *
24  * @author William Giordano, Scott White
25  */
26 public class SimpleRandomGenerator implements GraphGenerator {
27     private int mNumVertices;
28     private int mNumEdges;
29     
30     protected Vertex newVertex() {
310        return new SparseVertex();
32     }
33     
34     /**
35      * Constructs the generator
36      *
37      * @param numVertices
38      * number of vertices the graph should have
39      * @param numEdges
40      * number of edges the graph should have
41      */
420    public SimpleRandomGenerator(int numVertices, int numEdges) {
430        if (numVertices <= 0) {
440            throw new IllegalArgumentException(
45                     "A positive # of vertices must be specified.");
46         }
470        mNumVertices = numVertices;
480        long calcVertices = numVertices;
490        if (numEdges < 0 || numEdges > (calcVertices * (calcVertices - 1) / 2)) {
500            throw new IllegalArgumentException(
51                     "# of edges [" + numEdges +"] must be between 0 and |V|(|V|-1)/2, v=" + numVertices);
52         }
530        mNumEdges = numEdges;
540    }
55     
56     public void setSeed( long seed ) {
570        this.seedSet = true;
580        this.seed = seed;
590    }
60     
610    boolean seedSet = false;
620    long seed = 0;
63     
64     /**
65      * Generated the graph by creating |V| vertics and then picking |E| random
66      * edges
67      *
68      * @return generated graph
69      */
70     public ArchetypeGraph generateGraph() {
71         Random rand;
720        if ( seedSet ) {
730            rand = new Random(seed);
74         } else {
750            rand = new Random();
76         }
770        UndirectedGraph g = new UndirectedSparseGraph();
780        for( int i = 0; i < mNumVertices; i ++ ) {
790            g.addVertex( newVertex() );
80         }
81 // GraphUtils.addVertices(g, mNumVertices);
820        Indexer id = Indexer.getIndexer(g);
830        int ctr = 0;
840        while (ctr < mNumEdges) {
850            Vertex v1 = (Vertex) id.getVertex(rand.nextInt(mNumVertices));
860            Vertex v2 = (Vertex) id.getVertex(rand.nextInt(mNumVertices));
870            if ((v1 != v2) && !v1.isNeighborOf(v2)) {
880                g.addEdge(new UndirectedSparseEdge(v1, v2));
890                ctr++;
90             }
91         }
920        return g;
93     }
94     /**
95      * @return Returns the mNumEdges.
96      */
97     public long getNumEdges() {
980        return mNumEdges;
99     }
100     /**
101      * @return Returns the mNumVertices.
102      */
103     public long getNumVertices() {
1040        return mNumVertices;
105     }
106 }
107         

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.