src/hugo/bfs.h
author alpar
Wed, 01 Sep 2004 15:08:41 +0000
changeset 780 e06d0d16595f
parent 774 4297098d9677
child 781 d4d182ab75bd
permissions -rw-r--r--
- DFS class (bfs.h and bfs_test.cc) added
- Bugfixes in Dijkstra and Bfs
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@774
    19
  ///%Bfs algorithm class.
alpar@774
    20
alpar@774
    21
  ///This class provides an efficient implementation of %Bfs algorithm.
alpar@774
    22
  ///The edge lengths are passed to the algorithm using a
alpar@774
    23
  ///\ref ReadMapSkeleton "readable map",
alpar@774
    24
  ///so it is easy to change it to any kind of length.
alpar@774
    25
  ///
alpar@774
    26
  ///The type of the length is determined by the \c ValueType of the length map.
alpar@774
    27
  ///
alpar@774
    28
  ///It is also possible to change the underlying priority heap.
alpar@774
    29
  ///
alpar@774
    30
  ///\param GR The graph type the algorithm runs on.
alpar@774
    31
  ///\param LM This read-only
alpar@774
    32
  ///EdgeMap
alpar@774
    33
  ///determines the
alpar@774
    34
  ///lengths of the edges. It is read once for each edge, so the map
alpar@774
    35
  ///may involve in relatively time consuming process to compute the edge
alpar@774
    36
  ///length if it is necessary. The default map type is
alpar@774
    37
  ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
alpar@774
    38
  ///\param Heap The heap type used by the %Bfs
alpar@774
    39
  ///algorithm. The default
alpar@774
    40
  ///is using \ref BinHeap "binary heap".
alpar@774
    41
  ///
alpar@774
    42
  ///\author Jacint Szabo and Alpar Juttner
alpar@774
    43
  ///\todo We need a typedef-names should be standardized. (-:
alpar@774
    44
  ///\todo Type of \c PredMap, \c PredNodeMap and \c DistMap
alpar@774
    45
  ///should not be fixed. (Problematic to solve).
alpar@774
    46
alpar@774
    47
#ifdef DOXYGEN
alpar@774
    48
  template <typename GR>
alpar@774
    49
#else
alpar@774
    50
  template <typename GR>
alpar@774
    51
