Java Code Examples for org.jgrapht.Graphs#addAllVertices()

The following examples show how to use org.jgrapht.Graphs#addAllVertices() . 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: SlotsBuilder.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Compute the matrix of inter-chords relationships.
 */
private void buildRelationships ()
{
    // Sort measure standard chords by abscissa
    List<AbstractChordInter> stdChords = new ArrayList<>(stack.getStandardChords());
    Collections.sort(stdChords, Inters.byAbscissa);

    // Populate graph with chords
    Graphs.addAllVertices(graph, stdChords);

    // BeamGroup-based relationships
    inspectBeams();

    // Mirror-based relationships
    inspectMirrors();

    // RootStem-based relationships
    inspectRootStems();

    // Finally, default location-based relationships
    inspectLocations(stdChords);

    if (logger.isDebugEnabled()) {
        dumpRelationships(stdChords);
    }
}
 
Example 2
Source File: PeakGraph.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Use individual staff projections to retrieve bar peaks.
 */
private void findBarPeaks ()
{
    // Analysis staff per staff
    for (Staff staff : staffManager.getStaves()) {
        StaffProjector projector = new StaffProjector(sheet, staff, this);
        projectors.add(projector);
        projector.process();
        Graphs.addAllVertices(this, projector.getPeaks());
    }
}
 
Example 3
Source File: GlyphCluster.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Extract a subgraph limited to the provided collection of glyphs.
 *
 * @param collection the provided collection of glyphs
 * @param graph      the global graph to extract from
 * @param checkEdges true if glyph edges may point outside the provided set.
 * @return the graph limited to glyph set and related edges
 */
public static SimpleGraph<Glyph, GlyphLink> getSubGraph (Collection<Glyph> collection,
                                                         SimpleGraph<Glyph, GlyphLink> graph,
                                                         boolean checkEdges)
{
    // Which edges should be extracted for this set?
    Set<GlyphLink> setEdges = new LinkedHashSet<>();
    for (Glyph glyph : collection) {
        Set<GlyphLink> glyphEdges = graph.edgesOf(glyph);

        if (!checkEdges) {
            setEdges.addAll(glyphEdges); // Take all edges
        } else {
            // Keep only the edges that link within the set
            for (GlyphLink link : glyphEdges) {
                Glyph opposite = Graphs.getOppositeVertex(graph, link, glyph);

                if (collection.contains(opposite)) {
                    setEdges.add(link);
                }
            }
        }
    }
    SimpleGraph<Glyph, GlyphLink> subGraph = new SimpleGraph<>(GlyphLink.class);
    Graphs.addAllVertices(subGraph, collection);
    Graphs.addAllEdges(subGraph, graph, setEdges);
    return subGraph;
}
 
Example 4
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 5
Source File: GamaSpatialGraph.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public GamaSpatialGraph copy(final IScope scope) {
	final GamaSpatialGraph g = new GamaSpatialGraph(GamaListFactory.EMPTY_LIST, true, directed, vertexRelation,
			edgeSpecies, scope, type.getKeyType(), type.getContentType());

	Graphs.addAllVertices(g, this.getVertices());
	Graphs.addAllEdges(g, this, this.edgeSet());
	return g;
}
 
Example 6
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public IGraph copy(final IScope scope) {
	final GamaGraph g = new GamaGraph(scope, GamaListFactory.EMPTY_LIST, true, directed, vertexRelation,
			edgeSpecies, type.getKeyType(), type.getContentType());

	Graphs.addAllVertices(g, this.getVertices());
	Graphs.addAllEdges(g, this, this.edgeSet());
	return g;
}
 
