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