Bfs

Declared in lemon/bfs.h, in the group Graph Search; the same class on the Doxygen site.

template< typename GR , typename TR >
class Bfs

BFS algorithm class.

This class provides an efficient implementation of the BFS algorithm.

There is also a function-type interface for the BFS algorithm, which is convenient in the simplier cases and it can be used easier.

Template Parameters:
  • GR – The type of the digraph the algorithm runs on. The default type is ListDigraph.

  • TR – The traits class that defines various types used by the algorithm. By default, it is BfsDefaultTraits< GR >. In most cases, this parameter should not be set directly, consider to use the named template parameters instead.

Execution Control

The simplest way to execute the BFS algorithm is to use one of the member functions called run()

.

If you need better control on the execution, you have to call

inline void init()

Initializes the internal data structures.

init() first, then you can add several source nodes with addSource(). Finally the actual path computation can be performed with one of the start() functions.

Initializes the internal data structures.

inline void addSource( Node s )

Adds a new source node.

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

inline Node processNextNode()

Processes the next node.

Returns:

The processed node.

Pre:

The queue must not be empty.

inline Node processNextNode( Node target , bool & reach )

Processes the next node.

Processes the next node and checks if the given target node is reached. If the target node is reachable from the processed node, then the reach parameter will be set to true.

Parameters:
  • target – The target node.

  • reach – [inout] Indicates if the target node is reached. It should be initially false.

Returns:

The processed node.

Pre:

The queue must not be empty.

template< class NM >
inline Node processNextNode( const NM & nm , Node & rnode )

Processes the next node.

Processes the next node and checks if at least one of reached nodes 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 – A bool (or convertible) node map that indicates the possible targets.

  • rnode – [inout] The reached target node. It should be initially INVALID.

Returns:

The processed node.

Pre:

The queue must not be empty.

inline Node nextNode() const

The next node to be processed.

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

inline bool emptyQueue() const

Returns false if there are nodes to be processed.

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

inline int queueSize() const

Returns the number of the nodes to be processed.

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

inline void start()

Executes the algorithm.

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 (forest),

  • the distance of each node from the root(s).

Note

b.start() is just a shortcut of the following code.

while( ! b.emptyQueue() ) {
 b.processNextNode();
 }

Pre:

init() must be called and at least one root node should be added with addSource() before using this function.

inline void start( Node t )

Executes the algorithm until the given target node is reached.

This method runs the BFS algorithm from the root node(s) in order to compute the shortest path to t.

The algorithm computes

  • the shortest path to t,

  • the distance of t from the root(s).

Note

b.start(t) is just a shortcut of the following code.

bool reach = false;
while( ! b.emptyQueue() && ! reach ) {
 b.processNextNode( t , reach );
 }

Pre:

init() must be called and at least one root node should be added with addSource() before using this function.

template< class NodeBoolMap >
inline Node start( const NodeBoolMap & nm )

Executes the algorithm until a condition is met.

This method runs the BFS algorithm from the root node(s) in order to compute the shortest path to a node v with nm[v] true, if such a node can be found.

Note

b.start(nm) is just a shortcut of the following code.

Node rnode = INVALID;
while( ! b.emptyQueue() && rnode == INVALID ) {
 b.processNextNode( nm , rnode );
 }
return( rnode );

Parameters:

nm – 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.

Pre:

init() must be called and at least one root node should be added with addSource() before using this function.

inline void run( Node s )

Runs the algorithm from the given source node.

This method runs the BFS algorithm from 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();

inline bool run( Node s , Node t )

Finds the shortest path between s and t.

This method runs the BFS algorithm from node s in order to compute the shortest path to node t (it stops searching when t is reached).

Note

Apart from the return value, b.run(s,t) is just a shortcut of the following code.

b.init();
b.addSource( s );
b.start( t );

Returns:

true if t is reachable form s.

inline void run()

Runs the algorithm to visit all nodes in the digraph.

This method runs the BFS algorithm in order to visit all nodes in the digraph.

Note

b.run(s) is just a shortcut of the following code.

b.init();
for( NodeIt n( gr ) ; n != INVALID ; ++n )
{
 if( ! b.reached( n ) ) {
  b.addSource( n );
  b.start();
  }
 }

Query Functions

The results of the BFS algorithm can be obtained using these functions.

Either

run() or start() should be called before using them.

inline Path path( Node t ) const

The shortest path to the given node.

Returns the shortest path to the given node from the root(s).

Warning

t should be reached from the root(s).

Pre:

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

inline int dist( Node v ) const

The distance of the given node from the root(s).

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

Warning

If node v is not reached from the root(s), then the return value of this function is undefined.

Pre:

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

inline Arc predArc( Node v ) const

Returns the ‘previous arc’ of the shortest path tree for the given node.

This function returns the ‘previous arc’ of the shortest path tree for the node v, i.e. it returns the last arc of a shortest path from a root to v. It is INVALID if v is not reached from the root(s) or if v is a root.

The shortest path tree used here is equal to the shortest path tree used in predNode() and predMap().

Pre:

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

inline Node predNode( Node v ) const

Returns the ‘previous node’ of the shortest path tree for the given node.

