lemon/johnson.h
author alpar
Fri, 07 Oct 2005 11:05:08 +0000
changeset 1715 e71778873dd0
parent 1699 29428f7b8b66
child 1723 fb4f801dd692
permissions -rw-r--r--
Doc improvments
deba@1699
     1
/* -*- C++ -*-
deba@1699
     2
 * lemon/johnson.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_JOHNSON_H
deba@1699
    18
#define LEMON_JOHNSON_H
deba@1699
    19
deba@1699
    20
///\ingroup flowalgs
deba@1699
    21
/// \file
deba@1699
    22
/// \brief Johnson algorithm.
deba@1699
    23
///
deba@1699
    24
deba@1699
    25
#include <lemon/list_graph.h>
deba@1699
    26
#include <lemon/graph_utils.h>
deba@1699
    27
#include <lemon/dijkstra.h>
deba@1699
    28
#include <lemon/belmann_ford.h>
deba@1699
    29
#include <lemon/invalid.h>
deba@1699
    30
#include <lemon/error.h>
deba@1699
    31
#include <lemon/maps.h>
deba@1699
    32
deba@1699
    33
#include <limits>
deba@1699
    34
deba@1699
    35
namespace lemon {
deba@1699
    36
deba@1699
    37
  /// \brief Default OperationTraits for the Johnson algorithm class.
deba@1699
    38
  ///  
deba@1699
    39
  /// It defines all computational operations and constants which are
deba@1699
    40
  /// used in the Floyd-Warshall algorithm. The default implementation
deba@1699
    41
  /// is based on the numeric_limits class. If the numeric type does not
deba@1699
    42
  /// have infinity value then the maximum value is used as extremal
deba@1699
    43
  /// infinity value.
deba@1699
    44
  template <
deba@1699
    45
    typename Value, 
deba@1699
    46
    bool has_infinity = std::numeric_limits<Value>::has_infinity>
deba@1699
    47
  struct JohnsonDefaultOperationTraits {
deba@1699
    48
    /// \brief Gives back the zero value of the type.
deba@1699
    49
    static Value zero() {
deba@1699
    50
      return static_cast<Value>(0);
deba@1699
    51
    }
deba@1699
    52
    /// \brief Gives back the positive infinity value of the type.
deba@1699
    53
    static Value infinity() {
deba@1699
    54
      return std::numeric_limits<Value>::infinity();
deba@1699
    55
    }
deba@1699
    56
    /// \brief Gives back the sum of the given two elements.
deba@1699
    57
    static Value plus(const Value& left, const Value& right) {
deba@1699
    58
      return left + right;
deba@1699
    59
    }
deba@1699
    60
    /// \brief Gives back true only if the first value less than the second.
deba@1699
    61
    static bool less(const Value& left, const Value& right) {
deba@1699
    62
      return left < right;
deba@1699
    63
    }
deba@1699
    64
  };
deba@1699
    65
deba@1699
    66
  template <typename Value>
deba@1699
    67
  struct JohnsonDefaultOperationTraits<Value, false> {
deba@1699
    68
    static Value zero() {
deba@1699
    69
      return static_cast<Value>(0);
deba@1699
    70
    }
deba@1699
    71
    static Value infinity() {
deba@1699
    72
      return std::numeric_limits<Value>::max();
deba@1699
    73
    }
deba@1699
    74
    static Value plus(const Value& left, const Value& right) {
deba@1699
    75
      if (left == infinity() || right == infinity()) return infinity();
deba@1699
    76
      return left + right;
deba@1699
    77
    }
deba@1699
    78
    static bool less(const Value& left, const Value& right) {
deba@1699
    79
      return left < right;
deba@1699
    80
    }
deba@1699
    81
  };
deba@1699
    82
  
deba@1699
    83
  /// \brief Default traits class of Johnson class.
deba@1699
    84
  ///
deba@1699
    85
  /// Default traits class of Johnson class.
deba@1699
    86
  /// \param _Graph Graph type.
deba@1699
    87
  /// \param _LegthMap Type of length map.
deba@1699
    88
  template<class _Graph, class _LengthMap>
deba@1699
    89
  struct JohnsonDefaultTraits {
deba@1699
    90
    /// The graph type the algorithm runs on. 
deba@1699
    91
    typedef _Graph Graph;
deba@1699
    92
deba@1699
    93
    /// \brief The type of the map that stores the edge lengths.
deba@1699
    94
    ///
deba@1699
    95
    /// The type of the map that stores the edge lengths.
deba@1699
    96
    /// It must meet the \ref concept::ReadMap "ReadMap" concept.
deba@1699
    97
    typedef _LengthMap LengthMap;
deba@1699
    98
deba@1699
    99
    // The type of the length of the edges.
deba@1699
   100
    typedef typename _LengthMap::Value Value;
deba@1699
   101
deba@1699
   102
    /// \brief Operation traits for belmann-ford algorithm.
deba@1699
   103
    ///
deba@1699
   104
    /// It defines the infinity type on the given Value type
deba@1699
   105
    /// and the used operation.
deba@1699
   106
    /// \see JohnsonDefaultOperationTraits
deba@1699
   107
    typedef JohnsonDefaultOperationTraits<Value> OperationTraits;
deba@1699
   108
 
deba@1699
   109
    /// \brief The type of the map that stores the last edges of the 
deba@1699
   110
    /// shortest paths.
deba@1699
   111
    /// 
deba@1699
   112
    /// The type of the map that stores the last
deba@1699
   113
    /// edges of the shortest paths.
deba@1699
   114
    /// It must be a matrix map with \c Graph::Edge value type.
deba@1699
   115
    ///
deba@1699
   116
    typedef NodeMatrixMap<Graph, typename Graph::Edge> PredMap;
deba@1699
   117
deba@1699
   118
    /// \brief Instantiates a PredMap.
deba@1699
   119
    /// 
deba@1699
   120
    /// This function instantiates a \ref PredMap. 
deba@1699
   121
    /// \param G is the graph, to which we would like to define the PredMap.
deba@1699
   122
    /// \todo The graph alone may be insufficient for the initialization
deba@1699
   123
    static PredMap *createPredMap(const _Graph& graph) {
deba@1699
   124
      return new PredMap(graph);
deba@1699
   125
    }
deba@1699
   126
deba@1699
   127
    /// \brief The type of the map that stores the dists of the nodes.
deba@1699
   128
    ///
deba@1699
   129
    /// The type of the map that stores the dists of the nodes.
deba@1699
   130
    /// It must meet the \ref concept::WriteMap "WriteMap" concept.
deba@1699
   131
    ///
deba@1699
   132
    typedef NodeMatrixMap<Graph, Value> DistMap;
deba@1699
   133
deba@1699
   134
    /// \brief Instantiates a DistMap.
deba@1699
   135
    ///
deba@1699
   136
    /// This function instantiates a \ref DistMap. 
deba@1699
   137
    /// \param G is the graph, to which we would like to define the 
deba@1699
   138
    /// \ref DistMap
deba@1699
   139
    static DistMap *createDistMap(const _Graph& graph) {
deba@1699
   140
      return new DistMap(graph);
deba@1699
   141
    }
deba@1699
   142
deba@1699
   143
  };
deba@1699
   144
deba@1699
   145
  /// \brief Johnson algorithm class.
deba@1699
   146
  ///
deba@1699
   147
  /// \ingroup flowalgs
deba@1699
   148
  /// This class provides an efficient implementation of \c Johnson 
deba@1699
   149
  /// algorithm. The edge lengths are passed to the algorithm using a
deba@1699
   150
  /// \ref concept::ReadMap "ReadMap", so it is easy to change it to any 
deba@1699
   151
  /// kind of length.
deba@1699
   152
  ///
deba@1699
   153
  /// The type of the length is determined by the
deba@1699
   154
  /// \ref concept::ReadMap::Value "Value" of the length map.
deba@1699
   155
  ///
deba@1699
   156
  /// \param _Graph The graph type the algorithm runs on. The default value
deba@1699
   157
  /// is \ref ListGraph. The value of _Graph is not used directly by
deba@1699
   158
  /// Johnson, it is only passed to \ref JohnsonDefaultTraits.
deba@1699
   159
  /// \param _LengthMap This read-only EdgeMap determines the lengths of the
deba@1699
   160
  /// edges. It is read once for each edge, so the map may involve in
deba@1699
   161
  /// relatively time consuming process to compute the edge length if
deba@1699
   162
  /// it is necessary. The default map type is \ref
deba@1699
   163
  /// concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".  The value
deba@1699
   164
  /// of _LengthMap is not used directly by Johnson, it is only passed 
deba@1699
   165
  /// to \ref JohnsonDefaultTraits.  \param _Traits Traits class to set
deba@1699
   166
  /// various data types used by the algorithm.  The default traits
deba@1699
   167
  /// class is \ref JohnsonDefaultTraits
deba@1699
   168
  /// "JohnsonDefaultTraits<_Graph,_LengthMap>".  See \ref
deba@1699
   169
  /// JohnsonDefaultTraits for the documentation of a Johnson traits
deba@1699
   170
  /// class.
deba@1699
   171
  ///
deba@1699
   172
  /// \author Balazs Dezso
deba@1699
   173
deba@1710
   174
#ifdef DOXYGEN
deba@1710
   175
  template <typename _Graph, typename _LengthMap, typename _Traits>
deba@1710
   176
#else
deba@1699
   177
  template <typename _Graph=ListGraph,
deba@1699
   178
	    typename _LengthMap=typename _Graph::template EdgeMap<int>,
deba@1699
   179
	    typename _Traits=JohnsonDefaultTraits<_Graph,_LengthMap> >
deba@1710
   180
#endif
deba@1699
   181
  class Johnson {
deba@1699
   182
  public:
deba@1699
   183
    
deba@1699
   184
    /// \brief \ref Exception for uninitialized parameters.
deba@1699
   185
    ///
deba@1699
   186
    /// This error represents problems in the initialization
deba@1699
   187
    /// of the parameters of the algorithms.
deba@1699
   188
deba@1699
   189
    class UninitializedParameter : public lemon::UninitializedParameter {
deba@1699
   190
    public:
deba@1699
   191
      virtual const char* exceptionName() const {
deba@1699
   192
	return "lemon::Johnson::UninitializedParameter";
deba@1699
   193
      }
deba@1699
   194
    };
deba@1699
   195
deba@1699
   196
    typedef _Traits Traits;
deba@1699
   197
    ///The type of the underlying graph.
deba@1699
   198
    typedef typename _Traits::Graph Graph;
deba@1699
   199
deba@1699
   200
    typedef typename Graph::Node Node;
deba@1699
   201
    typedef typename Graph::NodeIt NodeIt;
deba@1699
   202
    typedef typename Graph::Edge Edge;
deba@1699
   203
    typedef typename Graph::EdgeIt EdgeIt;
deba@1699
   204
    
deba@1699
   205
    /// \brief The type of the length of the edges.
deba@1699
   206
    typedef typename _Traits::LengthMap::Value Value;
deba@1699
   207
    /// \brief The type of the map that stores the edge lengths.
deba@1699
   208
    typedef typename _Traits::LengthMap LengthMap;
deba@1699
   209
    /// \brief The type of the map that stores the last
deba@1699
   210
    /// edges of the shortest paths. The type of the PredMap
deba@1699
   211
    /// is a matrix map for Edges
deba@1699
   212
    typedef typename _Traits::PredMap PredMap;
deba@1699
   213
    /// \brief The type of the map that stores the dists of the nodes.
deba@1699
   214
    /// The type of the DistMap is a matrix map for Values
deba@1699
   215
    typedef typename _Traits::DistMap DistMap;
deba@1699
   216
    /// \brief The operation traits.
deba@1699
   217
    typedef typename _Traits::OperationTraits OperationTraits;
deba@1699
   218
  private:
deba@1699
   219
    /// Pointer to the underlying graph.
deba@1699
   220
    const Graph *graph;
deba@1699
   221
    /// Pointer to the length map
deba@1699
   222
    const LengthMap *length;
deba@1699
   223
    ///Pointer to the map of predecessors edges.
deba@1699
   224
    PredMap *_pred;
deba@1699
   225
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
deba@1699
   226
    bool local_pred;
deba@1699
   227
    ///Pointer to the map of distances.
deba@1699
   228
    DistMap *_dist;
deba@1699
   229
    ///Indicates if \ref _dist is locally allocated (\c true) or not.
deba@1699
   230
    bool local_dist;
deba@1699
   231
deba@1699
   232
    /// Creates the maps if necessary.
deba@1699
   233
    void create_maps() {
deba@1699
   234
      if(!_pred) {
deba@1699
   235
	local_pred = true;
deba@1699
   236
	_pred = Traits::createPredMap(*graph);
deba@1699
   237
      }
deba@1699
   238
      if(!_dist) {
deba@1699
   239
	local_dist = true;
deba@1699
   240
	_dist = Traits::createDistMap(*graph);
deba@1699
   241
      }
deba@1699
   242
    }
deba@1699
   243
    
deba@1699
   244
  public :
deba@1699
   245
 
deba@1699
   246
    /// \name Named template parameters
deba@1699
   247
deba@1699
   248
    ///@{
deba@1699
   249
deba@1699
   250
    template <class T>
deba@1699
   251
    struct DefPredMapTraits : public Traits {
deba@1699
   252
      typedef T PredMap;
deba@1699
   253
      static PredMap *createPredMap(const Graph& graph) {
deba@1699
   254
	throw UninitializedParameter();
deba@1699
   255
      }
deba@1699
   256
    };
deba@1699
   257
deba@1699
   258
    /// \brief \ref named-templ-param "Named parameter" for setting PredMap 
deba@1699
   259
    /// type
deba@1699
   260
    /// \ref named-templ-param "Named parameter" for setting PredMap type
deba@1699
   261
    ///
deba@1699
   262
    template <class T>
deba@1710
   263
    struct DefPredMap 
deba@1710
   264
      : public Johnson< Graph, LengthMap, DefPredMapTraits<T> > {
deba@1710
   265
      typedef Johnson< Graph, LengthMap, DefPredMapTraits<T> > Create;
deba@1710
   266
    };
deba@1699
   267
    
deba@1699
   268
    template <class T>
deba@1699
   269
    struct DefDistMapTraits : public Traits {
deba@1699
   270
      typedef T DistMap;
deba@1699
   271
      static DistMap *createDistMap(const Graph& graph) {
deba@1699
   272
	throw UninitializedParameter();
deba@1699
   273
      }
deba@1699
   274
    };
deba@1699
   275
    /// \brief \ref named-templ-param "Named parameter" for setting DistMap 
deba@1699
   276
    /// type
deba@1699
   277
    ///
deba@1699
   278
    /// \ref named-templ-param "Named parameter" for setting DistMap type
deba@1699
   279
    ///
deba@1699
   280
    template <class T>
deba@1710
   281
    struct DefDistMap 
deba@1710
   282
      : public Johnson< Graph, LengthMap, DefDistMapTraits<T> > {
deba@1710
   283
      typedef Johnson< Graph, LengthMap, DefDistMapTraits<T> > Create;
deba@1710
   284
    };
deba@1699
   285
    
deba@1699
   286
    template <class T>
deba@1699
   287
    struct DefOperationTraitsTraits : public Traits {
deba@1699
   288
      typedef T OperationTraits;
deba@1699
   289
    };
deba@1699
   290
    
deba@1699
   291
    /// \brief \ref named-templ-param "Named parameter" for setting 
deba@1699
   292
    /// OperationTraits type
deba@1699
   293
    ///
deba@1710
   294
    /// \ref named-templ-param "Named parameter" for setting 
deba@1710
   295
    /// OperationTraits type
deba@1699
   296
    template <class T>
deba@1710
   297
    struct DefOperationTraits
deba@1710
   298
      : public Johnson< Graph, LengthMap, DefOperationTraitsTraits<T> > {
deba@1710
   299
      typedef Johnson< Graph, LengthMap, DefOperationTraitsTraits<T> > Create;
deba@1710
   300
    };
deba@1699
   301
    
deba@1699
   302
    ///@}
deba@1699
   303
deba@1710
   304
  protected:
deba@1710
   305
deba@1710
   306
    Johnson() {}
deba@1710
   307
deba@1699
   308
  public:      
deba@1699
   309
    
deba@1699
   310
    /// \brief Constructor.
deba@1699
   311
    ///
deba@1699
   312
    /// \param _graph the graph the algorithm will run on.
deba@1699
   313
    /// \param _length the length map used by the algorithm.
deba@1699
   314
    Johnson(const Graph& _graph, const LengthMap& _length) :
deba@1699
   315
      graph(&_graph), length(&_length),
deba@1699
   316
      _pred(0), local_pred(false),
deba@1699
   317
      _dist(0), local_dist(false) {}
deba@1699
   318
    
deba@1699
   319
    ///Destructor.
deba@1699
   320
    ~Johnson() {
deba@1699
   321
      if(local_pred) delete _pred;
deba@1699
   322
      if(local_dist) delete _dist;
deba@1699
   323
    }
deba@1699
   324
deba@1699
   325
    /// \brief Sets the length map.
deba@1699
   326
    ///
deba@1699
   327
    /// Sets the length map.
deba@1699
   328
    /// \return \c (*this)
deba@1699
   329
    Johnson &lengthMap(const LengthMap &m) {
deba@1699
   330
      length = &m;
deba@1699
   331
      return *this;
deba@1699
   332
    }
deba@1699
   333
deba@1699
   334
    /// \brief Sets the map storing the predecessor edges.
deba@1699
   335
    ///
deba@1699
   336
    /// Sets the map storing the predecessor edges.
deba@1699
   337
    /// If you don't use this function before calling \ref run(),
deba@1699
   338
    /// it will allocate one. The destuctor deallocates this
deba@1699
   339
    /// automatically allocated map, of course.
deba@1699
   340
    /// \return \c (*this)
deba@1699
   341
    Johnson &predMap(PredMap &m) {
deba@1699
   342
      if(local_pred) {
deba@1699
   343
	delete _pred;
deba@1699
   344
	local_pred=false;
deba@1699
   345
      }
deba@1699
   346
      _pred = &m;
deba@1699
   347
      return *this;
deba@1699
   348
    }
deba@1699
   349
deba@1699
   350
    /// \brief Sets the map storing the distances calculated by the algorithm.
deba@1699
   351
    ///
deba@1699
   352
    /// Sets the map storing the distances calculated by the algorithm.
deba@1699
   353
    /// If you don't use this function before calling \ref run(),
deba@1699
   354
    /// it will allocate one. The destuctor deallocates this
deba@1699
   355
    /// automatically allocated map, of course.
deba@1699
   356
    /// \return \c (*this)
deba@1699
   357
    Johnson &distMap(DistMap &m) {
deba@1699
   358
      if(local_dist) {
deba@1699
   359
	delete _dist;
deba@1699
   360
	local_dist=false;
deba@1699
   361
      }
deba@1699
   362
      _dist = &m;
deba@1699
   363
      return *this;
deba@1699
   364
    }
deba@1699
   365
deba@1699
   366
    ///\name Execution control
deba@1699
   367
    /// The simplest way to execute the algorithm is to use
deba@1699
   368
    /// one of the member functions called \c run(...).
deba@1699
   369
    /// \n
deba@1699
   370
    /// If you need more control on the execution,
deba@1699
   371
    /// Finally \ref start() will perform the actual path
deba@1699
   372
    /// computation.
deba@1699
   373
deba@1699
   374
    ///@{
deba@1699
   375
deba@1699
   376
    /// \brief Initializes the internal data structures.
deba@1699
   377
    /// 
deba@1699
   378
    /// Initializes the internal data structures.
deba@1699
   379
    void init() {
deba@1699
   380
      create_maps();
deba@1699
   381
    }
deba@1699
   382
    
deba@1699
   383
    /// \brief Executes the algorithm.
deba@1699
   384
    ///
deba@1699
   385
    /// This method runs the %Johnson algorithm in order to compute 
deba@1699
   386
    /// the shortest path to each node pairs. The algorithm 
deba@1699
   387
    /// computes 
deba@1699
   388
    /// - The shortest path tree for each node.
deba@1699
   389
    /// - The distance between each node pairs.
deba@1699
   390
    void start() {
deba@1710
   391
      typedef typename BelmannFord<Graph, LengthMap>::
deba@1699
   392
      template DefOperationTraits<OperationTraits>::
deba@1710
   393
      template DefPredMap<NullMap<Node, Edge> >::
deba@1710
   394
      Create BelmannFordType;
deba@1710
   395
deba@1710
   396
      BelmannFordType belmannford(*graph, *length);
deba@1710
   397
deba@1710
   398
      NullMap<Node, Edge> predMap;
deba@1710
   399
deba@1710
   400
      belmannford.predMap(predMap);
deba@1699
   401
      
deba@1710
   402
      belmannford.init(OperationTraits::zero());
deba@1699
   403
      belmannford.start();
deba@1699
   404
deba@1699
   405
      for (NodeIt it(*graph); it != INVALID; ++it) {
deba@1699
   406
	typedef PotentialDifferenceMap<Graph, 
deba@1710
   407
	  typename BelmannFordType::DistMap> PotDiffMap;
deba@1699
   408
	PotDiffMap potdiff(*graph, belmannford.distMap());
deba@1699
   409
	typedef SubMap<LengthMap, PotDiffMap> ShiftLengthMap;
deba@1699
   410
	ShiftLengthMap shiftlen(*length, potdiff);
deba@1699
   411
	Dijkstra<Graph, ShiftLengthMap> dijkstra(*graph, shiftlen); 
deba@1699
   412
	dijkstra.run(it);
deba@1699
   413
	for (NodeIt jt(*graph); jt != INVALID; ++jt) {
deba@1699
   414
	  if (dijkstra.reached(jt)) {
deba@1699
   415
	    _dist->set(it, jt, dijkstra.dist(jt) + 
deba@1699
   416
		       belmannford.dist(jt) - belmannford.dist(it));
deba@1699
   417
	    _pred->set(it, jt, dijkstra.pred(jt));
deba@1699
   418
	  } else {
deba@1699
   419
	    _dist->set(it, jt, OperationTraits::infinity());
deba@1699
   420
	    _pred->set(it, jt, INVALID);
deba@1699
   421
	  }
deba@1699
   422
	}
deba@1699
   423
      }
deba@1699
   424
    }
deba@1699
   425
    
deba@1699
   426
    /// \brief Runs %Johnson algorithm.
deba@1699
   427
    ///    
deba@1699
   428
    /// This method runs the %Johnson algorithm from a each node
deba@1699
   429
    /// in order to compute the shortest path to each node pairs. 
deba@1699
   430
    /// The algorithm computes
deba@1699
   431
    /// - The shortest path tree for each node.
deba@1699
   432
    /// - The distance between each node pairs.
deba@1699
   433
    ///
deba@1699
   434
    /// \note d.run(s) is just a shortcut of the following code.
deba@1699
   435
    /// \code
deba@1699
   436
    ///  d.init();
deba@1699
   437
    ///  d.start();
deba@1699
   438
    /// \endcode
deba@1699
   439
    void run() {
deba@1699
   440
      init();
deba@1699
   441
      start();
deba@1699
   442
    }
deba@1699
   443
    
deba@1699
   444
    ///@}
deba@1699
   445
deba@1699
   446
    /// \name Query Functions
deba@1699
   447
    /// The result of the %Johnson algorithm can be obtained using these
deba@1699
   448
    /// functions.\n
deba@1699
   449
    /// Before the use of these functions,
deba@1699
   450
    /// either run() or start() must be called.
deba@1699
   451
    
deba@1699
   452
    ///@{
deba@1699
   453
deba@1699
   454
    /// \brief Copies the shortest path to \c t into \c p
deba@1699
   455
    ///    
deba@1699
   456
    /// This function copies the shortest path to \c t into \c p.
deba@1699
   457
    /// If it \c t is a source itself or unreachable, then it does not
deba@1699
   458
    /// alter \c p.
deba@1699
   459
    /// \todo Is it the right way to handle unreachable nodes?
deba@1699
   460
    /// \return Returns \c true if a path to \c t was actually copied to \c p,
deba@1699
   461
    /// \c false otherwise.
deba@1699
   462
    /// \sa DirPath
deba@1699
   463
    template <typename Path>
deba@1699
   464
    bool getPath(Path &p, Node source, Node target) {
deba@1699
   465
      if (connected(source, target)) {
deba@1699
   466
	p.clear();
deba@1699
   467
	typename Path::Builder b(target);
deba@1699
   468
	for(b.setStartNode(target); pred(source, target) != INVALID;
deba@1699
   469
	    target = predNode(target)) {
deba@1699
   470
	  b.pushFront(pred(source, target));
deba@1699
   471
	}
deba@1699
   472
	b.commit();
deba@1699
   473
	return true;
deba@1699
   474
      }
deba@1699
   475
      return false;
deba@1699
   476
    }
deba@1699
   477
	  
deba@1699
   478
    /// \brief The distance between two nodes.
deba@1699
   479
    ///
deba@1699
   480
    /// Returns the distance between two nodes.
deba@1699
   481
    /// \pre \ref run() must be called before using this function.
deba@1699
   482
    /// \warning If node \c v in unreachable from the root the return value
deba@1699
   483
    /// of this funcion is undefined.
deba@1699
   484
    Value dist(Node source, Node target) const { 
deba@1699
   485
      return (*_dist)(source, target); 
deba@1699
   486
    }
deba@1699
   487
deba@1699
   488
    /// \brief Returns the 'previous edge' of the shortest path tree.
deba@1699
   489
    ///
deba@1699
   490
    /// For the node \c node it returns the 'previous edge' of the shortest 
deba@1699
   491
    /// path tree to direction of the node \c root 
deba@1699
   492
    /// i.e. it returns the last edge of a shortest path from the node \c root 
deba@1699
   493
    /// to \c node. It is \ref INVALID if \c node is unreachable from the root
deba@1699
   494
    /// or if \c node=root. The shortest path tree used here is equal to the 
deba@1699
   495
    /// shortest path tree used in \ref predNode(). 
deba@1699
   496
    /// \pre \ref run() must be called before using this function.
deba@1699
   497
    /// \todo predEdge could be a better name.
deba@1699
   498
    Edge pred(Node root, Node node) const { 
deba@1699
   499
      return (*_pred)(root, node); 
deba@1699
   500
    }
deba@1699
   501
deba@1699
   502
    /// \brief Returns the 'previous node' of the shortest path tree.
deba@1699
   503
    ///
deba@1699
   504
    /// For a node \c node it returns the 'previous node' of the shortest path 
deba@1699
   505
    /// tree to direction of the node \c root, i.e. it returns the last but 
deba@1699
   506
    /// one node from a shortest path from the \c root to \c node. It is 
deba@1699
   507
    /// INVALID if \c node is unreachable from the root or if \c node=root. 
deba@1699
   508
    /// The shortest path tree used here is equal to the 
deba@1699
   509
    /// shortest path tree used in \ref pred().  
deba@1699
   510
    /// \pre \ref run() must be called before using this function.
deba@1699
   511
    Node predNode(Node root, Node node) const { 
deba@1699
   512
      return (*_pred)(root, node) == INVALID ? 
deba@1699
   513
      INVALID : graph->source((*_pred)(root, node)); 
deba@1699
   514
    }
deba@1699
   515
    
deba@1699
   516
    /// \brief Returns a reference to the matrix node map of distances.
deba@1699
   517
    ///
deba@1699
   518
    /// Returns a reference to the matrix node map of distances. 
deba@1699
   519
    ///
deba@1699
   520
    /// \pre \ref run() must be called before using this function.
deba@1699
   521
    const DistMap &distMap() const { return *_dist;}
deba@1699
   522
 
deba@1699
   523
    /// \brief Returns a reference to the shortest path tree map.
deba@1699
   524
    ///
deba@1699
   525
    /// Returns a reference to the matrix node map of the edges of the
deba@1699
   526
    /// shortest path tree.
deba@1699
   527
    /// \pre \ref run() must be called before using this function.
deba@1699
   528
    const PredMap &predMap() const { return *_pred;}
deba@1699
   529
 
deba@1699
   530
    /// \brief Checks if a node is reachable from the root.
deba@1699
   531
    ///
deba@1699
   532
    /// Returns \c true if \c v is reachable from the root.
deba@1699
   533
    /// \pre \ref run() must be called before using this function.
deba@1699
   534
    ///
deba@1699
   535
    bool connected(Node source, Node target) { 
deba@1699
   536
      return (*_dist)(source, target) != OperationTraits::infinity(); 
deba@1699
   537
    }
deba@1699
   538
    
deba@1699
   539
    ///@}
deba@1699
   540
  };
deba@1699
   541
 
deba@1699
   542
} //END OF NAMESPACE LEMON
deba@1699
   543
deba@1699
   544
#endif