bellman_ford Class Reference

Bellman Ford algorithm. More...

Inheritance diagram for bellman_ford:

Inheritance graph
[legend]
Collaboration diagram for bellman_ford:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 bellman_ford ()
 Constructor.
virtual ~bellman_ford ()
 Destructor.
int check (graph &G)
 Checks whether the preconditions for Bellman Ford are satisfied.
int run (graph &G)
 Applies algorithm to graph g.
void reset ()
 Resets the algorithm.
void source (const node &n)
 Sets source.
node source () const
 Returns source.
void weights (const edge_map< double > &weight)
 Sets weights of the edges.
void store_preds (bool set)
 Enables or disables the storing of predecessors.
bool store_preds () const
 Returns whether the storing of predecessors is enabled.
bool reached (const node &n) const
 Returns whether is reachable from source.
double distance (const node &n) const
 Returns the distance from source to n.
edge predecessor_edge (const node &n) const
 edge to predecessor of node n on the shortest path from source
node predecessor_node (const node &n) const
 predecessor of node n on the shortest path from source
bool negative_cycle () const
 Returns whether there is a cycle with negative weight.


Detailed Description

Bellman Ford algorithm.

Date
Revision

Implementation of the single source shortest path due to Bellman and Ford. Unlike Dijkstra's SSSP algorithm this one allows negative edge weights, as long as there are no cycles with negative weight. If there are negative cycles this implementation finds them.


Member Function Documentation

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

Checks whether the preconditions for Bellman Ford are satisfied.

The Precondition are that the weights of the edges have been set and that the graph has at least one node.

Parameters:
G graph.
Return values:
algorithm::GTL_OK if algorithm can be applied
algorithm::GTL_ERROR otherwise.

Implements algorithm.

int bellman_ford::run ( graph g  )  [virtual]

Applies algorithm to graph g.

Parameters:
g graph
Return values:
algorithm::GTL_OK on success
algorithm::GTL_ERROR otherwise

Implements algorithm.

void bellman_ford::reset (  )  [virtual]

Resets the algorithm.

The weights are not reset. You can apply this algorithms twice without setting the weights for the second call.

Implements algorithm.

void bellman_ford::source ( const node n  )  [inline]

Sets source.

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 bellman_ford::source (  )  const [inline]

Returns source.

Returns:
source.

void bellman_ford::weights ( const edge_map< double > &  weight  )  [inline]

Sets weights of the edges.

This method must be called before run.

Parameters:
w weights of the edges.

void bellman_ford::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 if true predecessors will be stored.
See also:
bellman_ford::predecessor_node, bellman_ford::predecessor_edge

bool bellman_ford::store_preds (  )  const [inline]

Returns whether the storing of predecessors is enabled.

Return values:
true iff the storing of predecessors is enabled.
See also:
bellman_ford::predecessor_node, bellman_ford::predecessor_edge

bool bellman_ford::reached ( const node n  )  const [inline]

Returns whether is reachable from source.

Parameters:
n node

double bellman_ford::distance ( const node n  )  const [inline]

Returns the distance from source to n.

Parameters:
n node

edge bellman_ford::predecessor_edge ( const node n  )  const [inline]

edge to predecessor of node n on the shortest path from source

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

Please note that this requires that this option was enabled during last run.

Parameters:
n node.
Returns:
predecessor of n.
See also:
bellman_ford::store_preds

node bellman_ford::predecessor_node ( const node n  )  const [inline]

predecessor of node n on the shortest path from source

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

Please note that this requires that this option was enabled during last run.

Parameters:
n node.
Returns:
predecessor of n.
See also:
bellman_ford::store_preds