This function returns the ‘previous node’ of the shortest path tree for the node v, i.e. it returns the last but one node of a shortest path from a root to v. It is INVALID if v is not reached from the root(s) or if v is a root.

The shortest path tree used here is equal to the shortest path tree used in predArc() and predMap().

Pre:

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

inline const DistMap & distMap() const

Returns a const reference to the node map that stores the distances of the nodes.

Returns a const reference to the node map that stores the distances of the nodes calculated by the algorithm.

Pre:

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

inline const PredMap & predMap() const

Returns a const reference to the node map that stores the predecessor arcs.

Returns a const reference to the node map that stores the predecessor arcs, which form the shortest path tree (forest).

Pre:

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

inline bool reached( Node v ) const

Checks if the given node is reached from the root(s).

Returns true if v is reached from the root(s).

Pre:

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

Public Types

typedef TR::Digraph Digraph

The type of the digraph the algorithm runs on.

typedef TR::PredMap PredMap

The type of the map that stores the predecessor arcs of the shortest paths.

typedef TR::DistMap DistMap

The type of the map that stores the distances of the nodes.

typedef TR::ReachedMap ReachedMap

The type of the map that indicates which nodes are reached.

typedef TR::ProcessedMap ProcessedMap

The type of the map that indicates which nodes are processed.

typedef PredMapPath< Digraph , PredMap > Path

The type of the paths.

typedef TR Traits

The traits class of the algorithm.

typedef Bfs Create

Public Functions

inline Bfs( const Digraph & g )

Constructor.

Parameters:

g – The digraph the algorithm runs on.

inline ~Bfs()

Destructor.

inline Bfs & predMap( PredMap & m )

Sets the map that stores the predecessor arcs. If you don’t use this function before calling run() or init(), an instance will be allocated automatically. The destructor deallocates this automatically allocated map, of course.

Returns:

(*this)

inline Bfs & reachedMap( ReachedMap & m )

Sets the map that indicates which nodes are reached. If you don’t use this function before calling run() or init(), an instance will be allocated automatically. The destructor deallocates this automatically allocated map, of course.

Returns:

(*this)

inline Bfs & processedMap( ProcessedMap & m )

Sets the map that indicates which nodes are processed. If you don’t use this function before calling run() or init(), an instance will be allocated automatically. The destructor deallocates this automatically allocated map, of course.

Returns:

(*this)

inline Bfs & distMap( DistMap & m )

Sets the map that stores the distances of the nodes.

Sets the map that stores the distances of the nodes calculated by the algorithm. If you don’t use this function before calling run() or init(), an instance will be allocated automatically. The destructor deallocates this automatically allocated map, of course.

Returns:

(*this)

Protected Functions

inline Bfs()
template< class T >
struct SetPredMapTraits : public Traits

Public Types

typedef T PredMap

Public Static Functions

static inline PredMap * createPredMap( const Digraph & )
template< class T >
struct SetPredMap : public Bfs< Digraph , SetPredMapTraits< T > >

Named parameter for setting

PredMap type. Named parameter for setting PredMap type. It must conform to the WriteMap concept.

Public Types

typedef Bfs< Digraph , SetPredMapTraits< T > > Create
template< class T >
struct SetDistMapTraits : public Traits

Public Types

typedef T DistMap

Public Static Functions

static inline DistMap * createDistMap( const Digraph & )
template< class T >
struct SetDistMap : public Bfs< Digraph , SetDistMapTraits< T > >

Named parameter for setting

DistMap type. Named parameter for setting DistMap type. It must conform to the WriteMap concept.

Public Types

typedef Bfs< Digraph , SetDistMapTraits< T > > Create
template< class T >
struct SetReachedMapTraits : public Traits

Public Types

typedef T ReachedMap

Public Static Functions

static inline ReachedMap * createReachedMap( const Digraph & )
template< class T >
struct SetReachedMap : public Bfs< Digraph , SetReachedMapTraits< T > >

Named parameter for setting

ReachedMap type. Named parameter for setting ReachedMap type. It must conform to the ReadWriteMap concept.

Public Types

typedef Bfs< Digraph , SetReachedMapTraits< T > > Create
template< class T >
struct SetProcessedMapTraits : public Traits

Public Types

typedef T ProcessedMap

Public Static Functions

static inline ProcessedMap * createProcessedMap( const Digraph & )
template< class T >
struct SetProcessedMap : public Bfs< Digraph , SetProcessedMapTraits< T > >

Named parameter for setting

ProcessedMap type. Named parameter for setting ProcessedMap type. It must conform to the WriteMap concept.

Public Types

typedef Bfs< Digraph , SetProcessedMapTraits< T > > Create
struct SetStandardProcessedMapTraits : public Traits

Public Types

typedef Digraph::template NodeMap< bool > ProcessedMap

Public Static Functions

static inline ProcessedMap * createProcessedMap( const Digraph & g )
struct SetStandardProcessedMap : public Bfs< Digraph , SetStandardProcessedMapTraits >

Named parameter for setting

ProcessedMap type to be Digraph::NodeMap< bool >. Named parameter for setting ProcessedMap type to be Digraph::NodeMap< bool >. If you don’t set it explicitly, it will be automatically allocated.

Public Types

typedef Bfs< Digraph , SetStandardProcessedMapTraits > Create