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