KickJava   Java API By Example, From Geeks To Geeks.

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


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  * RandomGraphHelper.java
27  * -------------------
28  * (C) Copyright 2003-2006, by Michael Behrisch and Contributors.
29  *
30  * Original Author: Michael Behrisch
31  * Contributor(s): -
32  *
33  * $Id: RandomGraphHelper.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
47
48 /**
49  * UniformRandomGraphGenerator generates a <a
50  * HREF="http://mathworld.wolfram.com/RandomGraph.html">uniform random graph</a>
51  * of any size. A uniform random graph contains edges chosen independently
52  * uniformly at random from the set of all possible edges.
53  *
54  * @author Michael Behrisch
55  * @since Sep 13, 2004
56  */

57 public final class RandomGraphHelper
58 {
59
60     //~ Static fields/initializers --------------------------------------------
61

62     private static final Random randSingleton = new Random();
63
64     //~ Constructors ----------------------------------------------------------
65

66     /**
67      * .
68      */

69     private RandomGraphHelper()
70     {
71     }
72
73     //~ Methods ---------------------------------------------------------------
74

75     /**
76      * @see GraphGenerator#generateGraph
77      */

78     @SuppressWarnings JavaDoc("unchecked")
79     public static void addEdges(
80         Graph target,
81         List sourceVertices,
82         List destVertices,
83         int numEdges)
84     {
85         int sourceSize = sourceVertices.size();
86         int destSize = destVertices.size();
87
88         for (int i = 0; i < numEdges; ++i) {
89             while (
90                 target.addEdge(
91                     sourceVertices.get(randSingleton.nextInt(
92                             sourceSize)),
93                     destVertices.get(randSingleton.nextInt(destSize)))
94                 == null) {
95                 ;
96             }
97         }
98     }
99
100     /**
101      * .
102      *
103      * @param target
104      * @param vertexFactory
105      * @param numVertices
106      *
107      * @return
108      */

109     @SuppressWarnings JavaDoc("unchecked")
110     public static Object JavaDoc [] addVertices(
111         Graph target,
112         VertexFactory vertexFactory,
113         int numVertices)
114     {
115         Object JavaDoc [] vertices = new Object JavaDoc [numVertices];
116
117         for (int i = 0; i < numVertices; ++i) {
118             vertices[i] = vertexFactory.createVertex();
119             target.addVertex(vertices[i]);
120         }
121
122         return vertices;
123     }
124 }
125
Popular Tags