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