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