Bfs< GR, TR > Class Template Reference
[Graph Search]


Detailed Description

template<typename GR, typename TR>
class lemon::Bfs< GR, TR >

This class provides an efficient implementation of the BFS algorithm.

Parameters:
GR The graph type the algorithm runs on. The default value is ListGraph. The value of GR is not used directly by Bfs, it is only passed to BfsDefaultTraits.
TR Traits class to set various data types used by the algorithm. The default traits class is BfsDefaultTraits<GR>. See BfsDefaultTraits for the documentation of a Bfs traits class.
Author:
Alpar Juttner
#include <lemon/bfs.h>

List of all members.

Query Functions

The result of the BFS algorithm can be obtained using these functions.
Before the use of these functions, either run() or start() must be calleb.

typedef PredMapPath< Graph,
PredMap
Path
Path path (Node t)
 Gives back the shortest path.
int dist (Node v) const
 The distance of a node from the root(s).
Edge predEdge (Node v) const
 Returns the 'previous edge' of the shortest path tree.
Node predNode (Node v) const
 Returns the 'previous node' of the shortest path tree.
const DistMapdistMap () const
 Returns a reference to the NodeMap of distances.
const PredMappredMap () const
 Returns a reference to the shortest path tree map.
bool reached (Node v)
 Checks if a node is reachable from the root.

Classes

struct  DefDistMap
struct  DefPredMap
struct  DefProcessedMap
struct  DefProcessedMapToBeDefaultMap
 Named parameter for setting the ProcessedMap type to be Graph::NodeMap<bool>. More...
struct  DefReachedMap
class  UninitializedParameter
 Exception for uninitialized parameters. More...

Public Types

typedef TR::Graph Graph
 The type of the underlying graph.
typedef TR::PredMap PredMap
 The type of the map that stores the last edges of the shortest paths.
typedef TR::ReachedMap ReachedMap
 The type of the map indicating which nodes are reached.
typedef TR::ProcessedMap ProcessedMap
 The type of the map indicating which nodes are processed.
typedef TR::DistMap DistMap
 The type of the map that stores the dists of the nodes.

Public Member Functions

 Bfs (const Graph &_G)
 Constructor.
 ~Bfs ()
 Destructor.
BfspredMap (PredMap &m)
 Sets the map storing the predecessor edges.
BfsreachedMap (ReachedMap &m)
 Sets the map indicating the reached nodes.
BfsprocessedMap (ProcessedMap &m)
 Sets the map indicating the processed nodes.
BfsdistMap (DistMap &m)
 Sets the map storing the distances calculated by the algorithm.
Execution control
The simplest way to execute the algorithm is to use one of the member functions called run(...).
If you need more control on the execution, first you must call init(), then you can add several source nodes with addSource(). Finally start() will perform the actual path computation.

void init ()
void addSource (Node s)
 Adds a new source node.
Node processNextNode ()
 Processes the next node.
Node processNextNode (Node target, bool &reach)
 Processes the next node.
template<class NM >
Node processNextNode (const NM &nm, Node &rnode)
 Processes the next node.
Node nextNode ()
 Next node to be processed.
bool emptyQueue ()
 Returns false if there are nodes to be processed in the queue.
int queueSize ()
 Returns the number of the nodes to be processed.
void start ()
 Executes the algorithm.
void start (Node dest)
 Executes the algorithm until dest is reached.
template<class NM >
Node start (const NM &nm)
 Executes the algorithm until a condition is met.
void run (Node s)
 Runs BFS algorithm from node s.
int run (Node s, Node t)
 Finds the shortest path between s and t.

Private Member Functions

void create_maps ()
 Creates the maps if necessary.

Private Attributes

const GraphG
 Pointer to the underlying graph.
PredMap_pred
 Pointer to the map of predecessors edges.
bool local_pred
 Indicates if _pred is locally allocated (true) or not.
DistMap_dist
 Pointer to the map of distances.
bool local_dist
 Indicates if _dist is locally allocated (true) or not.
ReachedMap_reached
 Pointer to the map of reached status of the nodes.
bool local_reached
 Indicates if _reached is locally allocated (true) or not.
ProcessedMap_processed
 Pointer to the map of processed status of the nodes.
bool local_processed
 Indicates if _processed is locally allocated (true) or not.


Constructor & Destructor Documentation

Bfs ( const Graph _G  )  [inline]

Parameters:
_G the graph the algorithm will run on.


Member Function Documentation

void create_maps (  )  [inline, private]

Todo:
Better memory allocation (instead of new).

Bfs& predMap ( PredMap m  )  [inline]

Sets the map storing the predecessor edges. If you don't use this function before calling run(), it will allocate one. The destructor deallocates this automatically allocated map, of course.

Returns:
(*this)

Bfs& reachedMap ( ReachedMap m  )  [inline]

Sets the map indicating the reached nodes. If you don't use this function before calling run(), it will allocate one. The destructor deallocates this automatically allocated map, of course.

Returns:
(*this)

Bfs& processedMap ( ProcessedMap m  )  [inline]

