1

I'm trying to get JGrapht's DijkstraShortestPath class to be able to find the simple path 1 => 4 => 8 => 9. For some reason, though, getRoute returns null for any input except 1, 4 [1=>8 and 1=>9 return null]. According to the docs, graphpath should only be null if there is no path between the two nodes. As I'm sure you can tell, I'm completely at a loss.

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);

    }
}

0 Answers0