lemon/belmann_ford.h
author deba
Mon, 19 Dec 2005 09:43:13 +0000
changeset 1864 1788205e36af
parent 1857 2e3a4481901e
permissions -rw-r--r--
Fixing Bellman's name
deba@1699
     1
/* -*- C++ -*-
deba@1699
     2
 * lemon/belmann_ford.h - Part of LEMON, a generic C++ optimization library
deba@1699
     3
 *
deba@1699
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1699
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1699
     6
 *
deba@1699
     7
 * Permission to use, modify and distribute this software is granted
deba@1699
     8
 * provided that this copyright notice appears in all copies. For
deba@1699
     9
 * precise terms see the accompanying LICENSE file.
deba@1699
    10
 *
deba@1699
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1699
    12
 * express or implied, and with no claim as to its suitability for any
deba@1699
    13
 * purpose.
deba@1699
    14
 *
deba@1699
    15
 */
deba@1699
    16
deba@1699
    17
#ifndef LEMON_BELMANN_FORD_H
deba@1699
    18
#define LEMON_BELMANN_FORD_H
deba@1699
    19
deba@1858
    20
/// \ingroup flowalgs
deba@1699
    21
/// \file
deba@1699
    22
/// \brief BelmannFord algorithm.
deba@1699
    23
///
deba@1699
    24
deba@1699
    25
#include <lemon/list_graph.h>
deba@1699
    26
#include <lemon/invalid.h>
deba@1699
    27
#include <lemon/error.h>
deba@1699
    28
#include <lemon/maps.h>
deba@1699
    29
deba@1699
    30
#include <limits>
deba@1699
    31
deba@1699
    32
