Java Code Examples for org.jgrapht.alg.DijkstraShortestPath#getPath()

The following examples show how to use org.jgrapht.alg.DijkstraShortestPath#getPath() . 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: RootEntitiesGraph.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
public Set<Relationship> getConnectingRelatiosnhips(Set<IModelEntity> entities) {

		Set<Relationship> connectingRelatiosnhips = new HashSet<Relationship>();

		Set<IModelEntity> connectedEntities = new HashSet<IModelEntity>();

		Iterator<IModelEntity> it = entities.iterator();
		connectedEntities.add(it.next());

		while (it.hasNext()) {
			IModelEntity entity = it.next();
			if (connectedEntities.contains(entity))
				continue;
			GraphPath minimumPath = null;
			double minPathLength = Double.MAX_VALUE;
			for (IModelEntity connectedEntity : connectedEntities) {
				DijkstraShortestPath dsp = new DijkstraShortestPath(rootEntitiesGraph, entity, connectedEntity);
				double pathLength = dsp.getPathLength();
				if (minPathLength > pathLength) {
					minPathLength = pathLength;
					minimumPath = dsp.getPath();
				}
			}
			List<Relationship> relationships = minimumPath.getEdgeList();
			connectingRelatiosnhips.addAll(relationships);
			for (Relationship relatioship : relationships) {
				connectedEntities.add(rootEntitiesGraph.getEdgeSource(relatioship));
				connectedEntities.add(rootEntitiesGraph.getEdgeTarget(relatioship));
			}
		}

		for (Relationship r : connectingRelatiosnhips) {
			IModelEntity source = rootEntitiesGraph.getEdgeSource(r);
			IModelEntity target = rootEntitiesGraph.getEdgeTarget(r);
			logger.error(source.getName() + " -> " + target.getName());
		}

		return connectingRelatiosnhips;
	}
 
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;
}