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