lemon/bfs.h
author deba
Fri, 04 Nov 2005 13:20:24 +0000
changeset 1761 896464fe9fbb
parent 1755 bf267b301a5e
child 1763 49045f2d28d4
permissions -rw-r--r--
Hiding :) todos
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/bfs.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_BFS_H
alpar@921
    18
#define LEMON_BFS_H
alpar@774
    19
alpar@774
    20
///\ingroup flowalgs
alpar@774
    21
///\file
alpar@774
    22
///\brief Bfs algorithm.
alpar@774
    23
alpar@1218
    24
#include <lemon/list_graph.h>
alpar@1218
    25
#include <lemon/graph_utils.h>
alpar@921
    26
#include <lemon/invalid.h>
alpar@1218
    27
#include <lemon/error.h>
alpar@1218
    28
#include <lemon/maps.h>
alpar@774
    29
alpar@921
    30
namespace lemon {
alpar@774
    31
alpar@774
    32
alpar@1218
    33
  
alpar@1218
    34
  ///Default traits class of Bfs class.
alpar@1218
    35
alpar@1218
    36
  ///Default traits class of Bfs class.
alpar@1218
    37
  ///\param GR Graph type.
alpar@1218
    38
  template<class GR>
alpar@1218
    39
  struct BfsDefaultTraits
alpar@1218
    40
  {
alpar@1218
    41
    ///The graph type the algorithm runs on. 
alpar@1218
    42
    typedef GR Graph;
alpar@1218
    43
    ///\brief The type of the map that stores the last
alpar@1218
    44
    ///edges of the shortest paths.
alpar@1218
    45
    /// 
alpar@1218
    46
    ///The type of the map that stores the last
alpar@1218
    47
    ///edges of the shortest paths.
alpar@1218
    48
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
    49
    ///
alpar@1218
    50
    typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
alpar@1218
    51
    ///Instantiates a PredMap.
alpar@1218
    52
 
alpar@1218
    53
    ///This function instantiates a \ref PredMap. 
alpar@1218
    54
    ///\param G is the graph, to which we would like to define the PredMap.
alpar@1218
    55
    ///\todo The graph alone may be insufficient to initialize
alpar@1218
    56
    static PredMap *createPredMap(const GR &G) 
alpar@1218
    57
    {
alpar@1218
    58
      return new PredMap(G);
alpar@1218
    59
    }
alpar@1218
    60
    ///The type of the map that indicates which nodes are processed.
alpar@1218
    61
 
alpar@1218
    62
    ///The type of the map that indicates which nodes are processed.
alpar@1218
    63
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
    64
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
    65
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
    66
    ///Instantiates a ProcessedMap.
alpar@1218
    67
 
alpar@1218
    68
    ///This function instantiates a \ref ProcessedMap. 
alpar@1536
    69
    ///\param g is the graph, to which
alpar@1218
    70
    ///we would like to define the \ref ProcessedMap
alpar@1536
    71
#ifdef DOXYGEN
alpar@1536
    72
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
    73
#else
alpar@1367
    74
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
    75
#endif
alpar@1218
    76
    {
alpar@1218
    77
      return new ProcessedMap();
alpar@1218
    78
    }
alpar@1218
    79
    ///The type of the map that indicates which nodes are reached.
alpar@1218
    80
 
alpar@1218
    81
    ///The type of the map that indicates which nodes are reached.
alpar@1218
    82
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
    83
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
    84
    typedef typename Graph::template NodeMap<bool> ReachedMap;
alpar@1218
    85
    ///Instantiates a ReachedMap.
alpar@1218
    86
 
alpar@1218
    87
    ///This function instantiates a \ref ReachedMap. 
alpar@1218
    88
    ///\param G is the graph, to which
alpar@1218
    89
    ///we would like to define the \ref ReachedMap.
alpar@1218
    90
    static ReachedMap *createReachedMap(const GR &G)
alpar@1218
    91
    {
alpar@1218
    92
      return new ReachedMap(G);
alpar@1218
    93
    }
alpar@1218
    94
    ///The type of the map that stores the dists of the nodes.
alpar@1218
    95
 
alpar@1218
    96
    ///The type of the map that stores the dists of the nodes.
alpar@1218
    97
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
    98
    ///
alpar@1218
    99
    typedef typename Graph::template NodeMap<int> DistMap;
alpar@1218
   100
    ///Instantiates a DistMap.
alpar@1218
   101
 
alpar@1218
   102
    ///This function instantiates a \ref DistMap. 
alpar@1218
   103
    ///\param G is the graph, to which we would like to define the \ref DistMap
alpar@1218
   104
    static DistMap *createDistMap(const GR &G)
alpar@1218
   105
    {
alpar@1218
   106
      return new DistMap(G);
alpar@1218
   107
    }
alpar@1218
   108
  };
alpar@1218
   109
  
alpar@781
   110
  ///%BFS algorithm class.
alpar@1218
   111
  
alpar@1218
   112
  ///\ingroup flowalgs
alpar@1218
   113
  ///This class provides an efficient implementation of the %BFS algorithm.
alpar@774
   114
  ///
alpar@1218
   115
  ///\param GR The graph type the algorithm runs on. The default value is
alpar@1218
   116
  ///\ref ListGraph. The value of GR is not used directly by Bfs, it
alpar@1218
   117
  ///is only passed to \ref BfsDefaultTraits.
alpar@1218
   118
  ///\param TR Traits class to set various data types used by the algorithm.
alpar@1218
   119
  ///The default traits class is
alpar@1218
   120
  ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
alpar@1218
   121
  ///See \ref BfsDefaultTraits for the documentation of
alpar@1218
   122
  ///a Bfs traits class.
alpar@1218
   123
  ///
jacint@1270
   124
  ///\author Alpar Juttner
alpar@774
   125
alpar@774
   126
#ifdef DOXYGEN
alpar@1218
   127
  template <typename GR,
alpar@1218
   128
	    typename TR>
alpar@774
   129
#else
alpar@1218
   130
  template <typename GR=ListGraph,
alpar@1218
   131
	    typename TR=BfsDefaultTraits<GR> >
alpar@774
   132
#endif
alpar@1218
   133
  class Bfs {
alpar@774
   134
  public:
alpar@1218
   135
    /**
alpar@1218
   136
     * \brief \ref Exception for uninitialized parameters.
alpar@1218
   137
     *
alpar@1218
   138
     * This error represents problems in the initialization
alpar@1218
   139
     * of the parameters of the algorithms.
alpar@1218
   140
     */
alpar@1218
   141
    class UninitializedParameter : public lemon::UninitializedParameter {
alpar@1218
   142
    public:
alpar@1218
   143
      virtual const char* exceptionName() const {
alpar@1218
   144
	return "lemon::Bfs::UninitializedParameter";
alpar@1218
   145
      }
alpar@1218
   146
    };
alpar@1218
   147
alpar@1218
   148
    typedef TR Traits;
alpar@774
   149
    ///The type of the underlying graph.
alpar@1218
   150
    typedef typename TR::Graph Graph;
alpar@911
   151
    ///\e
alpar@774
   152
    typedef typename Graph::Node Node;
alpar@911
   153
    ///\e
alpar@774
   154
    typedef typename Graph::NodeIt NodeIt;
alpar@911
   155
    ///\e
alpar@774
   156
    typedef typename Graph::Edge Edge;
alpar@911
   157
    ///\e
alpar@774
   158
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@774
   159
    
alpar@774
   160
    ///\brief The type of the map that stores the last
alpar@774
   161
    ///edges of the shortest paths.
alpar@1218
   162
    typedef typename TR::PredMap PredMap;
alpar@1218
   163
    ///The type of the map indicating which nodes are reached.
alpar@1218
   164
    typedef typename TR::ReachedMap ReachedMap;
alpar@1218
   165
    ///The type of the map indicating which nodes are processed.
alpar@1218
   166
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@774
   167
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   168
    typedef typename TR::DistMap DistMap;
alpar@774
   169
  private:
alpar@802
   170
    /// Pointer to the underlying graph.
alpar@774
   171
    const Graph *G;
alpar@802
   172
    ///Pointer to the map of predecessors edges.
alpar@1218
   173
    PredMap *_pred;
alpar@1218
   174
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
alpar@1218
   175
    bool local_pred;
alpar@802
   176
    ///Pointer to the map of distances.
alpar@1218
   177
    DistMap *_dist;
alpar@1218
   178
    ///Indicates if \ref _dist is locally allocated (\c true) or not.
alpar@1218
   179
    bool local_dist;
alpar@1218
   180
    ///Pointer to the map of reached status of the nodes.
alpar@1218
   181
    ReachedMap *_reached;
alpar@1218
   182
    ///Indicates if \ref _reached is locally allocated (\c true) or not.
alpar@1218
   183
    bool local_reached;
alpar@1218
   184
    ///Pointer to the map of processed status of the nodes.
alpar@1218
   185
    ProcessedMap *_processed;
alpar@1218
   186
    ///Indicates if \ref _processed is locally allocated (\c true) or not.
alpar@1218
   187
    bool local_processed;
alpar@774
   188
alpar@1218
   189
    std::vector<typename Graph::Node> _queue;
alpar@1218
   190
    int _queue_head,_queue_tail,_queue_next_dist;
alpar@1218
   191
    int _curr_dist;
alpar@774
   192
alpar@1218
   193
    ///Creates the maps if necessary.
alpar@1218
   194
    
alpar@1218
   195
    ///\todo Error if \c G are \c NULL.
alpar@1218
   196
    ///\todo Better memory allocation (instead of new).
alpar@1218
   197
    void create_maps() 
alpar@774
   198
    {
alpar@1218
   199
      if(!_pred) {
alpar@1218
   200
	local_pred = true;
alpar@1218
   201
	_pred = Traits::createPredMap(*G);
alpar@774
   202
      }
alpar@1218
   203
      if(!_dist) {
alpar@1218
   204
	local_dist = true;
alpar@1218
   205
	_dist = Traits::createDistMap(*G);
alpar@774
   206
      }
alpar@1218
   207
      if(!_reached) {
alpar@1218
   208
	local_reached = true;
alpar@1218
   209
	_reached = Traits::createReachedMap(*G);
alpar@1218
   210
      }
alpar@1218
   211
      if(!_processed) {
alpar@1218
   212
	local_processed = true;
alpar@1218
   213
	_processed = Traits::createProcessedMap(*G);
alpar@774
   214
      }
alpar@774
   215
    }
deba@1710
   216
deba@1710
   217
  protected:
alpar@774
   218
    
deba@1710
   219
    Bfs() {}
deba@1710
   220
    
deba@1710
   221
  public:
alpar@1218
   222
 
deba@1710
   223
    typedef Bfs Create;
deba@1710
   224
alpar@1218
   225
    ///\name Named template parameters
alpar@1218
   226
alpar@1218
   227
    ///@{
alpar@1218
   228
alpar@1218
   229
    template <class T>
alpar@1218
   230
    struct DefPredMapTraits : public Traits {
alpar@1218
   231
      typedef T PredMap;
alpar@1218
   232
      static PredMap *createPredMap(const Graph &G) 
alpar@1218
   233
      {
alpar@1218
   234
	throw UninitializedParameter();
alpar@1218
   235
      }
alpar@1218
   236
    };
alpar@1218
   237
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@1218
   238
alpar@1218
   239
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@1218
   240
    ///
alpar@1218
   241
    template <class T>
deba@1709
   242
    struct DefPredMap : public Bfs< Graph, DefPredMapTraits<T> > { 
deba@1709
   243
      typedef Bfs< Graph, DefPredMapTraits<T> > Create;
deba@1709
   244
    };
alpar@1218
   245
    
alpar@1218
   246
    template <class T>
alpar@1218
   247
    struct DefDistMapTraits : public Traits {
alpar@1218
   248
      typedef T DistMap;
alpar@1218
   249
      static DistMap *createDistMap(const Graph &G) 
alpar@1218
   250
      {
alpar@1218
   251
	throw UninitializedParameter();
alpar@1218
   252
      }
alpar@1218
   253
    };
alpar@1218
   254
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@1218
   255
alpar@1218
   256
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@1218
   257
    ///
alpar@1218
   258
    template <class T>
deba@1709
   259
    struct DefDistMap : public Bfs< Graph, DefDistMapTraits<T> > { 
deba@1709
   260
      typedef Bfs< Graph, DefDistMapTraits<T> > Create;
deba@1709
   261
    };
alpar@1218
   262
    
alpar@1218
   263
    template <class T>
alpar@1218
   264
    struct DefReachedMapTraits : public Traits {
alpar@1218
   265
      typedef T ReachedMap;
alpar@1218
   266
      static ReachedMap *createReachedMap(const Graph &G) 
alpar@1218
   267
      {
alpar@1218
   268
	throw UninitializedParameter();
alpar@1218
   269
      }
alpar@1218
   270
    };
alpar@1218
   271
    ///\ref named-templ-param "Named parameter" for setting ReachedMap type
alpar@1218
   272
alpar@1218
   273
    ///\ref named-templ-param "Named parameter" for setting ReachedMap type
alpar@1218
   274
    ///
alpar@1218
   275
    template <class T>
deba@1709
   276
    struct DefReachedMap : public Bfs< Graph, DefReachedMapTraits<T> > { 
deba@1709
   277
      typedef Bfs< Graph, DefReachedMapTraits<T> > Create;
deba@1709
   278
    };
alpar@1218
   279
    
alpar@1218
   280
    template <class T>
alpar@1218
   281
    struct DefProcessedMapTraits : public Traits {
alpar@1218
   282
      typedef T ProcessedMap;
alpar@1218
   283
      static ProcessedMap *createProcessedMap(const Graph &G) 
alpar@1218
   284
      {
alpar@1218
   285
	throw UninitializedParameter();
alpar@1218
   286
      }
alpar@1218
   287
    };
alpar@1218
   288
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
alpar@1218
   289
alpar@1218
   290
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
alpar@1218
   291
    ///
alpar@1218
   292
    template <class T>
deba@1709
   293
    struct DefProcessedMap : public Bfs< Graph, DefProcessedMapTraits<T> > {
deba@1709
   294
      typedef Bfs< Graph, DefProcessedMapTraits<T> > Create;
deba@1709
   295
    };
alpar@1218
   296
    
alpar@1218
   297
    struct DefGraphProcessedMapTraits : public Traits {
alpar@1218
   298
      typedef typename Graph::template NodeMap<bool> ProcessedMap;
alpar@1218
   299
      static ProcessedMap *createProcessedMap(const Graph &G) 
alpar@1218
   300
      {
alpar@1218
   301
	return new ProcessedMap(G);
alpar@1218
   302
      }
alpar@1218
   303
    };
alpar@1218
   304
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   305
    ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
alpar@1218
   306
    ///
alpar@1218
   307
    ///\ref named-templ-param "Named parameter"
alpar@1218
   308
    ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
jacint@1270
   309
    ///If you don't set it explicitly, it will be automatically allocated.
alpar@1218
   310
    template <class T>
deba@1709
   311
    struct DefProcessedMapToBeDefaultMap :
deba@1709
   312
      public Bfs< Graph, DefGraphProcessedMapTraits> { 
deba@1709
   313
      typedef Bfs< Graph, DefGraphProcessedMapTraits> Create;
deba@1709
   314
    };
alpar@1218
   315
    
alpar@1218
   316
    ///@}
alpar@1218
   317
alpar@1218
   318
  public:      
alpar@1218
   319
    
alpar@802
   320
    ///Constructor.
alpar@802
   321
    
alpar@802
   322
    ///\param _G the graph the algorithm will run on.
alpar@911
   323
    ///
alpar@774
   324
    Bfs(const Graph& _G) :
alpar@774
   325
      G(&_G),
alpar@1218
   326
      _pred(NULL), local_pred(false),
alpar@1218
   327
      _dist(NULL), local_dist(false),
alpar@1218
   328
      _reached(NULL), local_reached(false),
alpar@1218
   329
      _processed(NULL), local_processed(false)
alpar@774
   330
    { }
alpar@774
   331
    
alpar@802
   332
    ///Destructor.
alpar@774
   333
    ~Bfs() 
alpar@774
   334
    {
alpar@1218
   335
      if(local_pred) delete _pred;
alpar@1218
   336
      if(local_dist) delete _dist;
alpar@1218
   337
      if(local_reached) delete _reached;
alpar@1218
   338
      if(local_processed) delete _processed;
alpar@774
   339
    }
alpar@774
   340
alpar@774
   341
    ///Sets the map storing the predecessor edges.
alpar@774
   342
alpar@774
   343
    ///Sets the map storing the predecessor edges.
alpar@774
   344
    ///If you don't use this function before calling \ref run(),
jacint@1270
   345
    ///it will allocate one. The destructor deallocates this
alpar@774
   346
    ///automatically allocated map, of course.
alpar@774
   347
    ///\return <tt> (*this) </tt>
alpar@1218
   348
    Bfs &predMap(PredMap &m) 
alpar@774
   349
    {
alpar@1218
   350
      if(local_pred) {
alpar@1218
   351
	delete _pred;
alpar@1218
   352
	local_pred=false;
alpar@774
   353
      }
alpar@1218
   354
      _pred = &m;
alpar@774
   355
      return *this;
alpar@774
   356
    }
alpar@774
   357
alpar@1218
   358
    ///Sets the map indicating the reached nodes.
alpar@774
   359
alpar@1218
   360
    ///Sets the map indicating the reached nodes.
alpar@774
   361
    ///If you don't use this function before calling \ref run(),
jacint@1270
   362
    ///it will allocate one. The destructor deallocates this
alpar@774
   363
    ///automatically allocated map, of course.
alpar@774
   364
    ///\return <tt> (*this) </tt>
alpar@1218
   365
    Bfs &reachedMap(ReachedMap &m) 
alpar@774
   366
    {
alpar@1218
   367
      if(local_reached) {
alpar@1218
   368
	delete _reached;
alpar@1218
   369
	local_reached=false;
alpar@774
   370
      }
alpar@1218
   371
      _reached = &m;
alpar@774
   372
      return *this;
alpar@774
   373
    }
alpar@774
   374
alpar@1218
   375
    ///Sets the map indicating the processed nodes.
alpar@1218
   376
alpar@1218
   377
    ///Sets the map indicating the processed nodes.
alpar@1218
   378
    ///If you don't use this function before calling \ref run(),
jacint@1270
   379
    ///it will allocate one. The destructor deallocates this
alpar@1218
   380
    ///automatically allocated map, of course.
alpar@1218
   381
    ///\return <tt> (*this) </tt>
alpar@1218
   382
    Bfs &processedMap(ProcessedMap &m) 
alpar@1218
   383
    {
alpar@1218
   384
      if(local_processed) {
alpar@1218
   385
	delete _processed;
alpar@1218
   386
	local_processed=false;
alpar@1218
   387
      }
alpar@1218
   388
      _processed = &m;
alpar@1218
   389
      return *this;
alpar@1218
   390
    }
alpar@1218
   391
alpar@774
   392
    ///Sets the map storing the distances calculated by the algorithm.
alpar@774
   393
alpar@774
   394
    ///Sets the map storing the distances calculated by the algorithm.
alpar@774
   395
    ///If you don't use this function before calling \ref run(),
jacint@1270
   396
    ///it will allocate one. The destructor deallocates this
alpar@774
   397
    ///automatically allocated map, of course.
alpar@774
   398
    ///\return <tt> (*this) </tt>
alpar@1218
   399
    Bfs &distMap(DistMap &m) 
alpar@774
   400
    {
alpar@1218
   401
      if(local_dist) {
alpar@1218
   402
	delete _dist;
alpar@1218
   403
	local_dist=false;
alpar@774
   404
      }
alpar@1218
   405
      _dist = &m;
alpar@774
   406
      return *this;
alpar@774
   407
    }
alpar@774
   408
alpar@1218
   409
  public:
alpar@1218
   410
    ///\name Execution control
alpar@1218
   411
    ///The simplest way to execute the algorithm is to use
alpar@1218
   412
    ///one of the member functions called \c run(...).
alpar@1218
   413
    ///\n
alpar@1218
   414
    ///If you need more control on the execution,
alpar@1218
   415
    ///first you must call \ref init(), then you can add several source nodes
alpar@1218
   416
    ///with \ref addSource().
alpar@1218
   417
    ///Finally \ref start() will perform the actual path
alpar@1218
   418
    ///computation.
alpar@1218
   419
alpar@1218
   420
    ///@{
alpar@1218
   421
alpar@1218
   422
    ///Initializes the internal data structures.
alpar@1218
   423
alpar@1218
   424
    ///Initializes the internal data structures.
alpar@1218
   425
    ///
alpar@1218
   426
    void init()
alpar@1218
   427
    {
alpar@1218
   428
      create_maps();
alpar@1218
   429
      _queue.resize(countNodes(*G));
alpar@1218
   430
      _queue_head=_queue_tail=0;
alpar@1218
   431
      _curr_dist=1;
alpar@774
   432
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@1218
   433
	_pred->set(u,INVALID);
alpar@1218
   434
	_reached->set(u,false);
alpar@1218
   435
	_processed->set(u,false);
alpar@774
   436
      }
alpar@774
   437
    }
alpar@774
   438
    
alpar@1218
   439
    ///Adds a new source node.
alpar@774
   440
alpar@1218
   441
    ///Adds a new source node to the set of nodes to be processed.
alpar@1218
   442
    ///
alpar@1218
   443
    void addSource(Node s)
alpar@1218
   444
    {
alpar@1218
   445
      if(!(*_reached)[s])
alpar@1218
   446
	{
alpar@1218
   447
	  _reached->set(s,true);
alpar@1218
   448
	  _pred->set(s,INVALID);
alpar@1218
   449
	  _dist->set(s,0);
alpar@1218
   450
	  _queue[_queue_head++]=s;
alpar@1218
   451
	  _queue_next_dist=_queue_head;
alpar@1218
   452
	}
alpar@1218
   453
    }
alpar@1218
   454
    
alpar@1218
   455
    ///Processes the next node.
alpar@1218
   456
alpar@1218
   457
    ///Processes the next node.
alpar@1218
   458
    ///
alpar@1516
   459
    ///\return The processed node.
alpar@1516
   460
    ///
alpar@1218
   461
    ///\warning The queue must not be empty!
alpar@1516
   462
    Node processNextNode()
alpar@1218
   463
    {
alpar@1218
   464
      if(_queue_tail==_queue_next_dist) {
alpar@1218
   465
	_curr_dist++;
alpar@1218
   466
	_queue_next_dist=_queue_head;
alpar@1218
   467
      }
alpar@1218
   468
      Node n=_queue[_queue_tail++];
alpar@1218
   469
      _processed->set(n,true);
alpar@1218
   470
      Node m;
alpar@1218
   471
      for(OutEdgeIt e(*G,n);e!=INVALID;++e)
alpar@1218
   472
	if(!(*_reached)[m=G->target(e)]) {
alpar@1218
   473
	  _queue[_queue_head++]=m;
alpar@1218
   474
	  _reached->set(m,true);
alpar@1218
   475
	  _pred->set(m,e);
alpar@1218
   476
	  _dist->set(m,_curr_dist);
alpar@1218
   477
	}
alpar@1516
   478
      return n;
alpar@1218
   479
    }
alpar@1218
   480
      
alpar@1665
   481
    ///Next node to be processed.
alpar@1665
   482
alpar@1665
   483
    ///Next node to be processed.
alpar@1665
   484
    ///
alpar@1665
   485
    ///\return The next node to be processed or INVALID if the queue is
alpar@1665
   486
    /// empty.
deba@1694
   487
    Node nextNode()
alpar@1665
   488
    { 
alpar@1665
   489
      return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
alpar@1665
   490
    }
alpar@1665
   491
 
alpar@1218
   492
    ///\brief Returns \c false if there are nodes
alpar@1218
   493
    ///to be processed in the queue
alpar@1218
   494
    ///
alpar@1218
   495
    ///Returns \c false if there are nodes
alpar@1218
   496
    ///to be processed in the queue
alpar@1218
   497
    bool emptyQueue() { return _queue_tail==_queue_head; }
alpar@1218
   498
    ///Returns the number of the nodes to be processed.
alpar@1218
   499
    
alpar@1218
   500
    ///Returns the number of the nodes to be processed in the queue.
alpar@1218
   501
    ///
alpar@1218
   502
    int queueSize() { return _queue_head-_queue_tail; }
alpar@1218
   503
    
alpar@1218
   504
    ///Executes the algorithm.
alpar@1218
   505
alpar@1218
   506
    ///Executes the algorithm.
alpar@1218
   507
    ///
alpar@1218
   508
    ///\pre init() must be called and at least one node should be added
alpar@1218
   509
    ///with addSource() before using this function.
alpar@1218
   510
    ///
alpar@1218
   511
    ///This method runs the %BFS algorithm from the root node(s)
alpar@1218
   512
    ///in order to
alpar@1218
   513
    ///compute the
alpar@1218
   514
    ///shortest path to each node. The algorithm computes
alpar@1218
   515
    ///- The shortest path tree.
alpar@1218
   516
    ///- The distance of each node from the root(s).
alpar@1218
   517
    ///
alpar@1218
   518
    void start()
alpar@1218
   519
    {
alpar@1218
   520
      while ( !emptyQueue() ) processNextNode();
alpar@1218
   521
    }
alpar@1218
   522
    
alpar@1218
   523
    ///Executes the algorithm until \c dest is reached.
alpar@1218
   524
alpar@1218
   525
    ///Executes the algorithm until \c dest is reached.
alpar@1218
   526
    ///
alpar@1218
   527
    ///\pre init() must be called and at least one node should be added
alpar@1218
   528
    ///with addSource() before using this function.
alpar@1218
   529
    ///
alpar@1218
   530
    ///This method runs the %BFS algorithm from the root node(s)
alpar@1218
   531
    ///in order to
alpar@1218
   532
    ///compute the
alpar@1218
   533
    ///shortest path to \c dest. The algorithm computes
alpar@1218
   534
    ///- The shortest path to \c  dest.
alpar@1218
   535
    ///- The distance of \c dest from the root(s).
alpar@1218
   536
    ///
alpar@1218
   537
    void start(Node dest)
alpar@1218
   538
    {
alpar@1218
   539
      while ( !emptyQueue() && _queue[_queue_tail]!=dest ) processNextNode();
alpar@1218
   540
    }
alpar@1218
   541
    
alpar@1218
   542
    ///Executes the algorithm until a condition is met.
alpar@1218
   543
alpar@1218
   544
    ///Executes the algorithm until a condition is met.
alpar@1218
   545
    ///
alpar@1218
   546
    ///\pre init() must be called and at least one node should be added
alpar@1218
   547
    ///with addSource() before using this function.
alpar@1218
   548
    ///
alpar@1218
   549
    ///\param nm must be a bool (or convertible) node map. The algorithm
alpar@1218
   550
    ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
alpar@1218
   551
    template<class NM>
deba@1755
   552
    void start(const NM &nm)
deba@1755
   553
    {
deba@1755
   554
      while ( !emptyQueue() && !nm[_queue[_queue_tail]] ) processNextNode();
deba@1755
   555
    }
alpar@1218
   556
    
alpar@1218
   557
    ///Runs %BFS algorithm from node \c s.
alpar@1218
   558
    
alpar@1218
   559
    ///This method runs the %BFS algorithm from a root node \c s
alpar@1218
   560
    ///in order to
alpar@1218
   561
    ///compute the
alpar@1218
   562
    ///shortest path to each node. The algorithm computes
alpar@1218
   563
    ///- The shortest path tree.
alpar@1218
   564
    ///- The distance of each node from the root.
alpar@1218
   565
    ///
alpar@1218
   566
    ///\note d.run(s) is just a shortcut of the following code.
alpar@1218
   567
    ///\code
alpar@1218
   568
    ///  d.init();
alpar@1218
   569
    ///  d.addSource(s);
alpar@1218
   570
    ///  d.start();
alpar@1218
   571
    ///\endcode
alpar@1218
   572
    void run(Node s) {
alpar@1218
   573
      init();
alpar@1218
   574
      addSource(s);
alpar@1218
   575
      start();
alpar@1218
   576
    }
alpar@1218
   577
    
alpar@1218
   578
    ///Finds the shortest path between \c s and \c t.
alpar@1218
   579
    
alpar@1218
   580
    ///Finds the shortest path between \c s and \c t.
alpar@1218
   581
    ///
alpar@1218
   582
    ///\return The length of the shortest s---t path if there exists one,
alpar@1218
   583
    ///0 otherwise.
alpar@1218
   584
    ///\note Apart from the return value, d.run(s) is
alpar@1218
   585
    ///just a shortcut of the following code.
alpar@1218
   586
    ///\code
alpar@1218
   587
    ///  d.init();
alpar@1218
   588
    ///  d.addSource(s);
alpar@1218
   589
    ///  d.start(t);
alpar@1218
   590
    ///\endcode
alpar@1218
   591
    int run(Node s,Node t) {
alpar@1218
   592
      init();
alpar@1218
   593
      addSource(s);
alpar@1218
   594
      start(t);
alpar@1218
   595
      return reached(t)?_curr_dist-1+(_queue_tail==_queue_next_dist):0;
alpar@1218
   596
    }
alpar@1218
   597
    
alpar@1218
   598
    ///@}
alpar@1218
   599
alpar@1218
   600
    ///\name Query Functions
alpar@1218
   601
    ///The result of the %BFS algorithm can be obtained using these
alpar@1218
   602
    ///functions.\n
alpar@1218
   603
    ///Before the use of these functions,
alpar@1218
   604
    ///either run() or start() must be called.
alpar@1218
   605
    
alpar@1218
   606
    ///@{
alpar@1218
   607
alpar@1283
   608
    ///Copies the shortest path to \c t into \c p
alpar@1283
   609
    
alpar@1283
   610
    ///This function copies the shortest path to \c t into \c p.
alpar@1536
   611
    ///If \c t is a source itself or unreachable, then it does not
alpar@1283
   612
    ///alter \c p.
alpar@1283
   613
    ///\todo Is it the right way to handle unreachable nodes?
alpar@1283
   614
    ///\return Returns \c true if a path to \c t was actually copied to \c p,
alpar@1283
   615
    ///\c false otherwise.
alpar@1283
   616
    ///\sa DirPath
alpar@1283
   617
    template<class P>
alpar@1283
   618
    bool getPath(P &p,Node t) 
alpar@1283
   619
    {
alpar@1283
   620
      if(reached(t)) {
alpar@1283
   621
	p.clear();
alpar@1283
   622
	typename P::Builder b(p);
alpar@1283
   623
	for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
alpar@1283
   624
	  b.pushFront(pred(t));
alpar@1283
   625
	b.commit();
alpar@1283
   626
	return true;
alpar@1283
   627
      }
alpar@1283
   628
      return false;
alpar@1283
   629
    }
alpar@1283
   630
alpar@1218
   631
    ///The distance of a node from the root(s).
alpar@1218
   632
alpar@1218
   633
    ///Returns the distance of a node from the root(s).
alpar@774
   634
    ///\pre \ref run() must be called before using this function.
alpar@1218
   635
    ///\warning If node \c v in unreachable from the root(s) the return value
jacint@1270
   636
    ///of this function is undefined.
alpar@1218
   637
    int dist(Node v) const { return (*_dist)[v]; }
alpar@774
   638
alpar@1218
   639
    ///Returns the 'previous edge' of the shortest path tree.
alpar@774
   640
alpar@1218
   641
    ///For a node \c v it returns the 'previous edge'
alpar@1218
   642
    ///of the shortest path tree,
alpar@1218
   643
    ///i.e. it returns the last edge of a shortest path from the root(s) to \c
alpar@774
   644
    ///v. It is \ref INVALID
alpar@1218
   645
    ///if \c v is unreachable from the root(s) or \c v is a root. The
alpar@1218
   646
    ///shortest path tree used here is equal to the shortest path tree used in
alpar@1631
   647
    ///\ref predNode().
alpar@1218
   648
    ///\pre Either \ref run() or \ref start() must be called before using
alpar@774
   649
    ///this function.
alpar@1218
   650
    ///\todo predEdge could be a better name.
alpar@1218
   651
    Edge pred(Node v) const { return (*_pred)[v];}
alpar@774
   652
alpar@1218
   653
    ///Returns the 'previous node' of the shortest path tree.
alpar@774
   654
alpar@1218
   655
    ///For a node \c v it returns the 'previous node'
alpar@1218
   656
    ///of the shortest path tree,
alpar@774
   657
    ///i.e. it returns the last but one node from a shortest path from the
alpar@1218
   658
    ///root(a) to \c /v.
alpar@1218
   659
    ///It is INVALID if \c v is unreachable from the root(s) or
alpar@1218
   660
    ///if \c v itself a root.
alpar@1218
   661
    ///The shortest path tree used here is equal to the shortest path
alpar@1631
   662
    ///tree used in \ref pred().
alpar@1218
   663
    ///\pre Either \ref run() or \ref start() must be called before
alpar@774
   664
    ///using this function.
alpar@1218
   665
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@1218
   666
				  G->source((*_pred)[v]); }
