lemon/bfs.h
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 14 Jul 2008 16:20:12 +0200
changeset 216 6d7bfcf5b48e
parent 209 765619b7cbb2
child 220 a5d8c039f218
child 244 c30731a37f91
permissions -rw-r--r--
Doc improvement for kruskal() (ticket #114)
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@100
     2
 *
alpar@209
     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@209
    36
alpar@100
    37
  ///Default traits class of Bfs class.
alpar@100
    38
alpar@100
    39
  ///Default traits class of Bfs class.
kpeter@157
    40
  ///\tparam GR Digraph type.
alpar@100
    41
  template<class GR>
alpar@100
    42
  struct BfsDefaultTraits
alpar@100
    43
  {
alpar@209
    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@209
    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@209
    55
alpar@209
    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@209
    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@209
    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@209
    70
alpar@209
    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@209
    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@209
    89
alpar@209
    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@209
    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@209
   104
alpar@209
   105
    ///This function instantiates a \ref DistMap.
alpar@210
   106
    ///\param G is the digraph, to which we would like to define
alpar@210
   107
    ///the \ref DistMap
alpar@100
   108
    static DistMap *createDistMap(const GR &G)
alpar@100
   109
    {
alpar@100
   110
      return new DistMap(G);
alpar@100
   111
    }
alpar@100
   112
  };
alpar@209
   113
alpar@100
   114
  ///%BFS algorithm class.
alpar@209
   115
alpar@100
   116
  ///\ingroup search
alpar@100
   117
  ///This class provides an efficient implementation of the %BFS algorithm.
alpar@100
   118
  ///
kpeter@157
   119
  ///\tparam GR The digraph type the algorithm runs on. The default value is
alpar@100
   120
  ///\ref ListDigraph. The value of GR is not used directly by Bfs, it
alpar@100
   121
  ///is only passed to \ref BfsDefaultTraits.
kpeter@157
   122
  ///\tparam TR Traits class to set various data types used by the algorithm.
alpar@100
   123
  ///The default traits class is
alpar@100
   124
  ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
alpar@100
   125
  ///See \ref BfsDefaultTraits for the documentation of
alpar@100
   126
  ///a Bfs traits class.
alpar@100
   127
alpar@100
   128
#ifdef DOXYGEN
alpar@100
   129
  template <typename GR,
alpar@209
   130
            typename TR>
alpar@100
   131
#else
alpar@100
   132
  template <typename GR=ListDigraph,
alpar@209
   133
            typename TR=BfsDefaultTraits<GR> >
alpar@100
   134
#endif
alpar@100
   135
  class Bfs {
alpar@100
   136
  public:
alpar@100
   137
    /**
alpar@100
   138
     * \brief \ref Exception for uninitialized parameters.
alpar@100
   139
     *
alpar@100
   140
     * This error represents problems in the initialization
alpar@100
   141
     * of the parameters of the algorithms.
alpar@100
   142
     */
alpar@100
   143
    class UninitializedParameter : public lemon::UninitializedParameter {
alpar@100
   144
    public:
alpar@100
   145
      virtual const char* what() const throw() {
alpar@209
   146
        return "lemon::Bfs::UninitializedParameter";
alpar@100
   147
      }
alpar@100
   148
    };
alpar@100
   149
alpar@100
   150
    typedef TR Traits;
alpar@100
   151
    ///The type of the underlying digraph.
alpar@100
   152
    typedef typename TR::Digraph Digraph;
alpar@209
   153
alpar@100
   154
    ///\brief The type of the map that stores the last
alpar@100
   155
    ///arcs of the shortest paths.
alpar@100
   156
    typedef typename TR::PredMap PredMap;
alpar@100
   157
    ///The type of the map indicating which nodes are reached.
alpar@100
   158
    typedef typename TR::ReachedMap ReachedMap;
alpar@100
   159
    ///The type of the map indicating which nodes are processed.
alpar@100
   160
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@100
   161
    ///The type of the map that stores the dists of the nodes.
alpar@100
   162
    typedef typename TR::DistMap DistMap;
alpar@100
   163
  private:
alpar@100
   164
alpar@100
   165
    typedef typename Digraph::Node Node;
alpar@100
   166
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
   167
    typedef typename Digraph::Arc Arc;
alpar@100
   168
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@100
   169
alpar@100
   170
    /// Pointer to the underlying digraph.
alpar@100
   171
    const Digraph *G;
alpar@100
   172
    ///Pointer to the map of predecessors arcs.
alpar@100
   173
    PredMap *_pred;
alpar@100
   174
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
alpar@100
   175
    bool local_pred;
alpar@100
   176
    ///Pointer to the map of distances.
alpar@100
   177
    DistMap *_dist;
alpar@100
   178
    ///Indicates if \ref _dist is locally allocated (\c true) or not.
alpar@100
   179
    bool local_dist;
alpar@100
   180
    ///Pointer to the map of reached status of the nodes.
alpar@100
   181
    ReachedMap *_reached;
alpar@100
   182
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
alpar@100
   183
    bool local_reached;
alpar@100
   184
    ///Pointer to the map of processed status of the nodes.
alpar@100
   185
    ProcessedMap *_processed;
alpar@100
   186
    ///Indicates if \ref _processed is locally allocated (\c true) or not.
alpar@100
   187
    bool local_processed;
alpar@100
   188
alpar@100
   189
    std::vector<typename Digraph::Node> _queue;
alpar@100
   190
    int _queue_head,_queue_tail,_queue_next_dist;
alpar@100
   191
    int _curr_dist;
alpar@100
   192
alpar@100
   193
    ///Creates the maps if necessary.
alpar@209
   194
alpar@100
   195
    ///\todo Better memory allocation (instead of new).
alpar@209
   196
    void create_maps()
alpar@100
   197
    {
alpar@100
   198
      if(!_pred) {
alpar@209
   199
        local_pred = true;
alpar@209
   200
        _pred = Traits::createPredMap(*G);
alpar@100
   201
      }
alpar@100
   202
      if(!_dist) {
alpar@209
   203
        local_dist = true;
alpar@209
   204
        _dist = Traits::createDistMap(*G);
alpar@100
   205
      }
alpar@100
   206
      if(!_reached) {
alpar@209
   207
        local_reached = true;
alpar@209
   208
        _reached = Traits::createReachedMap(*G);
alpar@100
   209
      }
alpar@100
   210
      if(!_processed) {
alpar@209
   211
        local_processed = true;
alpar@209
   212
        _processed = Traits::createProcessedMap(*G);
alpar@100
   213
      }
alpar@100
   214
    }
alpar@100
   215
alpar@100
   216
  protected:
alpar@209
   217
alpar@100
   218
    Bfs() {}
alpar@209
   219
alpar@100
   220
  public:
alpar@209
   221
alpar@100
   222
    typedef Bfs Create;
alpar@100
   223
alpar@100
   224
    ///\name Named template parameters
alpar@100
   225
alpar@100
   226
    ///@{
alpar@100
   227
alpar@100
   228
    template <class T>
alpar@100
   229
    struct DefPredMapTraits : public Traits {
alpar@100
   230
      typedef T PredMap;
alpar@209
   231
      static PredMap *createPredMap(const Digraph &)
alpar@100
   232
      {
alpar@209
   233
        throw UninitializedParameter();
alpar@100
   234
      }
alpar@100
   235
    };
alpar@100
   236
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@100
   237
    ///PredMap type
alpar@100
   238
    ///
alpar@100
   239
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@100
   240
    ///
alpar@100
   241
    template <class T>
alpar@209
   242
    struct DefPredMap : public Bfs< Digraph, DefPredMapTraits<T> > {
alpar@100
   243
      typedef Bfs< Digraph, DefPredMapTraits<T> > Create;
alpar@100
   244
    };
alpar@209
   245
alpar@100
   246
    template <class T>
alpar@100
   247
    struct DefDistMapTraits : public Traits {
alpar@100
   248
      typedef T DistMap;
alpar@209
   249
      static DistMap *createDistMap(const Digraph &)
alpar@100
   250
      {
alpar@209
   251
        throw UninitializedParameter();
alpar@100
   252
      }
alpar@100
   253
    };
alpar@100
   254
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@100
   255
    ///DistMap type
alpar@100
   256
    ///
alpar@100
   257
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@100
   258
    ///
alpar@100
   259
    template <class T>
alpar@209
   260
    struct DefDistMap : public Bfs< Digraph, DefDistMapTraits<T> > {
alpar@100
   261
      typedef Bfs< Digraph, DefDistMapTraits<T> > Create;
alpar@100
   262
    };
alpar@209
   263
alpar@100
   264
    template <class T>
alpar@100
   265
    struct DefReachedMapTraits : public Traits {
alpar@100
   266
      typedef T ReachedMap;
alpar@209
   267
      static ReachedMap *createReachedMap(const Digraph &)
alpar@100
   268
      {
alpar@209
   269
        throw UninitializedParameter();
alpar@100
   270
      }
alpar@100
   271
    };
alpar@100
   272
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@100
   273
    ///ReachedMap type
alpar@100
   274
    ///
alpar@100
   275
    ///\ref named-templ-param "Named parameter" for setting ReachedMap type
alpar@100
   276
    ///
alpar@100
   277
    template <class T>
alpar@209
   278
    struct DefReachedMap : public Bfs< Digraph, DefReachedMapTraits<T> > {
alpar@100
   279
      typedef Bfs< Digraph, DefReachedMapTraits<T> > Create;
alpar@100
   280
    };
alpar@209
   281
alpar@100
   282
    template <class T>
alpar@100
   283
    struct DefProcessedMapTraits : public Traits {
alpar@100
   284
      typedef T ProcessedMap;
alpar@209
   285
      static ProcessedMap *createProcessedMap(const Digraph &)
alpar@100
   286
      {
alpar@209
   287
        throw UninitializedParameter();
alpar@100
   288
      }
alpar@100
   289
    };
alpar@100
   290
    ///\brief \ref named-templ-param "Named parameter" for setting
alpar@100
   291
    ///ProcessedMap type
alpar@100
   292
    ///
alpar@100
   293
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
alpar@100
   294
    ///
alpar@100
   295
    template <class T>
alpar@100
   296
    struct DefProcessedMap : public Bfs< Digraph, DefProcessedMapTraits<T> > {
alpar@100
   297
      typedef Bfs< Digraph, DefProcessedMapTraits<T> > Create;
alpar@100
   298
    };
alpar@209
   299
alpar@100
   300
    struct DefDigraphProcessedMapTraits : public Traits {
alpar@100
   301
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
alpar@209
   302
      static ProcessedMap *createProcessedMap(const Digraph &G)
alpar@100
   303
      {
alpar@209
   304
        return new ProcessedMap(G);
alpar@100
   305
      }
alpar@100
   306
    };
alpar@100
   307
    ///\brief \ref named-templ-param "Named parameter"
alpar@100
   308
    ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
alpar@100
   309
    ///
alpar@100
   310
    ///\ref named-templ-param "Named parameter"
alpar@100
   311
    ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
alpar@100
   312
    ///If you don't set it explicitly, it will be automatically allocated.
alpar@100
   313
    template <class T>
alpar@100
   314
    struct DefProcessedMapToBeDefaultMap :
alpar@209
   315
      public Bfs< Digraph, DefDigraphProcessedMapTraits> {
alpar@100
   316
      typedef Bfs< Digraph, DefDigraphProcessedMapTraits> Create;
alpar@100
   317
    };
alpar@209
   318
alpar@100
   319
    ///@}
alpar@100
   320
alpar@209
   321
  public:
alpar@209
   322
alpar@100
   323
    ///Constructor.
alpar@209
   324
alpar@100
   325
    ///\param _G the digraph the algorithm will run on.
alpar@100
   326
    ///
alpar@100
   327
    Bfs(const Digraph& _G) :
alpar@100
   328
      G(&_G),
alpar@100
   329
      _pred(NULL), local_pred(false),
alpar@100
   330
      _dist(NULL), local_dist(false),
alpar@100
   331
      _reached(NULL), local_reached(false),
alpar@100
   332
      _processed(NULL), local_processed(false)
alpar@100
   333
    { }
alpar@209
   334
alpar@100
   335
    ///Destructor.
alpar@209
   336
    ~Bfs()
alpar@100
   337
    {
alpar@100
   338
      if(local_pred) delete _pred;
alpar@100
   339
      if(local_dist) delete _dist;
alpar@100
   340
      if(local_reached) delete _reached;
alpar@100
   341
      if(local_processed) delete _processed;
alpar@100
   342
    }
alpar@100
   343
alpar@100
   344
    ///Sets the map storing the predecessor arcs.
alpar@100
   345
alpar@100
   346
    ///Sets the map storing the predecessor arcs.
alpar@100
   347
    ///If you don't use this function before calling \ref run(),
alpar@100
   348
    ///it will allocate one. The destructor deallocates this
alpar@100
   349
    ///automatically allocated map, of course.
alpar@100
   350
    ///\return <tt> (*this) </tt>
alpar@209
   351
    Bfs &predMap(PredMap &m)
alpar@100
   352
    {
alpar@100
   353
      if(local_pred) {
alpar@209
   354
        delete _pred;
alpar@209
   355
        local_pred=false;
alpar@100
   356
      }
alpar@100
   357
      _pred = &m;
alpar@100
   358
      return *this;
alpar@100
   359
    }
alpar@100
   360
alpar@100
   361
    ///Sets the map indicating the reached nodes.
alpar@100
   362
alpar@100
   363
    ///Sets the map indicating the reached nodes.
alpar@100
   364
    ///If you don't use this function before calling \ref run(),
alpar@100
   365
    ///it will allocate one. The destructor deallocates this
alpar@100
   366
    ///automatically allocated map, of course.
alpar@100
   367
    ///\return <tt> (*this) </tt>
alpar@209
   368
    Bfs &reachedMap(ReachedMap &m)
alpar@100
   369
    {
alpar@100
   370
      if(local_reached) {
alpar@209
   371
        delete _reached;
alpar@209
   372
        local_reached=false;
alpar@100
   373
      }
alpar@100
   374
      _reached = &m;
alpar@100
   375
      return *this;
alpar@100
   376
    }
alpar@100
   377
alpar@100
   378
    ///Sets the map indicating the processed nodes.
alpar@100
   379
alpar@100
   380
    ///Sets the map indicating the processed nodes.
alpar@100
   381
    ///If you don't use this function before calling \ref run(),
alpar@100
   382
    ///it will allocate one. The destructor deallocates this
alpar@100
   383
    ///automatically allocated map, of course.
alpar@100
   384
    ///\return <tt> (*this) </tt>
alpar@209
   385
    Bfs &processedMap(ProcessedMap &m)
alpar@100
   386
    {
alpar@100
   387
      if(local_processed) {
alpar@209
   388
        delete _processed;
alpar@209
   389
        local_processed=false;
alpar@100
   390
      }
alpar@100
   391
      _processed = &m;
alpar@100
   392
      return *this;
alpar@100
   393
    }
alpar@100
   394
alpar@100
   395
    ///Sets the map storing the distances calculated by the algorithm.
alpar@100
   396
alpar@100
   397
    ///Sets the map storing the distances calculated by the algorithm.
alpar@100
   398
    ///If you don't use this function before calling \ref run(),
alpar@100
   399
    ///it will allocate one. The destructor deallocates this
alpar@100
   400
    ///automatically allocated map, of course.
alpar@100
   401
    ///\return <tt> (*this) </tt>
alpar@209
   402
    Bfs &distMap(DistMap &m)
alpar@100
   403
    {
alpar@100
   404
      if(local_dist) {
alpar@209
   405
        delete _dist;
alpar@209
   406
        local_dist=false;
alpar@100
   407
      }
alpar@100
   408
      _dist = &m;
alpar@100
   409
      return *this;
alpar@100
   410
    }
alpar@100
   411
alpar@100
   412
  public:
alpar@100
   413
    ///\name Execution control
alpar@100
   414
    ///The simplest way to execute the algorithm is to use
alpar@100
   415
    ///one of the member functions called \c run(...).
alpar@100
   416
    ///\n
alpar@100
   417
    ///If you need more control on the execution,
alpar@100
   418
    ///first you must call \ref init(), then you can add several source nodes
alpar@100
   419
    ///with \ref addSource().
alpar@100
   420
    ///Finally \ref start() will perform the actual path
alpar@100
   421
    ///computation.
alpar@100
   422
alpar@100
   423
    ///@{
alpar@100
   424
alpar@100
   425
    ///\brief Initializes the internal data structures.
alpar@100
   426
    ///
alpar@100
   427
    ///Initializes the internal data structures.
alpar@100
   428
    ///
alpar@100
   429
    void init()
alpar@100
   430
    {
alpar@100
   431
      create_maps();
alpar@100
   432
      _queue.resize(countNodes(*G));
alpar@100
   433
      _queue_head=_queue_tail=0;
alpar@100
   434
      _curr_dist=1;
alpar@100
   435
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@209
   436
        _pred->set(u,INVALID);
alpar@209
   437
        _reached->set(u,false);
alpar@209
   438
        _processed->set(u,false);
alpar@100
   439
      }
alpar@100
   440
    }
alpar@209
   441
alpar@100
   442
    ///Adds a new source node.
alpar@100
   443
alpar@100
   444
    ///Adds a new source node to the set of nodes to be processed.
alpar@100
   445
    ///
alpar@100
   446
    void addSource(Node s)
alpar@100
   447
    {
alpar@100
   448
      if(!(*_reached)[s])
alpar@209
   449
        {
alpar@209
   450
          _reached->set(s,true);
alpar@209
   451
          _pred->set(s,INVALID);
alpar@209
   452
          _dist->set(s,0);
alpar@209
   453
          _queue[_queue_head++]=s;
alpar@209
   454
          _queue_next_dist=_queue_head;
alpar@209
   455
        }
alpar@100
   456
    }
alpar@209
   457
alpar@100
   458
    ///Processes the next node.
alpar@100
   459
alpar@100
   460
    ///Processes the next node.
alpar@100
   461
    ///
alpar@100
   462
    ///\return The processed node.
alpar@100
   463
    ///
alpar@100
   464
    ///\warning The queue must not be empty!
alpar@100
   465
    Node processNextNode()
alpar@100
   466
    {
alpar@100
   467
      if(_queue_tail==_queue_next_dist) {
alpar@209
   468
        _curr_dist++;
alpar@209
   469
        _queue_next_dist=_queue_head;
alpar@100
   470
      }
alpar@100
   471
      Node n=_queue[_queue_tail++];
alpar@100
   472
      _processed->set(n,true);
alpar@100
   473
      Node m;
alpar@100
   474
      for(OutArcIt e(*G,n);e!=INVALID;++e)
alpar@209
   475
        if(!(*_reached)[m=G->target(e)]) {
alpar@209
   476
          _queue[_queue_head++]=m;
alpar@209
   477
          _reached->set(m,true);
alpar@209
   478
          _pred->set(m,e);
alpar@209
   479
          _dist->set(m,_curr_dist);
alpar@209
   480
        }
alpar@100
   481
      return n;
alpar@100
   482
    }
alpar@100
   483
alpar@100
   484
    ///Processes the next node.
alpar@100
   485
alpar@100
   486
    ///Processes the next node. And checks that the given target node
alpar@100
   487
    ///is reached. If the target node is reachable from the processed
alpar@100
   488
    ///node then the reached parameter will be set true. The reached
alpar@100
   489
    ///parameter should be initially false.
alpar@100
   490
    ///
alpar@100
   491
    ///\param target The target node.
alpar@100
   492
    ///\retval reach Indicates that the target node is reached.
alpar@100
   493
    ///\return The processed node.
alpar@100
   494
    ///
alpar@100
   495
    ///\warning The queue must not be empty!
alpar@100
   496
    Node processNextNode(Node target, bool& reach)
alpar@100
   497
    {
alpar@100
   498
      if(_queue_tail==_queue_next_dist) {
alpar@209
   499
        _curr_dist++;
alpar@209
   500
        _queue_next_dist=_queue_head;
alpar@100
   501
      }
alpar@100
   502
      Node n=_queue[_queue_tail++];
alpar@100
   503
      _processed->set(n,true);
alpar@100
   504
      Node m;
alpar@100
   505
      for(OutArcIt e(*G,n);e!=INVALID;++e)
alpar@209
   506
        if(!(*_reached)[m=G->target(e)]) {
alpar@209
   507
          _queue[_queue_head++]=m;
alpar@209
   508
          _reached->set(m,true);
alpar@209
   509
          _pred->set(m,e);
alpar@209
   510
          _dist->set(m,_curr_dist);
alpar@100
   511
          reach = reach || (target == m);
alpar@209
   512
        }
alpar@100
   513
      return n;
alpar@100
   514
    }
alpar@100
   515
alpar@100
   516
    ///Processes the next node.
alpar@100
   517
alpar@100
   518
    ///Processes the next node. And checks that at least one of
alpar@100
   519
    ///reached node has true value in the \c nm node map. If one node
alpar@100
   520
    ///with true value is reachable from the processed node then the
alpar@100
   521
    ///rnode parameter will be set to the first of such nodes.
alpar@100
   522
    ///
alpar@100
   523
    ///\param nm The node map of possible targets.
alpar@100
   524
    ///\retval rnode The reached target node.
alpar@100
   525
    ///\return The processed node.
alpar@100
   526
    ///
alpar@100
   527
    ///\warning The queue must not be empty!
alpar@100
   528
    template<class NM>
alpar@100
   529
    Node processNextNode(const NM& nm, Node& rnode)
alpar@100
   530
    {
alpar@100
   531
      if(_queue_tail==_queue_next_dist) {
alpar@209
   532
        _curr_dist++;
alpar@209
   533
        _queue_next_dist=_queue_head;
alpar@100
   534
      }
alpar@100
   535
      Node n=_queue[_queue_tail++];
alpar@100
   536
      _processed->set(n,true);
alpar@100
   537
      Node m;
alpar@100
   538
      for(OutArcIt e(*G,n);e!=INVALID;++e)
alpar@209
   539
        if(!(*_reached)[m=G->target(e)]) {
alpar@209
   540
          _queue[_queue_head++]=m;
alpar@209
   541
          _reached->set(m,true);
alpar@209
   542
          _pred->set(m,e);
alpar@209
   543
          _dist->set(m,_curr_dist);
alpar@209
   544
          if (nm[m] && rnode == INVALID) rnode = m;
alpar@209
   545
        }
alpar@100
   546
      return n;
alpar@100
   547
    }
alpar@209
   548
alpar@100
   549
    ///Next node to be processed.
alpar@100
   550
alpar@100
   551
    ///Next node to be processed.
alpar@100
   552
    ///
alpar@100
   553
    ///\return The next node to be processed or INVALID if the queue is
alpar@100
   554
    /// empty.
alpar@100
   555
    Node nextNode()
alpar@209
   556
    {
alpar@100
   557
      return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
alpar@100
   558
    }
alpar@209
   559
alpar@100
   560
    ///\brief Returns \c false if there are nodes
alpar@100
   561
    ///to be processed in the queue
alpar@100
   562
    ///
alpar@100
   563
    ///Returns \c false if there are nodes
alpar@100
   564
    ///to be processed in the queue
alpar@100
   565
    bool emptyQueue() { return _queue_tail==_queue_head; }
alpar@100
   566
    ///Returns the number of the nodes to be processed.
alpar@209
   567
alpar@100
   568
    ///Returns the number of the nodes to be processed in the queue.
alpar@100
   569
    int queueSize() { return _queue_head-_queue_tail; }
alpar@209
   570
alpar@100
   571
    ///Executes the algorithm.
alpar@100
   572
alpar@100
   573
    ///Executes the algorithm.
alpar@100
   574
    ///
alpar@100
   575
    ///\pre init() must be called and at least one node should be added
alpar@100
   576
    ///with addSource() before using this function.
alpar@100
   577
    ///
alpar@100
   578
    ///This method runs the %BFS algorithm from the root node(s)
alpar@100
   579
    ///in order to
alpar@100
   580
    ///compute the
alpar@100
   581
    ///shortest path to each node. The algorithm computes
alpar@100
   582
    ///- The shortest path tree.
alpar@100
   583
    ///- The distance of each node from the root(s).
alpar@100
   584
    void start()
alpar@100
   585
    {
alpar@100
   586
      while ( !emptyQueue() ) processNextNode();
alpar@100
   587
    }
alpar@209
   588
alpar@100
   589
    ///Executes the algorithm until \c dest is reached.
alpar@100
   590
alpar@100
   591
    ///Executes the algorithm until \c dest is reached.
alpar@100
   592
    ///
alpar@100
   593
    ///\pre init() must be called and at least one node should be added
alpar@100
   594
    ///with addSource() before using this function.
alpar@100
   595
    ///
alpar@100
   596
    ///This method runs the %BFS algorithm from the root node(s)
alpar@100
   597
    ///in order to compute the shortest path to \c dest.
alpar@100
   598
    ///The algorithm computes
alpar@100
   599
    ///- The shortest path to \c  dest.
alpar@100
   600
    ///- The distance of \c dest from the root(s).
alpar@100
   601
    void start(Node dest)
alpar@100
   602
    {
alpar@100
   603
      bool reach = false;
alpar@100
   604
      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
alpar@100
   605
    }
alpar@209
   606
alpar@100
   607
    ///Executes the algorithm until a condition is met.
alpar@100
   608
alpar@100
   609
    ///Executes the algorithm until a condition is met.
alpar@100
   610
    ///
alpar@100
   611
    ///\pre init() must be called and at least one node should be added
alpar@100
   612
    ///with addSource() before using this function.
alpar@100
   613
    ///
alpar@100
   614
    ///\param nm must be a bool (or convertible) node map. The
alpar@100
   615
    ///algorithm will stop when it reaches a node \c v with
alpar@100
   616
    /// <tt>nm[v]</tt> true.
alpar@100
   617
    ///
alpar@100
   618
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
alpar@100
   619
    ///\c INVALID if no such node was found.
alpar@100
   620
    template<class NM>
alpar@100
   621
    Node start(const NM &nm)
alpar@100
   622
    {
alpar@100
   623
      Node rnode = INVALID;
alpar@100
   624
      while ( !emptyQueue() && rnode == INVALID ) {
alpar@209
   625
        processNextNode(nm, rnode);
alpar@100
   626
      }
alpar@100
   627
      return rnode;
alpar@100
   628
    }
alpar@209
   629
alpar@100
   630
    ///Runs %BFS algorithm from node \c s.
alpar@209
   631
alpar@100
   632
    ///This method runs the %BFS algorithm from a root node \c s
alpar@100
   633
    ///in order to
alpar@100
   634
    ///compute the
alpar@100
   635
    ///shortest path to each node. The algorithm computes
alpar@100
   636
    ///- The shortest path tree.
alpar@100
   637
    ///- The distance of each node from the root.
alpar@100
   638
    ///
alpar@100
   639
    ///\note b.run(s) is just a shortcut of the following code.
alpar@100
   640
    ///\code
alpar@100
   641
    ///  b.init();
alpar@100
   642
    ///  b.addSource(s);
alpar@100
   643
    ///  b.start();
alpar@100
   644
    ///\endcode
alpar@100
   645
    void run(Node s) {
alpar@100
   646
      init();
alpar@100
   647
      addSource(s);
alpar@100
   648
      start();
alpar@100
   649
    }
alpar@209
   650
alpar@100
   651
    ///Finds the shortest path between \c s and \c t.
alpar@209
   652
alpar@100
   653
    ///Finds the shortest path between \c s and \c t.
alpar@100
   654
    ///
alpar@100
   655
    ///\return The length of the shortest s---t path if there exists one,
alpar@100
   656
    ///0 otherwise.
alpar@100
   657
    ///\note Apart from the return value, b.run(s) is
alpar@100
   658
    ///just a shortcut of the following code.
alpar@100
   659
    ///\code
alpar@100
   660
    ///  b.init();
alpar@100
   661
    ///  b.addSource(s);
alpar@100
   662
    ///  b.start(t);
alpar@100
   663
    ///\endcode
alpar@100
   664
    int run(Node s,Node t) {
alpar@100
   665
      init();
alpar@100
   666
      addSource(s);
alpar@100
   667
      start(t);
alpar@100
   668
      return reached(t) ? _curr_dist : 0;
alpar@100
   669
    }
alpar@209
   670
alpar@100
   671
    ///@}
alpar@100
   672
alpar@100
   673
    ///\name Query Functions
alpar@100
   674
    ///The result of the %BFS algorithm can be obtained using these
alpar@100
   675
    ///functions.\n
alpar@100
   676
    ///Before the use of these functions,
alpar@100
   677
    ///either run() or start() must be calleb.
alpar@209
   678
alpar@100
   679
    ///@{
alpar@100
   680
alpar@100
   681
    typedef PredMapPath<Digraph, PredMap> Path;
alpar@100
   682
alpar@100
   683
    ///Gives back the shortest path.
alpar@209
   684
alpar@100
   685
    ///Gives back the shortest path.
alpar@100
   686
    ///\pre The \c t should be reachable from the source.
alpar@209
   687
    Path path(Node t)
alpar@100
   688
    {
alpar@100
   689
      return Path(*G, *_pred, t);
alpar@100
   690
    }
alpar@100
   691
alpar@100
   692
    ///The distance of a node from the root(s).
alpar@100
   693
alpar@100
   694
    ///Returns the distance of a node from the root(s).
alpar@100
   695
    ///\pre \ref run() must be called before using this function.
alpar@100
   696
    ///\warning If node \c v in unreachable from the root(s) the return value
alpar@100
   697
    ///of this function is undefined.
alpar@100
   698
    int dist(Node v) const { return (*_dist)[v]; }
alpar@100
   699
alpar@100
   700
    ///Returns the 'previous arc' of the shortest path tree.
alpar@100
   701
alpar@100
   702
    ///For a node \c v it returns the 'previous arc'
alpar@100
   703
    ///of the shortest path tree,
alpar@100
   704
    ///i.e. it returns the last arc of a shortest path from the root(s) to \c
alpar@100
   705
    ///v. It is \ref INVALID
alpar@100
   706
    ///if \c v is unreachable from the root(s) or \c v is a root. The
alpar@100
   707
    ///shortest path tree used here is equal to the shortest path tree used in
alpar@100
   708
    ///\ref predNode().
alpar@100
   709
    ///\pre Either \ref run() or \ref start() must be called before using
alpar@100
   710
    ///this function.
alpar@100
   711
    Arc predArc(Node v) const { return (*_pred)[v];}
alpar@100
   712
alpar@100
   713
    ///Returns the 'previous node' of the shortest path tree.
alpar@100
   714
alpar@100
   715
    ///For a node \c v it returns the 'previous node'
alpar@100
   716
    ///of the shortest path tree,
alpar@100
   717
    ///i.e. it returns the last but one node from a shortest path from the
alpar@100
   718
    ///root(a) to \c /v.
alpar@100
   719
    ///It is INVALID if \c v is unreachable from the root(s) or
alpar@100
   720
    ///if \c v itself a root.
alpar@100
   721
    ///The shortest path tree used here is equal to the shortest path
alpar@100
   722
    ///tree used in \ref predArc().
alpar@100
   723
    ///\pre Either \ref run() or \ref start() must be called before
alpar@100
   724
    ///using this function.
alpar@100
   725
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@209
   726
                                  G->source((*_pred)[v]); }
