KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > generate > WheelGraphGenerator


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  * WheelGraphGenerator.java
27  * -------------------
28  * (C) Copyright 2003-2006, by John V. Sichi and Contributors.
29  *
30  * Original Author: John V. Sichi
31  * Contributor(s): -
32  *
33  * $Id: WheelGraphGenerator.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 16-Sep-2003 : Initial revision (JVS);
38  *
39  */

40 package org.jgrapht.generate;
41
42 import java.util.*;
43
44 import org.jgrapht.*;
45
46
47 /**
48  * Generates a <a HREF="http://mathworld.wolfram.com/WheelGraph.html">wheel
49  * graph</a> of any size. Reminding a bicycle wheel, a wheel graph has a hub
50  * vertex in the center and a rim of vertices around it that are connected to
51  * each other (as a ring). The rim vertices are also connected to the hub with
52  * edges that are called "spokes".
53  *
54  * @author John V. Sichi
55  * @since Sep 16, 2003
56  */

57 public class WheelGraphGenerator<V, E>
58     implements GraphGenerator<V, E, V>
59 {
60
61     //~ Static fields/initializers --------------------------------------------
62

63     /**
64      * Role for the hub vertex.
65      */

66     public static final String JavaDoc HUB_VERTEX = "Hub Vertex";
67
68     //~ Instance fields -------------------------------------------------------
69

70     private boolean inwardSpokes;
71     private int size;
72
73     //~ Constructors ----------------------------------------------------------
74

75     /**
76      * Creates a new WheelGraphGenerator object. This constructor is more
77      * suitable for undirected graphs, where spokes' direction is meaningless.
78      * In the directed case, spokes will be oriented from rim to hub.
79      *
80      * @param size number of vertices to be generated.
81      */

82     public WheelGraphGenerator(int size)
83     {
84         this(size, true);
85     }
86
87     /**
88      * Construct a new WheelGraphGenerator.
89      *
90      * @param size number of vertices to be generated.
91      * @param inwardSpokes if <code>true</code> and graph is directed, spokes
92      * are oriented from rim to hub; else from hub to rim.
93      *
94      * @throws IllegalArgumentException
95      */

96     public WheelGraphGenerator(int size, boolean inwardSpokes)
97     {
98         if (size < 0) {
99             throw new IllegalArgumentException JavaDoc("must be non-negative");
100         }
101
102         this.size = size;
103         this.inwardSpokes = inwardSpokes;
104     }
105
106     //~ Methods ---------------------------------------------------------------
107

108     /**
109      * {@inheritDoc}
110      */

111     public void generateGraph(
112         Graph<V, E> target,
113         final VertexFactory<V> vertexFactory,
114         Map<String JavaDoc, V> resultMap)
115     {
116         if (size < 1) {
117             return;
118         }
119
120         // A little trickery to intercept the rim generation. This is
121
// necessary since target may be initially non-empty, meaning we can't
122
// rely on its vertex set after the rim is generated.
123
final Collection<V> rim = new ArrayList<V>();
124         VertexFactory<V> rimVertexFactory =
125             new VertexFactory<V>() {
126                 public V createVertex()
127                 {
128                     V vertex = vertexFactory.createVertex();
129                     rim.add(vertex);
130
131                     return vertex;
132                 }
133             };
134
135         RingGraphGenerator<V, E> ringGenerator =
136             new RingGraphGenerator<V, E>(size - 1);
137         ringGenerator.generateGraph(target, rimVertexFactory, resultMap);
138
139         V hubVertex = vertexFactory.createVertex();
140         target.addVertex(hubVertex);
141
142         if (resultMap != null) {
143             resultMap.put(HUB_VERTEX, hubVertex);
144         }
145
146         for (V rimVertex : rim) {
147             if (inwardSpokes) {
148                 target.addEdge(rimVertex, hubVertex);
149             } else {
150                 target.addEdge(hubVertex, rimVertex);
151             }
152         }
153     }
154 }
155
Popular Tags