dijkstra Class Reference

Dijkstra's Algorithm for computing single source shortest path. More...

Inheritance diagram for dijkstra:

Inheritance graph
[legend]
Collaboration diagram for dijkstra:

Collaboration graph
[legend]

List of all members.

Public Types

typedef list< node >
::const_iterator 
shortest_path_node_iterator
 Iterator type for traversing nodes on one shortest path.
typedef list< edge >
::const_iterator 
shortest_path_edge_iterator
 Iterator type for traversing edges on one shortest path.

Public Member Functions

 dijkstra ()
 Default constructor.
virtual ~dijkstra ()
 Destructor.
void source (const node &n)
 Sets source node.
void target (const node &n)
 Sets target node.
void weights (const edge_map< double > &weight)
 Sets weights of the edges.
void store_preds (bool set)
 Enables or disables the storing of predecessors.
virtual int check (graph &G)
 Checks whether the preconditions for Dijkstra are satisfied.
int run (graph &G)
 Runs shortest path algorithm on G.
node source () const
 Returns source node.
node target () const
 Returns target node if set, node::node() else.
bool store_preds () const
 Returns whether the storing of predecessors is enabled.
bool reached (const node &n) const
 Returns whether n is reachable from source node.
double distance (const node &n) const
 Returns the distance from source node to node n.
node predecessor_node (const node &n) const
 Predecessor node of node n on the shortest path from the source node.
edge predecessor_edge (const node &n) const
 Predecessor edge of node n on the shortest path from the source node.
shortest_path_node_iterator shortest_path_nodes_begin (const node &dest)
 Returns an iterator to the beginning (to the source node) of a shortest node path to node dest.
shortest_path_node_iterator shortest_path_nodes_end (const node &dest)
 Returns an iterator one after the end (one after node dest) of a shortest node path to node dest.
shortest_path_edge_iterator shortest_path_edges_begin (const node &dest)
 Returns an iterator to the beginning edge of a shortest edge path to node dest.
shortest_path_edge_iterator shortest_path_edges_end (const node &dest)
 Returns an iterator one after the end of a shortest edge path to node dest.
virtual void reset ()
 Resets Dijkstra's algorithm.


Detailed Description

Dijkstra's Algorithm for computing single source shortest path.

This class implements Dijkstra's algorithm for computing single source shortest path in $\mathcal{O}((|V| + |E|) log |V|)$ worst case.

See also:
bellman_ford
Author:
Christian Bachmaier chris@infosun.fmi.uni-passau.de

Constructor & Destructor Documentation

dijkstra::dijkstra (  ) 

Default constructor.

Enables only the calculation of shortest paths.

See also:
algorithm::algorithm

virtual dijkstra::~dijkstra (  )  [virtual]

Destructor.

See also:
algorithm::~algorithm


Member Function Documentation

void dijkstra::source ( const node n  ) 

Sets source node.

The default source is the invalid node (node::node()), in this case an arbitrary node is chosen and stored when this algorithm is run.

Parameters:
n source node

void dijkstra::target ( const node n  ) 

Sets target node.

If a target is set with this method the algorithm stops if a shortest distance to n is found. Ohterwise shortest paths are computed from source to any node in the graph.

Parameters:
n target node

void dijkstra::weights ( const edge_map< double > &  weight  ) 

Sets weights of the edges.

This method must be called before check run.

Parameters:
weight weights of the edges

void dijkstra::store_preds ( bool  set  ) 

Enables or disables the storing of predecessors.

If enabled for every node the predecessor on the shortest path from will be stored.

Parameters:
set true if predecessors should be stored
See also:
dijkstra::predecessor_node

dijkstra::predecessor_edge

virtual int dijkstra::check ( graph G  )  [virtual]

Checks whether the preconditions for Dijkstra are satisfied.

Necessary preconditions are:

Parameters:
G graph
Return values:
algorithm::GTL_OK if algorithm can be applied
algorithm::GTL_ERROR otherwise
See also:
dijkstra::source

dijkstra::weights

