lemon/bfs.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 24 Apr 2008 20:26:14 +0100
changeset 151 4d79daa40e9b
child 157 2ccc1afc2c52
permissions -rw-r--r--
Turn on built in Doxygen STL support
alpar@100
     1
/* -*- C++ -*-
alpar@100
     2
 *
alpar@100
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@100
     4
 *
alpar@100
     5
 * Copyright (C) 2003-2008
alpar@100
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@100
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@100
     8
 *
alpar@100
     9
 * Permission to use, modify and distribute this software is granted
alpar@100
    10
 * provided that this copyright notice appears in all copies. For
alpar@100
    11
 * precise terms see the accompanying LICENSE file.
alpar@100
    12
 *
alpar@100
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@100
    14
 * express or implied, and with no claim as to its suitability for any
alpar@100
    15
 * purpose.
alpar@100
    16
 *
alpar@100
    17
 */
alpar@100
    18
alpar@100
    19
#ifndef LEMON_BFS_H
alpar@100
    20
#define LEMON_BFS_H
alpar@100
    21
alpar@100
    22
///\ingroup search
alpar@100
    23
///\file
alpar@100
    24
///\brief Bfs algorithm.
alpar@100
    25
alpar@100
    26
#include <lemon/list_graph.h>
alpar@100
    27
#include <lemon/graph_utils.h>
alpar@100
    28
#include <lemon/bits/path_dump.h>
alpar@100
    29
#include <lemon/bits/invalid.h>
alpar@100
    30
#include <lemon/error.h>
alpar@100
    31
#include <lemon/maps.h>
alpar@100
    32
alpar@100
    33
