| Line | Hits | Source |
|---|---|---|
| 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 | /* | |
| 11 | * Created on Jan 8, 2004 | |
| 12 | * | |
| 13 | */ | |
| 14 | package edu.uci.ics.jung.random.generators; | |
| 15 | ||
| 16 | import edu.uci.ics.jung.graph.ArchetypeGraph; | |
| 17 | import edu.uci.ics.jung.graph.Graph; | |
| 18 | import edu.uci.ics.jung.graph.Vertex; | |
| 19 | import edu.uci.ics.jung.graph.decorators.Indexer; | |
| 20 | import edu.uci.ics.jung.graph.impl.UndirectedSparseGraph; | |
| 21 | import edu.uci.ics.jung.utils.GraphUtils; | |
| 22 | ||
| 23 | /** | |
| 24 | * Simple generator of an n x 1 lattice where each vertex | |
| 25 | * is incident with each of its 2 neighbors (except possibly | |
| 26 | * for the vertices on the edge depending upon whether the lattice | |
| 27 | * is specified to be toroidal or not). | |
| 28 | * @author Scott White | |
| 29 | * | |
| 30 | */ | |
| 31 | public class Lattice1DGenerator implements GraphGenerator | |
| 32 | { | |
| 33 | private int mNumNodes; | |
| 34 | private boolean mIsToroidal; | |
| 35 | ||
| 36 | /** | |
| 37 | * Constructs an instance of the lattice generator | |
| 38 | * @param numNodes # of nodes in the generated graph | |
| 39 | * @param isToroidal whether the lattice wraps around or not | |
| 40 | */ | |
| 41 | public Lattice1DGenerator(int numNodes, boolean isToroidal) | |
| 42 | 62 | { |
| 43 | 62 | if (numNodes < 1) |
| 44 | { | |
| 45 | 0 | throw new IllegalArgumentException("Lattice size must be at least 1."); |
| 46 | } | |
| 47 | ||
| 48 | 62 | mNumNodes = numNodes; |
| 49 | 62 | mIsToroidal = isToroidal; |
| 50 | 62 | } |
| 51 | ||
| 52 | /* (non-Javadoc) | |
| 53 | * @see edu.uci.ics.jung.random.generators.GraphGenerator#generateGraph() | |
| 54 | */ | |
| 55 | public ArchetypeGraph generateGraph() | |
| 56 | { | |
| 57 | 1 | Graph g = new UndirectedSparseGraph(); |
| 58 | 1 | GraphUtils.addVertices(g, mNumNodes); |
| 59 | 1 | int upI = 0;//, downI = 0; |
| 60 | 1 | Indexer id = Indexer.getIndexer(g); |
| 61 | ||
| 62 | //create lattice structure | |
| 63 | 6 | for (int i = 0; i < mNumNodes; i++) |
| 64 | { | |
| 65 | 10 | for (int s = 1; s <= 1; s++) |
| 66 | { | |
| 67 | 5 | Vertex ithVertex = (Vertex) id.getVertex(i); |
| 68 | 5 | upI = upIndex(i, s); |
| 69 | 5 | if (i != mNumNodes - 1 || (i == mNumNodes - 1 && mIsToroidal)) |
| 70 | { | |
| 71 | 5 | GraphUtils.addEdge( |
| 72 | g, | |
| 73 | ithVertex, | |
| 74 | (Vertex) id.getVertex(upI)); | |
| 75 | } | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | 1 | return g; |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Determines the vertices with a smaller index that are in the neighborhood of currentIndex. | |
| 84 | * @param numSteps indicates the number of steps away from the current index that are being considered. | |
| 85 | * @param currentIndex the index of the selected vertex. | |
| 86 | */ | |
| 87 | protected int downIndex(int currentIndex, int numSteps) | |
| 88 | { | |
| 89 | 0 | if (currentIndex - numSteps < 0) |
| 90 | { | |
| 91 | 0 | return (mNumNodes - 1) + (currentIndex - numSteps); |
| 92 | } | |
| 93 | 0 | return currentIndex + numSteps; |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Determines the index of the neighbor ksteps above | |
| 98 | * @param numSteps is the number of steps away from the current index that is being considered. | |
| 99 | * @param currentIndex the index of the selected vertex. | |
| 100 | */ | |
| 101 | protected int upIndex(int currentIndex, int numSteps) | |
| 102 | { | |
| 103 | 11195 | if (currentIndex + numSteps > mNumNodes - 1) |
| 104 | { | |
| 105 | 199 | return numSteps - (mNumNodes - currentIndex); |
| 106 | } | |
| 107 | 10996 | return currentIndex + numSteps; |
| 108 | } | |
| 109 | ||
| 110 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |