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