alpar@774
   667
    
alpar@774
   668
    ///Returns a reference to the NodeMap of distances.
alpar@1218
   669
alpar@1218
   670
    ///Returns a reference to the NodeMap of distances.
alpar@1218
   671
    ///\pre Either \ref run() or \ref init() must
alpar@774
   672
    ///be called before using this function.
alpar@1218
   673
    const DistMap &distMap() const { return *_dist;}
alpar@774
   674
 
alpar@1218
   675
    ///Returns a reference to the shortest path tree map.
alpar@774
   676
alpar@774
   677
    ///Returns a reference to the NodeMap of the edges of the
alpar@1218
   678
    ///shortest path tree.
alpar@1218
   679
    ///\pre Either \ref run() or \ref init()
alpar@1218
   680
    ///must be called before using this function.
alpar@1218
   681
    const PredMap &predMap() const { return *_pred;}
alpar@774
   682
 
alpar@774
   683
    ///Checks if a node is reachable from the root.
alpar@774
   684
alpar@774
   685
    ///Returns \c true if \c v is reachable from the root.
jacint@1270
   686
    ///\warning The source nodes are indicated as unreached.
alpar@1218
   687
    ///\pre Either \ref run() or \ref start()
alpar@1218
   688
    ///must be called before using this function.
alpar@774
   689
    ///
alpar@1218
   690
    bool reached(Node v) { return (*_reached)[v]; }
alpar@1218
   691
    
alpar@1218
   692
    ///@}
alpar@1218
   693
  };
alpar@1218
   694
alpar@1218
   695
  ///Default traits class of Bfs function.
alpar@1218
   696
alpar@1218
   697
  ///Default traits class of Bfs function.
alpar@1218
   698
  ///\param GR Graph type.
alpar@1218
   699
  template<class GR>
alpar@1218
   700
  struct BfsWizardDefaultTraits
alpar@1218
   701
  {
alpar@1218
   702
    ///The graph type the algorithm runs on. 
alpar@1218
   703
    typedef GR Graph;
alpar@1218
   704
    ///\brief The type of the map that stores the last
alpar@1218
   705
    ///edges of the shortest paths.
alpar@1218
   706
    /// 
alpar@1218
   707
    ///The type of the map that stores the last
alpar@1218
   708
    ///edges of the shortest paths.
alpar@1218
   709
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@774
   710
    ///
alpar@1218
   711
    typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
alpar@1218
   712
    ///Instantiates a PredMap.
alpar@1218
   713
 
alpar@1218
   714
    ///This function instantiates a \ref PredMap. 
alpar@1536
   715
    ///\param g is the graph, to which we would like to define the PredMap.
alpar@1218
   716
    ///\todo The graph alone may be insufficient to initialize
alpar@1536
   717
#ifdef DOXYGEN
alpar@1536
   718
    static PredMap *createPredMap(const GR &g) 
alpar@1536
   719
#else
alpar@1367
   720
    static PredMap *createPredMap(const GR &) 
alpar@1536
   721
#endif
alpar@1218
   722
    {
alpar@1218
   723
      return new PredMap();
alpar@1218
   724
    }
alpar@1218
   725
alpar@1218
   726
    ///The type of the map that indicates which nodes are processed.
alpar@1218
   727
 
alpar@1218
   728
    ///The type of the map that indicates which nodes are processed.
alpar@1218
   729
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
   730
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   731
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
   732
    ///Instantiates a ProcessedMap.
alpar@1218
   733
 
alpar@1218
   734
    ///This function instantiates a \ref ProcessedMap. 
alpar@1536
   735
    ///\param g is the graph, to which
alpar@1218
   736
    ///we would like to define the \ref ProcessedMap
alpar@1536
   737
#ifdef DOXYGEN
alpar@1536
   738
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
   739
#else
alpar@1367
   740
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
   741
#endif
alpar@1218
   742
    {
alpar@1218
   743
      return new ProcessedMap();
alpar@1218
   744
    }
alpar@1218
   745
    ///The type of the map that indicates which nodes are reached.
alpar@1218
   746
 
alpar@1218
   747
    ///The type of the map that indicates which nodes are reached.
alpar@1218
   748
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
   749
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   750
    typedef typename Graph::template NodeMap<bool> ReachedMap;
alpar@1218
   751
    ///Instantiates a ReachedMap.
alpar@1218
   752
 
alpar@1218
   753
    ///This function instantiates a \ref ReachedMap. 
alpar@1218
   754
    ///\param G is the graph, to which
alpar@1218
   755
    ///we would like to define the \ref ReachedMap.
alpar@1218
   756
    static ReachedMap *createReachedMap(const GR &G)
alpar@1218
   757
    {
alpar@1218
   758
      return new ReachedMap(G);
alpar@1218
   759
    }
alpar@1218
   760
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   761
 
alpar@1218
   762
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   763
    ///It must meet the \ref concept::WriteMap "WriteMap" concept.
alpar@1218
   764
    ///
alpar@1218
   765
    typedef NullMap<typename Graph::Node,int> DistMap;
alpar@1218
   766
    ///Instantiates a DistMap.
alpar@1218
   767
 
alpar@1218
   768
    ///This function instantiates a \ref DistMap. 
alpar@1536
   769
    ///\param g is the graph, to which we would like to define the \ref DistMap
alpar@1536
   770
#ifdef DOXYGEN
alpar@1536
   771
    static DistMap *createDistMap(const GR &g)
alpar@1536
   772
#else
alpar@1367
   773
    static DistMap *createDistMap(const GR &)
alpar@1536
   774
#endif
alpar@1218
   775
    {
alpar@1218
   776
      return new DistMap();
alpar@1218
   777
    }
alpar@1218
   778
  };
