org.jgrapht.VertexFactory Java Examples

The following examples show how to use org.jgrapht.VertexFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModelBasedGraphGenerator.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static VertexFactory<Integer> createNodeFactory() {
    return new VertexFactory<Integer>() {
        int nodeId = 1;

        @Override
        public Integer createVertex() {
            return this.nodeId++;
        }
    };
}
 
Example #2
Source File: DIMACSImporter.java    From jorlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void generateGraph(
        Graph<V, E> target,
        VertexFactory<V> vertexFactory,
        Map<String, V> resultMap)
{
    final int size = readNodeCount();
    if (resultMap == null) {
        resultMap = new HashMap<>();
    }

    for (int i = 0; i < size; i++) {
        V newVertex = vertexFactory.createVertex();
        target.addVertex(newVertex);
        resultMap.put(Integer.toString(i + 1), newVertex);
    }
    String[] cols = skipComments();
    while (cols != null) {
        if (cols[0].equals("e")) {
            E edge = target
                    .addEdge(resultMap.get(cols[1]), resultMap.get(cols[2]));
            if (target instanceof WeightedGraph && (edge != null)) {
                double weight = defaultWeight;
                if (cols.length > 3) {
                    weight = Double.parseDouble(cols[3]);
                }
                ((WeightedGraph<V, E>) target).setEdgeWeight(edge, weight);
            }
        }
        cols = skipComments();
    }
}
 
Example #3
Source File: CompleteGraphUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Before
public void createCompleteGraph() {
    completeGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
    CompleteGraphGenerator<String, DefaultEdge> completeGenerator = new CompleteGraphGenerator<String, DefaultEdge>(size);
    VertexFactory<String> vFactory = new VertexFactory<String>() {
        private int id = 0;
        public String createVertex() {
            return "v" + id++;
        }
    };
    completeGenerator.generateGraph(completeGraph, vFactory, null);
}
 
Example #4
Source File: BitSetBasedSimpleGraphGenerator.java    From JQF with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void generateGraph(Graph<V, E> graph, VertexFactory<V> vertexFactory, Map<String, V> map) {
    // Calculate number of edges to generate
    boolean isDirected = graph instanceof DirectedGraph;
    int numEdges = numEdges(isDirected);

    // Figure out how many random bytes to generate for this purpose
    int numBytes = (numEdges + 7) / 8;  // Equal to ceil(numEdges/8.0)
    byte[] bytes = new byte[numBytes];

    // Generate random bytes
    rng.nextBytes(bytes);

    // Convert to bitset
    BitSet bitSet = BitSet.valueOf(bytes);

    // Generate nodes
    V[] vertices = (V[]) new Object[n];
    for(int i = 0; i < n; ++i) {
        V v = vertexFactory.createVertex();
        graph.addVertex(v);
        vertices[i] = v;
    }

    // Add edges as necessary
    int k = 0;
    for (int i = 0; i < n; i++) {
        V s = vertices[i];
        for (int j = 0; j < i; j++) {
            V t = vertices[j];
            // Get next boolean to decide s --> t
            if (bitSet.get(k++)) {
                graph.addEdge(s, t);
            }

            if (isDirected) {
                // Get next boolean to decide t --> t
                if (bitSet.get(k++)) {
                    graph.addEdge(t, s);
                }
            }
        }
    }
    // Sanity check
    assert(k == numEdges);




}