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