src/lemon/bfs.h
author ladanyi
Thu, 04 Nov 2004 18:52:31 +0000
changeset 958 75f749682240
parent 921 818510fa3d99
child 977 48962802d168
permissions -rw-r--r--
Updated because of the recent changes in simann.h.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/bfs.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_BFS_H
alpar@921
    18
#define LEMON_BFS_H
alpar@774
    19
alpar@774
    20
///\ingroup flowalgs
alpar@774
    21
///\file
alpar@774
    22
///\brief Bfs algorithm.
alpar@774
    23
///
alpar@774
    24
///\todo Revise Manual.
alpar@774
    25
alpar@921
    26
#include <lemon/bin_heap.h>
alpar@921
    27
#include <lemon/invalid.h>
alpar@774
    28
alpar@921
    29
namespace lemon {
alpar@774
    30
alpar@774
    31
/// \addtogroup flowalgs
alpar@774
    32
/// @{
alpar@774
    33
alpar@781
    34
  ///%BFS algorithm class.
alpar@774
    35
alpar@781
    36
  ///This class provides an efficient implementation of %BFS algorithm.
alpar@781
    37
  ///\param GR The graph type the algorithm runs on.
alpar@781
    38
  ///This class does the same as Dijkstra does with constant 1 edge length,
alpar@781
    39
  ///but it is faster.
alpar@774
    40
  ///
alpar@781
    41
  ///\author Alpar Juttner
alpar@774
    42
alpar@774
    43
#ifdef DOXYGEN
alpar@774
    44
  template <typename GR>
alpar@774
    45
#else
alpar@774
    46
  template <typename GR>
alpar@774
    47
#endif
alpar@774
    48
  class Bfs{
alpar@774
    49
  public:
alpar@774
    50
    ///The type of the underlying graph.
alpar@774
    51
    typedef GR Graph;
alpar@911
    52
    ///\e
alpar@774
    53
    typedef typename Graph::Node Node;
alpar@911
    54
    ///\e
alpar@774
    55
    typedef typename Graph::NodeIt NodeIt;
alpar@911
    56
    ///\e
alpar@774
    57
    typedef typename Graph::Edge Edge;
alpar@911
    58
    ///\e
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@802
    71
    /// Pointer to the underlying graph.
alpar@774
    72
    const Graph *G;
alpar@802
    73
    ///Pointer to the map of predecessors edges.
alpar@774
    74
    PredMap *predecessor;
alpar@802
    75
    ///Indicates if \ref predecessor is locally allocated (\c true) or not.
alpar@774
    76
    bool local_predecessor;
alpar@802
    77
    ///Pointer to the map of predecessors nodes.
alpar@774
    78
    PredNodeMap *pred_node;
alpar@802
    79
    ///Indicates if \ref pred_node is locally allocated (\c true) or not.
alpar@774
    80
    bool local_pred_node;
alpar@802
    81
    ///Pointer to the map of distances.
alpar@774
    82
    DistMap *distance;
alpar@802
    83
    ///Indicates if \ref distance is locally allocated (\c true) or not.
alpar@774
    84
    bool local_distance;
alpar@774
    85
alpar@802
    86
    ///The source node of the last execution.
alpar@774
    87
    Node source;
alpar@774
    88
alpar@774
    89
alpar@781
    90
    ///Initializes the maps.
alpar@774
    91
    void init_maps() 
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@802
   108
    ///Constructor.
alpar@802
   109
    
alpar@802
   110
    ///\param _G the graph the algorithm will run on.
alpar@911
   111
    ///
alpar@774
   112
    Bfs(const Graph& _G) :
alpar@774
   113
      G(&_G),
alpar@774
   114
      predecessor(NULL), local_predecessor(false),
alpar@774
   115
      pred_node(NULL), local_pred_node(false),
alpar@774
   116
      distance(NULL), local_distance(false)
alpar@774
   117
    { }
alpar@774
   118
    
alpar@802
   119
    ///Destructor.
alpar@774
   120
    ~Bfs() 
alpar@774
   121
    {
alpar@774
   122
      if(local_predecessor) delete predecessor;
alpar@774
   123
      if(local_pred_node) delete pred_node;
alpar@774
   124
      if(local_distance) delete distance;
alpar@774
   125
    }
alpar@774
   126
alpar@774
   127
    ///Sets the map storing the predecessor edges.
alpar@774
   128
alpar@774
   129
    ///Sets the map storing the predecessor edges.
alpar@774
   130
    ///If you don't use this function before calling \ref run(),
alpar@774
   131
    ///it will allocate one. The destuctor deallocates this
alpar@774
   132
    ///automatically allocated map, of course.
alpar@774
   133
    ///\return <tt> (*this) </tt>
alpar@774
   134
    Bfs &setPredMap(PredMap &m) 
alpar@774
   135
    {
alpar@774
   136
      if(local_predecessor) {
alpar@774
   137
	delete predecessor;
alpar@774
   138
	local_predecessor=false;
alpar@774
   139
      }
alpar@774
   140
      predecessor = &m;
alpar@774
   141
      return *this;
alpar@774
   142
    }
alpar@774
   143
alpar@774
   144
    ///Sets the map storing the predecessor nodes.
alpar@774
   145
alpar@774
   146
    ///Sets the map storing the predecessor nodes.
alpar@774
   147
    ///If you don't use this function before calling \ref run(),
alpar@774
   148
    ///it will allocate one. The destuctor deallocates this
alpar@774
   149
    ///automatically allocated map, of course.
alpar@774
   150
    ///\return <tt> (*this) </tt>
alpar@774
   151
    Bfs &setPredNodeMap(PredNodeMap &m) 
alpar@774
   152
    {
alpar@774
   153
      if(local_pred_node) {
alpar@774
   154
	delete pred_node;
alpar@774
   155
	local_pred_node=false;
alpar@774
   156
      }
alpar@774
   157
      pred_node = &m;
alpar@774
   158
      return *this;
alpar@774
   159
    }
alpar@774
   160
alpar@774
   161
    ///Sets the map storing the distances calculated by the algorithm.
alpar@774
   162
alpar@774
   163
    ///Sets the map storing the distances calculated by the algorithm.
alpar@774
   164
    ///If you don't use this function before calling \ref run(),
alpar@774
   165
    ///it will allocate one. The destuctor deallocates this
alpar@774
   166
    ///automatically allocated map, of course.
alpar@774
   167
    ///\return <tt> (*this) </tt>
alpar@774
   168
    Bfs &setDistMap(DistMap &m) 
alpar@774
   169
    {
alpar@774
   170
      if(local_distance) {
alpar@774
   171
	delete distance;
alpar@774
   172
	local_distance=false;
alpar@774
   173
      }
alpar@774
   174
      distance = &m;
alpar@774
   175
      return *this;
alpar@774
   176
    }
alpar@774
   177
    
alpar@774
   178
  ///Runs %BFS algorithm from node \c s.
alpar@774
   179
alpar@774
   180
  ///This method runs the %BFS algorithm from a root node \c s
alpar@774
   181
  ///in order to
alpar@781
   182
  ///compute a
alpar@774
   183
  ///shortest path to each node. The algorithm computes
alpar@781
   184
  ///- The %BFS tree.
alpar@774
   185
  ///- The distance of each node from the root.
alpar@774
   186
 
alpar@774
   187
    void run(Node s) {
alpar@774
   188
      
alpar@774
   189
      init_maps();
alpar@774
   190
      
alpar@774
   191
      source = s;
alpar@774
   192
      
alpar@774
   193
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@774
   194
	predecessor->set(u,INVALID);
alpar@774
   195
	pred_node->set(u,INVALID);
alpar@774
   196
      }
alpar@774
   197
      
klao@946
   198
      int N = countNodes(*G);
alpar@774
   199
      std::vector<typename Graph::Node> Q(N);
alpar@774
   200
      int Qh=0;
alpar@774
   201
      int Qt=0;
alpar@774
   202
      
alpar@774
   203
      Q[Qh++]=source;
alpar@774
   204
      distance->set(s, 0);
alpar@774
   205
      do {
alpar@774
   206
	Node m;
alpar@774
   207
	Node n=Q[Qt++];
alpar@774
   208
	int d= (*distance)[n]+1;
alpar@774
   209
	
alpar@774
   210
	for(OutEdgeIt e(*G,n);e!=INVALID;++e)
alpar@774
   211
	  if((m=G->head(e))!=s && (*predecessor)[m]==INVALID) {
alpar@774
   212
	    Q[Qh++]=m;
alpar@774
   213
	    predecessor->set(m,e);
alpar@774
   214
	    pred_node->set(m,n);
alpar@774
   215
	    distance->set(m,d);
alpar@774
   216
	  }
alpar@774
   217
      } while(Qt!=Qh);
alpar@774
   218
    }
alpar@774
   219
    
alpar@774
   220
    ///The distance of a node from the root.
alpar@774
   221
alpar@774
   222
    ///Returns the distance of a node from the root.
alpar@774
   223
    ///\pre \ref run() must be called before using this function.
alpar@774
   224
    ///\warning If node \c v in unreachable from the root the return value
alpar@774
   225
    ///of this funcion is undefined.
alpar@774
   226
    int dist(Node v) const { return (*distance)[v]; }
alpar@774
   227
alpar@781
   228
    ///Returns the 'previous edge' of the %BFS path tree.
alpar@774
   229
alpar@781
   230
    ///For a node \c v it returns the 'previous edge' of the %BFS tree,
alpar@781
   231
    ///i.e. it returns the last edge of a shortest path from the root to \c
alpar@774
   232
    ///v. It is \ref INVALID
alpar@774
   233
    ///if \c v is unreachable from the root or if \c v=s. The
alpar@781
   234
    ///%BFS tree used here is equal to the %BFS tree used in
alpar@774
   235
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
alpar@774
   236
    ///this function.
alpar@774
   237
    Edge pred(Node v) const { return (*predecessor)[v]; }
alpar@774
   238
alpar@781
   239
    ///Returns the 'previous node' of the %BFS tree.
alpar@774
   240
alpar@781
   241
    ///For a node \c v it returns the 'previous node' on the %BFS tree,
alpar@774
   242
    ///i.e. it returns the last but one node from a shortest path from the
alpar@774
   243
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
alpar@781
   244
    ///\c v=s. The shortest path tree used here is equal to the %BFS
alpar@774
   245
    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
alpar@774
   246
    ///using this function.
alpar@774
   247
    Node predNode(Node v) const { return (*pred_node)[v]; }
alpar@774
   248
    
alpar@774
   249
    ///Returns a reference to the NodeMap of distances.
alpar@774
   250
    
alpar@774
   251
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
alpar@774
   252
    ///be called before using this function.
alpar@774
   253
    const DistMap &distMap() const { return *distance;}
alpar@774
   254
 
alpar@781
   255
    ///Returns a reference to the %BFS tree map.
alpar@774
   256
alpar@774
   257
    ///Returns a reference to the NodeMap of the edges of the
alpar@781
   258
    ///%BFS tree.
alpar@774
   259
    ///\pre \ref run() must be called before using this function.
alpar@774
   260
    const PredMap &predMap() const { return *predecessor;}
alpar@774
   261
 
alpar@781
   262
    ///Returns a reference to the map of last but one nodes of shortest paths.
alpar@774
   263
alpar@781
   264
    ///Returns a reference to the NodeMap of the last but one nodes on the
alpar@781
   265
    ///%BFS tree.
alpar@774
   266
    ///\pre \ref run() must be called before using this function.
alpar@774
   267
    const PredNodeMap &predNodeMap() const { return *pred_node;}
alpar@774
   268
alpar@774
   269
    ///Checks if a node is reachable from the root.
alpar@774
   270
alpar@774
   271
    ///Returns \c true if \c v is reachable from the root.
alpar@802
   272
    ///\note The root node is reported to be reached!
alpar@774
   273
    ///
alpar@774
   274
    ///\pre \ref run() must be called before using this function.
alpar@774
   275
    ///
alpar@780
   276
    bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
alpar@774
   277
    
alpar@774
   278
  };
alpar@774
   279
  
alpar@774
   280
/// @}
alpar@774
   281
  
alpar@921
   282
} //END OF NAMESPACE LEMON
alpar@774
   283
alpar@774
   284
#endif
alpar@774
   285
alpar@774
   286