org.jgrapht.graph.SimpleWeightedGraph Java Examples

The following examples show how to use org.jgrapht.graph.SimpleWeightedGraph. 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: Z0BusGroup.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
private void addToGraph(Line line, Bus b1, Bus b2) {
    if (graph == null) {
        // Lazy creation of graph
        graph = new SimpleWeightedGraph<>(Z0Edge.class);
    }
    graph.addVertex(b1);
    graph.addVertex(b2);
    graph.addEdge(b1, b2, new Z0Edge(line));
}
 
Example #2
Source File: CutClustering.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
protected void initializeGraph() {
    weightedGraph = new SimpleWeightedGraph<>(DefaultWeightedEdge.class);

    String sinkLabel = "" + noOfEntities;
    weightedGraph.addVertex(sinkLabel); //add the artificial sink
    for (int i = 0; i < noOfEntities; i++) {
        String edgeLabel = i + "";
        weightedGraph.addVertex(edgeLabel);
        DefaultWeightedEdge e = (DefaultWeightedEdge) weightedGraph.addEdge(sinkLabel, edgeLabel); // add the capacity edges "a"
        weightedGraph.setEdgeWeight(e, Acap); //connecting the artificial sink with all vertices
    }

    Log.info("Added " + noOfEntities + " nodes in the graph");
}
 
Example #3
Source File: GomoryHuTree.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
private DefaultDirectedWeightedGraph<V, DefaultWeightedEdge> makeDirectedCopy(SimpleWeightedGraph<V, E> graph) {
    final DefaultDirectedWeightedGraph<V, DefaultWeightedEdge> copy = new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class);

    Graphs.addAllVertices(copy, graph.vertexSet());
    graph.edgeSet().forEach((e) -> {
        V v1 = graph.getEdgeSource(e);
        V v2 = graph.getEdgeTarget(e);
        Graphs.addEdge(copy, v1, v2, graph.getEdgeWeight(e));
        Graphs.addEdge(copy, v2, v1, graph.getEdgeWeight(e));
    });
    
    return copy;
}
 
Example #4
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public IPath getCircuit(final IScope scope) {
	final SimpleWeightedGraph g = new SimpleWeightedGraph(getEdgeFactory());
	Graphs.addAllEdges(g, this, edgeSet());
	final List vertices = HamiltonianCycle.getApproximateOptimalForCompleteGraph(g);
	final int size = vertices.size();
	final IList edges = GamaListFactory.create(getGamlType().getContentType());
	for (int i = 0; i < size - 1; i++) {
		edges.add(this.getEdge(vertices.get(i), vertices.get(i + 1)));
	}
	return pathFromEdges(scope, null, null, edges);
}
 
Example #5
Source File: EulerianCircuitUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Before
public void createGraphWithEulerianCircuit() {
    simpleGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
    IntStream.range(1, 6).forEach(i -> {
        simpleGraph.addVertex("v" + i);
    });
    IntStream.range(1, 6).forEach(i -> {
        int endVertexNo = (i + 1) > 5 ? 1 : i + 1;
        simpleGraph.addEdge("v" + i, "v" + endVertexNo);
    });
}
 
Example #6
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 #7
Source File: ModelBasedGraphGenerator.java    From JQF with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private Graph<Integer, DefaultEdge> createGraph() {
    Class<? extends DefaultEdge> edgeClass =
            model.weighted() ? DefaultWeightedEdge.class : DefaultEdge.class;

    if (model.loops()) {
        if (model.multiGraph() == false) {
            throw new IllegalArgumentException("Self-loops are only supported " +
                    "with multi-graphs");
        }
        if (isDirected()) {
            if (model.weighted()) {
                return new DirectedWeightedPseudograph<>(edgeClass);
            } else {
                return new DirectedPseudograph<>(edgeClass);
            }
        } else {
            if (model.weighted()) {
                return new WeightedPseudograph<>(edgeClass);
            } else {
                return new Pseudograph<>(edgeClass);
            }
        }
    } else {
        if (model.multiGraph()) {
            if (isDirected()) {
                if (model.weighted()) {
                    return new DirectedWeightedMultigraph<>(edgeClass);
                } else {
                    return new DirectedMultigraph<>(edgeClass);
                }
            } else {
                if (model.weighted()) {
                    return new WeightedMultigraph<>(edgeClass);
                } else {
                    return new Multigraph<>(edgeClass);
                }
            }
        } else {
            if (isDirected()) {
                if (model.weighted()) {
                    return new SimpleDirectedWeightedGraph<>(edgeClass);
                } else {
                    return new SimpleDirectedGraph<>(edgeClass);
                }
            } else {
                if (model.weighted()) {
                    return new SimpleWeightedGraph<>(edgeClass);
                } else {
                    return new SimpleGraph<>(edgeClass);
                }
            }
        }
    }

}
 
Example #8
Source File: GomoryHuTree.java    From JedAIToolkit with Apache License 2.0 4 votes vote down vote up
public GomoryHuTree(SimpleWeightedGraph<V, E> graph) {
    this.graph = graph;
}
 
Example #9
Source File: SubtourSeparator.java    From jorlib with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * This method instantiates the Subtour Separator. The input can be any type of graph: directed, undirected, or mixed,
 * complete or incomplete, weighted or without weights. Internally, this class converts the given graph to a undirected graph. 
 * Multiple edges between two vertices i,j, for example two direct arc (i,j) and (j,i)) are aggregated into in undirected edge (i,j).
 * WARNING: if the input graph is modified, i.e. edges or vertices are added/removed then the behavior of this class is undefined!
 * 			A new instance should of this class should be made if this happens!
 * @param inputGraph input graph
 */
public SubtourSeparator(Graph<V,E> inputGraph){
	this.inputGraph=inputGraph;
	this.workingGraph=new SimpleWeightedGraph<>(DefaultWeightedEdge.class);
	Graphs.addAllVertices(workingGraph, inputGraph.vertexSet());
	for(E edge : inputGraph.edgeSet())
		Graphs.addEdge(workingGraph, inputGraph.getEdgeSource(edge), inputGraph.getEdgeTarget(edge),0);
}