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