org.javanetworkanalyzer.alg
Class Dijkstra<V extends VDijkstra,E extends EdgeSPT>

java.lang.Object
  extended by org.javanetworkanalyzer.alg.GraphSearchAlgorithm<V,E>
      extended by org.javanetworkanalyzer.alg.Dijkstra<V,E>
Type Parameters:
V - Vertices
E - Edges
All Implemented Interfaces:
TraversalAlg<V,E>
Direct Known Subclasses:
DijkstraForAccessibility, DijkstraForCentrality

public class Dijkstra<V extends VDijkstra,E extends EdgeSPT>
extends GraphSearchAlgorithm<V,E>

Home-brewed implementation of Dijkstra's algorithm.

Author:
Adam Gouge

Field Summary
protected static double TOLERANCE
          Tolerance to be used when determining if two potential shortest paths have the same length.
 
Fields inherited from class org.javanetworkanalyzer.alg.GraphSearchAlgorithm
currentStartNode, graph
 
Constructor Summary
Dijkstra(org.jgrapht.Graph<V,E> graph)
          Constructor.
 
Method Summary
 void calculate(V startNode)
          Does a Dijkstra search from the given start node to all other nodes.
 void calculate(V startNode, double radius)
          Does a Dijkstra search from the given start node to all other nodes, limiting the search by the given radius.
protected  void init(V startNode)
          Performs any initializations to be done at the start of the TraversalAlg.calculate(V) method.
 Map<V,Map<V,Double>> manyToMany(Set<V> sources, Set<V> targets)
          Performs a Dijkstra search from each source to each target using a oneToMany(V, java.util.Set) search from each source.
 Map<V,Double> manyToOne(Set<V> sources, V target)
          Performs a Dijkstra search from each source to the given target by reversing the graph and using a oneToMany(V, java.util.Set) from the target to all the sources.
protected  void multipleShortestPathUpdate(V u, V v, E e)
          Updates to be performed if the path to v through u is a new multiple shortest path.
 Map<V,Double> oneToMany(V source, Set<V> targets)
          Performs a Dijkstra search from the source, stopping once all the targets are found.
 double oneToOne(V source, V target)
          Performs a Dijkstra search from the source, stopping once the target is found.
protected  boolean preRelaxStep(V startNode, V u)
          Any work to be done using vertex u before relaxing the outgoing edges of u.
 TraversalGraph<V,E> reconstructTraversalGraph(double radius)
          Returns the SPT from the last start node calculate(V) was called on, limited by the given radius.
protected  void relax(V startNode, V u, E e, PriorityQueue<V> queue)
          Relaxes the edge outgoing from u and updates the queue appropriately.
protected  void shortestPathSoFarUpdate(V startNode, V u, V v, Double uvWeight, E e, PriorityQueue<V> queue)
          Updates to be performed if the path to v through u is the shortest path to v found so far.
 
Methods inherited from class org.javanetworkanalyzer.alg.GraphSearchAlgorithm
outdegree, outgoingEdgesOf, outgoingEdgesOf, reconstructTraversalGraph, successorListOf
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TOLERANCE

protected static final double TOLERANCE
Tolerance to be used when determining if two potential shortest paths have the same length.

See Also:
Constant Field Values
Constructor Detail

Dijkstra

public Dijkstra(org.jgrapht.Graph<V,E> graph)
Constructor.

Parameters:
graph - The graph.
Method Detail

calculate

public void calculate(V startNode)
Does a Dijkstra search from the given start node to all other nodes.

Parameters:
startNode - Start node

calculate

public void calculate(V startNode,
                      double radius)
Does a Dijkstra search from the given start node to all other nodes, limiting the search by the given radius.

Parameters:
startNode - Start node
radius - Radius by which to limit the search

init

protected void init(V startNode)
Description copied from class: GraphSearchAlgorithm
Performs any initializations to be done at the start of the TraversalAlg.calculate(V) method.

Overrides:
init in class GraphSearchAlgorithm<V extends VDijkstra,E extends EdgeSPT>
Parameters:
startNode - Start node

preRelaxStep

protected boolean preRelaxStep(V startNode,
                               V u)
Any work to be done using vertex u before relaxing the outgoing edges of u. Must return true if the search should be stopped.

Parameters:
u - Vertex u.
Returns:
true if we should stop the Dijkstra search.

relax

protected void relax(V startNode,
                     V u,
                     E e,
                     PriorityQueue<V> queue)
Relaxes the edge outgoing from u and updates the queue appropriately.

Parameters:
u - Vertex u.
e - Edge e.
queue - The queue.

shortestPathSoFarUpdate

protected void shortestPathSoFarUpdate(V startNode,
                                       V u,
                                       V v,
                                       Double uvWeight,
                                       E e,
                                       PriorityQueue<V> queue)
Updates to be performed if the path to v through u is the shortest path to v found so far.

Parameters:
u - Vertex u
v - Vertex v
uvWeight - w(u,v)
e - Edge e
queue - Queue

multipleShortestPathUpdate

protected void multipleShortestPathUpdate(V u,
                                          V v,
                                          E e)
Updates to be performed if the path to v through u is a new multiple shortest path. There is no need to set the distance on v since this is a multiple shortest path.

Parameters:
u - Vertex u
v - Vertex v
e - Edge e

reconstructTraversalGraph

public TraversalGraph<V,E> reconstructTraversalGraph(double radius)
Returns the SPT from the last start node calculate(V) was called on, limited by the given radius. The shortest path "tree" we return may contain multiple shortest paths. Note: GraphSearchAlgorithm.reconstructTraversalGraph() should not be used when limiting by radius as it will include edges to vertices with a distance greater than the radius.

Parameters:
radius - The radius to limit by
Returns:
The SPT/traversal graph from the last start node calculate(V) was called on

oneToOne

public double oneToOne(V source,
                       V target)
Performs a Dijkstra search from the source, stopping once the target is found.

Parameters:
source - Source
target - Target
Returns:
The distance from the source to the target.

oneToMany

public Map<V,Double> oneToMany(V source,
                               Set<V> targets)
Performs a Dijkstra search from the source, stopping once all the targets are found.

Parameters:
source - Source
targets - Targets
Returns:
A map of distances from the source keyed by the target vertex.

manyToOne

public Map<V,Double> manyToOne(Set<V> sources,
                               V target)
Performs a Dijkstra search from each source to the given target by reversing the graph and using a oneToMany(V, java.util.Set) from the target to all the sources. If the graph is undirected, there is no need to reverse the graph.

Parameters:
sources - Sources
target - Target
Returns:
A map of the distance to the target keyed by the source vertex.

manyToMany

public Map<V,Map<V,Double>> manyToMany(Set<V> sources,
                                       Set<V> targets)
Performs a Dijkstra search from each source to each target using a oneToMany(V, java.util.Set) search from each source.

Note: Using oneToMany rather than manyToOne is more efficient since we don't have to create an edge-reversed graph.

Parameters:
sources - Sources
targets - Targets
Returns:
A map of maps of distances. The first V is keyed by the source and the second V is keyed by the target.


Copyright © 2014. All Rights Reserved.