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