alpar@209
   727
alpar@100
   728
    ///Returns a reference to the NodeMap of distances.
alpar@100
   729
alpar@100
   730
    ///Returns a reference to the NodeMap of distances.
alpar@100
   731
    ///\pre Either \ref run() or \ref init() must
alpar@100
   732
    ///be called before using this function.
alpar@100
   733
    const DistMap &distMap() const { return *_dist;}
alpar@209
   734
alpar@100
   735
    ///Returns a reference to the shortest path tree map.
alpar@100
   736
alpar@100
   737
    ///Returns a reference to the NodeMap of the arcs of the
alpar@100
   738
    ///shortest path tree.
alpar@100
   739
    ///\pre Either \ref run() or \ref init()
alpar@100
   740
    ///must be called before using this function.
alpar@100
   741
    const PredMap &predMap() const { return *_pred;}
alpar@209
   742
alpar@100
   743
    ///Checks if a node is reachable from the root.
alpar@100
   744
alpar@100
   745
    ///Returns \c true if \c v is reachable from the root.
alpar@100
   746
    ///\warning The source nodes are indicated as unreached.
alpar@100
   747
    ///\pre Either \ref run() or \ref start()
alpar@100
   748
    ///must be called before using this function.
alpar@100
   749
    ///
alpar@100
   750
    bool reached(Node v) { return (*_reached)[v]; }
