edu.upc.dama.dex.algorithms
Class SinglePairShortestPathBFS

java.lang.Object
  extended by edu.upc.dama.dex.tasks.Task
      extended by edu.upc.dama.dex.tasks.CompositeTask
          extended by edu.upc.dama.dex.algorithms.ShortestPath
              extended by edu.upc.dama.dex.algorithms.SinglePairShortestPath
                  extended by edu.upc.dama.dex.algorithms.SinglePairShortestPathBFS
All Implemented Interfaces:
Algorithm

public class SinglePairShortestPathBFS
extends SinglePairShortestPath

This class can be used to solve the single-pair shortest path problem in an unweighted graph.

Concretely, it consists in finding a path between two nodes with the condition that the total of edges which belong to the path is minimized. This implementation is based on the Breadth-First Search (BFS) algorithm.

It is possible to set some restrictions after constructing a new instance of this class and before running it in order to limit the results. Those restrictions are:

Navigation

There are three kinds of edge navigation:

For each instance of this class, it is only allowed to run the algorithm once. After the execution, we can retrieve the following results: Please, see edu.upc.dama.dex.algorithms for further information related to how to use this class.

NOTE: It is required to indicate the set of edge types before running this algorithm.

Author:
Sparsity Technologies

Field Summary
 
Fields inherited from interface edu.upc.dama.dex.algorithms.Algorithm
NAVIGATION_BACKWARD, NAVIGATION_FORWARD, NAVIGATION_UNDIRECTED
 
Constructor Summary
SinglePairShortestPathBFS(Graph graph, long source, long dest)
          Creates a new instance of SinglePairShortestPathBFS.
 
Method Summary
 void addAllEdges(short navigation)
          Adds all the edge types contained in the graph and their navigation in order to restrict the set of edge types allowed and their direction while traversing the graph to find the path.
 void addEdge(int edgetype, short navigation)
          Adds an edge type and its navigation in order to restrict the set of edge types allowed and their direction while traversing the graph to find the path.
 void close()
          Closes the instance.
 int getCost()
          Returns the cost, that is, the total hops done while achieving the destination node from the source node.
 void run()
          Runs the algorithm in order to solve the shortest path problem.
 
Methods inherited from class edu.upc.dama.dex.algorithms.SinglePairShortestPath
existsShortestPath, getPathAsEdges, getPathAsNodes
 
Methods inherited from class edu.upc.dama.dex.algorithms.ShortestPath
setMaximumHops
 
Methods inherited from class edu.upc.dama.dex.tasks.CompositeTask
cancel, execute, init
 
Methods inherited from class edu.upc.dama.dex.tasks.Task
main
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SinglePairShortestPathBFS

public SinglePairShortestPathBFS(Graph graph,
                                 long source,
                                 long dest)
Creates a new instance of SinglePairShortestPathBFS. After creating this instance is required to indicate the set of edge types which will be navigated through while traversing the graph in order to find the path.

Parameters:
graph - Graph on which the path will be found.
source - The node identifier which identifies the source node at which the algorithm execution will start.
dest - The node identifier which identifies the destination node at which the algorithm execution will end.
Method Detail

addAllEdges

public void addAllEdges(short navigation)
Adds all the edge types contained in the graph and their navigation in order to restrict the set of edge types allowed and their direction while traversing the graph to find the path.

Parameters:
navigation - The direction that has to be followed while traversing each edge during the execution of this algorithm. It must be Algorithm.NAVIGATION_BACKWARD, Algorithm.NAVIGATION_FORWARD or Algorithm.NAVIGATION_UNDIRECTED.

addEdge

public void addEdge(int edgetype,
                    short navigation)
Adds an edge type and its navigation in order to restrict the set of edge types allowed and their direction while traversing the graph to find the path. It is possible to add as many edge types as all the different edge types contained in the graph.

Parameters:
edgetype - The edge type to restrict the set of edges allowed while traversing the graph during the execution of this algorithm.
navigation - The direction that has to be followed while traversing each edge of the given edge type during the execution of this algorithm. It must be Algorithm.NAVIGATION_BACKWARD, Algorithm.NAVIGATION_FORWARD or Algorithm.NAVIGATION_UNDIRECTED.

close

public void close()
Description copied from class: ShortestPath
Closes the instance.

Once executed, the instance becomes invalid.

Specified by:
close in class ShortestPath

getCost

public int getCost()
Returns the cost, that is, the total hops done while achieving the destination node from the source node. If there isn't any shortest path found, it returns -1 .

Returns:
The minimum number of hops between the source node and the destination node.

run

public void run()
         throws java.lang.Throwable
Description copied from class: ShortestPath
Runs the algorithm in order to solve the shortest path problem.

This method can be called only once.

Specified by:
run in class ShortestPath
Throws:
java.lang.Throwable