lemon/dag_shortest_path.h
author deba
Tue, 31 Jan 2006 20:04:36 +0000
changeset 1933 a876a3d6a4c7
child 1946 17eb3eaad9f8
permissions -rw-r--r--
Revising the bpugraph concept

We need a public but very limited ANode and BNode class
It can be used with ItemSetTraits and with some special maps

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