alpar@1218
   779
  
alpar@1218
   780
  /// Default traits used by \ref BfsWizard
alpar@1218
   781
alpar@1218
   782
  /// To make it easier to use Bfs algorithm
alpar@1218
   783
  ///we have created a wizard class.
alpar@1218
   784
  /// This \ref BfsWizard class needs default traits,
alpar@1218
   785
  ///as well as the \ref Bfs class.
alpar@1218
   786
  /// The \ref BfsWizardBase is a class to be the default traits of the
alpar@1218
   787
  /// \ref BfsWizard class.
alpar@1218
   788
  template<class GR>
alpar@1218
   789
  class BfsWizardBase : public BfsWizardDefaultTraits<GR>
alpar@1218
   790
  {
alpar@1218
   791
alpar@1218
   792
    typedef BfsWizardDefaultTraits<GR> Base;
alpar@1218
   793
  protected:
alpar@1218
   794
    /// Type of the nodes in the graph.
alpar@1218
   795
    typedef typename Base::Graph::Node Node;
alpar@1218
   796
alpar@1218
   797
    /// Pointer to the underlying graph.
alpar@1218
   798
    void *_g;
alpar@1218
   799
    ///Pointer to the map of reached nodes.
alpar@1218
   800
    void *_reached;
alpar@1218
   801
    ///Pointer to the map of processed nodes.
alpar@1218
   802
    void *_processed;
alpar@1218
   803
    ///Pointer to the map of predecessors edges.
alpar@1218
   804
    void *_pred;
alpar@1218
   805
    ///Pointer to the map of distances.
alpar@1218
   806
    void *_dist;
alpar@1218
   807
    ///Pointer to the source node.
alpar@1218
   808
    Node _source;
alpar@1218
   809
    
alpar@1218
   810
    public:
alpar@1218
   811
    /// Constructor.
alpar@1218
   812
    
alpar@1218
   813
    /// This constructor does not require parameters, therefore it initiates
alpar@1218
   814
    /// all of the attributes to default values (0, INVALID).
alpar@1218
   815
    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
alpar@1218
   816
			   _dist(0), _source(INVALID) {}
alpar@1218
   817
alpar@1218
   818
    /// Constructor.
alpar@1218
   819
    
alpar@1218
   820
    /// This constructor requires some parameters,
alpar@1218
   821
    /// listed in the parameters list.
alpar@1218
   822
    /// Others are initiated to 0.
alpar@1218
   823
    /// \param g is the initial value of  \ref _g
alpar@1218
   824
    /// \param s is the initial value of  \ref _source
alpar@1218
   825
    BfsWizardBase(const GR &g, Node s=INVALID) :
alpar@1218
   826
      _g((void *)&g), _reached(0), _processed(0), _pred(0),
alpar@1218
   827
      _dist(0), _source(s) {}
alpar@1218
   828
alpar@1218
   829
  };