alpar@209
   751
alpar@100
   752
    ///@}
alpar@100
   753
  };
alpar@100
   754
alpar@100
   755
  ///Default traits class of Bfs function.
alpar@100
   756
alpar@100
   757
  ///Default traits class of Bfs function.
kpeter@157
   758
  ///\tparam GR Digraph type.
alpar@100
   759
  template<class GR>
alpar@100
   760
  struct BfsWizardDefaultTraits
alpar@100
   761
  {
alpar@209
   762
    ///The digraph type the algorithm runs on.
alpar@100
   763
    typedef GR Digraph;
alpar@100
   764
    ///\brief The type of the map that stores the last
alpar@100
   765
    ///arcs of the shortest paths.
alpar@209
   766
    ///
alpar@100
   767
    ///The type of the map that stores the last
alpar@100
   768
    ///arcs of the shortest paths.
alpar@100
   769
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   770
    ///
alpar@100
   771
    typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap;
alpar@100
   772
    ///Instantiates a PredMap.
alpar@209
   773
alpar@209
   774
    ///This function instantiates a \ref PredMap.
alpar@100
   775
    ///\param g is the digraph, to which we would like to define the PredMap.
alpar@100
   776
    ///\todo The digraph alone may be insufficient to initialize
alpar@100
   777
#ifdef DOXYGEN
alpar@209
   778
    static PredMap *createPredMap(const GR &g)
alpar@100
   779
#else
alpar@209
   780
    static PredMap *createPredMap(const GR &)
alpar@100
   781
#endif
alpar@100
   782
    {
alpar@100
   783
      return new PredMap();
alpar@100
   784
    }
alpar@100
   785
alpar@100
   786
    ///The type of the map that indicates which nodes are processed.
alpar@209
   787
alpar@100
   788
    ///The type of the map that indicates which nodes are processed.
alpar@100
   789
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   790
    ///\todo named parameter to set this type, function to read and write.
alpar@100
   791
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
alpar@100
   792
    ///Instantiates a ProcessedMap.
alpar@209
   793
alpar@209
   794
    ///This function instantiates a \ref ProcessedMap.
alpar@100
   795
    ///\param g is the digraph, to which
alpar@100
   796
    ///we would like to define the \ref ProcessedMap
alpar@100
   797
#ifdef DOXYGEN
alpar@100
   798
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@100
   799
#else
alpar@100
   800
    static ProcessedMap *createProcessedMap(const GR &)
alpar@100
   801
#endif
alpar@100
   802
    {
alpar@100
   803
      return new ProcessedMap();
alpar@100
   804
    }
alpar@100
   805
    ///The type of the map that indicates which nodes are reached.
alpar@209
   806
alpar@100
   807
    ///The type of the map that indicates which nodes are reached.
alpar@100
   808
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   809
    ///\todo named parameter to set this type, function to read and write.
alpar@100
   810
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
alpar@100
   811
    ///Instantiates a ReachedMap.
alpar@209
   812
alpar@209
   813
    ///This function instantiates a \ref ReachedMap.
alpar@100
   814
    ///\param G is the digraph, to which
alpar@100
   815
    ///we would like to define the \ref ReachedMap.
alpar@100
   816
    static ReachedMap *createReachedMap(const GR &G)
alpar@100
   817
    {
alpar@100
   818
      return new ReachedMap(G);
alpar@100
   819
    }
alpar@100
   820
    ///The type of the map that stores the dists of the nodes.
alpar@209
   821
alpar@100
   822
    ///The type of the map that stores the dists of the nodes.
alpar@100
   823
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@100
   824
    ///
alpar@100
   825
    typedef NullMap<typename Digraph::Node,int> DistMap;
alpar@100
   826
    ///Instantiates a DistMap.
alpar@209
   827
alpar@209
   828
    ///This function instantiates a \ref DistMap.
alpar@210
   829
    ///\param g is the digraph, to which we would like to define
alpar@210
   830
    ///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@209
   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@209
   870
alpar@100
   871
    public:
alpar@100
   872
    /// Constructor.
alpar@209
   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@209
   877
                           _dist(0), _source(INVALID) {}
alpar@100
   878
alpar@100
   879
    /// Constructor.
alpar@209
   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@209
   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@209
   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@209
   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@209
   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@209
   964
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
alpar@209
   965
      if(Base::_processed)
alpar@100
   966
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
alpar@209
   967
      if(Base::_pred)
alpar@100
   968
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
alpar@209
   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@209
   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@209
   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@209
  1003
alpar@209
  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@209
  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@209
  1019
    BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
alpar@100
  1020
    {
deba@158
  1021
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@100
  1022
      return BfsWizard<DefReachedMapBase<T> >(*this);
alpar@100
  1023
    }
alpar@209
  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@209
  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@209
  1040
    BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
alpar@100
  1041
    {
deba@158
  1042
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@100
  1043
      return BfsWizard<DefProcessedMapBase<T> >(*this);
alpar@100
  1044
    }
alpar@209
  1045
alpar@209
  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@209
  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@209
  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@209
  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@209
  1071
    BfsWizard<TR> &source(Node s)
alpar@100
  1072
    {
alpar@100
  1073
      Base::_source=s;
alpar@100
  1074
      return *this;
alpar@100
  1075
    }
alpar@209
  1076
alpar@100
  1077
  };
