lemon/bfs.h
author deba
Fri, 20 Apr 2007 14:47:19 +0000
changeset 2428 c06e86364234
parent 2386 81b47fc5c444
child 2438 718479989797
permissions -rw-r--r--
Doc change
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@2300
   518
    ///reached node has true value in the \c nm nodemap. If one node
deba@2300
   519
    ///with true value is reachable from the processed node then the
deba@2300
   520
    ///reached parameter will be set true. The reached parameter
deba@2300
   521
    ///should be initially false.
deba@2300
   522
    ///
alpar@2350
   523
    ///\param nm The nodemaps of possible targets.
deba@2386
   524
    ///\retval reach Indicates that one of the target nodes is reached.
deba@2300
   525
    ///\return The processed node.
deba@2300
   526
    ///
deba@2300
   527
    ///\warning The queue must not be empty!
deba@2300
   528
    template<class NM>
deba@2386
   529
    Node processNextNode(const NM& nm, bool& reach)
deba@2300
   530
    {
deba@2300
   531
      if(_queue_tail==_queue_next_dist) {
deba@2300
   532
	_curr_dist++;
deba@2300
   533
	_queue_next_dist=_queue_head;
deba@2300
   534
      }
deba@2300
   535
      Node n=_queue[_queue_tail++];
deba@2300
   536
      _processed->set(n,true);
deba@2300
   537
      Node m;
deba@2300
   538
      for(OutEdgeIt e(*G,n);e!=INVALID;++e)
deba@2300
   539
	if(!(*_reached)[m=G->target(e)]) {
deba@2300
   540
	  _queue[_queue_head++]=m;
deba@2300
   541
	  _reached->set(m,true);
deba@2300
   542
	  _pred->set(m,e);
deba@2300
   543
	  _dist->set(m,_curr_dist);
deba@2386
   544
          reached = reach || nm[m];
deba@2300
   545
	}
deba@2300
   546
      return n;
deba@2300
   547
    }
alpar@1218
   548
      
alpar@1665
   549
    ///Next node to be processed.
alpar@1665
   550
alpar@1665
   551
    ///Next node to be processed.
alpar@1665
   552
    ///
alpar@1665
   553
    ///\return The next node to be processed or INVALID if the queue is
alpar@1665
   554
    /// empty.
deba@1694
   555
    Node nextNode()
alpar@1665
   556
    { 
alpar@1665
   557
      return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
alpar@1665
   558
    }
alpar@1665
   559
 
alpar@1218
   560
    ///\brief Returns \c false if there are nodes
alpar@1218
   561
    ///to be processed in the queue
alpar@1218
   562
    ///
alpar@1218
   563
    ///Returns \c false if there are nodes
alpar@1218
   564
    ///to be processed in the queue
alpar@1218
   565
    bool emptyQueue() { return _queue_tail==_queue_head; }
alpar@1218
   566
    ///Returns the number of the nodes to be processed.
alpar@1218
   567
    
alpar@1218
   568
    ///Returns the number of the nodes to be processed in the queue.
alpar@1218
   569
    ///
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
    ///
alpar@1218
   586
    void start()
alpar@1218
   587
    {
alpar@1218
   588
      while ( !emptyQueue() ) processNextNode();
alpar@1218
   589
    }
alpar@1218
   590
    
deba@2307
   591
    ///Executes the algorithm until \c dest is reached.
alpar@1218
   592
deba@2307
   593
    ///Executes the algorithm until \c dest is reached.
alpar@1218
   594
    ///
alpar@1218
   595
    ///\pre init() must be called and at least one node should be added
alpar@1218
   596
    ///with addSource() before using this function.
alpar@1218
   597
    ///
alpar@1218
   598
    ///This method runs the %BFS algorithm from the root node(s)
alpar@1218
   599
    ///in order to
alpar@1218
   600
    ///compute the
alpar@1218
   601
    ///shortest path to \c dest. The algorithm computes
alpar@1218
   602
    ///- The shortest path to \c  dest.
alpar@1218
   603
    ///- The distance of \c dest from the root(s).
alpar@1218
   604
    ///
alpar@1218
   605
    void start(Node dest)
alpar@1218
   606
    {
deba@2386
   607
      bool reach = false;
deba@2386
   608
      while ( !emptyQueue() && !reach) processNextNode(dest, reach);
alpar@1218
   609
    }
alpar@1218
   610
    
alpar@1218
   611
    ///Executes the algorithm until a condition is met.
alpar@1218
   612
alpar@1218
   613
    ///Executes the algorithm until a condition is met.
alpar@1218
   614
    ///
alpar@1218
   615
    ///\pre init() must be called and at least one node should be added
alpar@1218
   616
    ///with addSource() before using this function.
alpar@1218
   617
    ///
deba@2306
   618
    ///\param nm must be a bool (or convertible) node map. The
deba@2307
   619
    ///algorithm will stop when it reached a node \c v with
alpar@2350
   620
    /// <tt>nm[v]</tt> true.
deba@2300
   621
    ///\todo query the reached target
alpar@1218
   622
    template<class NM>
deba@1755
   623
    void start(const NM &nm)
deba@1755
   624
    {
deba@2386
   625
      bool reach = false;
deba@2386
   626
      while ( !emptyQueue() && !reach) processNextNode(nm, reach);
deba@1755
   627
    }
alpar@1218
   628
    
alpar@1218
   629
    ///Runs %BFS algorithm from node \c s.
alpar@1218
   630
    
alpar@1218
   631
    ///This method runs the %BFS algorithm from a root node \c s
alpar@1218
   632
    ///in order to
alpar@1218
   633
    ///compute the
alpar@1218
   634
    ///shortest path to each node. The algorithm computes
alpar@1218
   635
    ///- The shortest path tree.
alpar@1218
   636
    ///- The distance of each node from the root.
alpar@1218
   637
    ///
deba@2306
   638
    ///\note b.run(s) is just a shortcut of the following code.
alpar@1218
   639
    ///\code
deba@2306
   640
    ///  b.init();
deba@2306
   641
    ///  b.addSource(s);
deba@2306
   642
    ///  b.start();
alpar@1218
   643
    ///\endcode
alpar@1218
   644
    void run(Node s) {
alpar@1218
   645
      init();
alpar@1218
   646
      addSource(s);
alpar@1218
   647
      start();
alpar@1218
   648
    }
alpar@1218
   649
    
alpar@1218
   650
    ///Finds the shortest path between \c s and \c t.
alpar@1218
   651
    
alpar@1218
   652
    ///Finds the shortest path between \c s and \c t.
alpar@1218
   653
    ///