alpar@1218
   830
  
alpar@1218
   831
  /// A class to make the usage of Bfs algorithm easier
alpar@1218
   832
alpar@1218
   833
  /// This class is created to make it easier to use Bfs algorithm.
alpar@1218
   834
  /// It uses the functions and features of the plain \ref Bfs,
alpar@1218
   835
  /// but it is much simpler to use it.
alpar@1218
   836
  ///
alpar@1218
   837
  /// Simplicity means that the way to change the types defined
alpar@1218
   838
  /// in the traits class is based on functions that returns the new class
alpar@1218
   839
  /// and not on templatable built-in classes.
alpar@1218
   840
  /// When using the plain \ref Bfs
alpar@1218
   841
  /// the new class with the modified type comes from
alpar@1218
   842
  /// the original class by using the ::
alpar@1218
   843
  /// operator. In the case of \ref BfsWizard only
alpar@1218
   844
  /// a function have to be called and it will
alpar@1218
   845
  /// return the needed class.
alpar@1218
   846
  ///
alpar@1218
   847
  /// It does not have own \ref run method. When its \ref run method is called
alpar@1218
   848
  /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
alpar@1218
   849
  /// method of it.
alpar@1218
   850
  template<class TR>
alpar@1218
   851
  class BfsWizard : public TR