alpar@209
  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@209
  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@209
  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@209
  1119
    ///
alpar@100
  1120
    /// It is Called when the node reached first time.
alpar@100
  1121
    void reach(const Node& node) {}
alpar@209
  1122
    /// \brief Called when the arc examined but target of the arc
alpar@100
  1123
    /// already discovered.
alpar@209
  1124
    ///
alpar@209
  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@209
  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@209
  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@209
  1152
        Arc arc;
alpar@209
  1153
        Node node;
alpar@209
  1154
        visitor.discover(arc);
alpar@209
  1155
        visitor.reach(node);
alpar@209
  1156
        visitor.examine(arc);
alpar@209
  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.
kpeter@157
  1168
  /// \tparam _Digraph Digraph type.
alpar@100
  1169
  template<class _Digraph>
alpar@100
  1170
  struct BfsVisitDefaultTraits {
alpar@100
  1171
alpar@209
  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@209
  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@209
  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@209
  1194
  ///
alpar@100
  1195
  /// \brief %BFS Visit algorithm class.
alpar@209
  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@209
  1202
  /// on every bfs event the \c Visitor class member functions.
alpar@100
  1203
  ///
alpar@210
  1204
  /// \tparam _Digraph The digraph type the algorithm runs on.
alpar@210
  1205
  /// The default value is
alpar@100
  1206
  /// \ref ListDigraph. The value of _Digraph is not used directly by Bfs, it
alpar@100
  1207
  /// is only passed to \ref BfsDefaultTraits.
alpar@209
  1208
  /// \tparam _Visitor The Visitor object for the algorithm. The
alpar@100
  1209
  /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty Visitor which
alpar@100
  1210
  /// does not observe the Bfs events. If you want to observe the bfs
alpar@100
  1211
  /// events you should implement your own Visitor class.
alpar@209
  1212
  /// \tparam _Traits Traits class to set various data types used by the
alpar@100
  1213
  /// algorithm. The default traits class is
alpar@100
  1214
  /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
alpar@100
  1215
  /// See \ref BfsVisitDefaultTraits for the documentation of
alpar@100
  1216
  /// a Bfs visit traits class.
alpar@100
  1217
#ifdef DOXYGEN
alpar@100
  1218
  template <typename _Digraph, typename _Visitor, typename _Traits>
alpar@100
  1219
#else
alpar@100
  1220
  template <typename _Digraph = ListDigraph,
alpar@209
  1221
            typename _Visitor = BfsVisitor<_Digraph>,
alpar@209
  1222
            typename _Traits = BfsDefaultTraits<_Digraph> >
alpar@100
  1223
#endif
alpar@100
  1224
  class BfsVisit {
alpar@100
  1225
  public:
alpar@209
  1226
alpar@100
  1227
    /// \brief \ref Exception for uninitialized parameters.
alpar@100
  1228
    ///
alpar@100
  1229
    /// This error represents problems in the initialization
alpar@100
  1230
    /// of the parameters of the algorithms.
alpar@100
  1231
    class UninitializedParameter : public lemon::UninitializedParameter {
alpar@100
  1232
    public:
alpar@209
  1233
      virtual const char* what() const throw()
alpar@100
  1234
      {
alpar@209
  1235
        return "lemon::BfsVisit::UninitializedParameter";
alpar@100
  1236
      }
alpar@100
  1237
    };
alpar@100
  1238
alpar@100
  1239
    typedef _Traits Traits;
alpar@100
  1240
alpar@100
  1241
    typedef typename Traits::Digraph Digraph;
alpar@100
  1242
alpar@100
  1243
    typedef _Visitor Visitor;
alpar@100
  1244
alpar@100
  1245
    ///The type of the map indicating which nodes are reached.
alpar@100
  1246
    typedef typename Traits::ReachedMap ReachedMap;
alpar@100
  1247
alpar@100
  1248
  private:
alpar@100
  1249
alpar@100
  1250
    typedef typename Digraph::Node Node;
alpar@100
  1251
    typedef typename Digraph::NodeIt NodeIt;
alpar@100
  1252
    typedef typename Digraph::Arc Arc;
alpar@100
  1253
    typedef typename Digraph::OutArcIt OutArcIt;
alpar@100
  1254
alpar@100
  1255
    /// Pointer to the underlying digraph.
alpar@100
  1256
    const Digraph *_digraph;
alpar@100
  1257
    /// Pointer to the visitor object.
alpar@100
  1258
    Visitor *_visitor;
alpar@100
  1259
    ///Pointer to the map of reached status of the nodes.
alpar@100
  1260
    ReachedMap *_reached;
alpar@100
  1261
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
alpar@100
  1262
    bool local_reached;
alpar@100
  1263
alpar@100
  1264
    std::vector<typename Digraph::Node> _list;
alpar@100
  1265
    int _list_front, _list_back;
alpar@100
  1266
alpar@100
  1267
    /// \brief Creates the maps if necessary.
alpar@100
  1268
    ///
alpar@100
  1269
    /// Creates the maps if necessary.
alpar@100
  1270
    void create_maps() {
alpar@100
  1271
      if(!_reached) {
alpar@209
  1272
        local_reached = true;
alpar@209
  1273
        _reached = Traits::createReachedMap(*_digraph);
alpar@100
  1274
      }
alpar@100
  1275
    }
alpar@100
  1276
alpar@100
  1277
  protected:
alpar@100
  1278
alpar@100
  1279
    BfsVisit() {}
alpar@209
  1280
alpar@100
  1281
  public:
alpar@100
  1282
alpar@100
  1283
    typedef BfsVisit Create;
alpar@100
  1284
alpar@100
  1285
    /// \name Named template parameters
alpar@100
  1286
alpar@100
  1287
    ///@{
alpar@100
  1288
    template <class T>
alpar@100
  1289
    struct DefReachedMapTraits : public Traits {
alpar@100
  1290
      typedef T ReachedMap;
alpar@100
  1291
      static ReachedMap *createReachedMap(const Digraph &digraph) {
alpar@209
  1292
        throw UninitializedParameter();
alpar@100
  1293
      }
alpar@100
  1294
    };
alpar@209
  1295
    /// \brief \ref named-templ-param "Named parameter" for setting
alpar@100
  1296
    /// ReachedMap type
alpar@100
  1297
    ///
alpar@100
  1298
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type
alpar@100
  1299
    template <class T>
alpar@100
  1300
    struct DefReachedMap : public BfsVisit< Digraph, Visitor,
alpar@209
  1301
                                            DefReachedMapTraits<T> > {
alpar@100
  1302
      typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;
alpar@100
  1303
    };
alpar@100
  1304
    ///@}
alpar@100
  1305
alpar@209
  1306
  public:
alpar@209
  1307
alpar@100
  1308
    /// \brief Constructor.
alpar@100
  1309
    ///
alpar@100
  1310
    /// Constructor.
alpar@100
  1311
    ///
alpar@100
  1312
    /// \param digraph the digraph the algorithm will run on.
alpar@100
  1313
    /// \param visitor The visitor of the algorithm.
alpar@100
  1314
    ///
alpar@209
  1315
    BfsVisit(const Digraph& digraph, Visitor& visitor)
alpar@100
  1316
      : _digraph(&digraph), _visitor(&visitor),
alpar@209
  1317
        _reached(0), local_reached(false) {}
alpar@209
  1318
alpar@100
  1319
    /// \brief Destructor.
alpar@100
  1320
    ///
alpar@100
  1321
    /// Destructor.
alpar@100
  1322
    ~BfsVisit() {
alpar@100
  1323
      if(local_reached) delete _reached;
alpar@100
  1324
    }
alpar@100
  1325
alpar@100
  1326
    /// \brief Sets the map indicating if a node is reached.
alpar@100
  1327
    ///
alpar@100
  1328
    /// Sets the map indicating if a node is reached.
alpar@100
  1329
    /// If you don't use this function before calling \ref run(),
alpar@100
  1330
    /// it will allocate one. The destuctor deallocates this
alpar@100
  1331
    /// automatically allocated map, of course.
alpar@100
  1332
    /// \return <tt> (*this) </tt>
alpar@100
  1333
    BfsVisit &reachedMap(ReachedMap &m) {
alpar@100
  1334
      if(local_reached) {
alpar@209
  1335
        delete _reached;
alpar@209
  1336
        local_reached = false;
alpar@100
  1337
      }
alpar@100
  1338
      _reached = &m;
alpar@100
  1339
      return *this;
alpar@100
  1340
    }
alpar@100
  1341
alpar@100
  1342
  public:
alpar@100
  1343
    /// \name Execution control
alpar@100
  1344
    /// The simplest way to execute the algorithm is to use
alpar@100
  1345
    /// one of the member functions called \c run(...).
alpar@100
  1346
    /// \n
alpar@100
  1347
    /// If you need more control on the execution,
alpar@100
  1348
    /// first you must call \ref init(), then you can adda source node
alpar@100
  1349
    /// with \ref addSource().
alpar@100
  1350
    /// Finally \ref start() will perform the actual path
alpar@100
  1351
    /// computation.
alpar@100
  1352
alpar@100
  1353
    /// @{
alpar@100
  1354
    /// \brief Initializes the internal data structures.
alpar@100
  1355
    ///
alpar@100
  1356
    /// Initializes the internal data structures.
alpar@100
  1357
    ///
alpar@100
  1358
    void init() {
alpar@100
  1359
      create_maps();
alpar@100
  1360
      _list.resize(countNodes(*_digraph));
alpar@100
  1361
      _list_front = _list_back = -1;
alpar@100
  1362
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
alpar@209
  1363
        _reached->set(u, false);
alpar@100
  1364
      }
alpar@100
  1365
    }
alpar@209
  1366
alpar@100
  1367
    /// \brief Adds a new source node.
alpar@100
  1368
    ///
alpar@100
  1369
    /// Adds a new source node to the set of nodes to be processed.
alpar@100
  1370
    void addSource(Node s) {
alpar@100
  1371
      if(!(*_reached)[s]) {
alpar@209
  1372
          _reached->set(s,true);
alpar@209
  1373
          _visitor->start(s);
alpar@209
  1374
          _visitor->reach(s);
alpar@100
  1375
          _list[++_list_back] = s;
alpar@209
  1376
        }
alpar@100
  1377
    }
alpar@209
  1378
alpar@100
  1379
    /// \brief Processes the next node.
alpar@100
  1380
    ///
alpar@100
  1381
    /// Processes the next node.
alpar@100
  1382
    ///
alpar@100
  1383
    /// \return The processed node.
alpar@100
  1384
    ///
alpar@100
  1385
    /// \pre The queue must not be empty!
alpar@209
  1386
    Node processNextNode() {
alpar@100
  1387
      Node n = _list[++_list_front];
alpar@100
  1388
      _visitor->process(n);
alpar@100
  1389
      Arc e;
alpar@100
  1390
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
alpar@100
  1391
        Node m = _digraph->target(e);
alpar@100
  1392
        if (!(*_reached)[m]) {
alpar@100
  1393
          _visitor->discover(e);
alpar@100
  1394
          _visitor->reach(m);
alpar@100
  1395
          _reached->set(m, true);
alpar@100
  1396
          _list[++_list_back] = m;
alpar@100
  1397
        } else {
alpar@100
  1398
          _visitor->examine(e);
alpar@100
  1399
        }
alpar@100
  1400
      }
alpar@100
  1401
      return n;
alpar@100
  1402
    }
