org.jgrapht.WeightedGraph Java Examples

The following examples show how to use org.jgrapht.WeightedGraph. 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 6 votes vote down vote up
@Override
public Graph generate(SourceOfRandomness sourceOfRandomness, GenerationStatus generationStatus) {
    // Ensure that a model exists
    if (model == null) {
        throw new IllegalArgumentException("Graph generators MUST be configured with @GraphModel");
    }

    // Create graph instance
    Graph<Integer, DefaultEdge> graph = createGraph();

    // Generate nodes and edges using JGraphT generators
    getModel(model, sourceOfRandomness)
            .generateGraph(graph, createNodeFactory(), null);

    // If weighted, generate edge weights
    if (model.weighted()) {
        WeightedGraph<Integer, DefaultEdge> wgraph = (WeightedGraph<Integer, DefaultEdge>) graph;
        // Set edges
        for (DefaultEdge e : wgraph.edgeSet()) {
            wgraph.setEdgeWeight(e, sourceOfRandomness.nextFloat());
        }
    }

    // Return handle to this graph
    return graph;
}
 
Example #2
Source File: GraphMLReader.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private void setEdgeWeightIfRequired(final E e, final Map<String, Attribute> attributes) {
    // special handling for weighted graphs
    if (g instanceof WeightedGraph<?, ?> && attributes.containsKey(EDGE_WEIGHT_ATTRIBUTE_NAME)) {
        try {
            ((WeightedGraph<V, E>) g).setEdgeWeight(e, Float.valueOf(attributes.get(EDGE_WEIGHT_ATTRIBUTE_NAME).getValue()));
        } catch (final NumberFormatException nfe) {
            ((WeightedGraph<V, E>) g).setEdgeWeight(e, getEdgeWeight());
        }
    }
}
 
Example #3
Source File: GraphMLReader.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private double getEdgeWeight() {
    for (final Key k : edgeValidKeys.values()) {
        if (k.attributeName.equals(EDGE_WEIGHT_ATTRIBUTE_NAME)) {
            try {
                if (k.defaultValue != null) {
                    return Double.parseDouble(k.defaultValue);
                }
            } catch (final NumberFormatException e) {
                return WeightedGraph.DEFAULT_EDGE_WEIGHT;
            }
        }
    }

    return WeightedGraph.DEFAULT_EDGE_WEIGHT;
}
 
Example #4
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 #5
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
@Override
public double getEdgeWeight(final Object e) {
	if (!containsEdge(e)) { return WeightedGraph.DEFAULT_EDGE_WEIGHT; }
	return getEdge(e).getWeight();
}
 
Example #6
Source File: _Edge.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
public _Edge(final GamaGraph<V, E> gamaGraph, final Object edge, final Object source, final Object target)
		throws GamaRuntimeException {
	this(gamaGraph, edge, source, target, WeightedGraph.DEFAULT_EDGE_WEIGHT);
}