alpar@1218
   654
    ///\return The length of the shortest s---t path if there exists one,
alpar@1218
   655
    ///0 otherwise.
deba@2306
   656
    ///\note Apart from the return value, b.run(s) is
alpar@1218
   657
    ///just a shortcut of the following code.
alpar@1218
   658
    ///\code
deba@2306
   659
    ///  b.init();
deba@2306
   660
    ///  b.addSource(s);
deba@2306
   661
    ///  b.start(t);
alpar@1218
   662
    ///\endcode
alpar@1218
   663
    int run(Node s,Node t) {
alpar@1218
   664
      init();
alpar@1218
   665
      addSource(s);
alpar@1218
   666
      start(t);
deba@2300
   667
      return reached(t)? _curr_dist : 0;
alpar@1218
   668
    }
alpar@1218
   669
    
alpar@1218
   670
    ///@}
alpar@1218
   671
alpar@1218
   672
    ///\name Query Functions
alpar@1218
   673
    ///The result of the %BFS algorithm can be obtained using these
alpar@1218
   674
    ///functions.\n
alpar@1218
   675
    ///Before the use of these functions,
deba@2306
   676
    ///either run() or start() must be calleb.
alpar@1218
   677
    
alpar@1218
   678
    ///@{
alpar@1218
   679
deba@2335
   680
    typedef PredMapPath<Graph, PredMap> Path;
deba@2335
   681
deba@2335
   682
    ///Gives back the shortest path.
alpar@1283
   683
    
deba@2335
   684
    ///Gives back the shortest path.
deba@2335
   685
    ///\pre The \c t should be reachable from the source.
deba@2335
   686
    Path path(Node t) 
alpar@1283
   687
    {
deba@2335
   688
      return Path(*G, *_pred, t);
alpar@1283
   689
    }
alpar@1283
   690
alpar@1218
   691
    ///The distance of a node from the root(s).
alpar@1218
   692
alpar@1218
   693
    ///Returns the distance of a node from the root(s).
alpar@774
   694
    ///\pre \ref run() must be called before using this function.
alpar@1218
   695
    ///\warning If node \c v in unreachable from the root(s) the return value
jacint@1270
   696
    ///of this function is undefined.
alpar@1218
   697
    int dist(Node v) const { return (*_dist)[v]; }
alpar@774
   698
alpar@1218
   699
    ///Returns the 'previous edge' of the shortest path tree.
alpar@774
   700
alpar@1218
   701
    ///For a node \c v it returns the 'previous edge'
alpar@1218
   702
    ///of the shortest path tree,
alpar@1218
   703
    ///i.e. it returns the last edge of a shortest path from the root(s) to \c
alpar@774
   704
    ///v. It is \ref INVALID
alpar@1218
   705
    ///if \c v is unreachable from the root(s) or \c v is a root. The
alpar@1218
   706
    ///shortest path tree used here is equal to the shortest path tree used in
alpar@1631
   707
    ///\ref predNode().
alpar@1218
   708
    ///\pre Either \ref run() or \ref start() must be called before using
alpar@774
   709
    ///this function.
deba@1763
   710
    Edge predEdge(Node v) const { return (*_pred)[v];}
alpar@774
   711
alpar@1218
   712
    ///Returns the 'previous node' of the shortest path tree.
alpar@774
   713
alpar@1218
   714
    ///For a node \c v it returns the 'previous node'
alpar@1218
   715
    ///of the shortest path tree,
alpar@774
   716
    ///i.e. it returns the last but one node from a shortest path from the
alpar@1218
   717
    ///root(a) to \c /v.
alpar@1218
   718
    ///It is INVALID if \c v is unreachable from the root(s) or
alpar@1218
   719
    ///if \c v itself a root.
alpar@1218
   720
    ///The shortest path tree used here is equal to the shortest path
deba@1763
   721
    ///tree used in \ref predEdge().
alpar@1218
   722
    ///\pre Either \ref run() or \ref start() must be called before
alpar@774
   723
    ///using this function.
alpar@1218
   724
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@1218
   725
				  G->source((*_pred)[v]); }
alpar@774
   726
    
alpar@774
   727
    ///Returns a reference to the NodeMap of distances.
alpar@1218
   728
alpar@1218
   729
    ///Returns a reference to the NodeMap of distances.
alpar@1218
   730
    ///\pre Either \ref run() or \ref init() must
alpar@774
   731
    ///be called before using this function.
alpar@1218
   732
    const DistMap &distMap() const { return *_dist;}
alpar@774
   733
 
alpar@1218
   734
    ///Returns a reference to the shortest path tree map.
alpar@774
   735
alpar@774
   736
    ///Returns a reference to the NodeMap of the edges of the
alpar@1218
   737
    ///shortest path tree.
alpar@1218
   738
    ///\pre Either \ref run() or \ref init()
alpar@1218
   739
    ///must be called before using this function.
alpar@1218
   740
    const PredMap &predMap() const { return *_pred;}
alpar@774
   741
 
alpar@774
   742
    ///Checks if a node is reachable from the root.
alpar@774
   743
alpar@774
   744
    ///Returns \c true if \c v is reachable from the root.
jacint@1270
   745
    ///\warning The source nodes are indicated as unreached.
alpar@1218
   746
    ///\pre Either \ref run() or \ref start()
alpar@1218
   747
    ///must be called before using this function.
alpar@774
   748
    ///
alpar@1218
   749
    bool reached(Node v) { return (*_reached)[v]; }
alpar@1218
   750
    
alpar@1218
   751
    ///@}
alpar@1218
   752
  };
alpar@1218
   753
alpar@1218
   754
  ///Default traits class of Bfs function.
alpar@1218
   755
alpar@1218
   756
  ///Default traits class of Bfs function.
alpar@1218
   757
  ///\param GR Graph type.
alpar@1218
   758
  template<class GR>
alpar@1218
   759
  struct BfsWizardDefaultTraits