alpar@100
  1403
alpar@100
  1404
    /// \brief Processes the next node.
alpar@100
  1405
    ///
alpar@100
  1406
    /// Processes the next node. And checks that the given target node
alpar@100
  1407
    /// is reached. If the target node is reachable from the processed
alpar@100
  1408
    /// node then the reached parameter will be set true. The reached
alpar@100
  1409
    /// parameter should be initially false.
alpar@100
  1410
    ///
alpar@100
  1411
    /// \param target The target node.
alpar@100
  1412
    /// \retval reach Indicates that the target node is reached.
alpar@100
  1413
    /// \return The processed node.
alpar@100
  1414
    ///
alpar@100
  1415
    /// \warning The queue must not be empty!
alpar@100
  1416
    Node processNextNode(Node target, bool& reach) {
alpar@100
  1417
      Node n = _list[++_list_front];
alpar@100
  1418
      _visitor->process(n);
alpar@100
  1419
      Arc e;
alpar@100
  1420
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
alpar@100
  1421
        Node m = _digraph->target(e);
alpar@100
  1422
        if (!(*_reached)[m]) {
alpar@100
  1423
          _visitor->discover(e);
alpar@100
  1424
          _visitor->reach(m);
alpar@100
  1425
          _reached->set(m, true);
alpar@100
  1426
          _list[++_list_back] = m;
alpar@100
  1427
          reach = reach || (target == m);
alpar@100
  1428
        } else {
alpar@100
  1429
          _visitor->examine(e);
alpar@100
  1430
        }
alpar@100
  1431
      }
