/** {@inheritDoc} */ public void generateGraph( Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap) { if (size < 1) { return; } // Add all the vertices to the set for (int i = 0; i < size; i++) { V newVertex = vertexFactory.createVertex(); target.addVertex(newVertex); } /* * We want two iterators over the vertex set, one fast and one slow. * The slow one will move through the set once. For each vertex, * the fast iterator moves through the set, adding an edge to all * vertices we haven't connected to yet. * * If we have an undirected graph, the second addEdge call will return * nothing; it will not add a second edge. */ Iterator<V> slowI = target.vertexSet().iterator(); Iterator<V> fastI; while (slowI.hasNext()) { // While there are more vertices in the set V latestVertex = slowI.next(); fastI = target.vertexSet().iterator(); // Jump to the first vertex *past* latestVertex while (fastI.next() != latestVertex) {; } // And, add edges to all remaining vertices V temp; while (fastI.hasNext()) { temp = fastI.next(); target.addEdge(latestVertex, temp); target.addEdge(temp, latestVertex); } } }
/** {@inheritDoc} */ public void generateGraph( Graph<V, E> target, final VertexFactory<V> vertexFactory, Map<String, V> resultMap) { if (size < 1) { return; } // A little trickery to intercept the rim generation. This is // necessary since target may be initially non-empty, meaning we can't // rely on its vertex set after the rim is generated. final Collection<V> rim = new ArrayList<V>(); VertexFactory<V> rimVertexFactory = new VertexFactory<V>() { public V createVertex() { V vertex = vertexFactory.createVertex(); rim.add(vertex); return vertex; } }; RingGraphGenerator<V, E> ringGenerator = new RingGraphGenerator<V, E>(size - 1); ringGenerator.generateGraph(target, rimVertexFactory, resultMap); V hubVertex = vertexFactory.createVertex(); target.addVertex(hubVertex); if (resultMap != null) { resultMap.put(HUB_VERTEX, hubVertex); } for (V rimVertex : rim) { if (inwardSpokes) { target.addEdge(rimVertex, hubVertex); } else { target.addEdge(hubVertex, rimVertex); } } }
/** {@inheritDoc} */ public void generateGraph( Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap) { V lastVertex = null; for (int i = 0; i < size; ++i) { V newVertex = vertexFactory.createVertex(); target.addVertex(newVertex); if (lastVertex == null) { if (resultMap != null) { resultMap.put(START_VERTEX, newVertex); } } else { target.addEdge(lastVertex, newVertex); } lastVertex = newVertex; } if ((resultMap != null) && (lastVertex != null)) { resultMap.put(END_VERTEX, lastVertex); } }