Sets the map indicating the processed nodes. If you don't use this function before calling run(), it will allocate one. The destructor deallocates this automatically allocated map, of course.

Returns:
(*this)

Bfs& distMap ( DistMap m  )  [inline]

Sets the map storing the distances calculated by the algorithm. If you don't use this function before calling run(), it will allocate one. The destructor deallocates this automatically allocated map, of course.

Returns:
(*this)

void init (  )  [inline]

Initializes the internal data structures.

void addSource ( Node  s  )  [inline]

Adds a new source node to the set of nodes to be processed.

Node processNextNode (  )  [inline]

Processes the next node.

Returns:
The processed node.
Warning:
The queue must not be empty!

Node processNextNode ( Node  target,
bool &  reach 
) [inline]

Processes the next node. And checks that the given target node is reached. If the target node is reachable from the processed node then the reached parameter will be set true. The reached parameter should be initially false.

Parameters:
target The target node.
Return values:
reach Indicates that the target node is reached.
Returns:
The processed node.
Warning:
The queue must not be empty!

Node processNextNode ( const NM &  nm,
Node &  rnode 
) [inline]

Processes the next node. And checks that at least one of reached node has true value in the nm node map. If one node with true value is reachable from the processed node then the rnode parameter will be set to the first of such nodes.

Parameters:
nm The node map of possible targets.
Return values:
rnode The reached target node.
Returns:
The processed node.
Warning:
The queue must not be empty!

Node nextNode (  )  [inline]

Next node to be processed.

Returns:
The next node to be processed or INVALID if the queue is empty.

bool emptyQueue (  )  [inline]

Returns false if there are nodes to be processed in the queue

int queueSize (  )  [inline]

Returns the number of the nodes to be processed in the queue.

void start (  )  [inline]

Executes the algorithm.

Precondition:
init() must be called and at least one node should be added with addSource() before using this function.
This method runs the BFS algorithm from the root node(s) in order to compute the shortest path to each node. The algorithm computes
  • The shortest path tree.
  • The distance of each node from the root(s).

void start ( Node  dest  )  [inline]

Executes the algorithm until dest is reached.

Precondition:
init() must be called and at least one node should be added with addSource() before using this function.
This method runs the BFS algorithm from the root node(s) in order to compute the shortest path to dest. The algorithm computes
  • The shortest path to dest.
  • The distance of dest from the root(s).

Node start ( const NM &  nm  )  [inline]

Executes the algorithm until a condition is met.

Precondition:
init() must be called and at least one node should be added with addSource() before using this function.
Parameters:
nm must be a bool (or convertible) node map. The algorithm will stop when it reaches a node v with nm[v] true.
Returns:
The reached node v with nm[v] true or INVALID if no such node was found.

void run ( Node  s  )  [inline]

This method runs the BFS algorithm from a root node s in order to compute the shortest path to each node. The algorithm computes

  • The shortest path tree.
  • The distance of each node from the root.

Note:
b.run(s) is just a shortcut of the following code.
         b.init();
         b.addSource(s);
         b.start();

int run ( Node  s,
Node  t 
) [inline]

Finds the shortest path between s and t.

Returns:
The length of the shortest s---t path if there exists one, 0 otherwise.
Note:
Apart from the return value, b.run(s) is just a shortcut of the following code.
         b.init();
         b.addSource(s);
         b.start(t);

Path path ( Node  t  )  [inline]

Gives back the shortest path.

Precondition:
The t should be reachable from the source.

int dist ( Node  v  )  const [inline]

Returns the distance of a node from the root(s).

Precondition:
run() must be called before using this function.
Warning:
If node v in unreachable from the root(s) the return value of this function is undefined.

Edge predEdge ( Node  v  )  const [inline]

For a node v it returns the 'previous edge' of the shortest path tree, i.e. it returns the last edge of a shortest path from the root(s) to v. It is INVALID if v is unreachable from the root(s) or v is a root. The shortest path tree used here is equal to the shortest path tree used in predNode().

Precondition:
Either run() or start() must be called before using this function.

Node predNode ( Node  v  )  const [inline]

For a node v it returns the 'previous node' of the shortest path tree, i.e. it returns the last but one node from a shortest path from the root(a) to /v. It is INVALID if v is unreachable from the root(s) or if v itself a root. The shortest path tree used here is equal to the shortest path tree used in predEdge().

Precondition:
Either run() or start() must be called before using this function.

const DistMap& distMap (  )  const [inline]

Returns a reference to the NodeMap of distances.

Precondition:
Either run() or init() must be called before using this function.

const PredMap& predMap (  )  const [inline]

Returns a reference to the NodeMap of the edges of the shortest path tree.

Precondition:
Either run() or init() must be called before using this function.

bool reached ( Node  v  )  [inline]

Returns true if v is reachable from the root.

Warning:
The source nodes are indicated as unreached.
Precondition:
Either run() or start() must be called before using this function.


Generated on Thu Jun 4 04:03:26 2009 for LEMON by  doxygen 1.5.9