alpar@100
  1432
      return n;
alpar@100
  1433
    }
alpar@100
  1434
alpar@100
  1435
    /// \brief Processes the next node.
alpar@100
  1436
    ///
alpar@100
  1437
    /// Processes the next node. And checks that at least one of
alpar@100
  1438
    /// reached node has true value in the \c nm node map. If one node
alpar@100
  1439
    /// with true value is reachable from the processed node then the
alpar@100
  1440
    /// rnode parameter will be set to the first of such nodes.
alpar@100
  1441
    ///
alpar@100
  1442
    /// \param nm The node map of possible targets.
alpar@100
  1443
    /// \retval rnode The reached target node.
alpar@100
  1444
    /// \return The processed node.
alpar@100
  1445
    ///
alpar@100
  1446
    /// \warning The queue must not be empty!
alpar@100
  1447
    template <typename NM>
alpar@100
  1448
    Node processNextNode(const NM& nm, Node& rnode) {
alpar@100
  1449
      Node n = _list[++_list_front];
alpar@100
  1450
      _visitor->process(n);
alpar@100
  1451
      Arc e;
alpar@100
  1452
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
alpar@100
  1453
        Node m = _digraph->target(e);
alpar@100
  1454
        if (!(*_reached)[m]) {
alpar@100
  1455
          _visitor->discover(e);
alpar@100
  1456
          _visitor->reach(m);
alpar@100
  1457
          _reached->set(m, true);
alpar@100
  1458
          _list[++_list_back] = m;
alpar@100
  1459
          if (nm[m] && rnode == INVALID) rnode = m;
alpar@100
  1460
        } else {
alpar@100
  1461
          _visitor->examine(e);
alpar@100
  1462
        }
alpar@100
  1463
      }
