Dijkstra Algorithm

Download

Download jar file or use maven. psjava requires Java 1.6 (or above)

<dependency>
    <groupId>org.psjava</groupId>
    <artifactId>psjava</artifactId>
    <version>0.1.19</version>
</dependency>

Example Code

IntegerNumberSystem NS = IntegerNumberSystem.getInstance();

// Let's construct a simple graph.

MutableDirectedWeightedGraph<String, Integer> graph = MutableDirectedWeightedGraph.create();
graph.insertVertex("A");
graph.insertVertex("B");
graph.insertVertex("C");
graph.insertVertex("D");
graph.addEdge("A", "C", 100);
graph.addEdge("A", "B", 10);
graph.addEdge("B", "C", 20);

// Calculate distances from a single source 'A'

DijkstraAlgorithm dijkstra = GoodDijkstraAlgorithm.getInstance();
SingleSourceShortestPathResult<String, Integer, DirectedWeightedEdge<String, Integer>> result = dijkstra.calc(graph, "A", NS);

boolean reachabilityOfC = result.isReachable("C"); // C is reachable.
boolean reachabilityOfD = result.isReachable("D"); // D is not reachable.
int distanceToC = result.getDistance("C"); // must be 30

See Also

Implementation


Copyright 2014 psjava team. View on GitHub