org.jgrapht.graph.DirectedMultigraph Java Examples

The following examples show how to use org.jgrapht.graph.DirectedMultigraph. 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: MinimumSteinerTreeApproximation.java    From jReto with MIT License 6 votes vote down vote up
/**
   * Computes an approximation of a directed minimum steiner tree.
   * @param graph The graph of which the minimum steiner tree should be computed
   * @param startVertex The vertex at which the directed minimum steiner tree should be rooted.
   * @param vertices A set of vertices that should be included in the minimum steiner tree.
   * @return A tree representation of the computed tree.
   */
public static <V, E> Tree<V> approximateSteinerTree(DirectedWeightedPseudograph<V, E> graph, V startVertex, Set<V> vertices) {
	// 1. Construct the metric closure for the given set of vertices.
	DirectedWeightedPseudograph<V, MetricClosureEdge<V>> metricClosure = createMetricClosure(graph, vertices);
	
	// 2. Compute the edges that compose an aborescence of the metric closure (aka. directed minimum spanning tree).
	Set<MetricClosureEdge<V>> arborescenceEdges = minimumArborescenceEdges(metricClosure, startVertex);

	// 3. Reduce the metric closure by removing all edges not computed before.
	metricClosure.removeAllEdges(inverseEdgeSet(metricClosure, arborescenceEdges));

	// 4. Reconstruct a graph containing all vertices of the original graph.
	DirectedMultigraph<V, DefaultEdge> steinerTreeGraphApproximation = reconstructGraphFromMetricClosure(metricClosure);
	
	// 5. Construct a tree representation.
	Tree<V> steinerTree = constructSteinerTreeApproximation(startVertex, steinerTreeGraphApproximation);
	
	return steinerTree;
}
 
Example #2
Source File: MinimumSteinerTreeApproximation.java    From jReto with MIT License 6 votes vote down vote up
/**
   * Reconstructs the original graph from a metric closure (i.e. a graph that contains all vertices that were used in the edges of the metric closure).
   * @param metricClosure a metric closure
   * @return The reconstructed graph
   */
public static <T> DirectedMultigraph<T, DefaultEdge> reconstructGraphFromMetricClosure(DirectedWeightedPseudograph<T, MetricClosureEdge<T>> metricClosure) {
	DirectedMultigraph<T, DefaultEdge> reconstructedGraph = new DirectedMultigraph<>(DefaultEdge.class);
	
	for (MetricClosureEdge<T> edge : metricClosure.edgeSet()) {
		T previousVertex = metricClosure.getEdgeSource(edge);
		T endVertex = metricClosure.getEdgeTarget(edge);
		reconstructedGraph.addVertex(previousVertex);
		reconstructedGraph.addVertex(endVertex);
		
		for (T subedge : edge.path) {
			if (reconstructedGraph.addVertex(subedge)) {
				reconstructedGraph.addEdge(previousVertex, subedge);
			}
			previousVertex = subedge;
		}
		
		reconstructedGraph.addEdge(previousVertex, endVertex);
	}
	
	return reconstructedGraph;
}
 
Example #3
Source File: SIGraph.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new SIGraph object at system level.
 *
 * @param system the containing system
 */
public SIGraph (SystemInfo system)
{
    super(new DirectedMultigraph(Relation.class), true /* reuseEvents */);

    Objects.requireNonNull(system, "A sig needs a non-null system");
    this.system = system;
}
 
Example #4
Source File: MinimumSteinerTreeApproximation.java    From jReto with MIT License 5 votes vote down vote up
/**
   * Constructs a Tree representation from this graph, by exploring it recursively. May only be called on a graph that does not contain cycles.
   * @param startVertex The vertex at which to start exploring the graph.
   * @param graph The graph that should be explored.
   * @return A Tree representing the explored graph.
   */
static <T> Tree<T> constructSteinerTreeApproximation(T vertex, DirectedMultigraph<T, DefaultEdge> graph) {
	Set<Tree<T>> children = new HashSet<>();
	
	if (!graph.containsVertex(vertex)) return null;
	
	for (DefaultEdge edge : graph.outgoingEdgesOf(vertex)) {
		T target = graph.getEdgeTarget(edge);
		
		children.add(constructSteinerTreeApproximation(target, graph));
	}

	return new Tree<T>(vertex, children);
}
 
