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