src/hugo/bfs.h
author alpar
Fri, 03 Sep 2004 10:43:22 +0000
changeset 795 cfa6c2ea48e5
parent 780 e06d0d16595f
child 802 bc0c74eeb151
permissions -rw-r--r--
Put a lot of missing header files to src/hugo/Makefile.am
alpar@774
     1
// -*- C++ -*-
alpar@774
     2
#ifndef HUGO_BFS_H
alpar@774
     3
#define HUGO_BFS_H
alpar@774
     4
alpar@774
     5
///\ingroup flowalgs
alpar@774
     6
///\file
alpar@774
     7
///\brief Bfs algorithm.
alpar@774
     8
///
alpar@774
     9
///\todo Revise Manual.
alpar@774
    10
alpar@774
    11
#include <hugo/bin_heap.h>
alpar@774
    12
#include <hugo/invalid.h>
alpar@774
    13
alpar@774
    14
namespace hugo {
alpar@774
    15
alpar@774
    16
/// \addtogroup flowalgs
alpar@774
    17
/// @{
alpar@774
    18
alpar@781
    19
  ///%BFS algorithm class.
alpar@774
    20
alpar@781
    21
  ///This class provides an efficient implementation of %BFS algorithm.
alpar@781
    22
  ///\param GR The graph type the algorithm runs on.
alpar@781
    23
  ///This class does the same as Dijkstra does with constant 1 edge length,
alpar@781
    24
  ///but it is faster.
alpar@774
    25
  ///
alpar@781
    26
  ///\author Alpar Juttner
alpar@774
    27
alpar@774
    28
#ifdef DOXYGEN
alpar@774
    29
  template <typename GR>
alpar@774
    30
#else
alpar@774
    31
  template <typename GR>
alpar@774
    32
#endif
alpar@774
    33
  class Bfs{
alpar@774
    34
  public:
alpar@774
    35
    ///The type of the underlying graph.
alpar@774
    36
    typedef GR Graph;
alpar@774
    37
    typedef typename Graph::Node Node;
alpar@774
    38
    typedef typename Graph::NodeIt NodeIt;
alpar@774
    39
    typedef typename Graph::Edge Edge;
alpar@774
    40
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@774
    41
    
alpar@774
    42
    ///\brief The type of the map that stores the last
alpar@774
    43
    ///edges of the shortest paths.
alpar@774
    44
    typedef typename Graph::template NodeMap<Edge> PredMap;
alpar@774
    45
    ///\brief The type of the map that stores the last but one
alpar@774
    46
    ///nodes of the shortest paths.
alpar@774
    47
    typedef typename Graph::template NodeMap<Node> PredNodeMap;
alpar@774
    48
    ///The type of the map that stores the dists of the nodes.
alpar@774
    49
    typedef typename Graph::template NodeMap<int> DistMap;
alpar@774
    50
alpar@774
    51
  private:
alpar@774
    52
    const Graph *G;
alpar@774
    53
    PredMap *predecessor;
alpar@774
    54
    bool local_predecessor;
alpar@774
    55
    PredNodeMap *pred_node;
alpar@774
    56
    bool local_pred_node;
alpar@774
    57
    DistMap *distance;
alpar@774
    58
    bool local_distance;
alpar@774
    59
alpar@774
    60
    //The source node of the last execution.
alpar@774
    61
    Node source;
alpar@774
    62
alpar@774
    63
alpar@781
    64
    ///Initializes the maps.
alpar@774
    65
    void init_maps() 
alpar@774
    66
    {
alpar@774
    67
      if(!predecessor) {
alpar@774
    68
	local_predecessor = true;
alpar@774
    69
	predecessor = new PredMap(*G);
alpar@774
    70
      }
alpar@774
    71
      if(!pred_node) {
alpar@774
    72
	local_pred_node = true;
alpar@774
    73
	pred_node = new PredNodeMap(*G);
alpar@774
    74
      }
alpar@774
    75
      if(!distance) {
alpar@774
    76
	local_distance = true;
alpar@774
    77
	distance = new DistMap(*G);
alpar@774
    78
      }
alpar@774
    79
    }
alpar@774
    80
    
alpar@774
    81
  public :    
alpar@774
    82
    Bfs(const Graph& _G) :
alpar@774
    83
      G(&_G),
alpar@774
    84
      predecessor(NULL), local_predecessor(false),
alpar@774
    85
      pred_node(NULL), local_pred_node(false),
alpar@774
    86
      distance(NULL), local_distance(false)
alpar@774
    87
    { }
alpar@774
    88
    
alpar@774
    89
    ~Bfs() 
alpar@774
    90
    {
alpar@774
    91
      if(local_predecessor) delete predecessor;
alpar@774
    92
      if(local_pred_node) delete pred_node;
alpar@774
    93
      if(local_distance) delete distance;
alpar@774
    94
    }
alpar@774
    95
alpar@774
    96
    ///Sets the graph the algorithm will run on.
alpar@774
    97
alpar@774
    98
    ///Sets the graph the algorithm will run on.
alpar@774
    99
    ///\return <tt> (*this) </tt>
alpar@781
   100
    ///\bug What about maps?
alpar@781
   101
    ///\todo It may be unnecessary
alpar@774
   102
    Bfs &setGraph(const Graph &_G) 
alpar@774
   103
    {
alpar@774
   104
      G = &_G;
alpar@774
   105
      return *this;
alpar@774
   106
    }
alpar@774
   107
    ///Sets the map storing the predecessor edges.
alpar@774
   108
alpar@774
   109
    ///Sets the map storing the predecessor edges.
alpar@774
   110
    ///If you don't use this function before calling \ref run(),
alpar@774
   111
    ///it will allocate one. The destuctor deallocates this
alpar@774
   112
    ///automatically allocated map, of course.
alpar@774
   113
    ///\return <tt> (*this) </tt>
alpar@774
   114
    Bfs &setPredMap(PredMap &m) 
alpar@774
   115
    {
alpar@774
   116
      if(local_predecessor) {
alpar@774
   117
	delete predecessor;
alpar@774
   118
	local_predecessor=false;
alpar@774
   119
      }
alpar@774
   120
      predecessor = &m;
alpar@774
   121
      return *this;
alpar@774
   122
    }
alpar@774
   123
alpar@774
   124
    ///Sets the map storing the predecessor nodes.
alpar@774
   125
alpar@774
   126
    ///Sets the map storing the predecessor nodes.
alpar@774
   127
    ///If you don't use this function before calling \ref run(),
alpar@774
   128
    ///it will allocate one. The destuctor deallocates this
alpar@774
   129
    ///automatically allocated map, of course.
alpar@774
   130
    ///\return <tt> (*this) </tt>
alpar@774
   131
    Bfs &setPredNodeMap(PredNodeMap &m) 
alpar@774
   132
    {
alpar@774
   133
      if(local_pred_node) {
alpar@774
   134
	delete pred_node;
alpar@774
   135
	local_pred_node=false;
alpar@774
   136
      }
alpar@774
   137
      pred_node = &m;
alpar@774
   138
      return *this;
alpar@774
   139
    }
alpar@774
   140
alpar@774
   141
    ///Sets the map storing the distances calculated by the algorithm.
alpar@774
   142
alpar@774
   143
    ///Sets the map storing the distances calculated by the algorithm.
alpar@774
   144
    ///If you don't use this function before calling \ref run(),
alpar@774
   145
    ///it will allocate one. The destuctor deallocates this
alpar@774
   146
    ///automatically allocated map, of course.
alpar@774
   147
    ///\return <tt> (*this) </tt>
alpar@774
   148
    Bfs &setDistMap(DistMap &m) 
alpar@774
   149
    {
alpar@774
   150
      if(local_distance) {
alpar@774
   151
	delete distance;
alpar@774
   152
	local_distance=false;
alpar@774
   153
      }
alpar@774
   154
      distance = &m;
alpar@774
   155
      return *this;
alpar@774
   156
    }
alpar@774
   157
    
alpar@774
   158
  ///Runs %BFS algorithm from node \c s.
alpar@774
   159
alpar@774
   160
  ///This method runs the %BFS algorithm from a root node \c s
alpar@774
   161
  ///in order to
alpar@781
   162
  ///compute a
alpar@774
   163
  ///shortest path to each node. The algorithm computes
alpar@781
   164
  ///- The %BFS tree.
alpar@774
   165
  ///- The distance of each node from the root.
alpar@774
   166
 
alpar@774
   167
    void run(Node s) {
alpar@774
   168
      
alpar@774
   169
      init_maps();
alpar@774
   170
      
alpar@774
   171
      source = s;
alpar@774
   172
      
alpar@774
   173
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@774
   174
	predecessor->set(u,INVALID);
alpar@774
   175
	pred_node->set(u,INVALID);
alpar@774
   176
      }
alpar@774
   177
      
alpar@774
   178
      int N=G->nodeNum();
alpar@774
   179
      std::vector<typename Graph::Node> Q(N);
alpar@774
   180
      int Qh=0;
alpar@774
   181
      int Qt=0;
alpar@774
   182
      
alpar@774
   183
      Q[Qh++]=source;
alpar@774
   184
      distance->set(s, 0);
alpar@774
   185
      do {
alpar@774
   186
	Node m;
alpar@774
   187
	Node n=Q[Qt++];
alpar@774
   188
	int d= (*distance)[n]+1;
alpar@774
   189
	
alpar@774
   190
	for(OutEdgeIt e(*G,n);e!=INVALID;++e)
alpar@774
   191
	  if((m=G->head(e))!=s && (*predecessor)[m]==INVALID) {
alpar@774
   192
	    Q[Qh++]=m;
alpar@774
   193
	    predecessor->set(m,e);
alpar@774
   194
	    pred_node->set(m,n);
alpar@774
   195
	    distance->set(m,d);
alpar@774
   196
	  }
alpar@774
   197
      } while(Qt!=Qh);
alpar@774
   198
    }
alpar@774
   199
    
alpar@774
   200
    ///The distance of a node from the root.
alpar@774
   201
alpar@774
   202
    ///Returns the distance of a node from the root.
alpar@774
   203
    ///\pre \ref run() must be called before using this function.
alpar@774
   204
    ///\warning If node \c v in unreachable from the root the return value
alpar@774
   205
    ///of this funcion is undefined.
alpar@774
   206
    int dist(Node v) const { return (*distance)[v]; }
alpar@774
   207
alpar@781
   208
    ///Returns the 'previous edge' of the %BFS path tree.
alpar@774
   209
alpar@781
   210
    ///For a node \c v it returns the 'previous edge' of the %BFS tree,
alpar@781
   211
    ///i.e. it returns the last edge of a shortest path from the root to \c
alpar@774
   212
    ///v. It is \ref INVALID
alpar@774
   213
    ///if \c v is unreachable from the root or if \c v=s. The
alpar@781
   214
    ///%BFS tree used here is equal to the %BFS tree used in
alpar@774
   215
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
alpar@774
   216
    ///this function.
alpar@774
   217
    Edge pred(Node v) const { return (*predecessor)[v]; }
alpar@774
   218
alpar@781
   219
    ///Returns the 'previous node' of the %BFS tree.
alpar@774
   220
alpar@781
   221
    ///For a node \c v it returns the 'previous node' on the %BFS tree,
alpar@774
   222
    ///i.e. it returns the last but one node from a shortest path from the
alpar@774
   223
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
alpar@781
   224
    ///\c v=s. The shortest path tree used here is equal to the %BFS
alpar@774
   225
    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
alpar@774
   226
    ///using this function.
alpar@774
   227
    Node predNode(Node v) const { return (*pred_node)[v]; }
alpar@774
   228
    
alpar@774
   229
    ///Returns a reference to the NodeMap of distances.
alpar@774
   230
    
alpar@774
   231
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
alpar@774
   232
    ///be called before using this function.
alpar@774
   233
    const DistMap &distMap() const { return *distance;}
alpar@774
   234
 
alpar@781
   235
    ///Returns a reference to the %BFS tree map.
alpar@774
   236
alpar@774
   237
    ///Returns a reference to the NodeMap of the edges of the
alpar@781
   238
    ///%BFS tree.
alpar@774
   239
    ///\pre \ref run() must be called before using this function.
alpar@774
   240
    const PredMap &predMap() const { return *predecessor;}
alpar@774
   241
 
alpar@781
   242
    ///Returns a reference to the map of last but one nodes of shortest paths.
alpar@774
   243
alpar@781
   244
    ///Returns a reference to the NodeMap of the last but one nodes on the
alpar@781
   245
    ///%BFS tree.
alpar@774
   246
    ///\pre \ref run() must be called before using this function.
alpar@774
   247
    const PredNodeMap &predNodeMap() const { return *pred_node;}
alpar@774
   248
alpar@774
   249
    ///Checks if a node is reachable from the root.
alpar@774
   250
alpar@774
   251
    ///Returns \c true if \c v is reachable from the root.
alpar@774
   252
    ///\warning The root node is reported to be reached!
alpar@774
   253
    ///
alpar@774
   254
    ///\pre \ref run() must be called before using this function.
alpar@774
   255
    ///
alpar@780
   256
    bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
alpar@774
   257
    
alpar@774
   258
  };
alpar@774
   259
  
alpar@774
   260
/// @}
alpar@774
   261
  
alpar@774
   262
} //END OF NAMESPACE HUGO
alpar@774
   263
alpar@774
   264
#endif
alpar@774
   265
alpar@774
   266