1

我试图让 JGrapht 的 DijkstraShortestPath 类能够找到简单的路径 1 => 4 => 8 => 9。但是,出于某种原因,getRoute 对于除 1、4 [1=>8 和1=>9 返回空]。根据文档,仅当两个节点之间没有路径时,graphpath 才应为 null。我敢肯定,你会说,我完全不知所措。

import java.util.List;

import org.jgrapht.GraphPath;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.graph.SimpleWeightedGraph;

public class main {
    static SimpleWeightedGraph<Integer, Double> graph;

    public static void main(String[] args) 
    {
        graph = new SimpleWeightedGraph<Integer, Double>(Double.class);

        graph.addVertex(1);
        graph.addVertex(4);
        graph.addVertex(8);
        graph.addVertex(9);

        graph.addEdge(1, 4, 0.001);
        graph.addEdge(4, 8, 0.001);
        graph.addEdge(8, 9, 0.001);

        getRoute(1, 9);
    }

    public static void getRoute(Integer startVertex, Integer endVertex)
    {
        DijkstraShortestPath<Integer, Double> dijkstra = new DijkstraShortestPath<Integer, Double>(graph);
         GraphPath<Integer, Double> graphpath = dijkstra.getPath(startVertex, endVertex);
         if(graphpath == null)
         {
             System.err.println("null");
             return;
         }

         List<Integer> path = graphpath.getVertexList();
         for(Integer p : path) System.err.println(p);

    }
}
4

0 回答 0