Example 7
Source File: TypeDescription.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public Collection<String> getOrderedAttributeNames(final Set<String> facetsToConsider) {
	// AD Revised in Aug 2019 for Issue #2869: keep constraints between superspecies and subspecies
	final DirectedGraph<String, Object> dependencies = new DefaultDirectedGraph<>(Object.class);
	final Map<String, VariableDescription> all = new HashMap<>();
	this.visitAllAttributes((d) -> {
		all.put(d.getName(), (VariableDescription) d);
		return true;
	});
	Graphs.addAllVertices(dependencies, all.keySet());
	final VariableDescription shape = getAttribute(SHAPE);
	final Collection<VariableDescription> shapeDependencies =
			shape == null ? Collections.EMPTY_LIST : shape.getDependencies(facetsToConsider, false, true);
	all.forEach((an, var) -> {
		for (final VariableDescription newVar : var.getDependencies(facetsToConsider, false, true)) {
			final String other = newVar.getName();
			// AD Revision in April 2019 for Issue #2624: prevent cycles when building the graph
			if (!dependencies.containsEdge(an, other)) {
				dependencies.addEdge(other, an);
			}
		}
		// Adding a constraint between the shape of the macrospecies and the populations of microspecies
		if (var.isSyntheticSpeciesContainer() && !shapeDependencies.contains(var)) {
			dependencies.addEdge(SHAPE, an);
		}
	});
	// June 2020: moving (back) to Iterables instead of Streams.
	return Lists.newArrayList(new TopologicalOrderIterator<>(dependencies));
	// return StreamEx.of(new TopologicalOrderIterator<>(dependencies)).toList();
}
 
Example 8
Source File: SubtourSeparatorDemo.java    From jorlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Example on undirected graph
 */
public static void example1(){
	//Define a new Undirected Graph. For simplicity we'll use a simple, unweighted graph, but in reality this class is mainly used
	//in combination with weighted graphs for TSP problems.
	Graph<Integer, DefaultEdge> undirectedGraph=new SimpleGraph<Integer, DefaultEdge>(DefaultEdge.class);
	Graphs.addAllVertices(undirectedGraph, Arrays.asList(1,2,3,4,5,6));
	undirectedGraph.addEdge(1, 2);
	undirectedGraph.addEdge(2, 3);
	undirectedGraph.addEdge(3, 4);
	undirectedGraph.addEdge(4, 1);
	undirectedGraph.addEdge(1, 5);
	undirectedGraph.addEdge(4, 5);
	undirectedGraph.addEdge(5, 6);
	undirectedGraph.addEdge(2, 6);
	undirectedGraph.addEdge(3, 6);
	
	//Define the x_e values for every edge e\in E
	Map<DefaultEdge, Double> edgeValueMap=new HashMap<DefaultEdge, Double>();
	edgeValueMap.put(undirectedGraph.getEdge(1,2), 0.0);
	edgeValueMap.put(undirectedGraph.getEdge(2,3), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(3,4), 0.0);
	edgeValueMap.put(undirectedGraph.getEdge(4,1), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(1,5), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(4,5), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(5,6), 0.0);
	edgeValueMap.put(undirectedGraph.getEdge(2,6), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(3,6), 1.0);
	
	//Invoke the separator
	SubtourSeparator<Integer, DefaultEdge> separator=new SubtourSeparator<Integer, DefaultEdge>(undirectedGraph);
	separator.separateSubtour(edgeValueMap);
	
	System.out.println("Has found a violated subtour: "+separator.hasSubtour());
	System.out.println("Cut value: "+separator.getCutValue());
	System.out.println("Cut set: "+separator.getCutSet());
	//The returned cut set is: {2,3,6}. This leads to the cut: \sum_{e\in \delta{2,3,6}} x_e >=2
}
 
Example 9
Source File: SubtourSeparatorDemo.java    From jorlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Example on directed graph
 */