namespace lemon {
alpar@100
    34
alpar@100
    35
alpar@100
    36
  
alpar@100
    37
  ///Default traits class of Bfs class.
alpar@100
    38
alpar@100
    39
  ///Default traits class of Bfs class.
alpar@100
    40
  ///\param GR Digraph type.
alpar@100
    41
  template<class GR>
alpar@100
    42
  struct BfsDefaultTraits
alpar@100
    43
  {
alpar@100
    44
    ///The digraph type the algorithm runs on. 
alpar@100
    45
    typedef GR Digraph;
alpar@100
    46
    ///\brief The type of the map that stores the last
alpar@100
    47
    ///arcs of the shortest paths.
alpar@100
    48
    /// 
alpar@100
    49
    ///The type of the map that stores the last
alpar@100
    50
    ///arcs of the shortest paths.
alpar@100
    51
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
    52
    ///
alpar@100
    53
    typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap;
alpar@100
    54
    ///Instantiates a PredMap.
alpar@100
    55
 
alpar@100
    56
    ///This function instantiates a \ref PredMap. 
alpar@100
    57
    ///\param G is the digraph, to which we would like to define the PredMap.
alpar@100
    58
    ///\todo The digraph alone may be insufficient to initialize
alpar@100
    59
    static PredMap *createPredMap(const GR &G) 
alpar@100
    60
    {
alpar@100
    61
      return new PredMap(G);
alpar@100
    62
    }
alpar@100
    63
    ///The type of the map that indicates which nodes are processed.
alpar@100
    64
 
alpar@100
    65
    ///The type of the map that indicates which nodes are processed.
alpar@100
    66
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
    67
    ///\todo named parameter to set this type, function to read and write.
alpar@100
    68
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
alpar@100
    69
    ///Instantiates a ProcessedMap.
alpar@100
    70
 
alpar@100
    71
    ///This function instantiates a \ref ProcessedMap. 
alpar@100
    72
    ///\param g is the digraph, to which
alpar@100
    73
    ///we would like to define the \ref ProcessedMap
alpar@100
    74
#ifdef DOXYGEN
alpar@100
    75
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@100
    76
#else
alpar@100
    77
    static ProcessedMap *createProcessedMap(const GR &)
alpar@100
    78
#endif
alpar@100
    79
    {
alpar@100
    80
      return new ProcessedMap();
alpar@100
    81
    }
alpar@100
    82
    ///The type of the map that indicates which nodes are reached.
alpar@100
    83
 
alpar@100
    84
    ///The type of the map that indicates which nodes are reached.
alpar@100
    85
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
    86
    ///\todo named parameter to set this type, function to read and write.
alpar@100
    87
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
alpar@100
    88
    ///Instantiates a ReachedMap.
alpar@100
    89
 
alpar@100
    90
    ///This function instantiates a \ref ReachedMap. 
alpar@100
    91
    ///\param G is the digraph, to which
alpar@100
    92
    ///we would like to define the \ref ReachedMap.
alpar@100
    93
    static ReachedMap *createReachedMap(const GR &G)
alpar@100
    94
    {
alpar@100
    95
      return new ReachedMap(G);
alpar@100
    96
    }
alpar@100
    97
    ///The type of the map that stores the dists of the nodes.
alpar@100
    98
 
alpar@100
    99
    ///The type of the map that stores the dists of the nodes.
alpar@100
   100
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   101
    ///
alpar@100
   102
    typedef typename Digraph::template NodeMap<int> DistMap;
alpar@100
   103
    ///Instantiates a DistMap.
alpar@100
   104
 
alpar@100
   105
    ///This function instantiates a \ref DistMap. 
alpar@100
   106
    ///\param G is the digraph, to which we would like to define the \ref DistMap
alpar@100
   107
    static DistMap *createDistMap(const GR &G)
alpar@100
   108
    {
alpar@100
   109
      return new DistMap(G);
alpar@100
   110
    }
alpar@100
   111
  };
alpar@100
   112
  
alpar@100
   113
  ///%BFS algorithm class.
alpar@100
   114
  
alpar@100
   115
  ///\ingroup search
alpar@100
   116
  ///This class provides an efficient implementation of the %BFS algorithm.
alpar@100
   117
  ///
alpar@100
   118
  ///\param GR The digraph type the algorithm runs on. The default value is
alpar@100
   119
  ///\ref ListDigraph. The value of GR is not used directly by Bfs, it
alpar@100
   120
  ///is only passed to \ref BfsDefaultTraits.
alpar@100
   121
  ///\param TR Traits class to set various data types used by the algorithm.
alpar@100
   122
  ///The default traits class is
alpar@100
   123
  ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
alpar@100
   124
  ///See \ref BfsDefaultTraits for the documentation of
alpar@100
   125
  ///a Bfs traits class.
alpar@100
   126
  ///
alpar@100
   127
  ///\author Alpar Juttner
alpar@100
   128
alpar@100
   129
#ifdef DOXYGEN
alpar@100
   130
  template <typename GR,
alpar@100
   131
	    typename TR>
alpar@100
   132
#else
alpar@100
   133
  template <typename GR=ListDigraph,
alpar@100
   134
	    typename TR=BfsDefaultTraits<GR> >
alpar@100
   135
#endif
alpar@100
   136
  class Bfs {
alpar@100
   137
  public:
alpar@100
   138
    /**
alpar@100
   139
     * \brief \ref Exception for uninitialized parameters.
alpar@100
   140
     *
alpar@100
   141
     * This error represents problems in the initialization
alpar@100
   142
     * of the parameters of the algorithms.
alpar@100
   143
     */
alpar@100
   144
    class UninitializedParameter : public lemon::UninitializedParameter {
alpar@100
   145
    public:
alpar@100
   146
      virtual const char* what() const throw() {
alpar@100
   147
	return "lemon::Bfs::UninitializedParameter";
alpar@100
   148
      }
alpar@100
   149
    };
alpar@100
   150
alpar@100
   151
    typedef TR Traits;
alpar@100
   152
    ///The type of the underlying digraph.
alpar@100
   153
    typedef typename TR::Digraph Digraph;
alpar@100
   154
    
alpar@100
   155
    ///\brief The type of the map that stores the last
alpar@100
   156
    ///arcs of the shortest paths.
alpar@100
   157
    typedef typename TR::PredMap PredMap;
alpar@100
   158
    ///The type of the map indicating which nodes are reached.
alpar@100
   159
    typedef typename TR::ReachedMap ReachedMap;
alpar@100
   160
    ///The type of the map indicating which nodes are processed.
alpar@100
   161
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@100
   162
    ///The type of the map that stores the dists of the nodes.
alpar@100
   163
    typedef typename TR::DistMap DistMap;
alpar@100
   164
  private:
alpar@100
   165
alpar@100
   166
    typedef typename Digraph::Node Node;
alpar@100
   167
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
   168
    typedef typename Digraph::Arc Arc;
alpar@100
   169
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@100
   170
alpar@100
   171
    /// Pointer to the underlying digraph.
alpar@100
   172
    const Digraph *G;
alpar@100
   173
    ///Pointer to the map of predecessors arcs.
alpar@100
   174
    PredMap *_pred;
alpar@100
   175
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
alpar@100
   176
    bool local_pred;
alpar@100
   177
    ///Pointer to the map of distances.
alpar@100
   178
    DistMap *_dist;
alpar@100
   179
    ///Indicates if \ref _dist is locally allocated (\c true) or not.
alpar@100
   180
    bool local_dist;
alpar@100
   181
    ///Pointer to the map of reached status of the nodes.
alpar@100
   182
    ReachedMap *_reached;
alpar@100
   183
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
alpar@100
   184
    bool local_reached;
alpar@100
   185
    ///Pointer to the map of processed status of the nodes.
alpar@100
   186
    ProcessedMap *_processed;
alpar@100
   187
    ///Indicates if \ref _processed is locally allocated (\c true) or not.
alpar@100
   188
    bool local_processed;
alpar@100
   189
alpar@100
   190
    std::vector<typename Digraph::Node> _queue;
alpar@100
   191
    int _queue_head,_queue_tail,_queue_next_dist;
alpar@100
   192
    int _curr_dist;
alpar@100
   193
alpar@100
   194
    ///Creates the maps if necessary.
alpar@100
   195
    
alpar@100
   196
    ///\todo Better memory allocation (instead of new).
alpar@100
   197
    void create_maps() 
alpar@100
   198
    {
alpar@100
   199
      if(!_pred) {
alpar@100
   200
	local_pred = true;
alpar@100
   201
	_pred = Traits::createPredMap(*G);
alpar@100
   202
      }
alpar@100
   203
      if(!_dist) {
alpar@100
   204
	local_dist = true;
alpar@100
   205
	_dist = Traits::createDistMap(*G);
alpar@100
   206
      }
alpar@100
   207
      if(!_reached) {
alpar@100
   208
	local_reached = true;
alpar@100
   209
	_reached = Traits::createReachedMap(*G);
alpar@100
   210
      }
alpar@100
   211
      if(!_processed) {
alpar@100
   212
	local_processed = true;
alpar@100
   213
	_processed = Traits::createProcessedMap(*G);
alpar@100
   214
      }
alpar@100
   215
    }
alpar@100
   216
alpar@100
   217
  protected:
alpar@100
   218
    
alpar@100
   219
    Bfs() {}
alpar@100
   220
    
alpar@100
   221
  public:
alpar@100
   222
 
alpar@100
   223
    typedef Bfs Create;
alpar@100
   224
alpar@100
   225
    ///\name Named template parameters
alpar@100
   226
alpar@100
   227
    ///@{
alpar@100
   228
alpar@100
   229
    template <class T>
alpar@100
   230
    struct DefPredMapTraits : public Traits {
alpar@100
   231
      typedef T PredMap;
alpar@100
   232
      static PredMap *createPredMap(const Digraph &) 
alpar@100
   233
      {
alpar@100
   234
	throw UninitializedParameter();
alpar@100
   235
      }
alpar@100
   236
    };
alpar@100
   237
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@100
   238
    ///PredMap type
alpar@100
   239
    ///
alpar@100
   240
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@100
   241
    ///
alpar@100
   242
    template <class T>
alpar@100
   243
    struct DefPredMap : public Bfs< Digraph, DefPredMapTraits<T> > { 
alpar@100
   244
      typedef Bfs< Digraph, DefPredMapTraits<T> > Create;
alpar@100
   245
    };
alpar@100
   246
    
alpar@100
   247
    template <class T>
alpar@100
   248
    struct DefDistMapTraits : public Traits {
alpar@100
   249
      typedef T DistMap;
alpar@100
   250
      static DistMap *createDistMap(const Digraph &) 
alpar@100
   251
      {
alpar@100
   252
	throw UninitializedParameter();
alpar@100
   253
      }
alpar@100
   254
    };
alpar@100
   255
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@100
   256
    ///DistMap type
alpar@100
   257
    ///
alpar@100
   258
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@100
   259
    ///
alpar@100
   260
    template <class T>
alpar@100
   261
    struct DefDistMap : public Bfs< Digraph, DefDistMapTraits<T> > { 
alpar@100
   262
      typedef Bfs< Digraph, DefDistMapTraits<T> > Create;
alpar@100
   263
    };
alpar@100
   264
    
alpar@100
   265
    template <class T>
alpar@100
   266
    struct DefReachedMapTraits : public Traits {
alpar@100
   267
      typedef T ReachedMap;
alpar@100
   268
      static ReachedMap *createReachedMap(const Digraph &) 
alpar@100
   269
      {
alpar@100
   270
	throw UninitializedParameter();
alpar@100
   271
      }
alpar@100
   272
    };
alpar@100
   273
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@100
   274
    ///ReachedMap type
alpar@100
   275
    ///
alpar@100
   276
    ///\ref named-templ-param "Named parameter" for setting ReachedMap type
alpar@100
   277
    ///
alpar@100
   278
    template <class T>
alpar@100
   279
    struct DefReachedMap : public Bfs< Digraph, DefReachedMapTraits<T> > { 
alpar@100
   280
      typedef Bfs< Digraph, DefReachedMapTraits<T> > Create;
alpar@100
   281
    };
alpar@100
   282
    
alpar@100
   283
    template <class T>
alpar@100
   284
    struct DefProcessedMapTraits : public Traits {
alpar@100
   285
      typedef T ProcessedMap;
alpar@100
   286
      static ProcessedMap *createProcessedMap(const Digraph &) 
alpar@100
   287
      {
alpar@100
   288
	throw UninitializedParameter();
alpar@100
   289
      }
alpar@100
   290
    };
alpar@100
   291
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@100
   292
    ///ProcessedMap type
alpar@100
   293
    ///
alpar@100
   294
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
alpar@100
   295
    ///
alpar@100
   296
    template <class T>
alpar@100
   297
    struct DefProcessedMap : public Bfs< Digraph, DefProcessedMapTraits<T> > {
alpar@100
   298
      typedef Bfs< Digraph, DefProcessedMapTraits<T> > Create;
alpar@100
   299
    };
alpar@100
   300
    
alpar@100
   301
    struct DefDigraphProcessedMapTraits : public Traits {
alpar@100
   302
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
alpar@100
   303
      static ProcessedMap *createProcessedMap(const Digraph &G) 
alpar@100
   304
      {
alpar@100
   305
	return new ProcessedMap(G);
alpar@100
   306
      }
alpar@100
   307
    };
alpar@100
   308
    ///\brief \ref named-templ-param "Named parameter"
alpar@100
   309
    ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
alpar@100
   310
    ///
alpar@100
   311
    ///\ref named-templ-param "Named parameter"
alpar@100
   312
    ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
alpar@100
   313
    ///If you don't set it explicitly, it will be automatically allocated.
alpar@100
   314
    template <class T>
alpar@100
   315
    struct DefProcessedMapToBeDefaultMap :
alpar@100
   316
      public Bfs< Digraph, DefDigraphProcessedMapTraits> { 
alpar@100
   317
      typedef Bfs< Digraph, DefDigraphProcessedMapTraits> Create;
alpar@100
   318
    };
alpar@100
   319
    
alpar@100
   320
    ///@}
alpar@100
   321
alpar@100
   322
  public:      
alpar@100
   323
    
alpar@100
   324
    ///Constructor.
alpar@100
   325
    
alpar@100
   326
    ///\param _G the digraph the algorithm will run on.
alpar@100
   327
    ///
alpar@100
   328
    Bfs(const Digraph& _G) :
alpar@100
   329
      G(&_G),
alpar@100
   330
      _pred(NULL), local_pred(false),
alpar@100
   331
      _dist(NULL), local_dist(false),
alpar@100
   332
      _reached(NULL), local_reached(false),
alpar@100
   333
      _processed(NULL), local_processed(false)
alpar@100
   334
    { }
alpar@100
   335
    
alpar@100
   336
    ///Destructor.
alpar@100
   337
    ~Bfs() 
alpar@100
   338
    {
alpar@100
   339
      if(local_pred) delete _pred;
alpar@100
   340
      if(local_dist) delete _dist;
alpar@100
   341
      if(local_reached) delete _reached;
alpar@100
   342
      if(local_processed) delete _processed;
alpar@100
   343
    }
alpar@100
   344
alpar@100
   345
    ///Sets the map storing the predecessor arcs.
alpar@100
   346
alpar@100
   347
    ///Sets the map storing the predecessor arcs.
alpar@100
   348
    ///If you don't use this function before calling \ref run(),
alpar@100
   349
    ///it will allocate one. The destructor deallocates this
alpar@100
   350
    ///automatically allocated map, of course.
alpar@100
   351
    ///\return <tt> (*this) </tt>
alpar@100
   352
    Bfs &predMap(PredMap &m) 
alpar@100
   353
    {
alpar@100
   354
      if(local_pred) {
alpar@100
   355
	delete _pred;
alpar@100
   356
	local_pred=false;
alpar@100
   357
      }
alpar@100
   358
      _pred = &m;
alpar@100
   359
      return *this;
alpar@100
   360
    }
alpar@100
   361
alpar@100
   362
    ///Sets the map indicating the reached nodes.
alpar@100
   363
alpar@100
   364
    ///Sets the map indicating the reached nodes.
alpar@100
   365
    ///If you don't use this function before calling \ref run(),
alpar@100
   366
    ///it will allocate one. The destructor deallocates this
alpar@100
   367
    ///automatically allocated map, of course.
alpar@100
   368
    ///\return <tt> (*this) </tt>
alpar@100
   369
    Bfs &reachedMap(ReachedMap &m) 
alpar@100
   370
    {
alpar@100
   371
      if(local_reached) {
alpar@100
   372
	delete _reached;
alpar@100
   373
	local_reached=false;
alpar@100
   374
      }
alpar@100
   375
      _reached = &m;
alpar@100
   376
      return *this;
alpar@100
   377
    }
alpar@100
   378
alpar@100
   379
    ///Sets the map indicating the processed nodes.
alpar@100
   380
alpar@100
   381
    ///Sets the map indicating the processed nodes.
alpar@100
   382
    ///If you don't use this function before calling \ref run(),
alpar@100
   383
    ///it will allocate one. The destructor deallocates this
alpar@100
   384
    ///automatically allocated map, of course.
alpar@100
   385
    ///\return <tt> (*this) </tt>
alpar@100
   386
    Bfs &processedMap(ProcessedMap &m) 
alpar@100
   387
    {
alpar@100
   388
      if(local_processed) {
alpar@100
   389
	delete _processed;
alpar@100
   390
	local_processed=false;
alpar@100
   391
      }
alpar@100
   392
      _processed = &m;
alpar@100
   393
      return *this;
alpar@100
   394
    }
alpar@100
   395
alpar@100
   396
    ///Sets the map storing the distances calculated by the algorithm.
alpar@100
   397
alpar@100
   398
    ///Sets the map storing the distances calculated by the algorithm.
alpar@100
   399
    ///If you don't use this function before calling \ref run(),
alpar@100
   400
    ///it will allocate one. The destructor deallocates this
alpar@100
   401
    ///automatically allocated map, of course.
alpar@100
   402
    ///\return <tt> (*this) </tt>
alpar@100
   403
    Bfs &distMap(DistMap &m) 
alpar@100
   404
    {
alpar@100
   405
      if(local_dist) {
alpar@100
   406
	delete _dist;
alpar@100
   407
	local_dist=false;
alpar@100
   408
      }
alpar@100
   409
      _dist = &m;
alpar@100
   410
      return *this;
alpar@100
   411
    }
alpar@100
   412
alpar@100
   413
  public:
alpar@100
   414
    ///\name Execution control
alpar@100
   415
    ///The simplest way to execute the algorithm is to use
alpar@100
   416
    ///one of the member functions called \c run(...).
alpar@100
   417
    ///\n
alpar@100
   418
    ///If you need more control on the execution,
alpar@100
   419
    ///first you must call \ref init(), then you can add several source nodes
alpar@100
   420
    ///with \ref addSource().
alpar@100
   421
    ///Finally \ref start() will perform the actual path
alpar@100
   422
    ///computation.
alpar@100
   423
alpar@100
   424
    ///@{
alpar@100
   425
alpar@100
   426
    ///\brief Initializes the internal data structures.
alpar@100
   427
    ///
alpar@100
   428
    ///Initializes the internal data structures.
alpar@100
   429
    ///
alpar@100
   430
    void init()
alpar@100
   431
    {
alpar@100
   432
      create_maps();
alpar@100
   433
      _queue.resize(countNodes(*G));
alpar@100
   434
      _queue_head=_queue_tail=0;
alpar@100
   435
      _curr_dist=1;
alpar@100
   436
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@100
   437
	_pred->set(u,INVALID);
alpar@100
   438
	_reached->set(u,false);
alpar@100
   439
	_processed->set(u,false);
alpar@100
   440
      }
alpar@100
   441
    }
alpar@100
   442
    
alpar@100
   443
    ///Adds a new source node.
alpar@100
   444
alpar@100
   445
    ///Adds a new source node to the set of nodes to be processed.
alpar@100
   446
    ///
alpar@100
   447
    void addSource(Node s)
alpar@100
   448
    {
alpar@100
   449
      if(!(*_reached)[s])
alpar@100
   450
	{
alpar@100
   451
	  _reached->set(s,true);
alpar@100
   452
	  _pred->set(s,INVALID);
alpar@100
   453
	  _dist->set(s,0);
alpar@100
   454
	  _queue[_queue_head++]=s;
alpar@100
   455
	  _queue_next_dist=_queue_head;
alpar@100
   456
	}
alpar@100
   457
    }
alpar@100
   458
    
alpar@100
   459
    ///Processes the next node.
alpar@100
   460
alpar@100
   461
    ///Processes the next node.
alpar@100
   462
    ///
alpar@100
   463
    ///\return The processed node.
alpar@100
   464
    ///
alpar@100
   465
    ///\warning The queue must not be empty!
alpar@100
   466
    Node processNextNode()
alpar@100
   467
    {
alpar@100
   468
      if(_queue_tail==_queue_next_dist) {
alpar@100
   469
	_curr_dist++;
alpar@100
   470
	_queue_next_dist=_queue_head;
alpar@100
   471
      }
alpar@100
   472
      Node n=_queue[_queue_tail++];
alpar@100
   473
      _processed->set(n,true);
alpar@100
   474
      Node m;
alpar@100
   475
      for(OutArcIt e(*G,n);e!=INVALID;++e)
alpar@100
   476
	if(!(*_reached)[m=G->target(e)]) {
alpar@100
   477
	  _queue[_queue_head++]=m;
alpar@100
   478
	  _reached->set(m,true);
alpar@100
   479
	  _pred->set(m,e);
alpar@100
   480
	  _dist->set(m,_curr_dist);
alpar@100
   481
	}
alpar@100
   482
      return n;
alpar@100
   483
    }
alpar@100
   484
alpar@100
   485
    ///Processes the next node.
alpar@100
   486
alpar@100
   487
    ///Processes the next node. And checks that the given target node
alpar@100
   488
    ///is reached. If the target node is reachable from the processed
alpar@100
   489
    ///node then the reached parameter will be set true. The reached
alpar@100
   490
    ///parameter should be initially false.
alpar@100
   491
    ///
alpar@100
   492
    ///\param target The target node.
alpar@100
   493
    ///\retval reach Indicates that the target node is reached.
alpar@100
   494
    ///\return The processed node.
alpar@100
   495
    ///
alpar@100
   496
    ///\warning The queue must not be empty!
alpar@100
   497
    Node processNextNode(Node target, bool& reach)
alpar@100
   498
    {
alpar@100
   499
      if(_queue_tail==_queue_next_dist) {
alpar@100
   500
	_curr_dist++;
alpar@100
   501
	_queue_next_dist=_queue_head;
alpar@100
   502
      }
alpar@100
   503
      Node n=_queue[_queue_tail++];
alpar@100
   504
      _processed->set(n,true);
alpar@100
   505
      Node m;
alpar@100
   506
      for(OutArcIt e(*G,n);e!=INVALID;++e)
alpar@100
   507
	if(!(*_reached)[m=G->target(e)]) {
alpar@100
   508
	  _queue[_queue_head++]=m;
alpar@100
   509
	  _reached->set(m,true);
alpar@100
   510
	  _pred->set(m,e);
alpar@100
   511
	  _dist->set(m,_curr_dist);
alpar@100
   512
          reach = reach || (target == m);
alpar@100
   513
	}
alpar@100
   514
      return n;
alpar@100
   515
    }
alpar@100
   516
alpar@100
   517
    ///Processes the next node.
alpar@100
   518
alpar@100
   519
    ///Processes the next node. And checks that at least one of
alpar@100
   520
    ///reached node has true value in the \c nm node map. If one node
alpar@100
   521
    ///with true value is reachable from the processed node then the
alpar@100
   522
    ///rnode parameter will be set to the first of such nodes.
alpar@100
   523
    ///
alpar@100
   524
    ///\param nm The node map of possible targets.
alpar@100
   525
    ///\retval rnode The reached target node.
alpar@100
   526
    ///\return The processed node.
alpar@100
   527
    ///
alpar@100
   528
    ///\warning The queue must not be empty!
alpar@100
   529
    template<class NM>
alpar@100
   530
    Node processNextNode(const NM& nm, Node& rnode)
alpar@100
   531
    {
alpar@100
   532
      if(_queue_tail==_queue_next_dist) {
alpar@100
   533
	_curr_dist++;
alpar@100
   534
	_queue_next_dist=_queue_head;
alpar@100
   535
      }
alpar@100
   536
      Node n=_queue[_queue_tail++];
alpar@100
   537
      _processed->set(n,true);
alpar@100
   538
      Node m;
alpar@100
   539
      for(OutArcIt e(*G,n);e!=INVALID;++e)
alpar@100
   540
	if(!(*_reached)[m=G->target(e)]) {
alpar@100
   541
	  _queue[_queue_head++]=m;
alpar@100
   542
	  _reached->set(m,true);
alpar@100
   543
	  _pred->set(m,e);
alpar@100
   544
	  _dist->set(m,_curr_dist);
alpar@100
   545
	  if (nm[m] && rnode == INVALID) rnode = m;
alpar@100
   546
	}
alpar@100
   547
      return n;
alpar@100
   548
    }
alpar@100
   549
      
alpar@100
   550
    ///Next node to be processed.
alpar@100
   551
alpar@100
   552
    ///Next node to be processed.
alpar@100
   553
    ///
alpar@100
   554
    ///\return The next node to be processed or INVALID if the queue is
alpar@100
   555
    /// empty.
alpar@100
   556
    Node nextNode()
alpar@100
   557
    { 
alpar@100
   558
      return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
alpar@100
   559
    }
alpar@100
   560
 
alpar@100
   561
    ///\brief Returns \c false if there are nodes
alpar@100
   562
    ///to be processed in the queue
alpar@100
   563
    ///
alpar@100
   564
    ///Returns \c false if there are nodes
alpar@100
   565
    ///to be processed in the queue
alpar@100
   566
    bool emptyQueue() { return _queue_tail==_queue_head; }
alpar@100
   567
    ///Returns the number of the nodes to be processed.
alpar@100
   568
    
alpar@100
   569
    ///Returns the number of the nodes to be processed in the queue.
alpar@100
   570
    int queueSize() { return _queue_head-_queue_tail; }
alpar@100
   571
    
alpar@100
   572
    ///Executes the algorithm.
alpar@100
   573
alpar@100
   574
    ///Executes the algorithm.
alpar@100
   575
    ///
alpar@100
   576
    ///\pre init() must be called and at least one node should be added
alpar@100
   577
    ///with addSource() before using this function.
alpar@100
   578
    ///
alpar@100
   579
    ///This method runs the %BFS algorithm from the root node(s)
alpar@100
   580
    ///in order to
alpar@100
   581
    ///compute the
alpar@100
   582
    ///shortest path to each node. The algorithm computes
alpar@100
   583
    ///- The shortest path tree.
alpar@100
   584
    ///- The distance of each node from the root(s).
alpar@100
   585
    void start()
alpar@100
   586
    {
alpar@100
   587
      while ( !emptyQueue() ) processNextNode();
alpar@100
   588
    }
alpar@100
   589
    
alpar@100
   590
    ///Executes the algorithm until \c dest is reached.
alpar@100
   591
alpar@100
   592
    ///Executes the algorithm until \c dest is reached.
alpar@100
   593
    ///
alpar@100
   594
    ///\pre init() must be called and at least one node should be added
alpar@100
   595
    ///with addSource() before using this function.
alpar@100
   596
    ///
alpar@100
   597
    ///This method runs the %BFS algorithm from the root node(s)
alpar@100
   598
    ///in order to compute the shortest path to \c dest.
alpar@100
   599
    ///The algorithm computes
alpar@100
   600
    ///- The shortest path to \c  dest.
alpar@100
   601
    ///- The distance of \c dest from the root(s).
alpar@100
   602
    void start(Node dest)
alpar@100
   603
    {
alpar@100
   604
      bool reach = false;
alpar@100
   605
      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
alpar@100
   606
    }
alpar@100
   607
    
alpar@100
   608
    ///Executes the algorithm until a condition is met.
alpar@100
   609
alpar@100
   610
    ///Executes the algorithm until a condition is met.
alpar@100
   611
    ///
alpar@100
   612
    ///\pre init() must be called and at least one node should be added
alpar@100
   613
    ///with addSource() before using this function.
alpar@100
   614
    ///
alpar@100
   615
    ///\param nm must be a bool (or convertible) node map. The
alpar@100
   616
    ///algorithm will stop when it reaches a node \c v with
alpar@100
   617
    /// <tt>nm[v]</tt> true.
alpar@100
   618
    ///
alpar@100
   619
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
alpar@100
   620
    ///\c INVALID if no such node was found.
alpar@100
   621
    template<class NM>
alpar@100
   622
    Node start(const NM &nm)
alpar@100
   623
    {
alpar@100
   624
      Node rnode = INVALID;
alpar@100
   625
      while ( !emptyQueue() && rnode == INVALID ) {
alpar@100
   626
	processNextNode(nm, rnode);
alpar@100
   627
      }
alpar@100
   628
      return rnode;
alpar@100
   629
    }
alpar@100
   630
    
alpar@100
   631
    ///Runs %BFS algorithm from node \c s.
alpar@100
   632
    
alpar@100
   633
    ///This method runs the %BFS algorithm from a root node \c s
alpar@100
   634
    ///in order to
alpar@100
   635
    ///compute the
alpar@100
   636
    ///shortest path to each node. The algorithm computes
alpar@100
   637
    ///- The shortest path tree.
alpar@100
   638
    ///- The distance of each node from the root.
alpar@100
   639
    ///
alpar@100
   640
    ///\note b.run(s) is just a shortcut of the following code.
alpar@100
   641
    ///\code
alpar@100
   642
    ///  b.init();
alpar@100
   643
    ///  b.addSource(s);
alpar@100
   644
    ///  b.start();
alpar@100
   645
    ///\endcode
alpar@100
   646
    void run(Node s) {
alpar@100
   647
      init();
alpar@100
   648
      addSource(s);
alpar@100
   649
      start();
alpar@100
   650
    }
alpar@100
   651
    
alpar@100
   652
    ///Finds the shortest path between \c s and \c t.
alpar@100
   653
    
alpar@100
   654
    ///Finds the shortest path between \c s and \c t.
alpar@100
   655
    ///
alpar@100
   656
    ///\return The length of the shortest s---t path if there exists one,
alpar@100
   657
    ///0 otherwise.
alpar@100
   658
    ///\note Apart from the return value, b.run(s) is
alpar@100
   659
    ///just a shortcut of the following code.
alpar@100
   660
    ///\code
alpar@100
   661
    ///  b.init();
alpar@100
   662
    ///  b.addSource(s);
alpar@100
   663
    ///  b.start(t);
alpar@100
   664
    ///\endcode
alpar@100
   665
    int run(Node s,Node t) {
alpar@100
   666
      init();
alpar@100
   667
      addSource(s);
alpar@100
   668
      start(t);
alpar@100
   669
      return reached(t) ? _curr_dist : 0;
alpar@100
   670
    }
alpar@100
   671
    
alpar@100
   672
    ///@}
alpar@100
   673
alpar@100
   674
    ///\name Query Functions
alpar@100
   675
    ///The result of the %BFS algorithm can be obtained using these
alpar@100
   676
    ///functions.\n
alpar@100
   677
    ///Before the use of these functions,
alpar@100
   678
    ///either run() or start() must be calleb.
alpar@100
   679
    
alpar@100
   680
    ///@{
alpar@100
   681
alpar@100
   682
    typedef PredMapPath<Digraph, PredMap> Path;
alpar@100
   683
alpar@100
   684
    ///Gives back the shortest path.
alpar@100
   685
    
alpar@100
   686
    ///Gives back the shortest path.
alpar@100
   687
    ///\pre The \c t should be reachable from the source.
alpar@100
   688
    Path path(Node t) 
alpar@100
   689
    {
alpar@100
   690
      return Path(*G, *_pred, t);
alpar@100
   691
    }
alpar@100
   692
alpar@100
   693
    ///The distance of a node from the root(s).
alpar@100
   694
alpar@100
   695
    ///Returns the distance of a node from the root(s).
alpar@100
   696
    ///\pre \ref run() must be called before using this function.
alpar@100
   697
    ///\warning If node \c v in unreachable from the root(s) the return value
alpar@100
   698
    ///of this function is undefined.
alpar@100
   699
    int dist(Node v) const { return (*_dist)[v]; }
alpar@100
   700
alpar@100
   701
    ///Returns the 'previous arc' of the shortest path tree.
alpar@100
   702
alpar@100
   703
    ///For a node \c v it returns the 'previous arc'
alpar@100
   704
    ///of the shortest path tree,
alpar@100
   705
    ///i.e. it returns the last arc of a shortest path from the root(s) to \c
alpar@100
   706
    ///v. It is \ref INVALID
alpar@100
   707
    ///if \c v is unreachable from the root(s) or \c v is a root. The
alpar@100
   708
    ///shortest path tree used here is equal to the shortest path tree used in
alpar@100
   709
    ///\ref predNode().
alpar@100
   710
    ///\pre Either \ref run() or \ref start() must be called before using
alpar@100
   711
    ///this function.
alpar@100
   712
    Arc predArc(Node v) const { return (*_pred)[v];}
alpar@100
   713
alpar@100
   714
    ///Returns the 'previous node' of the shortest path tree.
alpar@100
   715
alpar@100
   716
    ///For a node \c v it returns the 'previous node'
alpar@100
   717
    ///of the shortest path tree,
alpar@100
   718
    ///i.e. it returns the last but one node from a shortest path from the
alpar@100
   719
    ///root(a) to \c /v.
alpar@100
   720
    ///It is INVALID if \c v is unreachable from the root(s) or
alpar@100
   721
    ///if \c v itself a root.
alpar@100
   722
    ///The shortest path tree used here is equal to the shortest path
alpar@100
   723
    ///tree used in \ref predArc().
alpar@100
   724
    ///\pre Either \ref run() or \ref start() must be called before
alpar@100
   725
    ///using this function.
alpar@100
   726
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@100
   727
				  G->source((*_pred)[v]); }
