org.jgrapht.Graphs Java Examples

The following examples show how to use org.jgrapht.Graphs. 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: GraphFunctions.java    From symja_android_library with GNU General Public License v3.0 6 votes vote down vote up
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
	try {
		if (ast.isAST1()) {
			GraphExpr<ExprEdge> gex = createGraph(ast.arg1());
			if (gex == null) {
				return F.NIL;
			}
			Graph<IExpr, ExprEdge> g = gex.toData();
			SpanningTreeAlgorithm<ExprEdge> k = new BoruvkaMinimumSpanningTree<IExpr, ExprEdge>(g);
			Set<ExprEdge> edgeSet = k.getSpanningTree().getEdges();
			Graph<IExpr, ExprEdge> gResult = new DefaultDirectedGraph<IExpr, ExprEdge>(ExprEdge.class);
			Graphs.addAllEdges(gResult, g, edgeSet);
			return GraphExpr.newInstance(gResult);
		}
	} catch (RuntimeException rex) {
		if (FEConfig.SHOW_STACKTRACE) {
			rex.printStackTrace();
		}
	}
	return F.NIL;
}
 
Example #2
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean addVertex(final Object v) {
	if (v instanceof msi.gaml.operators.Graphs.GraphObjectToAdd) {
		if (v instanceof IAgent) {
			if (!this.getVertices().isEmpty() && ((IAgent) v).getSpecies() != vertexSpecies) {
				vertexSpecies = null;
			}
		}
		addValue(graphScope, (msi.gaml.operators.Graphs.GraphObjectToAdd) v);
		return ((msi.gaml.operators.Graphs.GraphObjectToAdd) v).getObject() != null;
	}
	if (v == null || containsVertex(v)) { return false; }
	_Vertex<V, E> vertex;
	try {
		vertex = newVertex(v);
	} catch (final GamaRuntimeException e) {
		e.addContext("Impossible to create vertex from " + StringUtils.toGaml(v, false) + " in graph " + this);
		throw e;
	}
	// if ( vertex == null ) { return false; }
	vertexMap.put((V) v, vertex);
	dispatchEvent(graphScope, new GraphEvent(graphScope, this, this, null, v, GraphEventType.VERTEX_ADDED));
	return true;

}
 
Example #3
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void addValue(final IScope scope, final msi.gaml.operators.Graphs.GraphObjectToAdd value) {
	if (value instanceof msi.gaml.operators.Graphs.EdgeToAdd) {
		final msi.gaml.operators.Graphs.EdgeToAdd edge = (msi.gaml.operators.Graphs.EdgeToAdd) value;
		if (edge.object == null) {
			edge.object = addEdge(edge.source, edge.target);
		}
		addEdge(edge.source, edge.target, edge.object);
		if (edge.weight != null) {
			setEdgeWeight(edge.object, edge.weight);
		}
	} else {
		final msi.gaml.operators.Graphs.NodeToAdd node = (msi.gaml.operators.Graphs.NodeToAdd) value;
		this.addVertex(node.object);
		if (node.weight != null) {
			this.setVertexWeight(node.object, node.weight);
		}
	}

}
 
Example #4
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Method buildValue()
 *
 * @see msi.gama.util.IContainer.Modifiable#buildValue(msi.gama.runtime.IScope, java.lang.Object,
 *      msi.gaml.types.IContainerType)
 */