alpar@1218
   760
  {
alpar@1218
   761
    ///The graph type the algorithm runs on. 
alpar@1218
   762
    typedef GR Graph;
alpar@1218
   763
    ///\brief The type of the map that stores the last
alpar@1218
   764
    ///edges of the shortest paths.
alpar@1218
   765
    /// 
alpar@1218
   766
    ///The type of the map that stores the last
alpar@1218
   767
    ///edges of the shortest paths.
alpar@2260
   768
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@774
   769
    ///
alpar@1218
   770
    typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
alpar@1218
   771
    ///Instantiates a PredMap.
alpar@1218
   772
 
alpar@1218
   773
    ///This function instantiates a \ref PredMap. 
alpar@1536
   774
    ///\param g is the graph, to which we would like to define the PredMap.
alpar@1218
   775
    ///\todo The graph alone may be insufficient to initialize
alpar@1536
   776
#ifdef DOXYGEN
alpar@1536
   777
    static PredMap *createPredMap(const GR &g) 
alpar@1536
   778
#else
alpar@1367
   779
    static PredMap *createPredMap(const GR &) 
alpar@1536
   780
#endif
alpar@1218
   781
    {
alpar@1218
   782
      return new PredMap();
alpar@1218
   783
    }
alpar@1218
   784
alpar@1218
   785
    ///The type of the map that indicates which nodes are processed.
alpar@1218
   786
 
alpar@1218
   787
    ///The type of the map that indicates which nodes are processed.
alpar@2260
   788
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   789
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   790
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
   791
    ///Instantiates a ProcessedMap.
alpar@1218
   792
 
alpar@1218
   793
    ///This function instantiates a \ref ProcessedMap. 
alpar@1536
   794
    ///\param g is the graph, to which
alpar@1218
   795
    ///we would like to define the \ref ProcessedMap
alpar@1536
   796
#ifdef DOXYGEN
alpar@1536
   797
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
   798
#else
alpar@1367
   799
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
   800
#endif
alpar@1218
   801
    {
alpar@1218
   802
      return new ProcessedMap();
alpar@1218
   803
    }
alpar@1218
   804
    ///The type of the map that indicates which nodes are reached.
alpar@1218
   805
 
alpar@1218
   806
    ///The type of the map that indicates which nodes are reached.
alpar@2260
   807
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   808
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   809
    typedef typename Graph::template NodeMap<bool> ReachedMap;
alpar@1218
   810
    ///Instantiates a ReachedMap.
alpar@1218
   811
 
alpar@1218
   812
    ///This function instantiates a \ref ReachedMap. 
alpar@1218
   813
    ///\param G is the graph, to which
alpar@1218
   814
    ///we would like to define the \ref ReachedMap.
alpar@1218
   815
    static ReachedMap *createReachedMap(const GR &G)
alpar@1218
   816
    {
alpar@1218
   817
      return new ReachedMap(G);
alpar@1218
   818
    }
alpar@1218
   819
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   820
 
alpar@1218
   821
    ///The type of the map that stores the dists of the nodes.
alpar@2260
   822
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   823
    ///
alpar@1218
   824
    typedef NullMap<typename Graph::Node,int> DistMap;
alpar@1218
   825
    ///Instantiates a DistMap.
alpar@1218
   826
 
alpar@1218
   827
    ///This function instantiates a \ref DistMap. 
alpar@1536
   828
    ///\param g is the graph, to which we would like to define the \ref DistMap
alpar@1536
   829
#ifdef DOXYGEN
alpar@1536
   830
    static DistMap *createDistMap(const GR &g)
alpar@1536
   831
#else
alpar@1367
   832
    static DistMap *createDistMap(const GR &)
alpar@1536
   833
#endif
alpar@1218
   834
    {
alpar@1218
   835
      return new DistMap();
alpar@1218
   836
    }
alpar@1218
   837
  };
alpar@1218
   838
  
alpar@1218
   839
  /// Default traits used by \ref BfsWizard
alpar@1218
   840
alpar@1218
   841
  /// To make it easier to use Bfs algorithm
alpar@1218
   842
  ///we have created a wizard class.
alpar@1218
   843
  /// This \ref BfsWizard class needs default traits,
alpar@1218
   844
  ///as well as the \ref Bfs class.
alpar@1218
   845
  /// The \ref BfsWizardBase is a class to be the default traits of the
alpar@1218
   846
  /// \ref BfsWizard class.
alpar@1218
   847
  template<class GR>
alpar@1218
   848
  class BfsWizardBase : public BfsWizardDefaultTraits<GR>
alpar@1218
   849
  {
alpar@1218
   850
alpar@1218
   851
    typedef BfsWizardDefaultTraits<GR> Base;
alpar@1218
   852
  protected:
alpar@1218
   853
    /// Type of the nodes in the graph.
alpar@1218
   854
    typedef typename Base::Graph::Node Node;
alpar@1218
   855
alpar@1218
   856
    /// Pointer to the underlying graph.
alpar@1218
   857
    void *_g;
alpar@1218
   858
    ///Pointer to the map of reached nodes.
alpar@1218
   859
    void *_reached;
alpar@1218
   860
    ///Pointer to the map of processed nodes.
alpar@1218
   861
    void *_processed;
alpar@1218
   862
    ///Pointer to the map of predecessors edges.
alpar@1218
   863
    void *_pred;
alpar@1218
   864
    ///Pointer to the map of distances.
alpar@1218
   865
    void *_dist;
alpar@1218
   866
    ///Pointer to the source node.
alpar@1218
   867
    Node _source;
alpar@1218
   868
    
alpar@1218
   869
    public:
alpar@1218
   870
    /// Constructor.
alpar@1218
   871
    
alpar@1218
   872
    /// This constructor does not require parameters, therefore it initiates
alpar@1218
   873
    /// all of the attributes to default values (0, INVALID).
alpar@1218
   874
    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
alpar@1218
   875
			   _dist(0), _source(INVALID) {}
alpar@1218
   876
alpar@1218
   877
    /// Constructor.
alpar@1218
   878
    
alpar@1218
   879
    /// This constructor requires some parameters,
alpar@1218
   880
    /// listed in the parameters list.
alpar@1218
   881
    /// Others are initiated to 0.
alpar@1218
   882
    /// \param g is the initial value of  \ref _g
alpar@1218
   883
    /// \param s is the initial value of  \ref _source
alpar@1218
   884
    BfsWizardBase(const GR &g, Node s=INVALID) :
deba@2386
   885
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 
deba@2386
   886
      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
alpar@1218
   887
alpar@1218
   888
  };
alpar@1218
   889
  
alpar@1218
   890
  /// A class to make the usage of Bfs algorithm easier
alpar@1218
   891
alpar@1218
   892
  /// This class is created to make it easier to use Bfs algorithm.
alpar@1218
   893
  /// It uses the functions and features of the plain \ref Bfs,
alpar@1218
   894
  /// but it is much simpler to use it.
alpar@1218
   895
  ///
alpar@1218
   896
  /// Simplicity means that the way to change the types defined
alpar@1218
   897
  /// in the traits class is based on functions that returns the new class
alpar@1218
   898
  /// and not on templatable built-in classes.
alpar@1218
   899
  /// When using the plain \ref Bfs