alpar@100
   728
    
alpar@100
   729
    ///Returns a reference to the NodeMap of distances.
alpar@100
   730
alpar@100
   731
    ///Returns a reference to the NodeMap of distances.
alpar@100
   732
    ///\pre Either \ref run() or \ref init() must
alpar@100
   733
    ///be called before using this function.
alpar@100
   734
    const DistMap &distMap() const { return *_dist;}
alpar@100
   735
 
alpar@100
   736
    ///Returns a reference to the shortest path tree map.
alpar@100
   737
alpar@100
   738
    ///Returns a reference to the NodeMap of the arcs of the
alpar@100
   739
    ///shortest path tree.
alpar@100
   740
    ///\pre Either \ref run() or \ref init()
alpar@100
   741
    ///must be called before using this function.
alpar@100
   742
    const PredMap &predMap() const { return *_pred;}
alpar@100
   743
 
alpar@100
   744
    ///Checks if a node is reachable from the root.
alpar@100
   745
alpar@100
   746
    ///Returns \c true if \c v is reachable from the root.
alpar@100
   747
    ///\warning The source nodes are indicated as unreached.
alpar@100
   748
    ///\pre Either \ref run() or \ref start()
alpar@100
   749
    ///must be called before using this function.
alpar@100
   750
    ///
