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