alpar@1218
   900
  /// the new class with the modified type comes from
alpar@1218
   901
  /// the original class by using the ::
alpar@1218
   902
  /// operator. In the case of \ref BfsWizard only
alpar@1218
   903
  /// a function have to be called and it will
alpar@1218
   904
  /// return the needed class.
alpar@1218
   905
  ///
alpar@1218
   906
  /// It does not have own \ref run method. When its \ref run method is called
alpar@1218
   907
  /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
alpar@1218
   908
  /// method of it.
alpar@1218
   909
  template<class TR>
alpar@1218
   910
  class BfsWizard : public TR
alpar@1218
   911
  {
alpar@1218
   912
    typedef TR Base;
alpar@1218
   913
alpar@1218
   914
    ///The type of the underlying graph.
alpar@1218
   915
    typedef typename TR::Graph Graph;
alpar@1218
   916
    //\e
alpar@1218
   917
    typedef typename Graph::Node Node;
alpar@1218
   918
    //\e
alpar@1218
   919
    typedef typename Graph::NodeIt NodeIt;
alpar@1218
   920
    //\e
alpar@1218
   921
    typedef typename Graph::Edge Edge;
alpar@1218
   922
    //\e
alpar@1218
   923
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1218
   924
    
alpar@1218
   925
    ///\brief The type of the map that stores
alpar@1218
   926
    ///the reached nodes
alpar@1218
   927
    typedef typename TR::ReachedMap ReachedMap;
alpar@1218
   928
    ///\brief The type of the map that stores
alpar@1218
   929
    ///the processed nodes
alpar@1218
   930
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@1218
   931
    ///\brief The type of the map that stores the last
alpar@1218
   932
    ///edges of the shortest paths.
alpar@1218
   933
    typedef typename TR::PredMap PredMap;
alpar@1218
   934
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   935
    typedef typename TR::DistMap DistMap;
alpar@1218
   936
deba@2306
   937
  public:
alpar@1218
   938
    /// Constructor.
alpar@1218
   939
    BfsWizard() : TR() {}
alpar@1218
   940
alpar@1218
   941
    /// Constructor that requires parameters.
alpar@1218
   942
alpar@1218
   943
    /// Constructor that requires parameters.
alpar@1218
   944
    /// These parameters will be the default values for the traits class.
alpar@1218
   945
    BfsWizard(const Graph &g, Node s=INVALID) :
alpar@1218
   946
      TR(g,s) {}
alpar@1218
   947
alpar@1218
   948
    ///Copy constructor
alpar@1218
   949
    BfsWizard(const TR &b) : TR(b) {}
alpar@1218
   950
alpar@1218
   951
    ~BfsWizard() {}
alpar@1218
   952
alpar@1218
   953
    ///Runs Bfs algorithm from a given node.
alpar@1218
   954
    
alpar@1218
   955
    ///Runs Bfs algorithm from a given node.
alpar@1218
   956
    ///The node can be given by the \ref source function.
alpar@1218
   957
    void run()
alpar@1218
   958
    {
alpar@1218
   959
      if(Base::_source==INVALID) throw UninitializedParameter();
deba@2386
   960
      Bfs<Graph,TR> alg(*reinterpret_cast<const Graph*>(Base::_g));
alpar@1218
   961
      if(Base::_reached)
deba@2386
   962
	alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
deba@2386
   963
      if(Base::_processed) 
deba@2386
   964
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
deba@2386
   965
      if(Base::_pred) 
deba@2386
   966
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
deba@2386
   967
      if(Base::_dist) 
deba@2386
   968
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
alpar@1218
   969
      alg.run(Base::_source);
alpar@1218
   970
    }
alpar@1218
   971
alpar@1218
   972
    ///Runs Bfs algorithm from the given node.
alpar@1218
   973
alpar@1218
   974
    ///Runs Bfs algorithm from the given node.
alpar@1218
   975
    ///\param s is the given source.
alpar@1218
   976
    void run(Node s)
alpar@1218
   977
    {
alpar@1218
   978
      Base::_source=s;
alpar@1218
   979
      run();
alpar@1218
   980
    }
alpar@1218
   981
alpar@1218
   982
    template<class T>
alpar@1218
   983
    struct DefPredMapBase : public Base {
alpar@1218
   984
      typedef T PredMap;
alpar@1367
   985
      static PredMap *createPredMap(const Graph &) { return 0; };
alpar@1236
   986
      DefPredMapBase(const TR &b) : TR(b) {}
alpar@1218
   987
    };
alpar@1218
   988
    
alpar@1218
   989
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   990
    ///function for setting PredMap
alpar@1218
   991
    ///
alpar@1218
   992
    /// \ref named-templ-param "Named parameter"
alpar@1218
   993
    ///function for setting PredMap
alpar@1218
   994
    ///
alpar@1218
   995
    template<class T>
alpar@1218
   996
    BfsWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@1218
   997
    {
deba@2386
   998
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1218
   999
      return BfsWizard<DefPredMapBase<T> >(*this);
alpar@1218
  1000
    }
alpar@1218
  1001
    
alpar@1218
  1002
 
alpar@1218
  1003
    template<class T>
alpar@1218
  1004
    struct DefReachedMapBase : public Base {
alpar@1218
  1005
      typedef T ReachedMap;
alpar@1367
  1006
      static ReachedMap *createReachedMap(const Graph &) { return 0; };
alpar@1236
  1007
      DefReachedMapBase(const TR &b) : TR(b) {}
alpar@1218
  1008
    };
alpar@1218
  1009
    
alpar@1218
  1010
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1011
    ///function for setting ReachedMap
alpar@1218
  1012
    ///
alpar@1218
  1013
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1014
    ///function for setting ReachedMap
alpar@1218
  1015
    ///
alpar@1218
  1016
    template<class T>
alpar@1218
  1017
    BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
alpar@1218
  1018
    {
deba@2386
  1019
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1218
  1020
      return BfsWizard<DefReachedMapBase<T> >(*this);
alpar@1218
  1021
    }
alpar@1218
  1022
    
alpar@1218
  1023
alpar@1218
  1024
    template<class T>
alpar@1218
  1025
    struct DefProcessedMapBase : public Base {
alpar@1218
  1026
      typedef T ProcessedMap;
alpar@1367
  1027
      static ProcessedMap *createProcessedMap(const Graph &) { return 0; };
alpar@1236
  1028
      DefProcessedMapBase(const TR &b) : TR(b) {}
alpar@1218
  1029
    };
alpar@1218
  1030
    
alpar@1218
  1031
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1032
    ///function for setting ProcessedMap
alpar@1218
  1033
    ///
alpar@1218
  1034
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1035
    ///function for setting ProcessedMap
alpar@1218
  1036
    ///
alpar@1218
  1037
    template<class T>
alpar@1218
  1038
    BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
alpar@1218
  1039
    {
deba@2386
  1040
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1218
  1041
      return BfsWizard<DefProcessedMapBase<T> >(*this);
alpar@1218
  1042
    }
alpar@1218
  1043
    
alpar@1218
  1044
   
alpar@1218
  1045
    template<class T>
alpar@1218
  1046
    struct DefDistMapBase : public Base {
alpar@1218
  1047
      typedef T DistMap;
alpar@1367
  1048
      static DistMap *createDistMap(const Graph &) { return 0; };
alpar@1236
  1049
      DefDistMapBase(const TR &b) : TR(b) {}
alpar@1218
  1050
    };
alpar@1218
  1051
    
alpar@1218
  1052
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
  1053
    ///function for setting DistMap type
alpar@1218
  1054
    ///
alpar@1218
  1055
    /// \ref named-templ-param "Named parameter"
alpar@1218
  1056
    ///function for setting DistMap type
alpar@1218
  1057
    ///
alpar@1218
  1058
    template<class T>
alpar@1218
  1059
    BfsWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@1218
  1060
    {
deba@2386
  1061
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1218
  1062
      return BfsWizard<DefDistMapBase<T> >(*this);
alpar@1218
  1063
    }
alpar@1218
  1064
    
alpar@1218
  1065
    /// Sets the source node, from which the Bfs algorithm runs.
alpar@1218
  1066
alpar@1218
  1067
    /// Sets the source node, from which the Bfs algorithm runs.
alpar@1218
  1068
    /// \param s is the source node.
alpar@1218
  1069
    BfsWizard<TR> &source(Node s) 
alpar@1218
  1070
    {
alpar@1218
  1071
      Base::_source=s;
alpar@1218
  1072
      return *this;
alpar@1218
  1073
    }
alpar@774
  1074
    
alpar@774
  1075
  };
