org.jgrapht.alg.DijkstraShortestPath Java Examples

The following examples show how to use org.jgrapht.alg.DijkstraShortestPath. 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: CategoryGraph.java    From dkpro-jwpl with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the path length between two category nodes - measured in "edges".
 * @param node1 The first category node.
 * @param node2 The second category node.
 * @return The number of edges of the path between node1 and node2. 0, if the nodes are identical. -1, if no path exists.
 */
public int getPathLengthInEdges(Category node1, Category node2) {
    if (this.graph.containsVertex(node1.getPageId()) && this.graph.containsVertex(node2.getPageId())) {
        if (node1.getPageId() == node2.getPageId()) {
            return 0;
        }

        // get the path from root node to node 1
        List edgeList = DijkstraShortestPath.findPathBetween(undirectedGraph, node1.getPageId(), node2.getPageId());
        if (edgeList == null) {
            return -1;
        }
        else {
            return edgeList.size();
        }
    }
    // if the given nodes are not in the category graph, return -1
    else {
        return -1;
    }
}
 
Example #2
Source File: CheckConflicts.java    From Llunatic with GNU General Public License v3.0 6 votes vote down vote up
private Set<AttributeRef> findReachableSourceAttributes(AttributeRef targetAttribute, DirectedGraph<AttributeRef, DefaultEdge> dependencyGraph) {
    Set<AttributeRef> result = new HashSet<AttributeRef>();
    for (AttributeRef attribute : dependencyGraph.vertexSet()) {
        if (!attribute.isSource()) {
            continue;
        }
        if (result.contains(attribute)) {
            continue;
        }
        List path = DijkstraShortestPath.findPathBetween(dependencyGraph, attribute, targetAttribute);
        if (path != null) {
            result.add(attribute);
        }
    }
    return result;
}
 
Example #3
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 #4
Source File: GenerateStratification.java    From BART with MIT License 5 votes vote down vote up
private boolean existsPath(Set<Dependency> s1, Set<Dependency> s2) {
    for (Dependency dependency1 : s1) {
        for (Dependency dependency2 : s2) {
            List<DefaultEdge> path = DijkstraShortestPath.findPathBetween(dependencyGraph, dependency1, dependency2);
            if (path != null) {
                return true;
            }
        }
    }
    return false;
}
 
Example #5
Source File: RepresentationsGraph.java    From DDF with Apache License 2.0 5 votes vote down vote up
public GraphPath<Representation, ConvertFunction> getShortestPath(Representation startVertex,
    Representation endVertex) {
  try {
    return new DijkstraShortestPath<Representation, ConvertFunction>(this.mGraph, startVertex, endVertex).getPath();
  } catch (Exception e) {
    return null;
  }
}
 
Example #6
Source File: FindAttributesInSameCellGroup.java    From Llunatic with GNU General Public License v3.0 5 votes vote down vote up
private Set<AttributeRef> findRelatedAttributes(AttributeRef attribute, Set<String> pathCache, DirectedGraph<AttributeRef, ExtendedEdge> dependencyGraph) {
    //All attributes corresponding to vertices of paths that pass throught the vertex of attribute
    Set<AttributeRef> result = new HashSet<AttributeRef>();
    for (AttributeRef otherAttribute : dependencyGraph.vertexSet()) {
        if (attribute.equals(otherAttribute)) {
            result.add(otherAttribute);
            continue;
        }
        String attributePair = buildAttributePair(attribute, otherAttribute);
        if (pathCache.contains(attributePair)) {
            result.add(otherAttribute);
            continue;
        }
        List<ExtendedEdge> outPath = DijkstraShortestPath.findPathBetween(dependencyGraph, attribute, otherAttribute);
        if (logger.isDebugEnabled()) logger.debug("Finding path between " + attribute + " and " + otherAttribute);
        if (outPath != null) {
            if (logger.isDebugEnabled()) logger.debug("Path found");
            addVerticesInPath(outPath, result, pathCache, dependencyGraph);
            continue;
        }
        List<ExtendedEdge> inPath = DijkstraShortestPath.findPathBetween(dependencyGraph, otherAttribute, attribute);
        if (logger.isDebugEnabled()) logger.debug("Finding path between " + otherAttribute + " and " + attribute);
        if (inPath != null) {
            if (logger.isDebugEnabled()) logger.debug("Path found");
            addVerticesInPath(inPath, result, pathCache, dependencyGraph);
        }
    }
    return result;
}
 
Example #7
Source File: AnalyzeDependencies.java    From Llunatic with GNU General Public License v3.0 5 votes vote down vote up
private boolean existsPath(EGDStratum t1, EGDStratum t2) {
    for (ExtendedEGD dependency1 : t1.getExtendedDependencies()) {
        for (ExtendedEGD dependency2 : t2.getExtendedDependencies()) {
            List<DefaultEdge> path = DijkstraShortestPath.findPathBetween(dependencyGraph, dependency1, dependency2);
            if (path != null) {
                return true;
            }
        }
    }
    return false;
}
 
Example #8
Source File: FindAttributesWithLabeledNulls.java    From Llunatic with GNU General Public License v3.0 5 votes vote down vote up
private boolean isReachable(AttributeRef attribute, Set<AttributeRef> initialAttributes, DirectedGraph<AttributeRef, ExtendedEdge> dependencyGraph) {
    for (AttributeRef initialAttribute : initialAttributes) {
        if (logger.isTraceEnabled()) logger.trace("Checking reachability of " + attribute + " from " + initialAttribute);
        if (!dependencyGraph.containsVertex(initialAttribute)) {
            continue;
        }
        List path = DijkstraShortestPath.findPathBetween(dependencyGraph, initialAttribute, attribute);
        if (path != null) {
            if (logger.isTraceEnabled()) logger.trace("Found!");
            return true;
        }
    }
    return false;
}
 
Example #9
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;
}