alpar@100
   751
    bool reached(Node v) { return (*_reached)[v]; }
alpar@100
   752
    
alpar@100
   753
    ///@}
alpar@100
   754
  };
alpar@100
   755
alpar@100
   756
  ///Default traits class of Bfs function.
alpar@100
   757
alpar@100
   758
  ///Default traits class of Bfs function.
alpar@100
   759
  ///\param GR Digraph type.
alpar@100
   760
  template<class GR>
alpar@100
   761
  struct BfsWizardDefaultTraits
alpar@100
   762
  {
alpar@100
   763
    ///The digraph type the algorithm runs on. 
alpar@100
   764
    typedef GR Digraph;
alpar@100
   765
    ///\brief The type of the map that stores the last
alpar@100
   766
    ///arcs of the shortest paths.
alpar@100
   767
    /// 
alpar@100
   768
    ///The type of the map that stores the last
alpar@100
   769
    ///arcs of the shortest paths.
alpar@100
   770
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   771
    ///
alpar@100
   772
    typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap;
alpar@100
   773
    ///Instantiates a PredMap.
alpar@100
   774
 
alpar@100
   775
    ///This function instantiates a \ref PredMap. 
alpar@100
   776
    ///\param g is the digraph, to which we would like to define the PredMap.
alpar@100
   777
    ///\todo The digraph alone may be insufficient to initialize
alpar@100
   778
#ifdef DOXYGEN
alpar@100
   779
    static PredMap *createPredMap(const GR &g) 
alpar@100
   780
#else
alpar@100
   781
    static PredMap *createPredMap(const GR &) 
alpar@100
   782
#endif
alpar@100
   783
    {
alpar@100
   784
      return new PredMap();
alpar@100
   785
    }
alpar@100
   786
alpar@100
   787
    ///The type of the map that indicates which nodes are processed.
alpar@100
   788
 
alpar@100
   789
    ///The type of the map that indicates which nodes are processed.
alpar@100
   790
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   791
    ///\todo named parameter to set this type, function to read and write.
alpar@100
   792
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
alpar@100
   793
    ///Instantiates a ProcessedMap.
alpar@100
   794
 
alpar@100
   795
    ///This function instantiates a \ref ProcessedMap. 
alpar@100
   796
    ///\param g is the digraph, to which
alpar@100
   797
    ///we would like to define the \ref ProcessedMap
alpar@100
   798
#ifdef DOXYGEN
alpar@100
   799
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@100
   800
#else
alpar@100
   801
    static ProcessedMap *createProcessedMap(const GR &)
alpar@100
   802
#endif
alpar@100
   803
    {
alpar@100
   804
      return new ProcessedMap();
alpar@100
   805
    }
alpar@100
   806
    ///The type of the map that indicates which nodes are reached.
alpar@100
   807
 
alpar@100
   808
    ///The type of the map that indicates which nodes are reached.
alpar@100
   809
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   810
    ///\todo named parameter to set this type, function to read and write.
alpar@100
   811
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
alpar@100
   812
    ///Instantiates a ReachedMap.
alpar@100
   813
 
alpar@100
   814
    ///This function instantiates a \ref ReachedMap. 
alpar@100
   815
    ///\param G is the digraph, to which
alpar@100
   816
    ///we would like to define the \ref ReachedMap.
alpar@100
   817
    static ReachedMap *createReachedMap(const GR &G)
alpar@100
   818
    {
alpar@100
   819
      return new ReachedMap(G);
alpar@100
   820
    }
alpar@100
   821
    ///The type of the map that stores the dists of the nodes.
alpar@100
   822
 
alpar@100
   823
    ///The type of the map that stores the dists of the nodes.
alpar@100
   824
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   825
    ///
alpar@100
   826
    typedef NullMap<typename Digraph::Node,int> DistMap;
alpar@100
   827
    ///Instantiates a DistMap.
alpar@100
   828
 
alpar@100
   829
    ///This function instantiates a \ref DistMap. 
alpar@100
   830
    ///\param g is the digraph, to which we would like to define the \ref DistMap
alpar@100
   831
#ifdef DOXYGEN
alpar@100
   832
    static DistMap *createDistMap(const GR &g)
alpar@100
   833
#else
alpar@100
   834
    static DistMap *createDistMap(const GR &)
alpar@100
   835
#endif
alpar@100
   836
    {
alpar@100
   837
      return new DistMap();
alpar@100
   838
    }
alpar@100
   839
  };
