KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > experimental > UniformRandomGraphGenerator


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* -------------------
26  * UniformRandomGraphGenerator.java
27  * -------------------
28  * (C) Copyright 2003-2006, by Michael Behrisch and Contributors.
29  *
30  * Original Author: Michael Behrisch
31  * Contributor(s): -
32  *
33  * $Id: UniformRandomGraphGenerator.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 13-Sep-2004 : Initial revision (MB);
38  *
39  */

40 // package org.jgrapht.generate;
41
package org.jgrapht.experimental;
42
43 import java.util.*;
44
45 import org.jgrapht.*;
46 import org.jgrapht.generate.*;
47
48
49 /**
50  * UniformRandomGraphGenerator generates a <a
51  * HREF="http://mathworld.wolfram.com/RandomGraph.html">uniform random graph</a>
52  * of any size. A uniform random graph contains edges chosen independently
53  * uniformly at random from the set of all possible edges.
54  *
55  * @author Michael Behrisch
56  * @since Sep 13, 2004
57  */

58 public class UniformRandomGraphGenerator
59     implements GraphGenerator
60 {
61
62     //~ Instance fields -------------------------------------------------------
63

64     private final int numEdges;
65     private final int numVertices;
66
67     //~ Constructors ----------------------------------------------------------
68

69     /**
70      * Construct a new UniformRandomGraphGenerator.
71      *
72      * @param numVertices number of vertices to be generated
73      * @param numEdges number of edges to be generated
74      *
75      * @throws IllegalArgumentException
76      */

77     public UniformRandomGraphGenerator(int numVertices, int numEdges)
78     {
79         if (numVertices < 0) {
80             throw new IllegalArgumentException JavaDoc("must be non-negative");
81         }
82
83         if (
84             (numEdges < 0)
85             || (numEdges > (numVertices * (numVertices - 1) / 2))) {
86             throw new IllegalArgumentException JavaDoc("illegal number of edges");
87         }
88
89         this.numVertices = numVertices;
90         this.numEdges = numEdges;
91     }
92
93     //~ Methods ---------------------------------------------------------------
94

95     /**
96      * @see GraphGenerator#generateGraph
97      */

98     public void generateGraph(
99         Graph target,
100         VertexFactory vertexFactory,
101         Map resultMap)
102     {
103         Object JavaDoc [] vertices =
104             RandomGraphHelper.addVertices(
105                 target,
106                 vertexFactory,
107                 numVertices);
108         RandomGraphHelper.addEdges(
109             target,
110             Arrays.asList(vertices),
111             Arrays.asList(vertices),
112             numEdges);
113     }
114 }
115
Popular Tags