@Override
public msi.gaml.operators.Graphs.GraphObjectToAdd buildValue(final IScope scope, final Object object) {
	if (object instanceof msi.gaml.operators.Graphs.NodeToAdd) {
		return new msi.gaml.operators.Graphs.NodeToAdd(
				type.getKeyType().cast(scope, ((msi.gaml.operators.Graphs.NodeToAdd) object).object, null, false),
				((msi.gaml.operators.Graphs.NodeToAdd) object).weight);
	}
	if (object instanceof msi.gaml.operators.Graphs.EdgeToAdd) {
		return new msi.gaml.operators.Graphs.EdgeToAdd(
				type.getKeyType().cast(scope, ((msi.gaml.operators.Graphs.EdgeToAdd) object).source, null, false),
				type.getKeyType().cast(scope, ((msi.gaml.operators.Graphs.EdgeToAdd) object).target, null, false),
				type.getContentType().cast(scope, ((msi.gaml.operators.Graphs.EdgeToAdd) object).object, null,
						false),
				((msi.gaml.operators.Graphs.EdgeToAdd) object).weight);
	}
	return new msi.gaml.operators.Graphs.EdgeToAdd(null, null,
			type.getContentType().cast(scope, object, null, false), 0.0);
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
Source File: TSP.java    From jorlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public TSP(String tspInstanceLocation) throws IOException{
	super(DefaultWeightedEdge.class);
	tspLibInstance= new TSPLibInstance(new File(tspInstanceLocation));
	this.N=tspLibInstance.getDimension();

	//Create the graph for jGrapht
	for(int i=0; i<tspLibInstance.getDimension()-1; i++){
		for(int j=i+1; j<tspLibInstance.getDimension(); j++){
			Graphs.addEdgeWithVertices(this, i, j, tspLibInstance.getDistanceTable().getDistanceBetween(i, j));
		}
	}
}
 
Example #10
Source File: TSP.java    From jorlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public TSP(String tspInstanceLocation) throws IOException{
	super(DefaultWeightedEdge.class);
	tspLibInstance= new TSPLibInstance(new File(tspInstanceLocation));
	this.N=tspLibInstance.getDimension();

	//Create the graph for jGrapht
	for(int i=0; i<tspLibInstance.getDimension()-1; i++){
		for(int j=i+1; j<tspLibInstance.getDimension(); j++){
			Graphs.addEdgeWithVertices(this, i, j, tspLibInstance.getDistanceTable().getDistanceBetween(i, j));
		}
	}
}
 
Example #11
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 #12
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 #13
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 #14
Source File: TSP.java    From jorlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public TSP(InputStream inputStream) throws IOException{
	super(DefaultWeightedEdge.class);
	tspLibInstance= new TSPLibInstance(inputStream);
	this.N=tspLibInstance.getDimension();

	//Create the graph for jGrapht
	for(int i=0; i<tspLibInstance.getDimension()-1; i++){
		for(int j=i+1; j<tspLibInstance.getDimension(); j++){
			Graphs.addEdgeWithVertices(this, i, j, tspLibInstance.getDistanceTable().getDistanceBetween(i, j));
		}
	}
}
 
Example #15
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 #16
Source File: AnalyzeDependencies.java    From Llunatic with GNU General Public License v3.0 5 votes vote down vote up
private DirectedGraph<AttributeRef, ExtendedEdge> removeSpecialEdges(DirectedGraph<AttributeRef, ExtendedEdge> faginDependencyGraph) {
    DirectedGraph<AttributeRef, ExtendedEdge> dependencyGraph = new DefaultDirectedGraph<AttributeRef, ExtendedEdge>(ExtendedEdge.class);
    if (faginDependencyGraph == null) {
        return dependencyGraph;
    }
    Graphs.addGraph(dependencyGraph, faginDependencyGraph);
    for (ExtendedEdge edge : faginDependencyGraph.edgeSet()) {
        if (edge.isSpecial() && !edge.isNormal()) {
            dependencyGraph.removeEdge(edge);
        }
    }
    return dependencyGraph;
}
 
Example #17
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 #18
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public IContainer reverse(final IScope scope) {
	final GamaGraph g = new GamaGraph(scope, GamaListFactory.create(type.getKeyType()), false, directed,
			vertexRelation, edgeSpecies, type.getKeyType(), type.getContentType());
	Graphs.addGraphReversed(g, this);
	return g;
}
 
Example #19
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void removeValue(final IScope scope, final Object value) {
	if (value instanceof msi.gaml.operators.Graphs.EdgeToAdd) {
		final msi.gaml.operators.Graphs.EdgeToAdd edge = (msi.gaml.operators.Graphs.EdgeToAdd) value;
		if (edge.object != null) {
			removeEdge(edge.object);
		} else if (edge.source != null && edge.target != null) {
			removeAllEdges(edge.source, edge.target);
		}
	} else if (value instanceof msi.gaml.operators.Graphs.NodeToAdd) {
		removeVertex(((msi.gaml.operators.Graphs.NodeToAdd) value).object);
	} else if (!removeVertex(value)) {
		removeEdge(value);
	}
}
 
Example #20
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 #21
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void addValueAtIndex(final IScope scope, final Object idx,
		final msi.gaml.operators.Graphs.GraphObjectToAdd value) {
	final GamaPair index = buildIndex(scope, idx);
	final EdgeToAdd edge = new EdgeToAdd(index.key, index.value, null, (Double) null);
	if (value instanceof EdgeToAdd) {
		edge.object = ((EdgeToAdd) value).object;
		edge.weight = ((EdgeToAdd) value).weight;
	} else {
		edge.object = value;
	}
	addValue(scope, edge);

	// else, shoud have been taken in consideration by the validator
}
 
Example #22
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object addEdge(final Object e) {
	incVersion();

	if (e instanceof GamaPair) {
		final GamaPair p = (GamaPair) e;
		return addEdge(p.first(), p.last());
	} else if (e instanceof msi.gaml.operators.Graphs.GraphObjectToAdd) {
		addValue(graphScope, (msi.gaml.operators.Graphs.GraphObjectToAdd) e);
		return ((msi.gaml.operators.Graphs.GraphObjectToAdd) e).getObject();
	}
	return addEdge(null, null, e) ? e : null;

}
 
Example #23
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
protected void buildByEdge(final IScope scope, final IContainer vertices, final Double tolerance) {
	if (vertices != null) {
		for (final Object p : vertices.iterable(scope)) {
			addEdge(p);
			final Object p2 = p instanceof msi.gaml.operators.Graphs.GraphObjectToAdd
					? ((msi.gaml.operators.Graphs.GraphObjectToAdd) p).getObject() : p;
			if (p2 instanceof IShape) {
				final _Edge ed = getEdge(p2);
				if (ed != null) {
					ed.setWeight(((IShape) p2).getPerimeter());
				}
			}
		}
	}
}
 
Example #24
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
protected void buildByEdge(final IScope scope, final IContainer edges) {
	if (edges != null) {
		for (final Object p : edges.iterable(scope)) {
			addEdge(p);
			final Object p2 = p instanceof msi.gaml.operators.Graphs.GraphObjectToAdd
					? ((msi.gaml.operators.Graphs.GraphObjectToAdd) p).getObject() : p;
			if (p2 instanceof IShape) {
				final _Edge ed = getEdge(p2);
				if (ed != null) {
					ed.setWeight(((IShape) p2).getPerimeter());
				}
			}
		}
	}
}
 
Example #25
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 #26
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 #27
Source File: FIPAProtocol.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the node corresponding to a performative after the current node of the protocol.
 */
protected ProtocolNode getNode(final IScope scope, final FIPAMessage message, final ProtocolNode currentNode,
		final Performative performative, final boolean initiator) throws GamaRuntimeException {
	if (currentNode == null) {
		if (root != null && root.getPerformative() == performative)
			return root;
		return null;
	}
	final List<ProtocolNode> followingNodes = Graphs.successorListOf(this, currentNode);
	if (followingNodes.size() == 0) {
		throw GamaRuntimeException.warning("Message received in a conversation which has already ended!", scope);
	}
	final List<ProtocolNode> potentialMatchingNodes = new ArrayList<>();
	for (final ProtocolNode followingNode : followingNodes) {
		if (performative == followingNode.getPerformative()) {
			potentialMatchingNodes.add(followingNode);
		}
	}
	if (potentialMatchingNodes.isEmpty()) {
		throw GamaRuntimeException.warning("Protocol : " + this.getName()
				+ ". Unexpected message received of performative : " + message.getPerformativeName(), scope);
	}
	ProtocolNode matchingNode = null;
	for (final ProtocolNode potentialMatchingNode : potentialMatchingNodes) {
		if (initiator == potentialMatchingNode.isSentByInitiator()) {
			matchingNode = potentialMatchingNode;
			break;
		}
	}

	if (matchingNode == null) {
		throw GamaRuntimeException.warning("Couldn't match expected message types and participant", scope);
	}
	return matchingNode;

}
 
Example #28
Source File: CleanupStrategyProvider.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private Graph<Node, Edge> computeGraphToBeDeleted(final Graph<Node, Edge> graph, final String... nodeTypesToRetain) {
    final DirectedGraph<Node, Edge> toDelete = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));

    // copy graph to a destination, which we are going to modify
    Graphs.addGraph(toDelete, graph);

    // remove the nodes, we have to retain from the graph
    Graphs.removeVerticesAndPreserveConnectivity(toDelete, v -> shouldRetainNode(v, nodeTypesToRetain));

    return toDelete;
}
 
Example #29
Source File: Neo4JDbFeatureExecutor.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private Graph<Node, Edge> mergeGraphs(final List<Graph<Node, Edge>> graphs) {
    final Graph<Node, Edge> mergedGraph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));

    for (final Graph<Node, Edge> graph : graphs) {
        Graphs.addGraph(mergedGraph, graph);
    }

    return mergedGraph;
}
 
Example #30
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;
}