next up previous index
Next: Enumerations of shortest paths Up: Shortest path methods Previous: Shortest path methods   Index

pathExists and shortestPathLength

The pathExists method is straightforward:
public boolean pathExists(Object origin, Object destination) {
    Node source = (Node) nodeMap.lookup(origin);
    Node target = (Node) nodeMap.lookup(destination);
    dijkstra(source);
    return ((WorkSpace) target.work).settled;
}
This looks up the labels of the nodes in the nodeMap table and identifies the corresponding nodes as source and target. The Dijkstra algorithm is then run for the given source node. A path exists if and only if the target node is flagged as settled at the end.

The shortestPathLength method is implemented similarly:

public int shortestPathLength(Object origin, Object destination) {
    Node source = (Node) nodeMap.lookup(origin);
    Node target = (Node) nodeMap.lookup(destination);
    dijkstra(source);
    if (((WorkSpace) target.work).settled) {
        return ((WorkSpace) target.work).distance;
    } else {
        throw new NoSuchElementException();
    }
}
In this case the distance to the target node is returned, assuming that it has been settled. Otherwise a NoSuchElementException is thrown, since no path exists between the source and target nodes.



Peter Williams 2005-06-07