algorithm::check

Implements algorithm.

int dijkstra::run ( graph G  )  [virtual]

Runs shortest path algorithm on G.

This should return always algorithm::GTL_OK. The return value only tracks errors that might occur. Afterwards the result of the test can be accessed via access methods.

Parameters:
G graph
Return values:
algorithm::GTL_OK on success
algorithm::GTL_ERROR otherwise
See also:
algorithm::run

Implements algorithm.

node dijkstra::source (  )  const

Returns source node.

Returns:
source node

node dijkstra::target (  )  const

Returns target node if set, node::node() else.

Returns:
target node

bool dijkstra::store_preds (  )  const

Returns whether the storing of predecessors is enabled.

Returns:
true iff the storing of predecessors is enabled
See also:
dijkstra::predecessor

bool dijkstra::reached ( const node n  )  const

Returns whether n is reachable from source node.

Parameters:
n node
Returns:
true iff n was reached from source

double dijkstra::distance ( const node n  )  const

Returns the distance from source node to node n.

Parameters:
n node
Returns:
distance if n is dijkstra::reached, -1.0 else

node dijkstra::predecessor_node ( const node n  )  const

Predecessor node of node n on the shortest path from the source node.

If n is a root or wasn't reached the return value is the invalid node node::node().

Parameters:
n node
Returns:
predecessor node of n
See also:
dijkstra::store_preds

dijkstra::predecessor_edge

Note:
The method requires that predecessor calculation option was enabled during last run.

edge dijkstra::predecessor_edge ( const node n  )  const

Predecessor edge of node n on the shortest path from the source node.

If n is a root or wasn't reached the return value is the invalid edge edge::edge().

Parameters:
n node
Returns:
predecessor edge of n
See also:
dijkstra::store_preds

dijkstra::predecessor_node

Note:
The method requires that predecessor calculation option was enabled during last run.

shortest_path_node_iterator dijkstra::shortest_path_nodes_begin ( const node dest  ) 

Returns an iterator to the beginning (to the source node) of a shortest node path to node dest.

Parameters:
dest target node
Returns:
beginning node iterator of a shortest path
Note:
The method requires that predecessor calculation option was enabled during last run. If this method is called on the shortest path to dest for the first time (before dijkstra::shortest_path_nodes_end) it needs $\mathcal{O}(\mbox{length of this path})$ time.

shortest_path_node_iterator dijkstra::shortest_path_nodes_end ( const node dest  ) 

Returns an iterator one after the end (one after node dest) of a shortest node path to node dest.

Parameters:
dest target node
Returns:
shortest path end node iterator
Note:
The method requires that predecessor calculation option was enabled during last run. If this method is called on the shortest path to dest for the first time (before dijkstra::shortest_path_nodes_begin) it needs $\mathcal{O}(\mbox{length of this path})$ time.

shortest_path_edge_iterator dijkstra::shortest_path_edges_begin ( const node dest  ) 

Returns an iterator to the beginning edge of a shortest edge path to node dest.

Parameters:
dest target node
Returns:
beginning edge iterator of a shortest path
Note:
The method requires that predecessor calculation option was enabled during last run. If this method is called on the shortest path to dest for the first time (before dijkstra::shortest_path_edges_end) it needs $\mathcal{O}(\mbox{length of this path})$ time.

shortest_path_edge_iterator dijkstra::shortest_path_edges_end ( const node dest  ) 

Returns an iterator one after the end of a shortest edge path to node dest.

Parameters:
dest target node
Returns:
shortest path end edge iterator
Note:
The method requires that predecessor calculation option was enabled during last run. If this method is called on the shortest path to dest for the first time (before dijkstra::shortest_path_edges_begin) it needs $\mathcal{O}(\mbox{length of this path})$ time.

virtual void dijkstra::reset (  )  [virtual]

Resets Dijkstra's algorithm.

It prepares the algorithm to be applied again, possibly to another graph.

Note:
The weights are not reset. You can apply this algorithms
See also:
algorithm::reset

Implements algorithm.