public static void example2(){
	//Define a new Directed Graph. For simplicity we'll use a simple, unweighted graph.
	Graph<Integer, DefaultEdge> directedGraph=new SimpleDirectedGraph<Integer, DefaultEdge>(DefaultEdge.class);
	Graphs.addAllVertices(directedGraph, Arrays.asList(1,2,3,4,5,6));
	directedGraph.addEdge(1, 2);
	directedGraph.addEdge(2, 3);
	directedGraph.addEdge(3, 4);
	directedGraph.addEdge(4, 1);
	directedGraph.addEdge(1, 4);
	directedGraph.addEdge(1, 5);
	directedGraph.addEdge(5, 1);
	directedGraph.addEdge(4, 5);
	directedGraph.addEdge(5, 4);
	directedGraph.addEdge(6, 2);
	directedGraph.addEdge(3, 6);
	
	//Define the x_e values for every edge e\in E
	Map<DefaultEdge, Double> edgeValueMap=new HashMap<DefaultEdge, Double>();
	edgeValueMap.put(directedGraph.getEdge(1,2), 0.0);
	edgeValueMap.put(directedGraph.getEdge(2,3), 1.0);
	edgeValueMap.put(directedGraph.getEdge(3,4), 0.0);
	edgeValueMap.put(directedGraph.getEdge(4,1), 0.5);
	edgeValueMap.put(directedGraph.getEdge(1,4), 0.5);
	edgeValueMap.put(directedGraph.getEdge(1,5), 0.5);
	edgeValueMap.put(directedGraph.getEdge(5,1), 0.5);
	edgeValueMap.put(directedGraph.getEdge(4,5), 0.5);
	edgeValueMap.put(directedGraph.getEdge(5,4), 0.5);
	edgeValueMap.put(directedGraph.getEdge(6,2), 1.0);
	edgeValueMap.put(directedGraph.getEdge(3,6), 1.0);
	
	//Invoke the separator
	SubtourSeparator<Integer, DefaultEdge> separator=new SubtourSeparator<Integer, DefaultEdge>(directedGraph);
	separator.separateSubtour(edgeValueMap);
	
	System.out.println("Has found a violated subtour: "+separator.hasSubtour());
	System.out.println("Cut value: "+separator.getCutValue());
	System.out.println("Cut set: "+separator.getCutSet());
	//The returned cut set is: {2,3,6}. This leads to the cut: \sum_{e\in \delta{2,3,6}} x_e >=2
}
 
Example 10
Source File: SubtourSeparatorTest.java    From jorlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Test 1 - Undirected, incomplete graph with a subtour.
 */
public void testUndirectedGraphWithSubtour(){
	//Define a new Undirected Graph. For simplicity we'll use a simple, unweighted graph, but in reality this class is mainly used
	//in combination with weighted graphs for TSP problems.
	Graph<Integer, DefaultEdge> undirectedGraph=new SimpleGraph<Integer, DefaultEdge>(DefaultEdge.class);
	Graphs.addAllVertices(undirectedGraph, Arrays.asList(1,2,3,4,5,6));
	undirectedGraph.addEdge(1, 2);
	undirectedGraph.addEdge(2, 3);
	undirectedGraph.addEdge(3, 4);
	undirectedGraph.addEdge(4, 1);
	undirectedGraph.addEdge(1, 5);
	undirectedGraph.addEdge(4, 5);
	undirectedGraph.addEdge(5, 6);
	undirectedGraph.addEdge(2, 6);
	undirectedGraph.addEdge(3, 6);
	
	//Define the x_e values for every edge e\in E
	Map<DefaultEdge, Double> edgeValueMap=new HashMap<DefaultEdge, Double>();
	edgeValueMap.put(undirectedGraph.getEdge(1,2), 0.0);
	edgeValueMap.put(undirectedGraph.getEdge(2,3), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(3,4), 0.0);
	edgeValueMap.put(undirectedGraph.getEdge(4,1), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(1,5), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(4,5), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(5,6), 0.0);
	edgeValueMap.put(undirectedGraph.getEdge(2,6), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(3,6), 1.0);
	
	//Invoke the separator
	SubtourSeparator<Integer, DefaultEdge> separator=new SubtourSeparator<Integer, DefaultEdge>(undirectedGraph);
	separator.separateSubtour(edgeValueMap);
	
	assertTrue(separator.hasSubtour());
	assertEquals(0, separator.getCutValue(), PRECISION);
	assertEquals(new HashSet<Integer>(Arrays.asList(2,3,6)), separator.getCutSet());
}
 
Example 11
Source File: SubtourSeparatorTest.java    From jorlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Test 2 - Undirected, incomplete graph without a subtour.
 */