alpar@774
  1076
  
alpar@1218
  1077
  ///Function type interface for Bfs algorithm.
alpar@1218
  1078
deba@2376
  1079
  /// \ingroup search
alpar@1218
  1080
  ///Function type interface for Bfs algorithm.
alpar@1218
  1081
  ///
alpar@1218
  1082
  ///This function also has several
alpar@1218
  1083
  ///\ref named-templ-func-param "named parameters",
alpar@1218
  1084
  ///they are declared as the members of class \ref BfsWizard.
alpar@1218
  1085
  ///The following
alpar@1218
  1086
  ///example shows how to use these parameters.
alpar@1218
  1087
  ///\code
alpar@1218
  1088
  ///  bfs(g,source).predMap(preds).run();
alpar@1218
  1089
  ///\endcode
alpar@1218
  1090
  ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
alpar@1218
  1091
  ///to the end of the parameter list.
alpar@1218
  1092
  ///\sa BfsWizard
alpar@1218
  1093
  ///\sa Bfs
alpar@1218
  1094
  template<class GR>
alpar@1218
  1095
  BfsWizard<BfsWizardBase<GR> >
alpar@1218
  1096
  bfs(const GR &g,typename GR::Node s=INVALID)
alpar@1218
  1097
  {
alpar@1218
  1098
    return BfsWizard<BfsWizardBase<GR> >(g,s);
alpar@1218
  1099
  }
alpar@1218
  1100
deba@2306
  1101
#ifdef DOXYGEN
deba@2306
  1102
  /// \brief Visitor class for bfs.
deba@2306
  1103
  ///  
deba@2306
  1104
  /// It gives a simple interface for a functional interface for bfs 
deba@2306
  1105
  /// traversal. The traversal on a linear data structure. 
deba@2306
  1106
  template <typename _Graph>
deba@2306
  1107
  struct BfsVisitor {
deba@2306
  1108
    typedef _Graph Graph;
deba@2306
  1109
    typedef typename Graph::Edge Edge;
deba@2306
  1110
    typedef typename Graph::Node Node;
deba@2306
  1111
    /// \brief Called when the edge reach a node.
deba@2306
  1112
    /// 
deba@2306
  1113
    /// It is called when the bfs find an edge which target is not
deba@2306
  1114
    /// reached yet.
deba@2306
  1115
    void discover(const Edge& edge) {}
deba@2306
  1116
    /// \brief Called when the node reached first time.
deba@2306
  1117
    /// 
deba@2306
  1118
    /// It is Called when the node reached first time.
deba@2306
  1119
    void reach(const Node& node) {}
deba@2306
  1120
    /// \brief Called when the edge examined but target of the edge 
deba@2306
  1121
    /// already discovered.
deba@2306
  1122
    /// 
deba@2306
  1123
    /// It called when the edge examined but the target of the edge 
deba@2306
  1124
    /// already discovered.
deba@2306
  1125
    void examine(const Edge& edge) {}
deba@2306
  1126
    /// \brief Called for the source node of the bfs.
deba@2306
  1127
    /// 
deba@2306
  1128
    /// It is called for the source node of the bfs.
deba@2306
  1129
    void start(const Node& node) {}
deba@2306
  1130
    /// \brief Called when the node processed.
deba@2306
  1131
    /// 
deba@2306
  1132
    /// It is Called when the node processed.
deba@2306
  1133
    void process(const Node& node) {}
deba@2306
  1134
  };
deba@2306
  1135
#else
deba@2306
  1136
  template <typename _Graph>
deba@2306
  1137
  struct BfsVisitor {
deba@2306
  1138
    typedef _Graph Graph;
deba@2306
  1139
    typedef typename Graph::Edge Edge;
deba@2306
  1140
    typedef typename Graph::Node Node;
deba@2306
  1141
    void discover(const Edge&) {}
deba@2306
  1142
    void reach(const Node&) {}
deba@2306
  1143
    void examine(const Edge&) {}
deba@2306
  1144
    void start(const Node&) {}
deba@2306
  1145
    void process(const Node&) {}
deba@2306
  1146
deba@2306
  1147
    template <typename _Visitor>
deba@2306
  1148
    struct Constraints {
deba@2306
  1149
      void constraints() {
deba@2306
  1150
	Edge edge;
deba@2306
  1151
	Node node;
deba@2306
  1152
	visitor.discover(edge);
deba@2306
  1153
	visitor.reach(node);
deba@2306
  1154
	visitor.examine(edge);
deba@2306
  1155
	visitor.start(node);
deba@2306
  1156
        visitor.process(node);
deba@2306
  1157
      }
deba@2306
  1158
      _Visitor& visitor;
deba@2306
  1159
    };
deba@2306
  1160
  };