#endif
alpar@774
    52
  class Bfs{
alpar@774
    53
  public:
alpar@774
    54
    ///The type of the underlying graph.
alpar@774
    55
    typedef GR Graph;
alpar@774
    56
    typedef typename Graph::Node Node;
alpar@774
    57
    typedef typename Graph::NodeIt NodeIt;
alpar@774
    58
    typedef typename Graph::Edge Edge;
alpar@774
    59
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@774
    60
    
alpar@774
    61
    ///\brief The type of the map that stores the last
alpar@774
    62
    ///edges of the shortest paths.
alpar@774
    63
    typedef typename Graph::template NodeMap<Edge> PredMap;
alpar@774
    64
    ///\brief The type of the map that stores the last but one
alpar@774
    65
    ///nodes of the shortest paths.
alpar@774
    66
    typedef typename Graph::template NodeMap<Node> PredNodeMap;
alpar@774
    67
    ///The type of the map that stores the dists of the nodes.
alpar@774
    68
    typedef typename Graph::template NodeMap<int> DistMap;
alpar@774
    69
alpar@774
    70
  private:
alpar@774
    71
    const Graph *G;
alpar@774
    72
    PredMap *predecessor;
alpar@774
    73
    bool local_predecessor;
alpar@774
    74
    PredNodeMap *pred_node;
alpar@774
    75
    bool local_pred_node;
alpar@774
    76
    DistMap *distance;
alpar@774
    77
    bool local_distance;
alpar@774
    78
alpar@774
    79
    //The source node of the last execution.
alpar@774
    80
    Node source;
alpar@774
    81
alpar@774
    82
alpar@774
    83
    ///Initialize maps.
alpar@774
    84
    
alpar@774
    85
    ///\todo Error if \c G or are \c NULL.
alpar@774
    86
    ///\todo Better memory allocation (instead of new).
alpar@774
    87
    void init_maps() 
alpar@774
    88
    {
alpar@774
    89
//       if(!length) {
alpar@774
    90
// 	local_length = true;
alpar@774
    91
// 	length = new LM(G);
alpar@774
    92
//       }
alpar@774
    93
      if(!predecessor) {
alpar@774
    94
	local_predecessor = true;
alpar@774
    95
	predecessor = new PredMap(*G);
alpar@774
    96
      }
alpar@774
    97
      if(!pred_node) {
alpar@774
    98
	local_pred_node = true;
alpar@774
    99
	pred_node = new PredNodeMap(*G);
alpar@774
   100
      }
alpar@774
   101
      if(!distance) {
alpar@774
   102
	local_distance = true;
alpar@774
   103
	distance = new DistMap(*G);
alpar@774
   104
      }
alpar@774
   105
    }
alpar@774
   106
    
alpar@774
   107
  public :    
alpar@774
   108
    Bfs(const Graph& _G) :
alpar@774
   109
      G(&_G),
alpar@774
   110
      predecessor(NULL), local_predecessor(false),
alpar@774
   111
      pred_node(NULL), local_pred_node(false),
alpar@774
   112
      distance(NULL), local_distance(false)
alpar@774
   113
    { }
alpar@774
   114
    
alpar@774
   115
    ~Bfs() 
alpar@774
   116
    {
alpar@774
   117
      //      if(local_length) delete length;
alpar@774
   118
      if(local_predecessor) delete predecessor;
alpar@774
   119
      if(local_pred_node) delete pred_node;
alpar@774
   120
      if(local_distance) delete distance;
alpar@774
   121
    }
alpar@774
   122
alpar@774
   123
    ///Sets the graph the algorithm will run on.
alpar@774
   124
alpar@774
   125
    ///Sets the graph the algorithm will run on.
alpar@774
   126
    ///\return <tt> (*this) </tt>
alpar@774
   127
    Bfs &setGraph(const Graph &_G) 
alpar@774
   128
    {
alpar@774
   129
      G = &_G;
alpar@774
   130
      return *this;
alpar@774
   131
    }
alpar@774
   132
    ///Sets the length map.
alpar@774
   133
alpar@774
   134
    ///Sets the map storing the predecessor edges.
alpar@774
   135
alpar@774
   136
    ///Sets the map storing the predecessor edges.
alpar@774
   137
    ///If you don't use this function before calling \ref run(),
alpar@774
   138
    ///it will allocate one. The destuctor deallocates this
alpar@774
   139
    ///automatically allocated map, of course.
alpar@774
   140
    ///\return <tt> (*this) </tt>
alpar@774
   141
    Bfs &setPredMap(PredMap &m) 
alpar@774
   142
    {
alpar@774
   143
      if(local_predecessor) {
alpar@774
   144
	delete predecessor;
alpar@774
   145
	local_predecessor=false;
alpar@774
   146
      }
alpar@774
   147
      predecessor = &m;
alpar@774
   148
      return *this;
alpar@774
   149
    }
alpar@774
   150
alpar@774
   151
    ///Sets the map storing the predecessor nodes.
alpar@774
   152
alpar@774
   153
    ///Sets the map storing the predecessor nodes.
alpar@774
   154
    ///If you don't use this function before calling \ref run(),
alpar@774
   155
    ///it will allocate one. The destuctor deallocates this
alpar@774
   156
    ///automatically allocated map, of course.
alpar@774
   157
    ///\return <tt> (*this) </tt>
alpar@774
   158
    Bfs &setPredNodeMap(PredNodeMap &m) 
alpar@774
   159
    {
alpar@774
   160
      if(local_pred_node) {
alpar@774
   161
	delete pred_node;
alpar@774
   162
	local_pred_node=false;
alpar@774
   163
      }
alpar@774
   164
      pred_node = &m;
alpar@774
   165
      return *this;
alpar@774
   166
    }
alpar@774
   167
alpar@774
   168
    ///Sets the map storing the distances calculated by the algorithm.
alpar@774
   169
alpar@774
   170
    ///Sets the map storing the distances calculated by the algorithm.
alpar@774
   171
    ///If you don't use this function before calling \ref run(),
alpar@774
   172
    ///it will allocate one. The destuctor deallocates this
alpar@774
   173
    ///automatically allocated map, of course.
alpar@774
   174
    ///\return <tt> (*this) </tt>
alpar@774
   175
    Bfs &setDistMap(DistMap &m) 
alpar@774
   176
    {
alpar@774
   177
      if(local_distance) {
alpar@774
   178
	delete distance;
alpar@774
   179
	local_distance=false;
alpar@774
   180
      }
alpar@774
   181
      distance = &m;
alpar@774
   182
      return *this;
alpar@774
   183
    }
alpar@774
   184
    
alpar@774
   185
  ///Runs %BFS algorithm from node \c s.
alpar@774
   186
alpar@774
   187
  ///This method runs the %BFS algorithm from a root node \c s
alpar@774
   188
  ///in order to
alpar@774
   189
  ///compute the
alpar@774
   190
  ///shortest path to each node. The algorithm computes
alpar@774
   191
  ///- The shortest path tree.
alpar@774
   192
  ///- The distance of each node from the root.
alpar@774
   193
 
alpar@774
   194
    void run(Node s) {
alpar@774
   195
      
alpar@774
   196
      init_maps();
alpar@774
   197
      
alpar@774
   198
      source = s;
alpar@774
   199
      
alpar@774
   200
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@774
   201
	predecessor->set(u,INVALID);
alpar@774
   202
	pred_node->set(u,INVALID);
alpar@774
   203
      }
alpar@774
   204
      
alpar@774
   205
      int N=G->nodeNum();
alpar@774
   206
      std::vector<typename Graph::Node> Q(N);
alpar@774
   207
      int Qh=0;
alpar@774
   208
      int Qt=0;
alpar@774
   209
      
alpar@774
   210
      Q[Qh++]=source;
alpar@774
   211
      distance->set(s, 0);
alpar@774
   212
      do {
alpar@774
   213
	Node m;
alpar@774
   214
	Node n=Q[Qt++];
alpar@774
   215
	int d= (*distance)[n]+1;
alpar@774
   216
	
alpar@774
   217
	for(OutEdgeIt e(*G,n);e!=INVALID;++e)
alpar@774
   218
	  if((m=G->head(e))!=s && (*predecessor)[m]==INVALID) {
alpar@774
   219
	    Q[Qh++]=m;
alpar@774
   220
	    predecessor->set(m,e);
alpar@774
   221
	    pred_node->set(m,n);
alpar@774
   222
	    distance->set(m,d);
alpar@774
   223
	  }
alpar@774
   224
      } while(Qt!=Qh);
alpar@774
   225
    }