alpar@100
  1464
      return n;
alpar@100
  1465
    }
alpar@100
  1466
alpar@100
  1467
    /// \brief Next node to be processed.
alpar@100
  1468
    ///
alpar@100
  1469
    /// Next node to be processed.
alpar@100
  1470
    ///
alpar@100
  1471
    /// \return The next node to be processed or INVALID if the stack is
alpar@100
  1472
    /// empty.
alpar@209
  1473
    Node nextNode() {
alpar@100
  1474
      return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
alpar@100
  1475
    }
alpar@100
  1476
alpar@100
  1477
    /// \brief Returns \c false if there are nodes
alpar@100
  1478
    /// to be processed in the queue
alpar@100
  1479
    ///
alpar@100
  1480
    /// Returns \c false if there are nodes
alpar@100
  1481
    /// to be processed in the queue
alpar@100
  1482
    bool emptyQueue() { return _list_front == _list_back; }
alpar@100
  1483
alpar@100
  1484
    /// \brief Returns the number of the nodes to be processed.
alpar@100
  1485
    ///
alpar@100
  1486
    /// Returns the number of the nodes to be processed in the queue.
alpar@100
  1487
    int queueSize() { return _list_back - _list_front; }
alpar@209
  1488
alpar@100
  1489
    /// \brief Executes the algorithm.