alpar@100
   840
  
alpar@100
   841
  /// Default traits used by \ref BfsWizard
alpar@100
   842
alpar@100
   843
  /// To make it easier to use Bfs algorithm
alpar@100
   844
  ///we have created a wizard class.
alpar@100
   845
  /// This \ref BfsWizard class needs default traits,
alpar@100
   846
  ///as well as the \ref Bfs class.
alpar@100
   847
  /// The \ref BfsWizardBase is a class to be the default traits of the
alpar@100
   848
  /// \ref BfsWizard class.
alpar@100
   849
  template<class GR>
alpar@100
   850
  class BfsWizardBase : public BfsWizardDefaultTraits<GR>
alpar@100
   851
  {
alpar@100
   852
alpar@100
   853
    typedef BfsWizardDefaultTraits<GR> Base;
alpar@100
   854
  protected:
alpar@100
   855
    /// Type of the nodes in the digraph.
alpar@100
   856
    typedef typename Base::Digraph::Node Node;
alpar@100
   857
alpar@100
   858
    /// Pointer to the underlying digraph.
alpar@100
   859
    void *_g;
alpar@100
   860
    ///Pointer to the map of reached nodes.
alpar@100
   861
    void *_reached;
alpar@100
   862
    ///Pointer to the map of processed nodes.
alpar@100
   863
    void *_processed;
alpar@100
   864
    ///Pointer to the map of predecessors arcs.
alpar@100
   865
    void *_pred;
alpar@100
   866
    ///Pointer to the map of distances.
alpar@100
   867
    void *_dist;
alpar@100
   868
    ///Pointer to the source node.
alpar@100
   869
    Node _source;
alpar@100
   870
    
alpar@100
   871
    public:
alpar@100
   872
    /// Constructor.
alpar@100
   873
    
alpar@100
   874
    /// This constructor does not require parameters, therefore it initiates
alpar@100
   875
    /// all of the attributes to default values (0, INVALID).
alpar@100
   876
    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
alpar@100
   877
			   _dist(0), _source(INVALID) {}
alpar@100
   878
alpar@100
   879
    /// Constructor.
alpar@100
   880
    
alpar@100
   881
    /// This constructor requires some parameters,
alpar@100
   882
    /// listed in the parameters list.
alpar@100
   883
    /// Others are initiated to 0.
alpar@100
   884
    /// \param g is the initial value of  \ref _g
alpar@100
   885
    /// \param s is the initial value of  \ref _source
alpar@100
   886
    BfsWizardBase(const GR &g, Node s=INVALID) :
alpar@100
   887
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 
alpar@100
   888
      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
alpar@100
   889
alpar@100
   890
  };
alpar@100
   891
  
alpar@100
   892
  /// A class to make the usage of Bfs algorithm easier
alpar@100
   893
alpar@100
   894
  /// This class is created to make it easier to use Bfs algorithm.
alpar@100
   895
  /// It uses the functions and features of the plain \ref Bfs,
alpar@100
   896
  /// but it is much simpler to use it.
alpar@100
   897
  ///
alpar@100
   898
  /// Simplicity means that the way to change the types defined
alpar@100
   899
  /// in the traits class is based on functions that returns the new class
alpar@100
   900
  /// and not on templatable built-in classes.
alpar@100
   901
  /// When using the plain \ref Bfs
alpar@100
   902
  /// the new class with the modified type comes from
alpar@100
   903
  /// the original class by using the ::
alpar@100
   904
  /// operator. In the case of \ref BfsWizard only
alpar@100
   905
  /// a function have to be called and it will
alpar@100
   906
  /// return the needed class.
alpar@100
   907
  ///
alpar@100
   908
  /// It does not have own \ref run method. When its \ref run method is called
alpar@100
   909
  /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
alpar@100
   910
  /// method of it.
alpar@100
   911
  template<class TR>
alpar@100
   912
  class BfsWizard : public TR
alpar@100
   913
  {
alpar@100
   914
    typedef TR Base;
alpar@100
   915
alpar@100
   916
    ///The type of the underlying digraph.
alpar@100
   917
    typedef typename TR::Digraph Digraph;
alpar@100
   918
    //\e
alpar@100
   919
    typedef typename Digraph::Node Node;
alpar@100
   920
    //\e
alpar@100
   921
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
   922
    //\e
alpar@100
   923
    typedef typename Digraph::Arc Arc;
alpar@100
   924
    //\e
alpar@100
   925
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@100
   926
    
alpar@100
   927
    ///\brief The type of the map that stores
alpar@100
   928
    ///the reached nodes
alpar@100
   929
    typedef typename TR::ReachedMap ReachedMap;
alpar@100
   930
    ///\brief The type of the map that stores
alpar@100
   931
    ///the processed nodes
alpar@100
   932
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@100
   933
    ///\brief The type of the map that stores the last
alpar@100
   934
    ///arcs of the shortest paths.
alpar@100
   935
    typedef typename TR::PredMap PredMap;
alpar@100
   936
    ///The type of the map that stores the dists of the nodes.
alpar@100
   937
    typedef typename TR::DistMap DistMap;
alpar@100
   938
alpar@100
   939
  public:
alpar@100
   940
    /// Constructor.
alpar@100
   941
    BfsWizard() : TR() {}
alpar@100
   942
alpar@100
   943
    /// Constructor that requires parameters.
alpar@100
   944
alpar@100
   945
    /// Constructor that requires parameters.
alpar@100
   946
    /// These parameters will be the default values for the traits class.
alpar@100
   947
    BfsWizard(const Digraph &g, Node s=INVALID) :
alpar@100
   948
      TR(g,s) {}
alpar@100
   949
alpar@100
   950
    ///Copy constructor
alpar@100
   951
    BfsWizard(const TR &b) : TR(b) {}
alpar@100
   952
alpar@100
   953
    ~BfsWizard() {}
alpar@100
   954
alpar@100
   955
    ///Runs Bfs algorithm from a given node.
alpar@100
   956
    
alpar@100
   957
    ///Runs Bfs algorithm from a given node.
alpar@100
   958
    ///The node can be given by the \ref source function.
alpar@100
   959
    void run()
alpar@100
   960
    {
alpar@100
   961
      if(Base::_source==INVALID) throw UninitializedParameter();
alpar@100
   962
      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
alpar@100
   963
      if(Base::_reached)
alpar@100
   964
	alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
alpar@100
   965
      if(Base::_processed) 
alpar@100
   966
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
alpar@100
   967
      if(Base::_pred) 
alpar@100
   968
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
alpar@100
   969
      if(Base::_dist) 
alpar@100
   970
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
alpar@100
   971
      alg.run(Base::_source);
alpar@100
   972
    }
alpar@100
   973
alpar@100
   974
    ///Runs Bfs algorithm from the given node.
alpar@100
   975
alpar@100
   976
    ///Runs Bfs algorithm from the given node.
alpar@100
   977
    ///\param s is the given source.
alpar@100
   978
    void run(Node s)
alpar@100
   979
    {
alpar@100
   980
      Base::_source=s;
alpar@100
   981
      run();
alpar@100
   982
    }
alpar@100
   983
alpar@100
   984
    template<class T>
alpar@100
   985
    struct DefPredMapBase : public Base {
alpar@100
   986
      typedef T PredMap;
alpar@100
   987
      static PredMap *createPredMap(const Digraph &) { return 0; };
alpar@100
   988
      DefPredMapBase(const TR &b) : TR(b) {}
alpar@100
   989
    };
alpar@100
   990
    
alpar@100
   991
    ///\brief \ref named-templ-param "Named parameter"
alpar@100
   992
    ///function for setting PredMap
alpar@100
   993
    ///
alpar@100
   994
    /// \ref named-templ-param "Named parameter"
alpar@100
   995
    ///function for setting PredMap
alpar@100
   996
    ///
alpar@100
   997
    template<class T>
alpar@100
   998
    BfsWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@100
   999
    {
alpar@100
  1000
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@100
  1001
      return BfsWizard<DefPredMapBase<T> >(*this);
alpar@100
  1002
    }
alpar@100
  1003
    
alpar@100
  1004
 
alpar@100
  1005
    template<class T>
alpar@100
  1006
    struct DefReachedMapBase : public Base {
alpar@100
  1007
      typedef T ReachedMap;
alpar@100
  1008
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
alpar@100
  1009
      DefReachedMapBase(const TR &b) : TR(b) {}
alpar@100
  1010
    };
alpar@100
  1011
    
alpar@100
  1012
    ///\brief \ref named-templ-param "Named parameter"
alpar@100
  1013
    ///function for setting ReachedMap
alpar@100
  1014
    ///
alpar@100
  1015
    /// \ref named-templ-param "Named parameter"
alpar@100
  1016
    ///function for setting ReachedMap
alpar@100
  1017
    ///
alpar@100
  1018
    template<class T>
alpar@100
  1019
    BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
alpar@100
  1020
    {
alpar@100
  1021
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@100
  1022
      return BfsWizard<DefReachedMapBase<T> >(*this);
alpar@100
  1023
    }
alpar@100
  1024
    
alpar@100
  1025
alpar@100
  1026
    template<class T>
alpar@100
  1027
    struct DefProcessedMapBase : public Base {
alpar@100
  1028
      typedef T ProcessedMap;
alpar@100
  1029
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
alpar@100
  1030
      DefProcessedMapBase(const TR &b) : TR(b) {}
alpar@100
  1031
    };
alpar@100
  1032
    
alpar@100
  1033
    ///\brief \ref named-templ-param "Named parameter"
alpar@100
  1034
    ///function for setting ProcessedMap
alpar@100
  1035
    ///
alpar@100
  1036
    /// \ref named-templ-param "Named parameter"
alpar@100
  1037
    ///function for setting ProcessedMap
alpar@100
  1038
    ///
alpar@100
  1039
    template<class T>
alpar@100
  1040
    BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
alpar@100
  1041
    {
alpar@100
  1042
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@100
  1043
      return BfsWizard<DefProcessedMapBase<T> >(*this);
alpar@100
  1044
    }
alpar@100
  1045
    
alpar@100
  1046
   
alpar@100
  1047
    template<class T>
alpar@100
  1048
    struct DefDistMapBase : public Base {
alpar@100
  1049
      typedef T DistMap;
alpar@100
  1050
      static DistMap *createDistMap(const Digraph &) { return 0; };
alpar@100
  1051
      DefDistMapBase(const TR &b) : TR(b) {}
alpar@100
  1052
    };
alpar@100
  1053
    
alpar@100
  1054
    ///\brief \ref named-templ-param "Named parameter"
alpar@100
  1055
    ///function for setting DistMap type
alpar@100
  1056
    ///
alpar@100
  1057
    /// \ref named-templ-param "Named parameter"
alpar@100
  1058
    ///function for setting DistMap type
alpar@100
  1059
    ///
alpar@100
  1060
    template<class T>
alpar@100
  1061
    BfsWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@100
  1062
    {
alpar@100
  1063
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@100
  1064
      return BfsWizard<DefDistMapBase<T> >(*this);
alpar@100
  1065
    }
alpar@100
  1066
    
alpar@100
  1067
    /// Sets the source node, from which the Bfs algorithm runs.
alpar@100
  1068
alpar@100
  1069
    /// Sets the source node, from which the Bfs algorithm runs.
alpar@100
  1070
    /// \param s is the source node.
alpar@100
  1071
    BfsWizard<TR> &source(Node s) 
alpar@100
  1072
    {
alpar@100
  1073
      Base::_source=s;
alpar@100
  1074
      return *this;
alpar@100
  1075
    }
alpar@100
  1076
    
alpar@100
  1077
  };