namespace lemon {
deba@1699
    33
deba@1699
    34
  /// \brief Default OperationTraits for the BelmannFord algorithm class.
deba@1699
    35
  ///  
deba@1699
    36
  /// It defines all computational operations and constants which are
deba@1699
    37
  /// used in the belmann ford algorithm. The default implementation
deba@1699
    38
  /// is based on the numeric_limits class. If the numeric type does not
deba@1699
    39
  /// have infinity value then the maximum value is used as extremal
deba@1699
    40
  /// infinity value.
deba@1699
    41
  template <
deba@1699
    42
    typename Value, 
deba@1699
    43
    bool has_infinity = std::numeric_limits<Value>::has_infinity>
deba@1699
    44
  struct BelmannFordDefaultOperationTraits {
deba@1699
    45
    /// \brief Gives back the zero value of the type.
deba@1699
    46
    static Value zero() {
deba@1699
    47
      return static_cast<Value>(0);
deba@1699
    48
    }
deba@1699
    49
    /// \brief Gives back the positive infinity value of the type.
deba@1699
    50
    static Value infinity() {
deba@1699
    51
      return std::numeric_limits<Value>::infinity();
deba@1699
    52
    }
deba@1699
    53
    /// \brief Gives back the sum of the given two elements.
deba@1699
    54
    static Value plus(const Value& left, const Value& right) {
deba@1699
    55
      return left + right;
deba@1699
    56
    }
deba@1699
    57
    /// \brief Gives back true only if the first value less than the second.
deba@1699
    58
    static bool less(const Value& left, const Value& right) {
deba@1699
    59
      return left < right;
deba@1699
    60
    }
deba@1699
    61
  };
deba@1699
    62
deba@1699
    63
  template <typename Value>
deba@1699
    64
  struct BelmannFordDefaultOperationTraits<Value, false> {
deba@1699
    65
    static Value zero() {
deba@1699
    66
      return static_cast<Value>(0);
deba@1699
    67
    }
deba@1699
    68
    static Value infinity() {
deba@1699
    69
      return std::numeric_limits<Value>::max();
deba@1699
    70
    }
deba@1699
    71
    static Value plus(const Value& left, const Value& right) {
deba@1699
    72
      if (left == infinity() || right == infinity()) return infinity();
deba@1699
    73
      return left + right;
deba@1699
    74
    }
deba@1699
    75
    static bool less(const Value& left, const Value& right) {
deba@1699
    76
      return left < right;
deba@1699
    77
    }
deba@1699
    78
  };
deba@1699
    79
  
deba@1699
    80
  /// \brief Default traits class of BelmannFord class.
deba@1699
    81
  ///
deba@1699
    82
  /// Default traits class of BelmannFord class.
deba@1699
    83
  /// \param _Graph Graph type.
deba@1699
    84
  /// \param _LegthMap Type of length map.
deba@1699
    85
  template<class _Graph, class _LengthMap>
deba@1699
    86
  struct BelmannFordDefaultTraits {
deba@1699
    87
    /// The graph type the algorithm runs on. 
deba@1699
    88
    typedef _Graph Graph;
deba@1699
    89
deba@1699
    90
    /// \brief The type of the map that stores the edge lengths.
deba@1699
    91
    ///
deba@1699
    92
    /// The type of the map that stores the edge lengths.
deba@1699
    93
    /// It must meet the \ref concept::ReadMap "ReadMap" concept.
deba@1699
    94
    typedef _LengthMap LengthMap;
deba@1699
    95
deba@1699
    96
    // The type of the length of the edges.
deba@1699
    97
    typedef typename _LengthMap::Value Value;
deba@1699
    98
deba@1699
    99
    /// \brief Operation traits for belmann-ford algorithm.
deba@1699
   100
    ///
deba@1699
   101
    /// It defines the infinity type on the given Value type
deba@1699
   102
    /// and the used operation.
deba@1699
   103
    /// \see BelmannFordDefaultOperationTraits
deba@1699
   104
    typedef BelmannFordDefaultOperationTraits<Value> OperationTraits;
deba@1699
   105
 
deba@1699
   106
    /// \brief The type of the map that stores the last edges of the 
deba@1699
   107
    /// shortest paths.
deba@1699
   108
    /// 
deba@1699
   109
    /// The type of the map that stores the last
deba@1699
   110
    /// edges of the shortest paths.
deba@1699
   111
    /// It must meet the \ref concept::WriteMap "WriteMap" concept.
deba@1699
   112
    ///
deba@1699
   113
    typedef typename Graph::template NodeMap<typename _Graph::Edge> PredMap;
deba@1699
   114
deba@1699
   115
    /// \brief Instantiates a PredMap.
deba@1699
   116
    /// 
deba@1699
   117
    /// This function instantiates a \ref PredMap. 
deba@1858
   118
    /// \param graph is the graph, to which we would like to define the PredMap.
deba@1699
   119
    static PredMap *createPredMap(const _Graph& graph) {
deba@1699
   120
      return new PredMap(graph);
deba@1699
   121
    }
deba@1699
   122
deba@1699
   123
    /// \brief The type of the map that stores the dists of the nodes.
deba@1699
   124
    ///
deba@1699
   125
    /// The type of the map that stores the dists of the nodes.
deba@1699
   126
    /// It must meet the \ref concept::WriteMap "WriteMap" concept.
deba@1699
   127
    ///
deba@1699
   128
    typedef typename Graph::template NodeMap<typename _LengthMap::Value> 
deba@1699
   129
    DistMap;
deba@1699
   130
deba@1699
   131
    /// \brief Instantiates a DistMap.
deba@1699
   132
    ///
deba@1699
   133
    /// This function instantiates a \ref DistMap. 
deba@1858
   134
    /// \param graph is the graph, to which we would like to define the 
deba@1699
   135
    /// \ref DistMap
deba@1699
   136
    static DistMap *createDistMap(const _Graph& graph) {
deba@1699
   137
      return new DistMap(graph);
deba@1699
   138
    }
deba@1699
   139
deba@1699
   140
  };
deba@1699
   141
  
deba@1754
   142
  /// \brief %BelmannFord algorithm class.
deba@1699
   143
  ///
deba@1699
   144
  /// \ingroup flowalgs
deba@1723
   145
  /// This class provides an efficient implementation of \c Belmann-Ford 
deba@1699
   146
  /// algorithm. The edge lengths are passed to the algorithm using a
deba@1699
   147
  /// \ref concept::ReadMap "ReadMap", so it is easy to change it to any 
deba@1699
   148
  /// kind of length.
deba@1699
   149
  ///
deba@1723
   150
  /// The Belmann-Ford algorithm solves the shortest path from one node
deba@1723
   151
  /// problem when the edges can have negative length but the graph should
deba@1754
   152
  /// not contain cycles with negative sum of length. If we can assume
deba@1723
   153
  /// that all edge is non-negative in the graph then the dijkstra algorithm
deba@1723
   154
  /// should be used rather.
deba@1723
   155
  ///
deba@1723
   156
  /// The complexity of the algorithm is O(n * e).
deba@1723
   157
  ///
deba@1699
   158
  /// The type of the length is determined by the
deba@1699
   159
  /// \ref concept::ReadMap::Value "Value" of the length map.
deba@1699
   160
  ///
deba@1699
   161
  /// \param _Graph The graph type the algorithm runs on. The default value
deba@1699
   162
  /// is \ref ListGraph. The value of _Graph is not used directly by
deba@1699
   163
  /// BelmannFord, it is only passed to \ref BelmannFordDefaultTraits.
deba@1699
   164
  /// \param _LengthMap This read-only EdgeMap determines the lengths of the
deba@1699
   165
  /// edges. The default map type is \ref concept::StaticGraph::EdgeMap 
deba@1699
   166
  /// "Graph::EdgeMap<int>".  The value of _LengthMap is not used directly 
deba@1699
   167
  /// by BelmannFord, it is only passed to \ref BelmannFordDefaultTraits.  
deba@1699
   168
  /// \param _Traits Traits class to set various data types used by the 
deba@1699
   169
  /// algorithm.  The default traits class is \ref BelmannFordDefaultTraits
deba@1699
   170
  /// "BelmannFordDefaultTraits<_Graph,_LengthMap>".  See \ref
deba@1699
   171
  /// BelmannFordDefaultTraits for the documentation of a BelmannFord traits
deba@1699
   172
  /// class.
deba@1699
   173
  ///
deba@1699
   174
  /// \author Balazs Dezso
deba@1699
   175
deba@1710
   176
#ifdef DOXYGEN
deba@1710
   177
  template <typename _Graph, typename _LengthMap, typename _Traits>
deba@1710
   178
#else
deba@1699
   179
  template <typename _Graph=ListGraph,
deba@1699
   180
	    typename _LengthMap=typename _Graph::template EdgeMap<int>,
deba@1699
   181
	    typename _Traits=BelmannFordDefaultTraits<_Graph,_LengthMap> >
deba@1710
   182
#endif
deba@1699
   183
  class BelmannFord {
deba@1699
   184
  public:
deba@1699
   185
    
deba@1699
   186
    /// \brief \ref Exception for uninitialized parameters.
deba@1699
   187
    ///
deba@1699
   188
    /// This error represents problems in the initialization
deba@1699
   189
    /// of the parameters of the algorithms.
deba@1699
   190
deba@1699
   191
    class UninitializedParameter : public lemon::UninitializedParameter {
deba@1699
   192
    public:
deba@1699
   193
      virtual const char* exceptionName() const {
deba@1699
   194
	return "lemon::BelmannFord::UninitializedParameter";
deba@1699
   195
      }
deba@1699
   196
    };
deba@1699
   197
deba@1699
   198
    typedef _Traits Traits;
deba@1699
   199
    ///The type of the underlying graph.
deba@1699
   200
    typedef typename _Traits::Graph Graph;
deba@1699
   201
deba@1699
   202
    typedef typename Graph::Node Node;
deba@1699
   203
    typedef typename Graph::NodeIt NodeIt;
deba@1699
   204
    typedef typename Graph::Edge Edge;
deba@1781
   205
    typedef typename Graph::OutEdgeIt OutEdgeIt;
deba@1699
   206
    
deba@1699
   207
    /// \brief The type of the length of the edges.
deba@1699
   208
    typedef typename _Traits::LengthMap::Value Value;
deba@1699
   209
    /// \brief The type of the map that stores the edge lengths.
deba@1699
   210
    typedef typename _Traits::LengthMap LengthMap;
deba@1699
   211
    /// \brief The type of the map that stores the last
deba@1699
   212
    /// edges of the shortest paths.
deba@1699
   213
    typedef typename _Traits::PredMap PredMap;
deba@1699
   214
    /// \brief The type of the map that stores the dists of the nodes.
deba@1699
   215
    typedef typename _Traits::DistMap DistMap;
deba@1699
   216
    /// \brief The operation traits.
deba@1699
   217
    typedef typename _Traits::OperationTraits OperationTraits;
deba@1699
   218
  private:
deba@1699
   219
    /// Pointer to the underlying graph.
deba@1699
   220
    const Graph *graph;
deba@1699
   221
    /// Pointer to the length map
deba@1699
   222
    const LengthMap *length;
deba@1699
   223
    ///Pointer to the map of predecessors edges.
deba@1699
   224
    PredMap *_pred;
deba@1699
   225
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
deba@1699
   226
    bool local_pred;
deba@1699
   227
    ///Pointer to the map of distances.
deba@1699
   228
    DistMap *_dist;
deba@1699
   229
    ///Indicates if \ref _dist is locally allocated (\c true) or not.
deba@1699
   230
    bool local_dist;
deba@1699
   231
deba@1781
   232
    typedef typename Graph::template NodeMap<bool> MaskMap;
deba@1781
   233
    MaskMap *_mask;
deba@1781
   234
deba@1781
   235
    std::vector<Node> _process;
deba@1781
   236
deba@1699
   237
    /// Creates the maps if necessary.
deba@1699
   238
    void create_maps() {
deba@1699
   239
      if(!_pred) {
deba@1699
   240
	local_pred = true;
deba@1699
   241
	_pred = Traits::createPredMap(*graph);
deba@1699
   242
      }
deba@1699
   243
      if(!_dist) {
deba@1699
   244
	local_dist = true;
deba@1699
   245
	_dist = Traits::createDistMap(*graph);
deba@1699
   246
      }
deba@1781
   247
      _mask = new MaskMap(*graph, false);
deba@1699
   248
    }
deba@1699
   249
    
deba@1699
   250
  public :
deba@1699
   251
 
deba@1710
   252
    typedef BelmannFord Create;
deba@1710
   253
deba@1699
   254
    /// \name Named template parameters
deba@1699
   255
deba@1699
   256
    ///@{
deba@1699
   257
deba@1699
   258
    template <class T>
deba@1699
   259
    struct DefPredMapTraits : public Traits {
deba@1699
   260
      typedef T PredMap;
deba@1710
   261
      static PredMap *createPredMap(const Graph&) {
deba@1699
   262
	throw UninitializedParameter();
deba@1699
   263
      }
deba@1699
   264
    };
deba@1699
   265
deba@1699
   266
    /// \brief \ref named-templ-param "Named parameter" for setting PredMap 
deba@1699
   267
    /// type
deba@1699
   268
    /// \ref named-templ-param "Named parameter" for setting PredMap type
deba@1699
   269
    ///
deba@1699
   270
    template <class T>
deba@1858
   271
    struct DefPredMap 
deba@1858
   272
      : public BelmannFord< Graph, LengthMap, DefPredMapTraits<T> > {
deba@1710
   273
      typedef BelmannFord< Graph, LengthMap, DefPredMapTraits<T> > Create;
deba@1710
   274
    };
deba@1699
   275
    
deba@1699
   276
    template <class T>
deba@1699
   277
    struct DefDistMapTraits : public Traits {
deba@1699
   278
      typedef T DistMap;
deba@1699
   279
      static DistMap *createDistMap(const Graph& graph) {
deba@1699
   280
	throw UninitializedParameter();
deba@1699
   281
      }
deba@1699
   282
    };
deba@1699
   283
deba@1699
   284
    /// \brief \ref named-templ-param "Named parameter" for setting DistMap 
deba@1699
   285
    /// type
deba@1699
   286
    ///
deba@1699
   287
    /// \ref named-templ-param "Named parameter" for setting DistMap type
deba@1699
   288
    ///
deba@1699
   289
    template <class T>
deba@1710
   290
    struct DefDistMap 
deba@1710
   291
      : public BelmannFord< Graph, LengthMap, DefDistMapTraits<T> > {
deba@1710
   292
      typedef BelmannFord< Graph, LengthMap, DefDistMapTraits<T> > Create;
deba@1710
   293
    };
deba@1699
   294
    
deba@1699
   295
    template <class T>
deba@1699
   296
    struct DefOperationTraitsTraits : public Traits {
deba@1699
   297
      typedef T OperationTraits;
deba@1699
   298
    };
deba@1699
   299
    
deba@1699
   300
    /// \brief \ref named-templ-param "Named parameter" for setting 
deba@1699
   301
    /// OperationTraits type
deba@1699
   302
    ///
deba@1710
   303
    /// \ref named-templ-param "Named parameter" for setting OperationTraits
deba@1710
   304
    /// type
deba@1699
   305
    template <class T>
deba@1710
   306
    struct DefOperationTraits
deba@1699
   307
      : public BelmannFord< Graph, LengthMap, DefOperationTraitsTraits<T> > {
deba@1699
   308
      typedef BelmannFord< Graph, LengthMap, DefOperationTraitsTraits<T> >
deba@1710
   309
      Create;
deba@1699
   310
    };
deba@1699
   311
    
deba@1699
   312
    ///@}
deba@1699
   313
deba@1710
   314
  protected:
deba@1710
   315
    
deba@1710
   316
    BelmannFord() {}
deba@1710
   317
deba@1699
   318
  public:      
deba@1699
   319
    
deba@1699
   320
    /// \brief Constructor.
deba@1699
   321
    ///
deba@1699
   322
    /// \param _graph the graph the algorithm will run on.
deba@1699
   323
    /// \param _length the length map used by the algorithm.
deba@1699
   324
    BelmannFord(const Graph& _graph, const LengthMap& _length) :
deba@1699
   325
      graph(&_graph), length(&_length),
deba@1699
   326
      _pred(0), local_pred(false),
deba@1699
   327
      _dist(0), local_dist(false) {}
deba@1699
   328
    
deba@1699
   329
    ///Destructor.
deba@1699
   330
    ~BelmannFord() {
deba@1699
   331
      if(local_pred) delete _pred;
deba@1699
   332
      if(local_dist) delete _dist;
deba@1781
   333
      delete _mask;
deba@1699
   334
    }
deba@1699
   335
deba@1699
   336
    /// \brief Sets the length map.
deba@1699
   337
    ///
deba@1699
   338
    /// Sets the length map.
deba@1699
   339
    /// \return \c (*this)
deba@1699
   340
    BelmannFord &lengthMap(const LengthMap &m) {
deba@1699
   341
      length = &m;
deba@1699
   342
      return *this;
deba@1699
   343
    }
deba@1699
   344
deba@1699
   345
    /// \brief Sets the map storing the predecessor edges.
deba@1699
   346
    ///
deba@1699
   347
    /// Sets the map storing the predecessor edges.
deba@1699
   348
    /// If you don't use this function before calling \ref run(),
deba@1699
   349
    /// it will allocate one. The destuctor deallocates this
deba@1699
   350
    /// automatically allocated map, of course.
deba@1699
   351
    /// \return \c (*this)
deba@1699
   352
    BelmannFord &predMap(PredMap &m) {
deba@1699
   353
      if(local_pred) {
deba@1699
   354
	delete _pred;
deba@1699
   355
	local_pred=false;
deba@1699
   356
      }
deba@1699
   357
      _pred = &m;
deba@1699
   358
      return *this;
deba@1699
   359
    }
deba@1699
   360
deba@1699
   361
    /// \brief Sets the map storing the distances calculated by the algorithm.
deba@1699
   362
    ///
deba@1699
   363
    /// Sets the map storing the distances calculated by the algorithm.
deba@1699
   364
    /// If you don't use this function before calling \ref run(),
deba@1699
   365
    /// it will allocate one. The destuctor deallocates this
deba@1699
   366
    /// automatically allocated map, of course.
deba@1699
   367
    /// \return \c (*this)
deba@1699
   368
    BelmannFord &distMap(DistMap &m) {
deba@1699
   369
      if(local_dist) {
deba@1699
   370
	delete _dist;
deba@1699
   371
	local_dist=false;
deba@1699
   372
      }
deba@1699
   373
      _dist = &m;
deba@1699
   374
      return *this;
deba@1699
   375
    }
deba@1699
   376
deba@1699
   377
    /// \name Execution control
deba@1699
   378
    /// The simplest way to execute the algorithm is to use
deba@1699
   379
    /// one of the member functions called \c run(...).
deba@1699
   380
    /// \n
deba@1699
   381
    /// If you need more control on the execution,
deba@1699
   382
    /// first you must call \ref init(), then you can add several source nodes
deba@1699
   383
    /// with \ref addSource().
deba@1699
   384
    /// Finally \ref start() will perform the actual path
deba@1699
   385
    /// computation.
deba@1699
   386
deba@1699
   387
    ///@{
deba@1699
   388
deba@1699
   389
    /// \brief Initializes the internal data structures.
deba@1699
   390
    /// 
deba@1699
   391
    /// Initializes the internal data structures.
deba@1710
   392
    void init(const Value value = OperationTraits::infinity()) {
deba@1699
   393
      create_maps();
deba@1699
   394
      for (NodeIt it(*graph); it != INVALID; ++it) {
deba@1699
   395
	_pred->set(it, INVALID);
deba@1710
   396
	_dist->set(it, value);
deba@1699
   397
      }
deba@1781
   398
      _process.clear();
deba@1781
   399
      if (OperationTraits::less(value, OperationTraits::infinity())) {
deba@1781
   400
	for (NodeIt it(*graph); it != INVALID; ++it) {
deba@1781
   401
	  _process.push_back(it);
deba@1783
   402
	  _mask->set(it, true);
deba@1781
   403
	}
deba@1781
   404
      }
deba@1699
   405
    }
deba@1699
   406
    
deba@1699
   407
    /// \brief Adds a new source node.
deba@1699
   408
    ///
deba@1699
   409
    /// The optional second parameter is the initial distance of the node.
deba@1699
   410
    /// It just sets the distance of the node to the given value.
deba@1699
   411
    void addSource(Node source, Value dst = OperationTraits::zero()) {
deba@1699
   412
      _dist->set(source, dst);
deba@1781
   413
      if (!(*_mask)[source]) {
deba@1781
   414
	_process.push_back(source);
deba@1781
   415
	_mask->set(source, true);
deba@1781
   416
      }
deba@1781
   417
    }
deba@1781
   418
deba@1781
   419
    /// \brief Executes one round from the belmann ford algorithm.
deba@1781
   420
    ///
deba@1781
   421
    /// If the algoritm calculated the distances in the previous round 
alpar@1816
   422
    /// strictly for all at most k length paths then it will calculate the 
alpar@1816
   423
    /// distances strictly for all at most k + 1 length paths. With k
alpar@1816
   424
    /// iteration this function calculates the at most k length paths.
deba@1858
   425
    /// \return %True when the algorithm have not found more shorter paths.
deba@1781
   426
    bool processNextRound() {
deba@1781
   427
      for (int i = 0; i < (int)_process.size(); ++i) {
deba@1781
   428
	_mask->set(_process[i], false);
deba@1781
   429
      }
deba@1781
   430
      std::vector<Node> nextProcess;
deba@1781
   431
      std::vector<Value> values(_process.size());
deba@1781
   432
      for (int i = 0; i < (int)_process.size(); ++i) {
klao@1857
   433
	values[i] = (*_dist)[_process[i]];
deba@1781
   434
      }
deba@1781
   435
      for (int i = 0; i < (int)_process.size(); ++i) {
deba@1781
   436
	for (OutEdgeIt it(*graph, _process[i]); it != INVALID; ++it) {
deba@1781
   437
	  Node target = graph->target(it);
deba@1781
   438
	  Value relaxed = OperationTraits::plus(values[i], (*length)[it]);
deba@1781
   439
	  if (OperationTraits::less(relaxed, (*_dist)[target])) {
deba@1781
   440
	    _pred->set(target, it);
deba@1781
   441
	    _dist->set(target, relaxed);
deba@1781
   442
	    if (!(*_mask)[target]) {
deba@1781
   443
	      _mask->set(target, true);
deba@1781
   444
	      nextProcess.push_back(target);
deba@1781
   445
	    }
deba@1781
   446
	  }	  
deba@1781
   447
	}
deba@1781
   448
      }
deba@1781
   449
      _process.swap(nextProcess);
deba@1781
   450
      return _process.empty();
deba@1781
   451
    }
deba@1781
   452
deba@1781
   453
    /// \brief Executes one weak round from the belmann ford algorithm.
deba@1781
   454
    ///
deba@1781
   455
    /// If the algorithm calculated the distances in the
alpar@1816
   456
    /// previous round at least for all at most k length paths then it will
alpar@1816
   457
    /// calculate the distances at least for all at most k + 1 length paths.
alpar@1816
   458
    /// This function does not make it possible to calculate strictly the
alpar@1816
   459
    /// at most k length minimal paths, this is why it is
alpar@1816
   460
    /// called just weak round.
deba@1858
   461
    /// \return %True when the algorithm have not found more shorter paths.
deba@1781
   462
    bool processNextWeakRound() {
deba@1781
   463
      for (int i = 0; i < (int)_process.size(); ++i) {
deba@1781
   464
	_mask->set(_process[i], false);
deba@1781
   465
      }
deba@1781
   466
      std::vector<Node> nextProcess;
deba@1781
   467
      for (int i = 0; i < (int)_process.size(); ++i) {
deba@1781
   468
	for (OutEdgeIt it(*graph, _process[i]); it != INVALID; ++it) {
deba@1781
   469
	  Node target = graph->target(it);
deba@1781
   470
	  Value relaxed = 
deba@1781
   471
	    OperationTraits::plus((*_dist)[_process[i]], (*length)[it]);
deba@1781
   472
	  if (OperationTraits::less(relaxed, (*_dist)[target])) {
deba@1781
   473
	    _pred->set(target, it);
deba@1781
   474
	    _dist->set(target, relaxed);
deba@1781
   475
	    if (!(*_mask)[target]) {
deba@1781
   476
	      _mask->set(target, true);
deba@1781
   477
	      nextProcess.push_back(target);
deba@1781
   478
	    }
deba@1781
   479
	  }	  
deba@1781
   480
	}
deba@1781
   481
      }
deba@1781
   482
      _process.swap(nextProcess);
deba@1781
   483
      return _process.empty();
deba@1699
   484
    }
deba@1699
   485
deba@1699
   486
    /// \brief Executes the algorithm.
deba@1699
   487
    ///
deba@1699
   488
    /// \pre init() must be called and at least one node should be added
deba@1699
   489
    /// with addSource() before using this function.
deba@1699
   490
    ///
deba@1699
   491
    /// This method runs the %BelmannFord algorithm from the root node(s)
deba@1699
   492
    /// in order to compute the shortest path to each node. The algorithm 
deba@1699
   493
    /// computes 
deba@1699
   494
    /// - The shortest path tree.
deba@1699
   495
    /// - The distance of each node from the root(s).
deba@1699
   496
    void start() {
deba@1723
   497
      int num = countNodes(*graph) - 1;
deba@1723
   498
      for (int i = 0; i < num; ++i) {
deba@1781
   499
	if (processNextWeakRound()) break;
deba@1699
   500
      }
deba@1699
   501
    }
deba@1723
   502
deba@1754
   503
    /// \brief Executes the algorithm and checks the negative cycles.
deba@1723
   504
    ///
deba@1723
   505
    /// \pre init() must be called and at least one node should be added
deba@1723
   506
    /// with addSource() before using this function. If there is
deba@1754
   507
    /// a negative cycles in the graph it gives back false.
deba@1723
   508
    ///
deba@1723
   509
    /// This method runs the %BelmannFord algorithm from the root node(s)
deba@1723
   510
    /// in order to compute the shortest path to each node. The algorithm 
deba@1723
   511
    /// computes 
deba@1723
   512
    /// - The shortest path tree.
deba@1723
   513
    /// - The distance of each node from the root(s).
deba@1723
   514
    bool checkedStart() {
deba@1723
   515
      int num = countNodes(*graph);
deba@1723
   516
      for (int i = 0; i < num; ++i) {
deba@1781
   517
	if (processNextWeakRound()) return true;
deba@1723
   518
      }
deba@1723
   519
      return false;
deba@1723
   520
    }
deba@1781
   521
deba@1781
   522
    /// \brief Executes the algorithm with path length limit.
deba@1781
   523
    ///
deba@1781
   524
    /// \pre init() must be called and at least one node should be added
deba@1781
   525
    /// with addSource() before using this function.
deba@1781
   526
    ///
deba@1781
   527
    /// This method runs the %BelmannFord algorithm from the root node(s)
deba@1781
   528
    /// in order to compute the shortest path with at most \c length edge 
alpar@1816
   529
    /// long paths to each node. The algorithm computes 
deba@1781
   530
    /// - The shortest path tree.
deba@1781
   531
    /// - The limited distance of each node from the root(s).
deba@1781
   532
    void limitedStart(int length) {
deba@1781
   533
      for (int i = 0; i < length; ++i) {
deba@1781
   534
	if (processNextRound()) break;
deba@1781
   535
      }
deba@1781
   536
    }
deba@1699
   537
    
deba@1699
   538
    /// \brief Runs %BelmannFord algorithm from node \c s.
deba@1699
   539
    ///    
deba@1699
   540
    /// This method runs the %BelmannFord algorithm from a root node \c s
deba@1699
   541
    /// in order to compute the shortest path to each node. The algorithm 
deba@1699
   542
    /// computes
deba@1699
   543
    /// - The shortest path tree.
deba@1699
   544
    /// - The distance of each node from the root.
deba@1699
   545
    ///
deba@1699
   546
    /// \note d.run(s) is just a shortcut of the following code.
deba@1699
   547
    /// \code
deba@1699
   548
    ///  d.init();
deba@1699
   549
    ///  d.addSource(s);
deba@1699
   550
    ///  d.start();
deba@1699
   551
    /// \endcode
deba@1699
   552
    void run(Node s) {
deba@1699
   553
      init();
deba@1699
   554
      addSource(s);
deba@1699
   555
      start();
deba@1699
   556
    }
deba@1699
   557
    
klao@1857
   558
    /// \brief Runs %BelmannFord algorithm with limited path length 
klao@1857
   559
    /// from node \c s.
klao@1857
   560
    ///    
klao@1857
   561
    /// This method runs the %BelmannFord algorithm from a root node \c s
klao@1857
   562
    /// in order to compute the shortest path with at most \c len edges 
klao@1857
   563
    /// to each node. The algorithm computes
klao@1857
   564
    /// - The shortest path tree.
klao@1857
   565
    /// - The distance of each node from the root.
klao@1857
   566
    ///
klao@1857
   567
    /// \note d.run(s, len) is just a shortcut of the following code.
klao@1857
   568
    /// \code
klao@1857
   569
    ///  d.init();
klao@1857
   570
    ///  d.addSource(s);
klao@1857
   571
    ///  d.limitedStart(len);
klao@1857
   572
    /// \endcode
klao@1857
   573
    void run(Node s, int len) {
klao@1857
   574
      init();
klao@1857
   575
      addSource(s);
klao@1857
   576
      limitedStart(len);
klao@1857
   577
    }
klao@1857
   578
    
deba@1699
   579
    ///@}
deba@1699
   580
deba@1699
   581
    /// \name Query Functions
deba@1699
   582
    /// The result of the %BelmannFord algorithm can be obtained using these
deba@1699
   583
    /// functions.\n
deba@1699
   584
    /// Before the use of these functions,
deba@1699
   585
    /// either run() or start() must be called.
deba@1699
   586
    
deba@1699
   587
    ///@{
deba@1699
   588
deba@1699
   589
    /// \brief Copies the shortest path to \c t into \c p
deba@1699
   590
    ///    
deba@1699
   591
    /// This function copies the shortest path to \c t into \c p.
deba@1699
   592
    /// If it \c t is a source itself or unreachable, then it does not
deba@1699
   593
    /// alter \c p.
deba@1765
   594
    ///
deba@1699
   595
    /// \return Returns \c true if a path to \c t was actually copied to \c p,
deba@1699
   596
    /// \c false otherwise.
deba@1699
   597
    /// \sa DirPath
deba@1699
   598
    template <typename Path>
deba@1699
   599
    bool getPath(Path &p, Node t) {
deba@1699
   600
      if(reached(t)) {
deba@1699
   601
	p.clear();
deba@1699
   602
	typename Path::Builder b(p);
deba@1763
   603
	for(b.setStartNode(t);predEdge(t)!=INVALID;t=predNode(t))
deba@1763
   604
	  b.pushFront(predEdge(t));
deba@1699
   605
	b.commit();
deba@1699
   606
	return true;
deba@1699
   607
      }
deba@1699
   608
      return false;
deba@1699
   609
    }
deba@1699
   610
	  
deba@1699
   611
    /// \brief The distance of a node from the root.
deba@1699
   612
    ///
deba@1699
   613
    /// Returns the distance of a node from the root.
deba@1699
   614
    /// \pre \ref run() must be called before using this function.
deba@1699
   615
    /// \warning If node \c v in unreachable from the root the return value
deba@1699
   616
    /// of this funcion is undefined.
deba@1699
   617
    Value dist(Node v) const { return (*_dist)[v]; }
deba@1699
   618
deba@1699
   619
    /// \brief Returns the 'previous edge' of the shortest path tree.
deba@1699
   620
    ///
deba@1699
   621
    /// For a node \c v it returns the 'previous edge' of the shortest path 
deba@1699
   622
    /// tree, i.e. it returns the last edge of a shortest path from the root 
deba@1699
   623
    /// to \c v. It is \ref INVALID if \c v is unreachable from the root or 
deba@1699
   624
    /// if \c v=s. The shortest path tree used here is equal to the shortest 
deba@1699
   625
    /// path tree used in \ref predNode(). 
deba@1699
   626
    /// \pre \ref run() must be called before using
deba@1699
   627
    /// this function.
deba@1763
   628
    Edge predEdge(Node v) const { return (*_pred)[v]; }
deba@1699
   629
deba@1699
   630
    /// \brief Returns the 'previous node' of the shortest path tree.
deba@1699
   631
    ///
deba@1699
   632
    /// For a node \c v it returns the 'previous node' of the shortest path 
deba@1699
   633
    /// tree, i.e. it returns the last but one node from a shortest path from 
deba@1699
   634
    /// the root to \c /v. It is INVALID if \c v is unreachable from the root 
deba@1699
   635
    /// or if \c v=s. The shortest path tree used here is equal to the 
deba@1763
   636
    /// shortest path tree used in \ref predEdge().  \pre \ref run() must be 
deba@1699
   637
    /// called before using this function.
deba@1699
   638
    Node predNode(Node v) const { 
deba@1699
   639
      return (*_pred)[v] == INVALID ? INVALID : graph->source((*_pred)[v]); 
deba@1699
   640
    }
deba@1699
   641
    
deba@1699
   642
    /// \brief Returns a reference to the NodeMap of distances.
deba@1699
   643
    ///
deba@1699
   644
    /// Returns a reference to the NodeMap of distances. \pre \ref run() must
deba@1699
   645
    /// be called before using this function.
deba@1699
   646
    const DistMap &distMap() const { return *_dist;}
deba@1699
   647
 
deba@1699
   648
    /// \brief Returns a reference to the shortest path tree map.
deba@1699
   649
    ///
deba@1699
   650
    /// Returns a reference to the NodeMap of the edges of the
deba@1699
   651
    /// shortest path tree.
deba@1699
   652
    /// \pre \ref run() must be called before using this function.
deba@1699
   653
    const PredMap &predMap() const { return *_pred; }
deba@1699
   654
 
deba@1699
   655
    /// \brief Checks if a node is reachable from the root.
deba@1699
   656
    ///
deba@1699
   657
    /// Returns \c true if \c v is reachable from the root.
deba@1699
   658
    /// \pre \ref run() must be called before using this function.
deba@1699
   659
    ///
deba@1699
   660
    bool reached(Node v) { return (*_dist)[v] != OperationTraits::infinity(); }
deba@1699
   661
    
deba@1699
   662
    ///@}
deba@1699
   663
  };
deba@1699
   664
 
deba@1699
   665
  /// \brief Default traits class of BelmannFord function.
deba@1699
   666
  ///
deba@1699
   667
  /// Default traits class of BelmannFord function.
deba@1699
   668
  /// \param _Graph Graph type.
deba@1699
   669
  /// \param _LengthMap Type of length map.
deba@1699
   670
  template <typename _Graph, typename _LengthMap>
deba@1699
   671
  struct BelmannFordWizardDefaultTraits {
deba@1699
   672
    /// \brief The graph type the algorithm runs on. 
deba@1699
   673
    typedef _Graph Graph;
deba@1699
   674
deba@1699
   675
    /// \brief The type of the map that stores the edge lengths.
deba@1699
   676
    ///
deba@1699
   677
    /// The type of the map that stores the edge lengths.
deba@1699
   678
    /// It must meet the \ref concept::ReadMap "ReadMap" concept.
deba@1699
   679
    typedef _LengthMap LengthMap;
deba@1699
   680
deba@1699
   681
    /// \brief The value type of the length map.
deba@1699
   682
    typedef typename _LengthMap::Value Value;
deba@1699
   683
deba@1699
   684
    /// \brief Operation traits for belmann-ford algorithm.
deba@1699
   685
    ///
deba@1699
   686
    /// It defines the infinity type on the given Value type
deba@1699
   687
    /// and the used operation.
deba@1699
   688
    /// \see BelmannFordDefaultOperationTraits
deba@1699
   689
    typedef BelmannFordDefaultOperationTraits<Value> OperationTraits;
deba@1699
   690
deba@1699
   691
    /// \brief The type of the map that stores the last
deba@1699
   692
    /// edges of the shortest paths.
deba@1699
   693
    /// 
deba@1699
   694
    /// The type of the map that stores the last
deba@1699
   695
    /// edges of the shortest paths.
deba@1699
   696
    /// It must meet the \ref concept::WriteMap "WriteMap" concept.
deba@1699
   697
    typedef NullMap <typename _Graph::Node,typename _Graph::Edge> PredMap;
deba@1699
   698
deba@1699
   699
    /// \brief Instantiates a PredMap.
deba@1699
   700
    /// 
deba@1699
   701
    /// This function instantiates a \ref PredMap. 
deba@1699
   702
    static PredMap *createPredMap(const _Graph &) {
deba@1699
   703
      return new PredMap();
deba@1699
   704
    }
deba@1699
   705
    /// \brief The type of the map that stores the dists of the nodes.
deba@1699
   706
    ///
deba@1699
   707
    /// The type of the map that stores the dists of the nodes.
deba@1699
   708
    /// It must meet the \ref concept::WriteMap "WriteMap" concept.
deba@1699
   709
    typedef NullMap<typename Graph::Node, Value> DistMap;
deba@1699
   710
    /// \brief Instantiates a DistMap.
deba@1699
   711
    ///
deba@1699
   712
    /// This function instantiates a \ref DistMap. 
deba@1699
   713
    static DistMap *createDistMap(const _Graph &) {
deba@1699
   714
      return new DistMap();
deba@1699
   715
    }
deba@1699
   716
  };
deba@1699
   717
  
deba@1699
   718
  /// \brief Default traits used by \ref BelmannFordWizard
deba@1699
   719
  ///
deba@1699
   720
  /// To make it easier to use BelmannFord algorithm
deba@1699
   721
  /// we have created a wizard class.
deba@1699
   722
  /// This \ref BelmannFordWizard class needs default traits,
deba@1699
   723
  /// as well as the \ref BelmannFord class.
deba@1699
   724
  /// The \ref BelmannFordWizardBase is a class to be the default traits of the
deba@1699
   725
  /// \ref BelmannFordWizard class.
deba@1699
   726
  /// \todo More named parameters are required...
deba@1699
   727
  template<class _Graph,class _LengthMap>
deba@1699
   728
  class BelmannFordWizardBase 
deba@1699
   729
    : public BelmannFordWizardDefaultTraits<_Graph,_LengthMap> {
deba@1699
   730
deba@1699
   731
    typedef BelmannFordWizardDefaultTraits<_Graph,_LengthMap> Base;
deba@1699
   732
  protected:
deba@1699
   733
    /// Type of the nodes in the graph.
deba@1699
   734
    typedef typename Base::Graph::Node Node;
deba@1699
   735
deba@1699
   736
    /// Pointer to the underlying graph.
deba@1699
   737
    void *_graph;
deba@1699
   738
    /// Pointer to the length map
deba@1699
   739
    void *_length;
deba@1699
   740
    ///Pointer to the map of predecessors edges.
deba@1699
   741
    void *_pred;
deba@1699
   742
    ///Pointer to the map of distances.
deba@1699
   743
    void *_dist;
deba@1699
   744
    ///Pointer to the source node.
deba@1699
   745
    Node _source;
deba@1699
   746
deba@1699
   747
    public:
deba@1699
   748
    /// Constructor.
deba@1699
   749
    
deba@1699
   750
    /// This constructor does not require parameters, therefore it initiates
deba@1699
   751
    /// all of the attributes to default values (0, INVALID).
deba@1699
   752
    BelmannFordWizardBase() : _graph(0), _length(0), _pred(0),
deba@1699
   753
			   _dist(0), _source(INVALID) {}
deba@1699
   754
deba@1699
   755
    /// Constructor.
deba@1699
   756
    
deba@1699
   757
    /// This constructor requires some parameters,
deba@1699
   758
    /// listed in the parameters list.
deba@1699
   759
    /// Others are initiated to 0.
deba@1699
   760
    /// \param graph is the initial value of  \ref _graph
deba@1699
   761
    /// \param length is the initial value of  \ref _length
deba@1699
   762
    /// \param source is the initial value of  \ref _source
deba@1699
   763
    BelmannFordWizardBase(const _Graph& graph, 
deba@1699
   764
			  const _LengthMap& length, 
deba@1699
   765
			  Node source = INVALID) :
deba@1699
   766
      _graph((void *)&graph), _length((void *)&length), _pred(0),
deba@1699
   767
      _dist(0), _source(source) {}
deba@1699
   768
deba@1699
   769
  };
deba@1699
   770
  
deba@1699
   771
  /// A class to make the usage of BelmannFord algorithm easier
deba@1699
   772
deba@1699
   773
  /// This class is created to make it easier to use BelmannFord algorithm.
deba@1699
   774
  /// It uses the functions and features of the plain \ref BelmannFord,
deba@1699
   775
  /// but it is much simpler to use it.
deba@1699
   776
  ///
deba@1699
   777
  /// Simplicity means that the way to change the types defined
deba@1699
   778
  /// in the traits class is based on functions that returns the new class
deba@1699
   779
  /// and not on templatable built-in classes.
deba@1699
   780
  /// When using the plain \ref BelmannFord
deba@1699
   781
  /// the new class with the modified type comes from
deba@1699
   782
  /// the original class by using the ::
deba@1699
   783
  /// operator. In the case of \ref BelmannFordWizard only
deba@1699
   784
  /// a function have to be called and it will
deba@1699
   785
  /// return the needed class.
deba@1699
   786
  ///
deba@1699
   787
  /// It does not have own \ref run method. When its \ref run method is called
deba@1699
   788
  /// it initiates a plain \ref BelmannFord class, and calls the \ref 
deba@1699
   789
  /// BelmannFord::run method of it.
deba@1699
   790
  template<class _Traits>
deba@1699
   791
  class BelmannFordWizard : public _Traits {
deba@1699
   792
    typedef _Traits Base;
deba@1699
   793
deba@1699
   794
    ///The type of the underlying graph.
deba@1699
   795
    typedef typename _Traits::Graph Graph;
deba@1699
   796
deba@1699
   797
    typedef typename Graph::Node Node;
deba@1699
   798
    typedef typename Graph::NodeIt NodeIt;
deba@1699
   799
    typedef typename Graph::Edge Edge;
deba@1699
   800
    typedef typename Graph::OutEdgeIt EdgeIt;
deba@1699
   801
    
deba@1699
   802
    ///The type of the map that stores the edge lengths.
deba@1699
   803
    typedef typename _Traits::LengthMap LengthMap;
deba@1699
   804
deba@1699
   805
    ///The type of the length of the edges.
deba@1699
   806
    typedef typename LengthMap::Value Value;
deba@1699
   807
deba@1699
   808
    ///\brief The type of the map that stores the last
deba@1699
   809
    ///edges of the shortest paths.
deba@1699
   810
    typedef typename _Traits::PredMap PredMap;
deba@1699
   811
deba@1699
   812
    ///The type of the map that stores the dists of the nodes.
deba@1699
   813
    typedef typename _Traits::DistMap DistMap;
deba@1699
   814
deba@1699
   815
  public:
deba@1699
   816
    /// Constructor.
deba@1699
   817
    BelmannFordWizard() : _Traits() {}
deba@1699
   818
deba@1699
   819
    /// \brief Constructor that requires parameters.
deba@1699
   820
    ///
deba@1699
   821
    /// Constructor that requires parameters.
deba@1699
   822
    /// These parameters will be the default values for the traits class.
deba@1699
   823
    BelmannFordWizard(const Graph& graph, const LengthMap& length, 
deba@1699
   824
		      Node source = INVALID) 
deba@1699
   825
      : _Traits(graph, length, source) {}
deba@1699
   826
deba@1699
   827
    /// \brief Copy constructor
deba@1699
   828
    BelmannFordWizard(const _Traits &b) : _Traits(b) {}
deba@1699
   829
deba@1699
   830
    ~BelmannFordWizard() {}
deba@1699
   831
deba@1699
   832
    /// \brief Runs BelmannFord algorithm from a given node.
deba@1699
   833
    ///    
deba@1699
   834
    /// Runs BelmannFord algorithm from a given node.
deba@1699
   835
    /// The node can be given by the \ref source function.
deba@1699
   836
    void run() {
deba@1699
   837
      if(Base::_source == INVALID) throw UninitializedParameter();
deba@1699
   838
      BelmannFord<Graph,LengthMap,_Traits> 
deba@1699
   839
	bf(*(Graph*)Base::_graph, *(LengthMap*)Base::_length);
deba@1699
   840
      if (Base::_pred) bf.predMap(*(PredMap*)Base::_pred);
deba@1699
   841
      if (Base::_dist) bf.distMap(*(DistMap*)Base::_dist);
deba@1699
   842
      bf.run(Base::_source);
deba@1699
   843
    }
deba@1699
   844
deba@1699
   845
    /// \brief Runs BelmannFord algorithm from the given node.
deba@1699
   846
    ///
deba@1699
   847
    /// Runs BelmannFord algorithm from the given node.
deba@1858
   848
    /// \param source is the given source.
deba@1699
   849
    void run(Node source) {
deba@1699
   850
      Base::_source = source;
deba@1699
   851
      run();
deba@1699
   852
    }
deba@1699
   853
deba@1699
   854
    template<class T>
deba@1699
   855
    struct DefPredMapBase : public Base {
deba@1699
   856
      typedef T PredMap;
deba@1699
   857
      static PredMap *createPredMap(const Graph &) { return 0; };
deba@1699
   858
      DefPredMapBase(const _Traits &b) : _Traits(b) {}
deba@1699
   859
    };
deba@1699
   860
    
deba@1699
   861
    ///\brief \ref named-templ-param "Named parameter"
deba@1699
   862
    ///function for setting PredMap type
deba@1699
   863
    ///
deba@1699
   864
    /// \ref named-templ-param "Named parameter"
deba@1699
   865
    ///function for setting PredMap type
deba@1699
   866
    ///
deba@1699
   867
    template<class T>
deba@1699
   868
    BelmannFordWizard<DefPredMapBase<T> > predMap(const T &t) 
deba@1699
   869
    {
deba@1699
   870
      Base::_pred=(void *)&t;
deba@1699
   871
      return BelmannFordWizard<DefPredMapBase<T> >(*this);
deba@1699
   872
    }
deba@1699
   873
    
deba@1699
   874
    template<class T>
deba@1699
   875
    struct DefDistMapBase : public Base {
deba@1699
   876
      typedef T DistMap;
deba@1699
   877
      static DistMap *createDistMap(const Graph &) { return 0; };
deba@1699
   878
      DefDistMapBase(const _Traits &b) : _Traits(b) {}
deba@1699
   879
    };
deba@1699
   880
    
deba@1699
   881
    ///\brief \ref named-templ-param "Named parameter"
deba@1699
   882
    ///function for setting DistMap type
deba@1699
   883
    ///
deba@1699
   884
    /// \ref named-templ-param "Named parameter"
deba@1699
   885
    ///function for setting DistMap type
deba@1699
   886
    ///
deba@1699
   887
    template<class T>
deba@1699
   888
    BelmannFordWizard<DefDistMapBase<T> > distMap(const T &t) {
deba@1699
   889
      Base::_dist=(void *)&t;
deba@1699
   890
      return BelmannFordWizard<DefDistMapBase<T> >(*this);
deba@1699
   891
    }
deba@1710
   892
deba@1710
   893
    template<class T>
deba@1710
   894
    struct DefOperationTraitsBase : public Base {
deba@1710
   895
      typedef T OperationTraits;
deba@1710
   896
      DefOperationTraitsBase(const _Traits &b) : _Traits(b) {}
deba@1710
   897
    };
deba@1710
   898
    
deba@1710
   899
    ///\brief \ref named-templ-param "Named parameter"
deba@1710
   900
    ///function for setting OperationTraits type
deba@1710
   901
    ///
deba@1710
   902
    /// \ref named-templ-param "Named parameter"
deba@1710
   903
    ///function for setting OperationTraits type
deba@1710
   904
    ///
deba@1710
   905
    template<class T>
deba@1710
   906
    BelmannFordWizard<DefOperationTraitsBase<T> > distMap() {
deba@1710
   907
      return BelmannFordWizard<DefDistMapBase<T> >(*this);
deba@1710
   908
    }
deba@1699
   909
    
deba@1699
   910
    /// \brief Sets the source node, from which the BelmannFord algorithm runs.
deba@1699
   911
    ///
deba@1699
   912
    /// Sets the source node, from which the BelmannFord algorithm runs.
deba@1858
   913
    /// \param source is the source node.
deba@1699
   914
    BelmannFordWizard<_Traits>& source(Node source) {
deba@1699
   915
      Base::_source = source;
deba@1699
   916
      return *this;
deba@1699
   917
    }
deba@1699
   918
    
deba@1699
   919
  };
deba@1699
   920
  
deba@1699
   921
  /// \brief Function type interface for BelmannFord algorithm.
deba@1699
   922
  ///
deba@1699
   923
  /// \ingroup flowalgs
deba@1699
   924
  /// Function type interface for BelmannFord algorithm.
deba@1699
   925
  ///
deba@1699
   926
  /// This function also has several \ref named-templ-func-param 
deba@1699
   927
  /// "named parameters", they are declared as the members of class 
deba@1699
   928
  /// \ref BelmannFordWizard.
deba@1699
   929
  /// The following
deba@1699
   930
  /// example shows how to use these parameters.
deba@1699
   931
  /// \code
deba@1699
   932
  /// belmannford(g,length,source).predMap(preds).run();
deba@1699
   933
  /// \endcode
deba@1699
   934
  /// \warning Don't forget to put the \ref BelmannFordWizard::run() "run()"
deba@1699
   935
  /// to the end of the parameter list.
deba@1699
   936
  /// \sa BelmannFordWizard
deba@1699
   937
  /// \sa BelmannFord
deba@1699
   938
  template<class _Graph, class _LengthMap>
deba@1699
   939
  BelmannFordWizard<BelmannFordWizardBase<_Graph,_LengthMap> >
deba@1699
   940
  belmannFord(const _Graph& graph,
deba@1699
   941
	      const _LengthMap& length, 
deba@1699
   942
	      typename _Graph::Node source = INVALID) {
deba@1699
   943
    return BelmannFordWizard<BelmannFordWizardBase<_Graph,_LengthMap> >
deba@1699
   944
      (graph, length, source);
deba@1699
   945
  }
deba@1699
   946
deba@1699
   947
} //END OF NAMESPACE LEMON
deba@1699
   948
deba@1699
   949
#endif
deba@1699
   950