deba@2306
  1161
#endif
deba@2306
  1162
deba@2306
  1163
  /// \brief Default traits class of BfsVisit class.
deba@2306
  1164
  ///
deba@2306
  1165
  /// Default traits class of BfsVisit class.
deba@2306
  1166
  /// \param _Graph Graph type.
deba@2306
  1167
  template<class _Graph>
deba@2306
  1168
  struct BfsVisitDefaultTraits {
deba@2306
  1169
deba@2306
  1170
    /// \brief The graph type the algorithm runs on. 
deba@2306
  1171
    typedef _Graph Graph;
deba@2306
  1172
deba@2306
  1173
    /// \brief The type of the map that indicates which nodes are reached.
deba@2306
  1174
    /// 
deba@2306
  1175
    /// The type of the map that indicates which nodes are reached.
deba@2306
  1176
    /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
deba@2306
  1177
    /// \todo named parameter to set this type, function to read and write.
deba@2306
  1178
    typedef typename Graph::template NodeMap<bool> ReachedMap;
deba@2306
  1179
deba@2306
  1180
    /// \brief Instantiates a ReachedMap.
deba@2306
  1181
    ///
deba@2306
  1182
    /// This function instantiates a \ref ReachedMap. 
deba@2306
  1183
    /// \param graph is the graph, to which
deba@2306
  1184
    /// we would like to define the \ref ReachedMap.
deba@2306
  1185
    static ReachedMap *createReachedMap(const Graph &graph) {
deba@2306
  1186
      return new ReachedMap(graph);
deba@2306
  1187
    }
deba@2306
  1188
deba@2306
  1189
  };
deba@2306
  1190
  
deba@2306
  1191
  /// %BFS Visit algorithm class.
deba@2306
  1192
  
deba@2376
  1193
  /// \ingroup search
deba@2306
  1194
  /// This class provides an efficient implementation of the %BFS algorithm
deba@2306
  1195
  /// with visitor interface.
deba@2306
  1196
  ///
deba@2306
  1197
  /// The %BfsVisit class provides an alternative interface to the Bfs
deba@2306
  1198
  /// class. It works with callback mechanism, the BfsVisit object calls
deba@2306
  1199
  /// on every bfs event the \c Visitor class member functions. 
deba@2306
  1200
  ///
deba@2306
  1201
  /// \param _Graph The graph type the algorithm runs on. The default value is
deba@2306
  1202
  /// \ref ListGraph. The value of _Graph is not used directly by Bfs, it
deba@2306
  1203
  /// is only passed to \ref BfsDefaultTraits.
deba@2306
  1204
  /// \param _Visitor The Visitor object for the algorithm. The 
deba@2306
  1205
  /// \ref BfsVisitor "BfsVisitor<_Graph>" is an empty Visitor which
deba@2306
  1206
  /// does not observe the Bfs events. If you want to observe the bfs
deba@2306
  1207
  /// events you should implement your own Visitor class.
deba@2306
  1208
  /// \param _Traits Traits class to set various data types used by the 
deba@2306
  1209
  /// algorithm. The default traits class is
deba@2306
  1210
  /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Graph>".
deba@2306
  1211
  /// See \ref BfsVisitDefaultTraits for the documentation of
deba@2306
  1212
  /// a Bfs visit traits class.
deba@2306
  1213
  ///
deba@2306
  1214
  /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
deba@2306
  1215
#ifdef DOXYGEN
deba@2306
  1216
  template <typename _Graph, typename _Visitor, typename _Traits>
deba@2306
  1217
#else
deba@2306
  1218
  template <typename _Graph = ListGraph,
deba@2306
  1219
	    typename _Visitor = BfsVisitor<_Graph>,
deba@2306
  1220
	    typename _Traits = BfsDefaultTraits<_Graph> >
deba@2306
  1221
