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