org.jgrapht.graph.DirectedSubgraph Java Examples

The following examples show how to use org.jgrapht.graph.DirectedSubgraph. 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: DirectedGraphUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDirectedGraph_whenGetStronglyConnectedSubgraphs_thenPathExistsBetweenStronglyconnectedVertices() {
    StrongConnectivityAlgorithm<String, DefaultEdge> scAlg = new KosarajuStrongConnectivityInspector<>(directedGraph);
    List<DirectedSubgraph<String, DefaultEdge>> stronglyConnectedSubgraphs = scAlg.stronglyConnectedSubgraphs();
    List<String> stronglyConnectedVertices = new ArrayList<>(stronglyConnectedSubgraphs.get(3).vertexSet());

    String randomVertex1 = stronglyConnectedVertices.get(0);
    String randomVertex2 = stronglyConnectedVertices.get(3);
    AllDirectedPaths<String, DefaultEdge> allDirectedPaths = new AllDirectedPaths<>(directedGraph);

    List<GraphPath<String, DefaultEdge>> possiblePathList = allDirectedPaths.getAllPaths(randomVertex1, randomVertex2, false, stronglyConnectedVertices.size());
    assertTrue(possiblePathList.size() > 0);
}
 
Example #2
Source File: ShortestPathsCoverGraph.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public Subgraph<IModelEntity, Relationship, Graph<IModelEntity, Relationship>> getCoverSubGraph(Graph<IModelEntity, Relationship> rootEntitiesGraph,
		Set<IModelEntity> entities) {
	Subgraph<IModelEntity, Relationship, Graph<IModelEntity, Relationship>> subGraph = null;
	Iterator<IModelEntity> it = entities.iterator();
	Set<Relationship> connectingRelatiosnhips = new HashSet<Relationship>();

	Set<IModelEntity> connectedEntities = new HashSet<IModelEntity>();
	if (it.hasNext())
		connectedEntities.add(it.next());

	// build the subgraph that contains all the nodes in entities
	logger.debug("Building the subgraph that contains only the entities involved in the query");
	while (it.hasNext()) {
		IModelEntity entity = it.next();
		for (IModelEntity otherEntity : entities) {

			GraphPath<IModelEntity, Relationship> minimumPath = null;
			if (otherEntity.getParent() == null) {
				// check the path from entity to connectedEntity
				DijkstraShortestPath<IModelEntity, Relationship> dsp = new DijkstraShortestPath(rootEntitiesGraph, entity, otherEntity);
				double pathLength = dsp.getPathLength();
				minimumPath = dsp.getPath();

				if (rootEntitiesGraph instanceof DirectedGraph) {
					// check the path from connectedEntity to entity
					DijkstraShortestPath dsp2 = new DijkstraShortestPath(rootEntitiesGraph, otherEntity, entity);
					double pathLength2 = dsp2.getPathLength();
					if (pathLength > pathLength2) {
						minimumPath = dsp2.getPath();
					}
				}

				if (minimumPath != null) {
					List<Relationship> pathRelations = minimumPath.getEdgeList();

					if (pathRelations != null) {
						for (int i = 0; i < pathRelations.size(); i++) {

							Relationship connectingRel = pathRelations.get(i);

							if (connectingRel != null && connectingRel.isConsidered() != false) {
								connectingRelatiosnhips.add(connectingRel);
								connectedEntities.add(connectingRel.getSourceEntity());
								connectedEntities.add(connectingRel.getTargetEntity());
							}

						}
					}
				}
			}
		}
	}

	if (rootEntitiesGraph instanceof DirectedGraph) {
		subGraph = (Subgraph) new DirectedSubgraph<IModelEntity, Relationship>((DirectedGraph) rootEntitiesGraph, connectedEntities,
				connectingRelatiosnhips);
	} else {
		subGraph = (Subgraph) new UndirectedSubgraph<IModelEntity, Relationship>((UndirectedGraph) rootEntitiesGraph, connectedEntities,
				connectingRelatiosnhips);
	}
	logger.debug("Subgraph built");

	return subGraph;
}