alpar@100
  1490
    ///
alpar@100
  1491
    /// Executes the algorithm.
alpar@100
  1492
    ///
alpar@100
  1493
    /// \pre init() must be called and at least one node should be added
alpar@100
  1494
    /// with addSource() before using this function.
alpar@100
  1495
    void start() {
alpar@100
  1496
      while ( !emptyQueue() ) processNextNode();
alpar@100
  1497
    }
alpar@209
  1498
alpar@100
  1499
    /// \brief Executes the algorithm until \c dest is reached.
alpar@100
  1500
    ///
alpar@100
  1501
    /// Executes the algorithm until \c dest is reached.
alpar@100
  1502
    ///
alpar@100
  1503
    /// \pre init() must be called and at least one node should be added
alpar@100
  1504
    /// with addSource() before using this function.
alpar@100
  1505
    void start(Node dest) {
alpar@100
  1506
      bool reach = false;
alpar@100
  1507
      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
alpar@100
  1508
    }
alpar@209
  1509
alpar@100
  1510
    /// \brief Executes the algorithm until a condition is met.
alpar@100
  1511
    ///
alpar@100
  1512
    /// Executes the algorithm until a condition is met.
alpar@100
  1513
    ///
alpar@100
  1514
    /// \pre init() must be called and at least one node should be added
alpar@100
  1515
    /// with addSource() before using this function.
alpar@100
  1516
    ///
alpar@100
  1517
    ///\param nm must be a bool (or convertible) node map. The
alpar@100
  1518
    ///algorithm will stop when it reaches a node \c v with
alpar@100
  1519
    /// <tt>nm[v]</tt> true.
alpar@100
  1520
    ///
alpar@100
  1521
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
alpar@100
  1522
    ///\c INVALID if no such node was found.
alpar@100
  1523
    template <typename NM>
alpar@100
  1524
    Node start(const NM &nm) {
alpar@100
  1525
      Node rnode = INVALID;
alpar@100
  1526
      while ( !emptyQueue() && rnode == INVALID ) {
alpar@209
  1527
        processNextNode(nm, rnode);
alpar@100
  1528
      }
alpar@100
  1529
      return rnode;
alpar@100
  1530
    }
alpar@100
  1531
alpar@100
  1532
    /// \brief Runs %BFSVisit algorithm from node \c s.
alpar@100
  1533
    ///
alpar@100
  1534
    /// This method runs the %BFS algorithm from a root node \c s.
alpar@100
  1535
    /// \note b.run(s) is just a shortcut of the following code.
alpar@100
  1536
    ///\code
alpar@100
  1537
    ///   b.init();
alpar@100
  1538
    ///   b.addSource(s);
alpar@100
  1539
    ///   b.start();
alpar@100
  1540
    ///\endcode
alpar@100
  1541
    void run(Node s) {
alpar@100
  1542
      init();
alpar@100
  1543
      addSource(s);
alpar@100
  1544
      start();
alpar@100
  1545
    }
alpar@100
  1546
alpar@100
  1547
    /// \brief Runs %BFSVisit algorithm to visit all nodes in the digraph.
alpar@209
  1548
    ///
alpar@100
  1549
    /// This method runs the %BFS algorithm in order to
alpar@100
  1550
    /// compute the %BFS path to each node. The algorithm computes
alpar@100
  1551
    /// - The %BFS tree.
alpar@100
  1552
    /// - The distance of each node from the root in the %BFS tree.
alpar@100
  1553
    ///
alpar@100
  1554
    ///\note b.run() is just a shortcut of the following code.
alpar@100
  1555
    ///\code
alpar@100
  1556
    ///  b.init();
alpar@100
  1557
    ///  for (NodeIt it(digraph); it != INVALID; ++it) {
alpar@100
  1558
    ///    if (!b.reached(it)) {
alpar@100
  1559
    ///      b.addSource(it);
alpar@100
  1560
    ///      b.start();
alpar@100
  1561
    ///    }
alpar@100
  1562
    ///  }
alpar@100
  1563
    ///\endcode
alpar@100
  1564
    void run() {
alpar@100
  1565
      init();
alpar@100
  1566
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
alpar@100
  1567
        if (!reached(it)) {
alpar@100
  1568
          addSource(it);
alpar@100
  1569
          start();
alpar@100
  1570
        }
alpar@100
  1571
      }
alpar@100
  1572
    }
alpar@100
  1573
    ///@}
alpar@100
  1574
alpar@100
  1575
    /// \name Query Functions
alpar@100
  1576
    /// The result of the %BFS algorithm can be obtained using these
alpar@100
  1577
    /// functions.\n
alpar@100
  1578
    /// Before the use of these functions,
alpar@100
  1579
    /// either run() or start() must be called.
alpar@100
  1580
    ///@{
alpar@100
  1581
alpar@100
  1582
    /// \brief Checks if a node is reachable from the root.
alpar@100
  1583
    ///
alpar@100
  1584
    /// Returns \c true if \c v is reachable from the root(s).
alpar@100
  1585
    /// \warning The source nodes are inditated as unreachable.
alpar@100
  1586
    /// \pre Either \ref run() or \ref start()
alpar@100
  1587
    /// must be called before using this function.
alpar@100
  1588
    ///
alpar@100
  1589
    bool reached(Node v) { return (*_reached)[v]; }
alpar@100
  1590
    ///@}
alpar@100
  1591
  };
alpar@100
  1592
alpar@100
  1593
} //END OF NAMESPACE LEMON
alpar@100
  1594
alpar@100
  1595
#endif
alpar@100
  1596