#endif
deba@2306
  1222
  class BfsVisit {
deba@2306
  1223
  public:
deba@2306
  1224
    
deba@2306
  1225
    /// \brief \ref Exception for uninitialized parameters.
deba@2306
  1226
    ///
deba@2306
  1227
    /// This error represents problems in the initialization
deba@2306
  1228
    /// of the parameters of the algorithms.
deba@2306
  1229
    class UninitializedParameter : public lemon::UninitializedParameter {
deba@2306
  1230
    public:
deba@2306
  1231
      virtual const char* what() const throw() 
deba@2306
  1232
      {
deba@2306
  1233
	return "lemon::BfsVisit::UninitializedParameter";
deba@2306
  1234
      }
deba@2306
  1235
    };
deba@2306
  1236
deba@2306
  1237
    typedef _Traits Traits;
deba@2306
  1238
deba@2306
  1239
    typedef typename Traits::Graph Graph;
deba@2306
  1240
deba@2306
  1241
    typedef _Visitor Visitor;
deba@2306
  1242
deba@2306
  1243
    ///The type of the map indicating which nodes are reached.
deba@2306
  1244
    typedef typename Traits::ReachedMap ReachedMap;
deba@2306
  1245
deba@2306
  1246
  private:
deba@2306
  1247
deba@2306
  1248
    typedef typename Graph::Node Node;
deba@2306
  1249
    typedef typename Graph::NodeIt NodeIt;
deba@2306
  1250
    typedef typename Graph::Edge Edge;
deba@2306
  1251
    typedef typename Graph::OutEdgeIt OutEdgeIt;
deba@2306
  1252
deba@2306
  1253
    /// Pointer to the underlying graph.
deba@2306
  1254
    const Graph *_graph;
deba@2306
  1255
    /// Pointer to the visitor object.
deba@2306
  1256
    Visitor *_visitor;
deba@2306
  1257
    ///Pointer to the map of reached status of the nodes.
deba@2306
  1258
    ReachedMap *_reached;
deba@2306
  1259
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
deba@2306
  1260
    bool local_reached;
deba@2306
  1261
deba@2306
  1262
    std::vector<typename Graph::Node> _list;
deba@2306
  1263
    int _list_front, _list_back;
deba@2306
  1264
deba@2306
  1265
    /// \brief Creates the maps if necessary.
deba@2306
  1266
    ///
deba@2306
  1267
    /// Creates the maps if necessary.
deba@2306
  1268
    void create_maps() {
deba@2306
  1269
      if(!_reached) {
deba@2306
  1270
	local_reached = true;
deba@2306
  1271
	_reached = Traits::createReachedMap(*_graph);
deba@2306
  1272
      }
deba@2306
  1273
    }
deba@2306
  1274
deba@2306
  1275
  protected:
deba@2306
  1276
deba@2306
  1277
    BfsVisit() {}
deba@2306
  1278
    
deba@2306
  1279
  public:
deba@2306
  1280
deba@2306
  1281
    typedef BfsVisit Create;
deba@2306
  1282
deba@2306
  1283
    /// \name Named template parameters
deba@2306
  1284
deba@2306
  1285
    ///@{
deba@2306
  1286
    template <class T>
deba@2306
  1287
    struct DefReachedMapTraits : public Traits {
deba@2306
  1288
      typedef T ReachedMap;
deba@2306
  1289
      static ReachedMap *createReachedMap(const Graph &graph) {
deba@2306
  1290
	throw UninitializedParameter();
deba@2306
  1291
      }
deba@2306
  1292
    };
deba@2306
  1293
    /// \brief \ref named-templ-param "Named parameter" for setting 
deba@2306
  1294
    /// ReachedMap type
deba@2306
  1295
    ///
deba@2306
  1296
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type
deba@2306
  1297
    template <class T>
deba@2306
  1298
    struct DefReachedMap : public BfsVisit< Graph, Visitor,
deba@2306
  1299
					    DefReachedMapTraits<T> > {
deba@2306
  1300
      typedef BfsVisit< Graph, Visitor, DefReachedMapTraits<T> > Create;
deba@2306
  1301
    };
deba@2306
  1302
    ///@}
deba@2306
  1303
deba@2306
  1304
  public:      
deba@2306
  1305
    
deba@2306
  1306
    /// \brief Constructor.
deba@2306
  1307
    ///
deba@2306
  1308
    /// Constructor.
deba@2306
  1309
    ///
deba@2306
  1310
    /// \param graph the graph the algorithm will run on.
deba@2306
  1311
    /// \param visitor The visitor of the algorithm.
deba@2306
  1312
    ///
deba@2306
  1313
    BfsVisit(const Graph& graph, Visitor& visitor) 
deba@2306
  1314
      : _graph(&graph), _visitor(&visitor),
deba@2306
  1315
	_reached(0), local_reached(false) {}
deba@2306
  1316
    
deba@2306
  1317
    /// \brief Destructor.
deba@2306
  1318
    ///
deba@2306
  1319
    /// Destructor.
deba@2306
  1320
    ~BfsVisit() {
deba@2306
  1321
      if(local_reached) delete _reached;
deba@2306
  1322
    }
deba@2306
  1323
deba@2306
  1324
    /// \brief Sets the map indicating if a node is reached.
deba@2306
  1325
    ///
deba@2306
  1326
    /// Sets the map indicating if a node is reached.
deba@2306
  1327
    /// If you don't use this function before calling \ref run(),
deba@2306
  1328
    /// it will allocate one. The destuctor deallocates this
deba@2306
  1329
    /// automatically allocated map, of course.
deba@2306
  1330
    /// \return <tt> (*this) </tt>
deba@2306
  1331
    BfsVisit &reachedMap(ReachedMap &m) {
deba@2306
  1332
      if(local_reached) {
deba@2306
  1333
	delete _reached;
deba@2306
  1334
	local_reached = false;
deba@2306
  1335
      }
deba@2306
  1336
      _reached = &m;
deba@2306
  1337
      return *this;
deba@2306
  1338
    }
deba@2306
  1339
deba@2306
  1340
  public:
deba@2306
  1341
    /// \name Execution control
deba@2306
  1342
    /// The simplest way to execute the algorithm is to use
deba@2306
  1343
    /// one of the member functions called \c run(...).
deba@2306
  1344
    /// \n
deba@2306
  1345
    /// If you need more control on the execution,
deba@2306
  1346
    /// first you must call \ref init(), then you can adda source node
deba@2306
  1347
    /// with \ref addSource().
deba@2306
  1348
    /// Finally \ref start() will perform the actual path
deba@2306
  1349
    /// computation.
deba@2306
  1350
deba@2306
  1351
    /// @{
deba@2306
  1352
    /// \brief Initializes the internal data structures.
deba@2306
  1353
    ///
deba@2306
  1354
    /// Initializes the internal data structures.
deba@2306
  1355
    ///
deba@2306
  1356
    void init() {
deba@2306
  1357
      create_maps();
deba@2306
  1358
      _list.resize(countNodes(*_graph));
deba@2306
  1359
      _list_front = _list_back = -1;
deba@2306
  1360
      for (NodeIt u(*_graph) ; u != INVALID ; ++u) {
deba@2306
  1361
	_reached->set(u, false);
deba@2306
  1362
      }
deba@2306
  1363
    }
deba@2306
  1364
    
deba@2306
  1365
    /// \brief Adds a new source node.
deba@2306
  1366
    ///
deba@2306
  1367
    /// Adds a new source node to the set of nodes to be processed.
deba@2306
  1368
    void addSource(Node s) {
deba@2306
  1369
      if(!(*_reached)[s]) {
deba@2306
  1370
	  _reached->set(s,true);
deba@2306
  1371
	  _visitor->start(s);
deba@2306
  1372
	  _visitor->reach(s);
deba@2306
  1373
          _list[++_list_back] = s;
deba@2306
  1374
	}
deba@2306
  1375
    }
deba@2306
  1376
    
deba@2306
  1377
    /// \brief Processes the next node.
deba@2306
  1378
    ///
deba@2306
  1379
    /// Processes the next node.
deba@2306
  1380
    ///
deba@2306
  1381
    /// \return The processed node.
deba@2306
  1382
    ///
deba@2306
  1383
    /// \pre The queue must not be empty!
deba@2306
  1384
    Node processNextNode() { 
deba@2306
  1385
      Node n = _list[++_list_front];
deba@2306
  1386
      _visitor->process(n);
deba@2306
  1387
      Edge e;
deba@2306
  1388
      for (_graph->firstOut(e, n); e != INVALID; _graph->nextOut(e)) {
deba@2306
  1389
        Node m = _graph->target(e);
deba@2306
  1390
        if (!(*_reached)[m]) {
deba@2306
  1391
          _visitor->discover(e);
deba@2306
  1392
          _visitor->reach(m);
deba@2306
  1393
          _reached->set(m, true);
deba@2306
  1394
          _list[++_list_back] = m;
deba@2306
  1395
        } else {
deba@2306
  1396
          _visitor->examine(e);
deba@2306
  1397
        }
deba@2306
  1398
      }
deba@2306
  1399
      return n;
deba@2306
  1400
    }
deba@2306
  1401
deba@2306
  1402
    /// \brief Processes the next node.
deba@2306
  1403
    ///
deba@2306
  1404
    /// Processes the next node. And checks that the given target node
deba@2306
  1405
    /// is reached. If the target node is reachable from the processed
deba@2306
  1406
    /// node then the reached parameter will be set true. The reached
deba@2306
  1407
    /// parameter should be initially false.
deba@2306
  1408
    ///
deba@2306
  1409
    /// \param target The target node.
deba@2386
  1410
    /// \retval reach Indicates that the target node is reached.
deba@2306
  1411
    /// \return The processed node.
deba@2306
  1412
    ///
deba@2306
  1413
    /// \warning The queue must not be empty!
deba@2386
  1414
    Node processNextNode(Node target, bool& reach) {
deba@2306
  1415
      Node n = _list[++_list_front];
deba@2306
  1416
      _visitor->process(n);
deba@2306
  1417
      Edge e;
deba@2306
  1418
      for (_graph->firstOut(e, n); e != INVALID; _graph->nextOut(e)) {
deba@2306
  1419
        Node m = _graph->target(e);
deba@2306
  1420
        if (!(*_reached)[m]) {
deba@2306
  1421
          _visitor->discover(e);
deba@2306
  1422
          _visitor->reach(m);
deba@2306
  1423
          _reached->set(m, true);
deba@2306
  1424
          _list[++_list_back] = m;
deba@2386
  1425
          reach = reach || (target == m);
deba@2306
  1426
        } else {
deba@2306
  1427
          _visitor->examine(e);
deba@2306
  1428
        }
deba@2306
  1429
      }
deba@2306
  1430
      return n;
deba@2306
  1431
    }
deba@2306
  1432
deba@2306
  1433
    /// \brief Processes the next node.
deba@2306
  1434
    ///
deba@2306
  1435
    /// Processes the next node. And checks that at least one of
deba@2306
  1436
    /// reached node has true value in the \c nm nodemap. If one node
deba@2306
  1437
    /// with true value is reachable from the processed node then the
deba@2306
  1438
    /// reached parameter will be set true. The reached parameter
deba@2306
  1439
    /// should be initially false.
deba@2306
  1440
    ///
alpar@2350
  1441
    /// \param nm The nodemaps of possible targets.
deba@2306
  1442
    /// \retval reached Indicates that one of the target nodes is reached.
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@2386
  1447
    Node processNextNode(const NM& nm, bool& reach) {
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@2386
  1458
          reach = reach || nm[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@2386
  1506
      while (!emptyQueue() && !reach) { 
deba@2386
  1507
	processNextNode(dest, reach);
deba@2306
  1508
      }
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@2307
  1519
    ///algorithm will stop when it reached a node \c v with
alpar@2350
  1520
    /// <tt>nm[v]</tt> true.
deba@2306
  1521
    template <typename NM>
deba@2306
  1522
    void start(const NM &nm) {
deba@2386
  1523
      bool reach = false;
deba@2386
  1524
      while (!emptyQueue() && !reach) {
deba@2386
  1525
        processNextNode(nm, reach);
deba@2306
  1526
      }
deba@2306
  1527
    }
deba@2306
  1528
deba@2306
  1529
    /// \brief Runs %BFSVisit algorithm from node \c s.
deba@2306
  1530
    ///
deba@2306
  1531
    /// This method runs the %BFS algorithm from a root node \c s.
deba@2306
  1532
    /// \note b.run(s) is just a shortcut of the following code.
deba@2306
  1533
    ///\code
deba@2306
  1534
    ///   b.init();
deba@2306
  1535
    ///   b.addSource(s);
deba@2306
  1536
    ///   b.start();
deba@2306
  1537
    ///\endcode
deba@2306
  1538
    void run(Node s) {
deba@2306
  1539
      init();
deba@2306
  1540
      addSource(s);
deba@2306
  1541
      start();
deba@2306
  1542
    }
deba@2306
  1543
deba@2306
  1544
    /// \brief Runs %BFSVisit algorithm to visit all nodes in the graph.
deba@2306
  1545
    ///    
deba@2306
  1546
    /// This method runs the %BFS algorithm in order to
deba@2306
  1547
    /// compute the %BFS path to each node. The algorithm computes
deba@2306
  1548
    /// - The %BFS tree.
deba@2306
  1549
    /// - The distance of each node from the root in the %BFS tree.
deba@2306
  1550
    ///
deba@2306
  1551
    ///\note b.run() is just a shortcut of the following code.
deba@2306
  1552
    ///\code
deba@2306
  1553
    ///  b.init();
deba@2306
  1554
    ///  for (NodeIt it(graph); it != INVALID; ++it) {
deba@2306
  1555
    ///    if (!b.reached(it)) {
deba@2306
  1556
    ///      b.addSource(it);
deba@2306
  1557
    ///      b.start();
deba@2306
  1558
    ///    }
deba@2306
  1559
    ///  }
deba@2306
  1560
    ///\endcode
deba@2306
  1561
    void run() {
deba@2306
  1562
      init();
deba@2306
  1563
      for (NodeIt it(*_graph); it != INVALID; ++it) {
deba@2306
  1564
        if (!reached(it)) {
deba@2306
  1565
          addSource(it);
deba@2306
  1566
          start();
deba@2306
  1567
        }
deba@2306
  1568
      }
deba@2306
  1569
    }
deba@2306
  1570
    ///@}
deba@2306
  1571
deba@2306
  1572
    /// \name Query Functions
deba@2306
  1573
    /// The result of the %BFS algorithm can be obtained using these
deba@2306
  1574
    /// functions.\n
deba@2306
  1575
    /// Before the use of these functions,
deba@2306
  1576
    /// either run() or start() must be called.
deba@2306
  1577
    ///@{
deba@2306
  1578
deba@2306
  1579
    /// \brief Checks if a node is reachable from the root.
deba@2306
  1580
    ///
deba@2306
  1581
    /// Returns \c true if \c v is reachable from the root(s).
deba@2306
  1582
    /// \warning The source nodes are inditated as unreachable.
deba@2306
  1583
    /// \pre Either \ref run() or \ref start()
deba@2306
  1584
    /// must be called before using this function.
deba@2306
  1585
    ///
deba@2306
  1586
    bool reached(Node v) { return (*_reached)[v]; }
deba@2306
  1587
    ///@}
deba@2306
  1588
  };
deba@2306
  1589
alpar@921
  1590
} //END OF NAMESPACE LEMON
alpar@774
  1591
alpar@774
  1592
#endif
alpar@774
  1593