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