org.neo4j.graphalgo.impl.shortestpath
Class Dijkstra<CostType>

java.lang.Object
  extended by org.neo4j.graphalgo.impl.shortestpath.Dijkstra<CostType>
Type Parameters:
CostType - The datatype the edge weights will be represented by.
All Implemented Interfaces:
SingleSourceSingleSinkShortestPath<CostType>
Direct Known Subclasses:
SingleSourceShortestPathDijkstra

public class Dijkstra<CostType>
extends Object
implements SingleSourceSingleSinkShortestPath<CostType>

Dijkstra class. This class can be used to perform shortest path computations between two nodes. The search is made simultaneously from both the start node and the end node. Note that per default, only one shortest path will be searched for. This will be done when the path or the cost is asked for. If at some later time getPaths is called to get all the paths, the calculation is redone. In order to avoid this double computation when all paths are desired, be sure to call getPaths (or calculateMultiple) before any call to getPath or getCost (or calculate) is made.

Author:
Patrik Larsson

Nested Class Summary
protected  class Dijkstra.DijstraIterator
          A DijkstraIterator computes the distances to nodes from a specified starting node, one at a time, following the dijkstra algorithm.
 
Field Summary
protected  boolean calculateAllShortestPaths
           
protected  CostAccumulator<CostType> costAccumulator
           
protected  Comparator<CostType> costComparator
           
protected  CostEvaluator<CostType> costEvaluator
           
protected  RelationshipType[] costRelationTypes
           
protected  boolean doneCalculation
           
protected  Node endNode
           
protected  CostType foundPathsCost
           
protected  Set<Node> foundPathsMiddleNodes
           
protected  CostType maxCost
           
protected  long maxNodesToTraverse
           
protected  long maxRelationShipsToTraverse
           
protected  long numberOfNodesTraversed
           
protected  long numberOfTraversedRelationShips
           
protected  HashMap<Node,List<Relationship>> predecessors1
           
protected  HashMap<Node,List<Relationship>> predecessors2
           
protected  Direction relationDirection
           
protected  CostType startCost
           
protected  Node startNode
           
 
Constructor Summary
Dijkstra(CostType startCost, Node startNode, Node endNode, CostEvaluator<CostType> costEvaluator, CostAccumulator<CostType> costAccumulator, Comparator<CostType> costComparator, Direction relationDirection, RelationshipType... costRelationTypes)
           
 
Method Summary
 boolean calculate()
          Makes the main calculation If some limit is set, the shortest path(s) that could be found within those limits will be calculated.
 boolean calculateMultiple()
          Same as calculate(), but will set the flag to calculate all shortest paths.
 CostType getCost()
          A call to this will run the algorithm to find the cost for the shortest paths between the start node and the end node, if not calculated before.
 Direction getDirection()
          This can be used to retrieve the Direction in which relationships should be in the shortest path(s).
 List<PropertyContainer> getPath()
          A call to this will run the algorithm to find a single shortest path, if not already done, and return it as an alternating list of Node/Relationship.
 List<Node> getPathAsNodes()
          A call to this will run the algorithm to find a single shortest path, if not already done, and return it as a list of nodes.
 List<Relationship> getPathAsRelationships()
          A call to this will run the algorithm to find a single shortest path, if not already done, and return it as a list of Relationships.
 List<List<PropertyContainer>> getPaths()
          A call to this will run the algorithm to find all shortest paths, if not already done, and return them as alternating lists of Node/Relationship.
 List<List<Node>> getPathsAsNodes()
          A call to this will run the algorithm to find all shortest paths, if not already done, and return them as lists of nodes.
 List<List<Relationship>> getPathsAsRelationships()
          A call to this will run the algorithm to find all shortest paths, if not already done, and return them as lists of relationships.
 RelationshipType[] getRelationshipTypes()
          This can be used to retrieve the types of relationships that are traversed.
 void limitMaxCostToTraverse(CostType maxCost)
          Set the evaluator for pruning the paths when the maximum cost is exceeded.
 void limitMaxNodesToTraverse(long maxNodesToTraverse)
          This sets the maximum depth in the form of a maximum number of nodes to scan.
 void limitMaxRelationShipsToTraverse(long maxRelationShipsToTraverse)
          This sets the maximum depth in the form of a maximum number of relationships to follow.
protected  boolean limitReached()
           
protected  boolean limitReached(CostType cost1, CostType cost2)
           
 void reset()
          Resets the result data to force the computation to be run again when some result is asked for.
 void setEndNode(Node endNode)
          Set the end node.
 void setStartNode(Node startNode)
          Set the start node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

startCost

protected CostType startCost

startNode

protected Node startNode

endNode

protected Node endNode

costRelationTypes

protected RelationshipType[] costRelationTypes

relationDirection

protected Direction relationDirection

costEvaluator

protected CostEvaluator<CostType> costEvaluator

costAccumulator

protected CostAccumulator<CostType> costAccumulator

costComparator

protected Comparator<CostType> costComparator

calculateAllShortestPaths

protected boolean calculateAllShortestPaths

maxRelationShipsToTraverse

protected long maxRelationShipsToTraverse

numberOfTraversedRelationShips

protected long numberOfTraversedRelationShips

maxNodesToTraverse

protected long maxNodesToTraverse

numberOfNodesTraversed

protected long numberOfNodesTraversed

maxCost

protected CostType maxCost

doneCalculation

protected boolean doneCalculation

foundPathsMiddleNodes

protected Set<Node> foundPathsMiddleNodes

foundPathsCost

protected CostType foundPathsCost

predecessors1

protected HashMap<Node,List<Relationship>> predecessors1

predecessors2

protected HashMap<Node,List<Relationship>> predecessors2
Constructor Detail

Dijkstra

