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