Example #5
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 #6
Source File: RootEntitiesGraph.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public RootEntitiesGraph() {
	relationships = new HashSet<Relationship>();
	rootEntitiesMap = new HashMap<String, IModelEntity>();
	rootEntitiesGraph = new DirectedMultigraph<IModelEntity, Relationship>(Relationship.class);
}
 
Example #7
Source File: AbstractGraphTestCase.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Create a graph with 4 entities and relation:
 * 
 * 
 * 
 * 0 product class
 * 1 promotion
 * 2 customer
 * 3 store
 * 
 * 0-->1
 * 1-->2
 * 0-->2
 * 2-->3
 * 
 */
protected void setUpGraph0() {
	graph = new DirectedMultigraph<IModelEntity, Relationship>(Relationship.class);
	 
	Iterator<IModelEntity> meIter = modelStructure.getRootEntityIterator("foodmart");
	entities = new ArrayList<IModelEntity>();
	
	for (int i = 0; i < 4; i++) {
		entities.add( meIter.next());
		graph.addVertex(entities.get(i));
	}
	
	Relationship r1 = new Relationship();
	r1.setSourceFields(entities.get(0).getAllFields());
	r1.setTargetFields(entities.get(1).getAllFields());
	r1.setName("r1");
	
	Relationship r2 = new Relationship();
	r2.setSourceFields(entities.get(1).getAllFields());
	r2.setTargetFields(entities.get(2).getAllFields());
	r2.setName("r2");
	
	Relationship r3 = new Relationship();
	r3.setSourceFields(entities.get(0).getAllFields());
	r3.setTargetFields(entities.get(2).getAllFields());
	r3.setName("r3");
	
	Relationship r4 = new Relationship();
	r4.setSourceFields(entities.get(2).getAllFields());
	r4.setTargetFields(entities.get(3).getAllFields());
	r4.setName("r4");
	
	graph.addEdge(entities.get(0), entities.get(1), r1);
	graph.addEdge(entities.get(1), entities.get(2), r2);
	graph.addEdge(entities.get(0), entities.get(2), r3);
	graph.addEdge(entities.get(2), entities.get(3), r4);
	relationShips.add(r1);
	relationShips.add(r2);
	relationShips.add(r3);
	relationShips.add(r4);
	mappaths = new HashMap<IModelEntity, Set<GraphPath<IModelEntity,Relationship>>>();
	
	//pathts from entity 1
	for(int i=0; i<4; i++){
		IModelEntity me = entities.get(i);
		Set<GraphPath<IModelEntity, Relationship>> graphPaths = new HashSet<GraphPath<IModelEntity,Relationship>>();
		
		KShortestPaths<IModelEntity, Relationship> kshortestPath = new KShortestPaths<IModelEntity, Relationship>(graph,me,10000 );
		for(int j=0; j<4; j++){
			if(j!=i){
				List<GraphPath<IModelEntity, Relationship>> graphPathsTemp = kshortestPath.getPaths(entities.get(j));
				if(graphPathsTemp!=null){
					graphPaths.addAll(graphPathsTemp);//add paths from i to j
					if(mappaths.get(entities.get(j))==null){//add paths fro j to i
						mappaths.put(entities.get(j),  new HashSet<GraphPath<IModelEntity,Relationship>>());
					}
					mappaths.get(entities.get(j)).addAll(graphPathsTemp);
				}
			}
		}
		if(mappaths.get(me)==null){//add paths fro j to i
			mappaths.put(me,  new HashSet<GraphPath<IModelEntity,Relationship>>());
		}
		mappaths.get(me).addAll(graphPaths);

	}	
	
}
 
Example #8
Source File: SIGraph.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * No-arg constructor meant for JAXB.
 */
private SIGraph ()
{
    super(new DirectedMultigraph(Relation.class), true /* reuseEvents */);
}