alpar@100
  1078
  
alpar@100
  1079
  ///Function type interface for Bfs algorithm.
alpar@100
  1080
alpar@100
  1081
  /// \ingroup search
alpar@100
  1082
  ///Function type interface for Bfs algorithm.
alpar@100
  1083
  ///
alpar@100
  1084
  ///This function also has several
alpar@100
  1085
  ///\ref named-templ-func-param "named parameters",
alpar@100
  1086
  ///they are declared as the members of class \ref BfsWizard.
alpar@100
  1087
  ///The following
alpar@100
  1088
  ///example shows how to use these parameters.
alpar@100
  1089
  ///\code
alpar@100
  1090
  ///  bfs(g,source).predMap(preds).run();
alpar@100
  1091
  ///\endcode
alpar@100
  1092
  ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
alpar@100
  1093
  ///to the end of the parameter list.
alpar@100
  1094
  ///\sa BfsWizard
alpar@100
  1095
  ///\sa Bfs
alpar@100
  1096
  template<class GR>
alpar@100
  1097
  BfsWizard<BfsWizardBase<GR> >
alpar@100
  1098
  bfs(const GR &g,typename GR::Node s=INVALID)
alpar@100
  1099
  {
alpar@100
  1100
    return BfsWizard<BfsWizardBase<GR> >(g,s);
alpar@100
  1101
  }
alpar@100
  1102
alpar@100
  1103
#ifdef DOXYGEN
alpar@100
  1104
  /// \brief Visitor class for bfs.
alpar@100
  1105
  ///  
alpar@100
  1106
  /// This class defines the interface of the BfsVisit events, and
alpar@100
  1107
  /// it could be the base of a real Visitor class.
alpar@100
  1108
  template <typename _Digraph>
alpar@100
  1109
  struct BfsVisitor {
alpar@100
  1110
    typedef _Digraph Digraph;
alpar@100
  1111
    typedef typename Digraph::Arc Arc;
alpar@100
  1112
    typedef typename Digraph::Node Node;
alpar@100
  1113
    /// \brief Called when the arc reach a node.
alpar@100
  1114
    /// 
alpar@100
  1115
    /// It is called when the bfs find an arc which target is not
alpar@100
  1116
    /// reached yet.
alpar@100
  1117
    void discover(const Arc& arc) {}
alpar@100
  1118
    /// \brief Called when the node reached first time.
alpar@100
  1119
    /// 
alpar@100
  1120
    /// It is Called when the node reached first time.
alpar@100
  1121
    void reach(const Node& node) {}
alpar@100
  1122
    /// \brief Called when the arc examined but target of the arc 
alpar@100
  1123
    /// already discovered.
alpar@100
  1124
    /// 
alpar@100
  1125
    /// It called when the arc examined but the target of the arc 
alpar@100
  1126
    /// already discovered.
alpar@100
  1127
    void examine(const Arc& arc) {}
alpar@100
  1128
    /// \brief Called for the source node of the bfs.
alpar@100
  1129
    /// 
alpar@100
  1130
    /// It is called for the source node of the bfs.
alpar@100
  1131
    void start(const Node& node) {}
alpar@100
  1132
    /// \brief Called when the node processed.
alpar@100
  1133
    /// 
alpar@100
  1134
    /// It is Called when the node processed.
alpar@100
  1135
    void process(const Node& node) {}
alpar@100
  1136
  };
alpar@100
  1137
#else
alpar@100
  1138
  template <typename _Digraph>
alpar@100
  1139
  struct BfsVisitor {
alpar@100
  1140
    typedef _Digraph Digraph;
alpar@100
  1141
    typedef typename Digraph::Arc Arc;
alpar@100
  1142
    typedef typename Digraph::Node Node;
alpar@100
  1143
    void discover(const Arc&) {}
alpar@100
  1144
    void reach(const Node&) {}
alpar@100
  1145
    void examine(const Arc&) {}
alpar@100
  1146
    void start(const Node&) {}
alpar@100
  1147
    void process(const Node&) {}
alpar@100
  1148
alpar@100
  1149
    template <typename _Visitor>
alpar@100
  1150
    struct Constraints {
alpar@100
  1151
      void constraints() {
alpar@100
  1152
	Arc arc;
alpar@100
  1153
	Node node;
alpar@100
  1154
	visitor.discover(arc);
alpar@100
  1155
	visitor.reach(node);
alpar@100
  1156
	visitor.examine(arc);
alpar@100
  1157
	visitor.start(node);
alpar@100
  1158
        visitor.process(node);
alpar@100
  1159
      }
alpar@100
  1160
      _Visitor& visitor;
alpar@100
  1161
    };
alpar@100
  1162
  };
alpar@100
  1163
#endif
alpar@100
  1164
alpar@100
  1165
  /// \brief Default traits class of BfsVisit class.
alpar@100
  1166
  ///
alpar@100
  1167
  /// Default traits class of BfsVisit class.
alpar@100
  1168
  /// \param _Digraph Digraph type.
alpar@100
  1169
  template<class _Digraph>
alpar@100
  1170
  struct BfsVisitDefaultTraits {
alpar@100
  1171
alpar@100
  1172
    /// \brief The digraph type the algorithm runs on. 
alpar@100
  1173
    typedef _Digraph Digraph;
alpar@100
  1174
alpar@100
  1175
    /// \brief The type of the map that indicates which nodes are reached.
alpar@100
  1176
    /// 
alpar@100
  1177
    /// The type of the map that indicates which nodes are reached.
alpar@100
  1178
    /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
  1179
    /// \todo named parameter to set this type, function to read and write.
alpar@100
  1180
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
alpar@100
  1181
alpar@100
  1182
    /// \brief Instantiates a ReachedMap.
alpar@100
  1183
    ///
alpar@100
  1184
    /// This function instantiates a \ref ReachedMap. 
alpar@100
  1185
    /// \param digraph is the digraph, to which
alpar@100
  1186
    /// we would like to define the \ref ReachedMap.
alpar@100
  1187
    static ReachedMap *createReachedMap(const Digraph &digraph) {
alpar@100
  1188
      return new ReachedMap(digraph);
alpar@100
  1189
    }
alpar@100
  1190
alpar@100
  1191
  };
alpar@100
  1192
alpar@100
  1193
  /// \ingroup search
alpar@100
  1194
  ///  
alpar@100
  1195
  /// \brief %BFS Visit algorithm class.
alpar@100
  1196
  ///  
alpar@100
  1197
  /// This class provides an efficient implementation of the %BFS algorithm
alpar@100
  1198
  /// with visitor interface.
alpar@100
  1199
  ///
alpar@100
  1200
  /// The %BfsVisit class provides an alternative interface to the Bfs
alpar@100
  1201
  /// class. It works with callback mechanism, the BfsVisit object calls
alpar@100
  1202
  /// on every bfs event the \c Visitor class member functions. 
alpar@100
  1203
  ///
alpar@100
  1204
  /// \param _Digraph The digraph type the algorithm runs on. The default value is