alpar@1218
   852
  {
alpar@1218
   853
    typedef TR Base;
alpar@1218
   854
alpar@1218
   855
    ///The type of the underlying graph.
alpar@1218
   856
    typedef typename TR::Graph Graph;
alpar@1218
   857
    //\e
alpar@1218
   858
    typedef typename Graph::Node Node;
alpar@1218
   859
    //\e
alpar@1218
   860
    typedef typename Graph::NodeIt NodeIt;
alpar@1218
   861
    //\e
alpar@1218
   862
    typedef typename Graph::Edge Edge;
alpar@1218
   863
    //\e
alpar@1218
   864
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@1218
   865
    
alpar@1218
   866
    ///\brief The type of the map that stores
alpar@1218
   867
    ///the reached nodes
alpar@1218
   868
    typedef typename TR::ReachedMap ReachedMap;
alpar@1218
   869
    ///\brief The type of the map that stores
alpar@1218
   870
    ///the processed nodes
alpar@1218
   871
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@1218
   872
    ///\brief The type of the map that stores the last
alpar@1218
   873
    ///edges of the shortest paths.
alpar@1218
   874
    typedef typename TR::PredMap PredMap;
alpar@1218
   875
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   876
    typedef typename TR::DistMap DistMap;
alpar@1218
   877
alpar@1218
   878
public:
alpar@1218
   879
    /// Constructor.
alpar@1218
   880
    BfsWizard() : TR() {}
alpar@1218
   881
alpar@1218
   882
    /// Constructor that requires parameters.
alpar@1218
   883
alpar@1218
   884
    /// Constructor that requires parameters.
alpar@1218
   885
    /// These parameters will be the default values for the traits class.
alpar@1218
   886
    BfsWizard(const Graph &g, Node s=INVALID) :
alpar@1218
   887
      TR(g,s) {}
alpar@1218
   888
alpar@1218
   889
    ///Copy constructor
alpar@1218
   890
    BfsWizard(const TR &b) : TR(b) {}
alpar@1218
   891
alpar@1218
   892
    ~BfsWizard() {}
alpar@1218
   893
alpar@1218
   894
    ///Runs Bfs algorithm from a given node.
alpar@1218
   895
    
alpar@1218
   896
    ///Runs Bfs algorithm from a given node.
alpar@1218
   897
    ///The node can be given by the \ref source function.
alpar@1218
   898
    void run()
alpar@1218
   899
    {
alpar@1218
   900
      if(Base::_source==INVALID) throw UninitializedParameter();
alpar@1218
   901
      Bfs<Graph,TR> alg(*(Graph*)Base::_g);
alpar@1218
   902
      if(Base::_reached)
alpar@1218
   903
	alg.reachedMap(*(ReachedMap*)Base::_reached);
alpar@1218
   904
      if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed);
alpar@1218
   905
      if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred);