alpar@774
   226
    
alpar@774
   227
    ///The distance of a node from the root.
alpar@774
   228
alpar@774
   229
    ///Returns the distance of a node from the root.
alpar@774
   230
    ///\pre \ref run() must be called before using this function.
alpar@774
   231
    ///\warning If node \c v in unreachable from the root the return value
alpar@774
   232
    ///of this funcion is undefined.
alpar@774
   233
    int dist(Node v) const { return (*distance)[v]; }
alpar@774
   234
alpar@774
   235
    ///Returns the 'previous edge' of the shortest path tree.
alpar@774
   236
alpar@774
   237
    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
alpar@774
   238
    ///i.e. it returns the last edge from a shortest path from the root to \c
alpar@774
   239
    ///v. It is \ref INVALID
alpar@774
   240
    ///if \c v is unreachable from the root or if \c v=s. The
alpar@774
   241
    ///shortest path tree used here is equal to the shortest path tree used in
alpar@774
   242
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
alpar@774
   243
    ///this function.
alpar@774
   244
    Edge pred(Node v) const { return (*predecessor)[v]; }
alpar@774
   245
alpar@774
   246
    ///Returns the 'previous node' of the shortest path tree.
alpar@774
   247
alpar@774
   248
    ///For a node \c v it returns the 'previous node' of the shortest path tree,
alpar@774
   249
    ///i.e. it returns the last but one node from a shortest path from the
alpar@774
   250
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
alpar@774
   251
    ///\c v=s. The shortest path tree used here is equal to the shortest path
alpar@774
   252
    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
alpar@774
   253
    ///using this function.
alpar@774
   254
    Node predNode(Node v) const { return (*pred_node)[v]; }
alpar@774
   255
    
alpar@774
   256
    ///Returns a reference to the NodeMap of distances.
alpar@774
   257
    
alpar@774
   258
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
alpar@774
   259
    ///be called before using this function.
alpar@774
   260
    const DistMap &distMap() const { return *distance;}
alpar@774
   261
 
alpar@774
   262
    ///Returns a reference to the shortest path tree map.
alpar@774
   263
alpar@774
   264
    ///Returns a reference to the NodeMap of the edges of the
alpar@774
   265
    ///shortest path tree.
alpar@774
   266
    ///\pre \ref run() must be called before using this function.
alpar@774
   267
    const PredMap &predMap() const { return *predecessor;}
alpar@774
   268
 
alpar@774
   269
    ///Returns a reference to the map of nodes of shortest paths.
alpar@774
   270
alpar@774
   271
    ///Returns a reference to the NodeMap of the last but one nodes of the
alpar@774
   272
    ///shortest path tree.
alpar@774
   273
    ///\pre \ref run() must be called before using this function.
alpar@774
   274
    const PredNodeMap &predNodeMap() const { return *pred_node;}
alpar@774
   275
alpar@774
   276
    ///Checks if a node is reachable from the root.
alpar@774
   277
alpar@774
   278
    ///Returns \c true if \c v is reachable from the root.
alpar@774
   279
    ///\warning The root node is reported to be reached!
alpar@774
   280
    ///
alpar@774
   281
    ///\pre \ref run() must be called before using this function.
alpar@774
   282
    ///
alpar@780
   283
    bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
alpar@774
   284
    
alpar@774
   285
  };
alpar@774
   286
  
alpar@774
   287
alpar@774
   288
  // **********************************************************************
alpar@774
   289
  //  IMPLEMENTATIONS
alpar@774
   290
  // **********************************************************************
alpar@774
   291
alpar@774
   292
/// @}
alpar@774
   293
  
alpar@774
   294
} //END OF NAMESPACE HUGO
alpar@774
   295
alpar@774
   296
#endif
alpar@774
   297
alpar@774
   298