本文整理汇总了Java中org.jgrapht.alg.DijkstraShortestPath类的典型用法代码示例。如果您正苦于以下问题:Java DijkstraShortestPath类的具体用法?Java DijkstraShortestPath怎么用?Java DijkstraShortestPath使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DijkstraShortestPath类属于org.jgrapht.alg包,在下文中一共展示了DijkstraShortestPath类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: exampleGraph
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
/**
* Shows how to create an FNSS Topology, convert it to a JGraphT Graph and
* then compute shortest paths
*/
public static void exampleGraph() {
// create a simple FNSS topology
Topology topology = new Topology();
topology.addEdge("1", "2", new Edge());
topology.addEdge("2", "3", new Edge());
// convert to JGraphT
Graph<String, Edge> graph = JGraphTConverter.getGraph(topology);
// Find shortest paths
String source = "3";
String destination = "1";
List<Edge> sp = DijkstraShortestPath.findPathBetween(graph, source, destination);
// Print results
System.out.println("Shortest path from " + source + " to " + destination + ":");
for (Edge e : sp) {
System.out.println(graph.getEdgeSource(e) + " -> " + graph.getEdgeTarget(e));
}
}
示例2: findShortestPath
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
public boolean findShortestPath(int startVertex, int endVertex, List<Integer> result)
{
DijkstraShortestPath<Integer, LocationEdge> dijkstra = new DijkstraShortestPath<Integer, LocationEdge>(graph, startVertex, endVertex);
if (dijkstra.getPathEdgeList() == null){
System.err.println("There is no shortest path between startVertex " + startVertex + " and endVertex " + endVertex);
return false;
} else {
for(LocationEdge edge : dijkstra.getPathEdgeList())
{
if (!result.contains(edge.getSource()))
result.add(edge.getSource());
if (!result.contains(edge.getTarget()))
result.add(edge.getTarget());
}
return true;
}
}
示例3: checkDeadlocks
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
/**
* Verify deadlock situation from serviceToID to serviceFromID, i.e., checks
* if the serviceFromID is not a children node of the serviceToID! If a deadlock
* is detected, the composition is not possible
*
* @param serviceToID - ID of the service that has an input in the service for composition
* @param serviceFromID - ID of the service that provides an output in the service composition
*
* @return true if a deadlock situation is found and false otherwise
*
*/
public boolean checkDeadlocks(String serviceToID, String serviceFromID){
// - We do this, because the was generating errors
// when we were trying to find deadlock in not connected graphs, i.e., graphs
// with parts that are connected!
//If serviceToID has no children nodes, deadlock can not occur
if(graphComposition.outgoingEdgesOf(serviceToID).isEmpty())
return false;
//Run DijkstraShortestPath algorithm to find paths between serviceToID and serviceFromID
List path = DijkstraShortestPath.findPathBetween(graphComposition, serviceToID, serviceFromID);
//If no paths were found, deadlock situation can not occur. Otherwise, deadlock is detected
if(path == null)
return false;
else
return true;
}
示例4: getPathLengthInEdges
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
/**
* 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;
}
}
示例5: getShortestPathBetween
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
private DijkstraShortestPath<TaxiNode, TaxiEdge> getShortestPathBetween(TaxiNode tn0, TaxiNode tn1) {
// in the cache?
Map<TaxiNode, DijkstraShortestPath<TaxiNode, TaxiEdge>> pathsForTN0 = shortestPathCache.get(tn0);
if (pathsForTN0 == null) {
pathsForTN0 = new ConcurrentHashMap<>();
shortestPathCache.put(tn0, pathsForTN0);
}
DijkstraShortestPath<TaxiNode, TaxiEdge> dsp = pathsForTN0.get(tn1);
if (dsp == null) {
dsp = new DijkstraShortestPath<TaxiNode, TaxiEdge>(graph, tn0, tn1);
pathsForTN0.put(tn1, dsp);
}
return dsp;
}
示例6: getParseTreePath
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
public List<GraphEdge> getParseTreePath(Artifact headWord1, Artifact headWord2) {
List<GraphEdge> path = null;
//link tokens
try {
int word1Offset = headWord1.getWordIndex()+1;
int word2Offset = headWord2.getWordIndex()+1;
path =
DijkstraShortestPath.findPathBetween(sentenceTree,
headWord1.getContent()+word1Offset,
headWord2.getContent()+word2Offset);
} catch (Exception e) {
e.printStackTrace();
System.out.println("error in getParseTreeDistance:"+headWord1+headWord2);
}
return path;
}
示例7: findReachableSourceAttributes
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
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;
}
示例8: findPath
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
/**
* Find the list of point to be traversed from the start to the end using the DijkstraShortestPath algorithm.
*
* @param graph
* the graph upon which we need to find the path
* @param start
* the start node from which to navigate
* @param end
* the end node to which to navigate to
* @return the succession of node to traverse from start to end, empty if no path was found
*/
public static List<NdPoint> findPath(WeightedGraph<NdPoint, DefaultWeightedEdge> graph, NdPoint start,
NdPoint end) {
List<DefaultWeightedEdge> edgeList = DijkstraShortestPath.findPathBetween(graph, start, end);
if (edgeList == null) {
return new ArrayList<>(0);
}
List<NdPoint> path = new LinkedList<>();
NdPoint current = start;
path.add(current);
// Add each path node, but also check for the order of the edges so that
// the correct point (source or target) is added.
for (DefaultWeightedEdge edge : edgeList) {
current = GraphHelper.getOpposite(graph, edge, current);
if (current != null) {
path.add(current);
}
}
return path;
}
示例9: getPath
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
@Override
public RoutingPath getPath(Device from, Device to) {
List<Channel> path = DijkstraShortestPath.findPathBetween(
getGraph(), from, to);
if (path == null) {
return null;
}
List<Device> nodes = new ArrayList<Device>();
Device v = from;
nodes.add(from);
for (Channel e : path) {
v = Graphs.getOppositeVertex(getGraph(), e, v);
nodes.add(v);
}
return new RoutingPath(nodes, path);
}
示例10: getPath
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
@Override
public RoutingPath getPath(Device from, Device to) {
List<Channel> path = DijkstraShortestPath.findPathBetween(getGraph(),
from, to);
if (path == null) {
return null;
}
List<Device> nodes = new ArrayList<Device>();
Device v = from;
nodes.add(from);
for (Channel e : path) {
v = Graphs.getOppositeVertex(getGraph(), e, v);
nodes.add(v);
}
return new RoutingPath(nodes, path);
}
示例11: exampleWeightedGraph
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
/**
* Shows how to create an FNSS Topology with weighted edges, convert it to
* a JGraphT WeightedGraph and then compute shortest paths
*/
public static void exampleWeightedGraph() {
// create unidrected topology
Topology topology = new Topology();
// Create edge with high weight and edge with low weight
Edge edgeHighWeight = new Edge();
edgeHighWeight.setWeight(1000);
Edge edgeLowWeight = new Edge();
edgeLowWeight.setWeight(1);
// Assign edges to topology
topology.addEdge("1", "4", edgeHighWeight);
topology.addEdge("2", "3", edgeLowWeight);
topology.addEdge("1", "2", edgeLowWeight);
topology.addEdge("4", "3", edgeLowWeight);
// convert to JGraphT
WeightedGraph<String, DefaultWeightedEdge> graph =
JGraphTConverter.getWeightedGraph(topology);
// Find shortest paths
String source = "1";
String destination = "3";
List<DefaultWeightedEdge> sp =
DijkstraShortestPath.findPathBetween(graph, source, destination);
// Print results
System.out.println("Shortest path from " + source + " to " + destination + ":");
for (DefaultWeightedEdge e : sp) {
System.out.println(graph.getEdgeSource(e) + " -> " + graph.getEdgeTarget(e));
}
}
示例12: getPath
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
public List<ITableRecordReference> getPath()
{
final List<ITableRecordReference> result = new ArrayList<>();
final AsUndirectedGraph<ITableRecordReference, DefaultEdge> undirectedGraph = new AsUndirectedGraph<>(g);
final List<DefaultEdge> path = DijkstraShortestPath.<ITableRecordReference, DefaultEdge> findPathBetween(
undirectedGraph, start, goal);
if (path == null || path.isEmpty())
{
return ImmutableList.of();
}
result.add(start);
for (final DefaultEdge e : path)
{
final ITableRecordReference edgeSource = undirectedGraph.getEdgeSource(e);
if (!result.contains(edgeSource))
{
result.add(edgeSource);
}
else
{
result.add(undirectedGraph.getEdgeTarget(e));
}
}
return ImmutableList.copyOf(result);
}
示例13: existsPath
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
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;
}
示例14: getDestinos
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
public List<Integer> getDestinos(int inicio, int tirada){
List<Integer> destinos = new ArrayList<Integer>();
for (int i = 0; i < casillas.length; i++) {
DijkstraShortestPath<Casilla, DefaultEdge> dijkstra = new DijkstraShortestPath<Casilla, DefaultEdge>(
grafo, casillas[inicio], casillas[i]);
GraphPath<Casilla, DefaultEdge> path = dijkstra.getPath();
if (path.getWeight() == tirada)
destinos.add(i);
}
return destinos;
}
示例15: getShortestPath
import org.jgrapht.alg.DijkstraShortestPath; //导入依赖的package包/类
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;
}
}