alpar@1218
   906
      if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist);
alpar@1218
   907
      alg.run(Base::_source);
alpar@1218
   908
    }
alpar@1218
   909
alpar@1218
   910
    ///Runs Bfs algorithm from the given node.
alpar@1218
   911
alpar@1218
   912
    ///Runs Bfs algorithm from the given node.
alpar@1218
   913
    ///\param s is the given source.
alpar@1218
   914
    void run(Node s)
alpar@1218
   915
    {
alpar@1218
   916
      Base::_source=s;
alpar@1218
   917
      run();
alpar@1218
   918
    }
alpar@1218
   919
alpar@1218
   920
    template<class T>
alpar@1218
   921
    struct DefPredMapBase : public Base {
alpar@1218
   922
      typedef T PredMap;
alpar@1367
   923
      static PredMap *createPredMap(const Graph &) { return 0; };
alpar@1236
   924
      DefPredMapBase(const TR &b) : TR(b) {}
alpar@1218
   925
    };
alpar@1218
   926
    
alpar@1218
   927
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   928
    ///function for setting PredMap
alpar@1218
   929
    ///
alpar@1218
   930
    /// \ref named-templ-param "Named parameter"
alpar@1218
   931
    ///function for setting PredMap
alpar@1218
   932
    ///
alpar@1218
   933
    template<class T>