public Dijkstra(CostType startCost,
                Node startNode,
                Node endNode,
                CostEvaluator<CostType> costEvaluator,
                CostAccumulator<CostType> costAccumulator,
                Comparator<CostType> costComparator,
                Direction relationDirection,
                RelationshipType... costRelationTypes)
Parameters:
startCost - Starting cost for both the start node and the end node
startNode - the start node
endNode - the end node
costRelationTypes - the relationship that should be included in the path
relationDirection - relationship direction to follow
costEvaluator - the cost function per relationship
costAccumulator - adding up the path cost
costComparator - comparing to path costs
Method Detail

limitReached

protected boolean limitReached()
Returns:
True if the set limits for the calculation has been reached (but not exceeded)

limitReached

protected boolean limitReached(CostType cost1,
                               CostType cost2)

reset

public void reset()
Resets the result data to force the computation to be run again when some result is asked for.

Specified by:
reset in interface SingleSourceSingleSinkShortestPath<CostType>

calculateMultiple

public boolean calculateMultiple()
Same as calculate(), but will set the flag to calculate all shortest paths. It sets the flag and then calls calculate, so inheriting classes only need to override calculate().

Returns:

calculate

public boolean calculate()
Makes the main calculation If some limit is set, the shortest path(s) that could be found within those limits will be calculated.

Returns:
True if a path was found.

getCost

public CostType getCost()
Description copied from interface: SingleSourceSingleSinkShortestPath
A call to this will run the algorithm to find the cost for the shortest paths between the start node and the end node, if not calculated before. This will usually find a single shortest path.

Specified by:
getCost in interface SingleSourceSingleSinkShortestPath<CostType>
Returns:
The cost for the found path(s).

getPaths

public List<List<PropertyContainer>> getPaths()
Description copied from interface: SingleSourceSingleSinkShortestPath
A call to this will run the algorithm to find all shortest paths, if not already done, and return them as alternating lists of Node/Relationship.

Specified by:
getPaths in interface SingleSourceSingleSinkShortestPath<CostType>
Returns:
All the found paths or null.

getPathsAsNodes

public List<List<Node>> getPathsAsNodes()
Description copied from interface: SingleSourceSingleSinkShortestPath
A call to this will run the algorithm to find all shortest paths, if not already done, and return them as lists of nodes.

Specified by:
getPathsAsNodes in interface SingleSourceSingleSinkShortestPath<CostType>
Returns:
All the found paths or null.

getPathsAsRelationships

public List<List<Relationship>> getPathsAsRelationships()
Description copied from interface: SingleSourceSingleSinkShortestPath
A call to this will run the algorithm to find all shortest paths, if not already done, and return them as lists of relationships.

Specified by:
getPathsAsRelationships in interface SingleSourceSingleSinkShortestPath<CostType>
Returns:
All the found paths or null.

getPath

public List<PropertyContainer> getPath()
Description copied from interface: SingleSourceSingleSinkShortestPath
A call to this will run the algorithm to find a single shortest path, if not already done, and return it as an alternating list of Node/Relationship.

Specified by:
getPath in interface SingleSourceSingleSinkShortestPath<CostType>
Returns:
One of the shortest paths found or null.

getPathAsNodes

public List<Node> getPathAsNodes()
Description copied from interface: SingleSourceSingleSinkShortestPath
A call to this will run the algorithm to find a single shortest path, if not already done, and return it as a list of nodes.

Specified by:
getPathAsNodes in interface SingleSourceSingleSinkShortestPath<CostType>
Returns:
One of the shortest paths found or null.

getPathAsRelationships

public List<Relationship> getPathAsRelationships()
Description copied from interface: SingleSourceSingleSinkShortestPath
A call to this will run the algorithm to find a single shortest path, if not already done, and return it as a list of Relationships.

Specified by:
getPathAsRelationships in interface SingleSourceSingleSinkShortestPath<CostType>
Returns:
One of the shortest paths found or null.

limitMaxRelationShipsToTraverse

public void limitMaxRelationShipsToTraverse(long maxRelationShipsToTraverse)
This sets the maximum depth in the form of a maximum number of relationships to follow.

Parameters:
maxRelationShipsToTraverse -

limitMaxNodesToTraverse

public void limitMaxNodesToTraverse(long maxNodesToTraverse)
This sets the maximum depth in the form of a maximum number of nodes to scan.

Parameters:
maxRelationShipsToTraverse -

setEndNode

public void setEndNode(Node endNode)
Set the end node. Will reset the calculation.

Specified by:
setEndNode in interface SingleSourceSingleSinkShortestPath<CostType>
Parameters:
endNode - the endNode to set

setStartNode

public void setStartNode(Node startNode)
Set the start node. Will reset the calculation.

Specified by:
setStartNode in interface SingleSourceSingleSinkShortestPath<CostType>
Parameters:
startNode - the startNode to set

getDirection

public Direction getDirection()
Description copied from interface: SingleSourceSingleSinkShortestPath
This can be used to retrieve the Direction in which relationships should be in the shortest path(s).

Specified by:
getDirection in interface SingleSourceSingleSinkShortestPath<CostType>
Returns:
the relationDirection

getRelationshipTypes

public RelationshipType[] getRelationshipTypes()
Description copied from interface: SingleSourceSingleSinkShortestPath
This can be used to retrieve the types of relationships that are traversed.

Specified by:
getRelationshipTypes in interface SingleSourceSingleSinkShortestPath<CostType>
Returns:
the costRelationType

limitMaxCostToTraverse

public void limitMaxCostToTraverse(CostType maxCost)
Set the evaluator for pruning the paths when the maximum cost is exceeded.

Parameters:
evaluator - The evaluator for


Copyright © 2002-2012 The Neo4j Graph Database Project. All Rights Reserved.