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