alpar@100
  1205
  /// \ref ListDigraph. The value of _Digraph is not used directly by Bfs, it
alpar@100
  1206
  /// is only passed to \ref BfsDefaultTraits.
alpar@100
  1207
  /// \param _Visitor The Visitor object for the algorithm. The 
alpar@100
  1208
  /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty Visitor which
alpar@100
  1209
  /// does not observe the Bfs events. If you want to observe the bfs
alpar@100
  1210
  /// events you should implement your own Visitor class.
alpar@100
  1211
  /// \param _Traits Traits class to set various data types used by the 
alpar@100
  1212
  /// algorithm. The default traits class is
alpar@100
  1213
  /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
alpar@100
  1214
  /// See \ref BfsVisitDefaultTraits for the documentation of
alpar@100
  1215
  /// a Bfs visit traits class.
alpar@100
  1216
  ///
alpar@100
  1217
  /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
alpar@100
  1218
#ifdef DOXYGEN
alpar@100
  1219
  template <typename _Digraph, typename _Visitor, typename _Traits>
alpar@100
  1220
#else
alpar@100
  1221
  template <typename _Digraph = ListDigraph,
alpar@100
  1222
	    typename _Visitor = BfsVisitor<_Digraph>,
alpar@100
  1223
	    typename _Traits = BfsDefaultTraits<_Digraph> >
alpar@100
  1224
#endif
alpar@100
  1225
  class BfsVisit {
alpar@100
  1226
  public:
alpar@100
  1227
    
alpar@100
  1228
    /// \brief \ref Exception for uninitialized parameters.
alpar@100
  1229
    ///
alpar@100
  1230
    /// This error represents problems in the initialization
alpar@100
  1231
    /// of the parameters of the algorithms.
alpar@100
  1232
    class UninitializedParameter : public lemon::UninitializedParameter {
alpar@100
  1233
    public:
alpar@100
  1234
      virtual const char* what() const throw() 
alpar@100
  1235
      {
alpar@100
  1236
	return "lemon::BfsVisit::UninitializedParameter";
alpar@100
  1237
      }
alpar@100
  1238
    };
alpar@100
  1239
alpar@100
  1240
    typedef _Traits Traits;
alpar@100
  1241
alpar@100
  1242
    typedef typename Traits::Digraph Digraph;
alpar@100
  1243
alpar@100
  1244
    typedef _Visitor Visitor;
alpar@100
  1245
alpar@100
  1246
    ///The type of the map indicating which nodes are reached.
alpar@100
  1247
    typedef typename Traits::ReachedMap ReachedMap;
alpar@100
  1248
alpar@100
  1249
  private:
alpar@100
  1250
alpar@100
  1251
    typedef typename Digraph::Node Node;
alpar@100
  1252
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
  1253
    typedef typename Digraph::Arc Arc;
alpar@100
  1254
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@100
  1255
alpar@100
  1256
    /// Pointer to the underlying digraph.
alpar@100
  1257
    const Digraph *_digraph;
alpar@100
  1258
    /// Pointer to the visitor object.
alpar@100
  1259
    Visitor *_visitor;
alpar@100
  1260
    ///Pointer to the map of reached status of the nodes.
alpar@100
  1261
    ReachedMap *_reached;
alpar@100
  1262
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
alpar@100
  1263
    bool local_reached;
alpar@100
  1264
alpar@100
  1265
    std::vector<typename Digraph::Node> _list;
alpar@100
  1266
    int _list_front, _list_back;
alpar@100
  1267
alpar@100
  1268
    /// \brief Creates the maps if necessary.
alpar@100
  1269
    ///
alpar@100
  1270
    /// Creates the maps if necessary.
alpar@100
  1271
    void create_maps() {
alpar@100
  1272
      if(!_reached) {
alpar@100
  1273
	local_reached = true;
alpar@100
  1274
	_reached = Traits::createReachedMap(*_digraph);
alpar@100
  1275
      }
alpar@100
  1276
    }
alpar@100
  1277
alpar@100
  1278
  protected:
alpar@100
  1279
alpar@100
  1280
    BfsVisit() {}
alpar@100
  1281
    
alpar@100
  1282
  public:
alpar@100
  1283
alpar@100
  1284
    typedef BfsVisit Create;
alpar@100
  1285
alpar@100
  1286
    /// \name Named template parameters
alpar@100
  1287
alpar@100
  1288
    ///@{
alpar@100
  1289
    template <class T>
alpar@100
  1290
    struct DefReachedMapTraits : public Traits {
alpar@100
  1291
      typedef T ReachedMap;
alpar@100
  1292
      static ReachedMap *createReachedMap(const Digraph &digraph) {
alpar@100
  1293
	throw UninitializedParameter();
alpar@100
  1294
      }
alpar@100
  1295
    };
alpar@100
  1296
    /// \brief \ref named-templ-param "Named parameter" for setting 
alpar@100
  1297
    /// ReachedMap type
alpar@100
  1298
    ///
alpar@100
  1299
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type
alpar@100
  1300
    template <class T>
alpar@100
  1301
    struct DefReachedMap : public BfsVisit< Digraph, Visitor,
alpar@100
  1302
					    DefReachedMapTraits<T> > {
alpar@100
  1303
      typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;
alpar@100
  1304
    };
alpar@100
  1305
    ///@}
alpar@100
  1306
alpar@100
  1307
  public:      
alpar@100
  1308
    
alpar@100
  1309
    /// \brief Constructor.
alpar@100
  1310
    ///
alpar@100
  1311
    /// Constructor.
alpar@100
  1312
    ///
alpar@100
  1313
    /// \param digraph the digraph the algorithm will run on.
alpar@100
  1314
    /// \param visitor The visitor of the algorithm.
alpar@100
  1315
    ///
alpar@100
  1316
    BfsVisit(const Digraph& digraph, Visitor& visitor) 
alpar@100
  1317
      : _digraph(&digraph), _visitor(&visitor),
alpar@100
  1318
	_reached(0), local_reached(false) {}
alpar@100
  1319
    
alpar@100
  1320
    /// \brief Destructor.
alpar@100
  1321
    ///
alpar@100
  1322
    /// Destructor.
alpar@100
  1323
    ~BfsVisit() {
alpar@100
  1324
      if(local_reached) delete _reached;
alpar@100
  1325
    }
alpar@100
  1326
alpar@100
  1327
    /// \brief Sets the map indicating if a node is reached.
alpar@100
  1328
    ///
alpar@100
  1329
    /// Sets the map indicating if a node is reached.
alpar@100
  1330
    /// If you don't use this function before calling \ref run(),
alpar@100
  1331
    /// it will allocate one. The destuctor deallocates this
alpar@100
  1332
    /// automatically allocated map, of course.
alpar@100
  1333
    /// \return <tt> (*this) </tt>
alpar@100
  1334
    BfsVisit &reachedMap(ReachedMap &m) {
alpar@100
  1335
      if(local_reached) {
alpar@100
  1336
	delete _reached;
alpar@100
  1337
	local_reached = false;
alpar@100
  1338
      }
alpar@100
  1339
      _reached = &m;
alpar@100
  1340
      return *this;
alpar@100
  1341
    }
alpar@100
  1342
alpar@100
  1343
  public:
alpar@100
  1344
    /// \name Execution control
alpar@100
  1345
    /// The simplest way to execute the algorithm is to use
alpar@100
  1346
    /// one of the member functions called \c run(...).
alpar@100
  1347
    /// \n
alpar@100
  1348
    /// If you need more control on the execution,
alpar@100
  1349
    /// first you must call \ref init(), then you can adda source node
alpar@100
  1350
    /// with \ref addSource().
alpar@100
  1351
    /// Finally \ref start() will perform the actual path
alpar@100
  1352
    /// computation.
alpar@100
  1353
alpar@100
  1354
    /// @{
alpar@100
  1355
    /// \brief Initializes the internal data structures.
alpar@100
  1356
    ///
alpar@100
  1357
    /// Initializes the internal data structures.
alpar@100
  1358
    ///
alpar@100
  1359
    void init() {
alpar@100
  1360
      create_maps();
alpar@100
  1361
      _list.resize(countNodes(*_digraph));
alpar@100
  1362
      _list_front = _list_back = -1;
alpar@100
  1363
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
alpar@100
  1364
	_reached->set(u, false);
alpar@100
  1365
      }
alpar@100
  1366
    }
alpar@100
  1367
    
alpar@100
  1368
    /// \brief Adds a new source node.
alpar@100
  1369
    ///
alpar@100
  1370
    /// Adds a new source node to the set of nodes to be processed.
alpar@100
  1371
    void addSource(Node s) {
alpar@100
  1372
      if(!(*_reached)[s]) {
alpar@100
  1373
	  _reached->set(s,true);
alpar@100
  1374
	  _visitor->start(s);
alpar@100
  1375
	  _visitor->reach(s);
alpar@100
  1376
          _list[++_list_back] = s;
alpar@100
  1377
	}
alpar@100
  1378
    }
alpar@100
  1379
    
alpar@100
  1380
    /// \brief Processes the next node.
alpar@100
  1381
    ///
alpar@100
  1382
    /// Processes the next node.
alpar@100
  1383
    ///
alpar@100
  1384
    /// \return The processed node.
alpar@100
  1385
    ///
alpar@100
  1386
    /// \pre The queue must not be empty!
alpar@100
  1387
    Node processNextNode() { 
alpar@100
  1388
      Node n = _list[++_list_front];
alpar@100
  1389
      _visitor->process(n);
alpar@100
  1390
      Arc e;
alpar@100
  1391
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
alpar@100
  1392
        Node m = _digraph->target(e);
alpar@100
  1393
        if (!(*_reached)[m]) {
alpar@100
  1394
          _visitor->discover(e);
alpar@100
  1395
          _visitor->reach(m);
alpar@100
  1396
          _reached->set(m, true);
alpar@100
  1397
          _list[++_list_back] = m;
alpar@100
  1398
        } else {
alpar@100
  1399
          _visitor->examine(e);
alpar@100
  1400
        }
alpar@100
  1401
      }
alpar@100
  1402
      return n;
