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