alpar@1218
   934
    BfsWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@1218
   935
    {
alpar@1218
   936
      Base::_pred=(void *)&t;
alpar@1218
   937
      return BfsWizard<DefPredMapBase<T> >(*this);
alpar@1218
   938
    }
alpar@1218
   939
    
alpar@1218
   940
 
alpar@1218
   941
    template<class T>
alpar@1218
   942
    struct DefReachedMapBase : public Base {
alpar@1218
   943
      typedef T ReachedMap;
alpar@1367
   944
      static ReachedMap *createReachedMap(const Graph &) { return 0; };
alpar@1236
   945
      DefReachedMapBase(const TR &b) : TR(b) {}
alpar@1218
   946
    };
alpar@1218
   947
    
alpar@1218
   948
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   949
    ///function for setting ReachedMap
alpar@1218
   950
    ///
alpar@1218
   951
    /// \ref named-templ-param "Named parameter"
alpar@1218
   952
    ///function for setting ReachedMap
alpar@1218
   953
    ///
alpar@1218
   954
    template<class T>
alpar@1218
   955
    BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
alpar@1218
   956
    {
alpar@1218
   957
      Base::_pred=(void *)&t;
alpar@1218
   958
      return BfsWizard<DefReachedMapBase<T> >(*this);
alpar@1218
   959
    }
alpar@1218
   960
    
alpar@1218
   961
alpar@1218
   962
    template<class T>
alpar@1218
   963
    struct DefProcessedMapBase : public Base {
alpar@1218
   964
      typedef T ProcessedMap;
alpar@1367
   965
      static ProcessedMap *createProcessedMap(const Graph &) { return 0; };
alpar@1236
   966
      DefProcessedMapBase(const TR &b) : TR(b) {}
alpar@1218
   967
    };
alpar@1218
   968
    
alpar@1218
   969
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   970
    ///function for setting ProcessedMap
alpar@1218
   971
    ///
alpar@1218
   972
    /// \ref named-templ-param "Named parameter"
alpar@1218
   973
    ///function for setting ProcessedMap
alpar@1218
   974
    ///
alpar@1218
   975
    template<class T>
alpar@1218
   976
    BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
alpar@1218
   977
    {
alpar@1218
   978
      Base::_pred=(void *)&t;
alpar@1218
   979
      return BfsWizard<DefProcessedMapBase<T> >(*this);
alpar@1218
   980
    }
alpar@1218
   981
    
alpar@1218
   982
   
alpar@1218
   983
    template<class T>
alpar@1218
   984
    struct DefDistMapBase : public Base {
alpar@1218
   985
      typedef T DistMap;
alpar@1367
   986
      static DistMap *createDistMap(const Graph &) { return 0; };
alpar@1236
   987
      DefDistMapBase(const TR &b) : TR(b) {}
alpar@1218
   988
    };
alpar@1218
   989
    
alpar@1218
   990
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   991
    ///function for setting DistMap type
alpar@1218
   992
    ///
alpar@1218
   993
    /// \ref named-templ-param "Named parameter"
alpar@1218
   994
    ///function for setting DistMap type
alpar@1218
   995
    ///
alpar@1218
   996
    template<class T>
alpar@1218
   997
    BfsWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@1218
   998
    {
alpar@1218
   999
      Base::_dist=(void *)&t;
alpar@1218
  1000
      return BfsWizard<DefDistMapBase<T> >(*this);
alpar@1218
  1001
    }
alpar@1218
  1002
    
alpar@1218
  1003
    /// Sets the source node, from which the Bfs algorithm runs.
alpar@1218
  1004
alpar@1218
  1005
    /// Sets the source node, from which the Bfs algorithm runs.
alpar@1218
  1006
    /// \param s is the source node.
alpar@1218
  1007
    BfsWizard<TR> &source(Node s) 
alpar@1218
  1008
    {
alpar@1218
  1009
      Base::_source=s;
alpar@1218
  1010
      return *this;
alpar@1218
  1011
    }
alpar@774
  1012
    
alpar@774
  1013
  };
alpar@774
  1014
  
alpar@1218
  1015
  ///Function type interface for Bfs algorithm.
alpar@1218
  1016
alpar@1218
  1017
  /// \ingroup flowalgs
alpar@1218
  1018
  ///Function type interface for Bfs algorithm.
alpar@1218
  1019
  ///
alpar@1218
  1020
  ///This function also has several
alpar@1218
  1021
  ///\ref named-templ-func-param "named parameters",
alpar@1218
  1022
  ///they are declared as the members of class \ref BfsWizard.
alpar@1218
  1023
  ///The following
alpar@1218
  1024
  ///example shows how to use these parameters.
alpar@1218
  1025
  ///\code
alpar@1218
  1026
  ///  bfs(g,source).predMap(preds).run();
alpar@1218
  1027
  ///\endcode
alpar@1218
  1028
  ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
alpar@1218
  1029
  ///to the end of the parameter list.
alpar@1218
  1030
  ///\sa BfsWizard
alpar@1218
  1031
  ///\sa Bfs
alpar@1218
  1032
  template<class GR>
alpar@1218
  1033
  BfsWizard<BfsWizardBase<GR> >
alpar@1218
  1034
  bfs(const GR &g,typename GR::Node s=INVALID)
alpar@1218
  1035
  {
alpar@1218
  1036
    return BfsWizard<BfsWizardBase<GR> >(g,s);
alpar@1218
  1037
  }
alpar@1218
  1038
alpar@921
  1039
} //END OF NAMESPACE LEMON
alpar@774
  1040
alpar@774
  1041
#endif
alpar@774
  1042