alpar@100
  1403
    }
alpar@100
  1404
alpar@100
  1405
    /// \brief Processes the next node.
alpar@100
  1406
    ///
alpar@100
  1407
    /// Processes the next node. And checks that the given target node
alpar@100
  1408
    /// is reached. If the target node is reachable from the processed
alpar@100
  1409
    /// node then the reached parameter will be set true. The reached
alpar@100
  1410
    /// parameter should be initially false.
alpar@100
  1411
    ///
alpar@100
  1412
    /// \param target The target node.
alpar@100
  1413
    /// \retval reach Indicates that the target node is reached.
alpar@100
  1414
    /// \return The processed node.
alpar@100
  1415
    ///
alpar@100
  1416
    /// \warning The queue must not be empty!
alpar@100
  1417
    Node processNextNode(Node target, bool& reach) {
alpar@100
  1418
      Node n = _list[++_list_front];
alpar@100
  1419
      _visitor->process(n);
alpar@100
  1420
      Arc e;
alpar@100
  1421
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
alpar@100
  1422
        Node m = _digraph->target(e);
alpar@100
  1423
        if (!(*_reached)[m]) {
alpar@100
  1424
          _visitor->discover(e);
alpar@100
  1425
          _visitor->reach(m);
alpar@100
  1426
          _reached->set(m, true);
alpar@100
  1427
          _list[++_list_back] = m;
alpar@100
  1428
          reach = reach || (target == m);
alpar@100
  1429
        } else {
alpar@100
  1430
          _visitor->examine(e);
alpar@100
  1431
        }
alpar@100
  1432
      }
alpar@100
  1433
      return n;
alpar@100
  1434
    }
alpar@100
  1435
alpar@100
  1436
    /// \brief Processes the next node.
alpar@100
  1437
    ///
alpar@100
  1438
    /// Processes the next node. And checks that at least one of
alpar@100
  1439
    /// reached node has true value in the \c nm node map. If one node
alpar@100
  1440
    /// with true value is reachable from the processed node then the
alpar@100
  1441
    /// rnode parameter will be set to the first of such nodes.
alpar@100
  1442
    ///
alpar@100
  1443
    /// \param nm The node map of possible targets.
alpar@100
  1444
    /// \retval rnode The reached target node.
alpar@100
  1445
    /// \return The processed node.
alpar@100
  1446
    ///
alpar@100
  1447
    /// \warning The queue must not be empty!
alpar@100
  1448
    template <typename NM>
alpar@100
  1449
    Node processNextNode(const NM& nm, Node& rnode) {
alpar@100
  1450
      Node n = _list[++_list_front];
alpar@100
  1451
      _visitor->process(n);
alpar@100
  1452
      Arc e;
alpar@100
  1453
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
alpar@100
  1454
        Node m = _digraph->target(e);
alpar@100
  1455
        if (!(*_reached)[m]) {
alpar@100
  1456
          _visitor->discover(e);
alpar@100
  1457
          _visitor->reach(m);
alpar@100
  1458
          _reached->set(m, true);
alpar@100
  1459
          _list[++_list_back] = m;
alpar@100
  1460
          if (nm[m] && rnode == INVALID) rnode = m;
alpar@100
  1461
        } else {
alpar@100
  1462
          _visitor->examine(e);
alpar@100
  1463
        }
alpar@100
  1464
      }
alpar@100
  1465
      return n;
alpar@100
  1466
    }
alpar@100
  1467
alpar@100
  1468
    /// \brief Next node to be processed.
alpar@100
  1469
    ///
alpar@100
  1470
    /// Next node to be processed.
alpar@100
  1471
    ///
alpar@100
  1472
    /// \return The next node to be processed or INVALID if the stack is
alpar@100
  1473
    /// empty.
alpar@100
  1474
    Node nextNode() { 
alpar@100
  1475
      return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
alpar@100
  1476
    }
alpar@100
  1477
alpar@100
  1478
    /// \brief Returns \c false if there are nodes
alpar@100
  1479
    /// to be processed in the queue
alpar@100
  1480
    ///
alpar@100
  1481
    /// Returns \c false if there are nodes
alpar@100
  1482
    /// to be processed in the queue
alpar@100
  1483
    bool emptyQueue() { return _list_front == _list_back; }
alpar@100
  1484
alpar@100
  1485
    /// \brief Returns the number of the nodes to be processed.
alpar@100
  1486
    ///
alpar@100
  1487
    /// Returns the number of the nodes to be processed in the queue.
alpar@100
  1488
    int queueSize() { return _list_back - _list_front; }
alpar@100
  1489
    
alpar@100
  1490
    /// \brief Executes the algorithm.
alpar@100
  1491
    ///
alpar@100
  1492
    /// Executes the algorithm.
alpar@100
  1493
    ///
alpar@100
  1494
    /// \pre init() must be called and at least one node should be added
alpar@100
  1495
    /// with addSource() before using this function.
alpar@100
  1496
    void start() {
alpar@100
  1497
      while ( !emptyQueue() ) processNextNode();
alpar@100
  1498
    }
alpar@100
  1499
    
alpar@100
  1500
    /// \brief Executes the algorithm until \c dest is reached.
alpar@100
  1501
    ///
alpar@100
  1502
    /// Executes the algorithm until \c dest is reached.
alpar@100
  1503
    ///
alpar@100
  1504
    /// \pre init() must be called and at least one node should be added
alpar@100
  1505
    /// with addSource() before using this function.
alpar@100
  1506
    void start(Node dest) {
alpar@100
  1507
      bool reach = false;
alpar@100
  1508
      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
alpar@100
  1509
    }
alpar@100
  1510
    
alpar@100
  1511
    /// \brief Executes the algorithm until a condition is met.
alpar@100
  1512
    ///
alpar@100
  1513
    /// Executes the algorithm until a condition is met.
alpar@100
  1514
    ///
alpar@100
  1515
    /// \pre init() must be called and at least one node should be added
alpar@100
  1516
    /// with addSource() before using this function.
alpar@100
  1517
    ///
alpar@100
  1518
    ///\param nm must be a bool (or convertible) node map. The
alpar@100
  1519
    ///algorithm will stop when it reaches a node \c v with
alpar@100
  1520
    /// <tt>nm[v]</tt> true.
alpar@100
  1521
    ///
alpar@100
  1522
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
alpar@100
  1523
    ///\c INVALID if no such node was found.
alpar@100
  1524
    template <typename NM>
alpar@100
  1525
    Node start(const NM &nm) {
alpar@100
  1526
      Node rnode = INVALID;
alpar@100
  1527
      while ( !emptyQueue() && rnode == INVALID ) {
alpar@100
  1528
	processNextNode(nm, rnode);
alpar@100
  1529
      }
alpar@100
  1530
      return rnode;
alpar@100
  1531
    }
alpar@100
  1532
alpar@100
  1533
    /// \brief Runs %BFSVisit algorithm from node \c s.
alpar@100
  1534
    ///
alpar@100
  1535
    /// This method runs the %BFS algorithm from a root node \c s.
alpar@100
  1536
    /// \note b.run(s) is just a shortcut of the following code.
alpar@100
  1537
    ///\code
alpar@100
  1538
    ///   b.init();
alpar@100
  1539
    ///   b.addSource(s);
alpar@100
  1540
    ///   b.start();
alpar@100
  1541
    ///\endcode
alpar@100
  1542
    void run(Node s) {
alpar@100
  1543
      init();
alpar@100
  1544
      addSource(s);
alpar@100
  1545
      start();
alpar@100
  1546
    }
alpar@100
  1547
alpar@100
  1548
    /// \brief Runs %BFSVisit algorithm to visit all nodes in the digraph.
alpar@100
  1549
    ///    
alpar@100
  1550
    /// This method runs the %BFS algorithm in order to
alpar@100
  1551
    /// compute the %BFS path to each node. The algorithm computes
alpar@100
  1552
    /// - The %BFS tree.
alpar@100
  1553
    /// - The distance of each node from the root in the %BFS tree.
alpar@100
  1554
    ///
alpar@100
  1555
    ///\note b.run() is just a shortcut of the following code.
alpar@100
  1556
    ///\code
alpar@100
  1557
    ///  b.init();
alpar@100
  1558
    ///  for (NodeIt it(digraph); it != INVALID; ++it) {
alpar@100
  1559
    ///    if (!b.reached(it)) {
alpar@100
  1560
    ///      b.addSource(it);
alpar@100
  1561
    ///      b.start();
alpar@100
  1562
    ///    }
alpar@100
  1563
    ///  }
alpar@100
  1564
    ///\endcode
alpar@100
  1565
    void run() {
alpar@100
  1566
      init();
alpar@100
  1567
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
alpar@100
  1568
        if (!reached(it)) {
alpar@100
  1569
          addSource(it);
alpar@100
  1570
          start();
alpar@100
  1571
        }
alpar@100
  1572
      }
alpar@100
  1573
    }
alpar@100
  1574
    ///@}
alpar@100
  1575
alpar@100
  1576
    /// \name Query Functions
alpar@100
  1577
    /// The result of the %BFS algorithm can be obtained using these
alpar@100
  1578
    /// functions.\n
alpar@100
  1579
    /// Before the use of these functions,
alpar@100
  1580
    /// either run() or start() must be called.
alpar@100
  1581
    ///@{
alpar@100
  1582
alpar@100
  1583
    /// \brief Checks if a node is reachable from the root.
alpar@100
  1584
    ///
alpar@100
  1585
    /// Returns \c true if \c v is reachable from the root(s).
alpar@100
  1586
    /// \warning The source nodes are inditated as unreachable.
alpar@100
  1587
    /// \pre Either \ref run() or \ref start()
alpar@100
  1588
    /// must be called before using this function.
alpar@100
  1589
    ///
alpar@100
  1590
    bool reached(Node v) { return (*_reached)[v]; }
alpar@100
  1591
    ///@}
alpar@100
  1592
  };
alpar@100
  1593
alpar@100
  1594
} //END OF NAMESPACE LEMON
alpar@100
  1595
alpar@100
  1596
#endif
alpar@100
  1597