public void testUndirectedGraphWithoutSubtour(){
	//Define a new Undirected Graph. For simplicity we'll use a simple, unweighted graph, but in reality this class is mainly used
	//in combination with weighted graphs for TSP problems.
	Graph<Integer, DefaultEdge> undirectedGraph=new SimpleGraph<Integer, DefaultEdge>(DefaultEdge.class);
	Graphs.addAllVertices(undirectedGraph, Arrays.asList(1,2,3,4,5,6));
	undirectedGraph.addEdge(1, 2);
	undirectedGraph.addEdge(2, 3);
	undirectedGraph.addEdge(3, 4);
	undirectedGraph.addEdge(4, 1);
	undirectedGraph.addEdge(1, 5);
	undirectedGraph.addEdge(4, 5);
	undirectedGraph.addEdge(5, 6);
	undirectedGraph.addEdge(2, 6);
	undirectedGraph.addEdge(3, 6);
	
	//Define the x_e values for every edge e\in E
	Map<DefaultEdge, Double> edgeValueMap=new HashMap<DefaultEdge, Double>();
	edgeValueMap.put(undirectedGraph.getEdge(1,2), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(2,3), 0.0);
	edgeValueMap.put(undirectedGraph.getEdge(3,4), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(4,1), 0.0);
	edgeValueMap.put(undirectedGraph.getEdge(1,5), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(4,5), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(5,6), 0.0);
	edgeValueMap.put(undirectedGraph.getEdge(2,6), 1.0);
	edgeValueMap.put(undirectedGraph.getEdge(3,6), 1.0);
	
	//Invoke the separator
	SubtourSeparator<Integer, DefaultEdge> separator=new SubtourSeparator<Integer, DefaultEdge>(undirectedGraph);
	separator.separateSubtour(edgeValueMap);
	
	assertFalse(separator.hasSubtour());
	assertEquals(2, separator.getCutValue(), PRECISION);
}
 
Example 12
Source File: SubtourSeparatorTest.java    From jorlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Test 3 - Directed, incomplete graph without a subtour.
 */
public void testDirectedGraphWithSubtour(){
	//Define a new Directed Graph. For simplicity we'll use a simple, unweighted graph.
	Graph<Integer, DefaultEdge> directedGraph=new SimpleDirectedGraph<Integer, DefaultEdge>(DefaultEdge.class);
	Graphs.addAllVertices(directedGraph, Arrays.asList(1,2,3,4,5,6));
	directedGraph.addEdge(1, 2);
	directedGraph.addEdge(2, 3);
	directedGraph.addEdge(3, 4);
	directedGraph.addEdge(4, 1);
	directedGraph.addEdge(1, 4);
	directedGraph.addEdge(1, 5);
	directedGraph.addEdge(5, 1);
	directedGraph.addEdge(4, 5);
	directedGraph.addEdge(5, 4);
	directedGraph.addEdge(6, 2);
	directedGraph.addEdge(3, 6);
	
	//Define the x_e values for every edge e\in E
	Map<DefaultEdge, Double> edgeValueMap=new HashMap<DefaultEdge, Double>();
	edgeValueMap.put(directedGraph.getEdge(1,2), 0.0);
	edgeValueMap.put(directedGraph.getEdge(2,3), 1.0);
	edgeValueMap.put(directedGraph.getEdge(3,4), 0.0);
	edgeValueMap.put(directedGraph.getEdge(4,1), 0.5);
	edgeValueMap.put(directedGraph.getEdge(1,4), 0.5);
	edgeValueMap.put(directedGraph.getEdge(1,5), 0.5);
	edgeValueMap.put(directedGraph.getEdge(5,1), 0.5);
	edgeValueMap.put(directedGraph.getEdge(4,5), 0.5);
	edgeValueMap.put(directedGraph.getEdge(5,4), 0.5);
	edgeValueMap.put(directedGraph.getEdge(6,2), 1.0);
	edgeValueMap.put(directedGraph.getEdge(3,6), 1.0);
	
	//Invoke the separator
	SubtourSeparator<Integer, DefaultEdge> separator=new SubtourSeparator<Integer, DefaultEdge>(directedGraph);
	separator.separateSubtour(edgeValueMap);
	
	assertTrue(separator.hasSubtour());
	assertEquals(0, separator.getCutValue(), PRECISION);
	assertEquals(new HashSet<Integer>(Arrays.asList(2,3,6)), separator.getCutSet());
}
 
Example 13
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);
}