lemon/johnson.h
author alpar
Mon, 05 Dec 2005 17:03:31 +0000
changeset 1847 7cbc12e42482
parent 1765 f15b3c09481c
child 1864 1788205e36af
permissions -rw-r--r--
- Changed and improved Timer interface
- several new member functions
- reset() -> restart() renaming
- TimeReport: a Timer that prints a report on destruction.
- counter.h: a tool to measure the number of streps of algorithms.
- New documentation module for time measuring and counting.
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@1723
    32
#include <lemon/matrix_maps.h>
deba@1699
    33
deba@1699
    34
#include <limits>
deba@1699
    35
deba@1699
    36
namespace lemon {
deba@1699
    37
deba@1699
    38
  /// \brief Default OperationTraits for the Johnson algorithm class.
deba@1699
    39
  ///  
deba@1699
    40
  /// It defines all computational operations and constants which are
deba@1699
    41
  /// used in the Floyd-Warshall algorithm. The default implementation
deba@1699
    42
  /// is based on the numeric_limits class. If the numeric type does not
deba@1699
    43
  /// have infinity value then the maximum value is used as extremal
deba@1699
    44
  /// infinity value.
deba@1699
    45
  template <
deba@1699
    46
    typename Value, 
deba@1699
    47
    bool has_infinity = std::numeric_limits<Value>::has_infinity>
deba@1699
    48
  struct JohnsonDefaultOperationTraits {
deba@1699
    49
    /// \brief Gives back the zero value of the type.
deba@1699
    50
    static Value zero() {
deba@1699
    51
      return static_cast<Value>(0);
deba@1699
    52
    }
deba@1699
    53
    /// \brief Gives back the positive infinity value of the type.
deba@1699
    54
    static Value infinity() {
deba@1699
    55
      return std::numeric_limits<Value>::infinity();
deba@1699
    56
    }
deba@1699
    57
    /// \brief Gives back the sum of the given two elements.
deba@1699
    58
    static Value plus(const Value& left, const Value& right) {
deba@1699
    59
      return left + right;
deba@1699
    60
    }
deba@1699
    61
    /// \brief Gives back true only if the first value less than the second.
deba@1699
    62
    static bool less(const Value& left, const Value& right) {
deba@1699
    63
      return left < right;
deba@1699
    64
    }
deba@1699
    65
  };
deba@1699
    66
deba@1699
    67
  template <typename Value>
deba@1699
    68
  struct JohnsonDefaultOperationTraits<Value, false> {
deba@1699
    69
    static Value zero() {
deba@1699
    70
      return static_cast<Value>(0);
deba@1699
    71
    }
deba@1699
    72
    static Value infinity() {
deba@1699
    73
      return std::numeric_limits<Value>::max();
deba@1699
    74
    }
deba@1699
    75
    static Value plus(const Value& left, const Value& right) {
deba@1699
    76
      if (left == infinity() || right == infinity()) return infinity();
deba@1699
    77
      return left + right;
deba@1699
    78
    }
deba@1699
    79
    static bool less(const Value& left, const Value& right) {
deba@1699
    80
      return left < right;
deba@1699
    81
    }
deba@1699
    82
  };
deba@1699
    83
  
deba@1699
    84
  /// \brief Default traits class of Johnson class.
deba@1699
    85
  ///
deba@1699
    86
  /// Default traits class of Johnson class.
deba@1699
    87
  /// \param _Graph Graph type.
deba@1699
    88
  /// \param _LegthMap Type of length map.
deba@1699
    89
  template<class _Graph, class _LengthMap>
deba@1699
    90
  struct JohnsonDefaultTraits {
deba@1699
    91
    /// The graph type the algorithm runs on. 
deba@1699
    92
    typedef _Graph Graph;
deba@1699
    93
deba@1699
    94
    /// \brief The type of the map that stores the edge lengths.
deba@1699
    95
    ///
deba@1699
    96
    /// The type of the map that stores the edge lengths.
deba@1699
    97
    /// It must meet the \ref concept::ReadMap "ReadMap" concept.
deba@1699
    98
    typedef _LengthMap LengthMap;
deba@1699
    99
deba@1699
   100
    // The type of the length of the edges.
deba@1699
   101
    typedef typename _LengthMap::Value Value;
deba@1699
   102
deba@1699
   103
    /// \brief Operation traits for belmann-ford algorithm.
deba@1699
   104
    ///
deba@1699
   105
    /// It defines the infinity type on the given Value type
deba@1699
   106
    /// and the used operation.
deba@1699
   107
    /// \see JohnsonDefaultOperationTraits
deba@1699
   108
    typedef JohnsonDefaultOperationTraits<Value> OperationTraits;
deba@1741
   109
deba@1741
   110
    /// The cross reference type used by heap.
deba@1741
   111
deba@1741
   112
    /// The cross reference type used by heap.
deba@1741
   113
    /// Usually it is \c Graph::NodeMap<int>.
deba@1741
   114
    typedef typename Graph::template NodeMap<int> HeapCrossRef;
deba@1741
   115
deba@1741
   116
    ///Instantiates a HeapCrossRef.
deba@1741
   117
deba@1741
   118
    ///This function instantiates a \ref HeapCrossRef. 
deba@1741
   119
    /// \param graph is the graph, to which we would like to define the 
deba@1741
   120
    /// HeapCrossRef.
deba@1741
   121
    static HeapCrossRef *createHeapCrossRef(const Graph& graph) {
deba@1741
   122
      return new HeapCrossRef(graph);
deba@1741
   123
    }
deba@1741
   124
    
deba@1741
   125
    ///The heap type used by Dijkstra algorithm.
deba@1741
   126
deba@1741
   127
    ///The heap type used by Dijkstra algorithm.
deba@1741
   128
    ///
deba@1741
   129
    ///\sa BinHeap
deba@1741
   130
    ///\sa Dijkstra
deba@1741
   131
    typedef BinHeap<typename Graph::Node, typename LengthMap::Value,
deba@1741
   132
		    HeapCrossRef, std::less<Value> > Heap;
deba@1741
   133
deba@1741
   134
    ///Instantiates a Heap.
deba@1741
   135
deba@1741
   136
    ///This function instantiates a \ref Heap. 
deba@1741
   137
    /// \param crossRef The cross reference for the heap.
deba@1741
   138
    static Heap *createHeap(HeapCrossRef& crossRef) {
deba@1741
   139
      return new Heap(crossRef);
deba@1741
   140
    }
deba@1699
   141
 
deba@1723
   142
    /// \brief The type of the matrix map that stores the last edges of the 
deba@1699
   143
    /// shortest paths.
deba@1699
   144
    /// 
deba@1723
   145
    /// The type of the map that stores the last edges of the shortest paths.
deba@1699
   146
    /// It must be a matrix map with \c Graph::Edge value type.
deba@1699
   147
    ///
deba@1723
   148
    typedef DynamicMatrixMap<Graph, typename Graph::Node, 
deba@1723
   149
			     typename Graph::Edge> PredMap;
deba@1699
   150
deba@1699
   151
    /// \brief Instantiates a PredMap.
deba@1699
   152
    /// 
deba@1699
   153
    /// This function instantiates a \ref PredMap. 
deba@1699
   154
    /// \param G is the graph, to which we would like to define the PredMap.
deba@1699
   155
    /// \todo The graph alone may be insufficient for the initialization
deba@1741
   156
    static PredMap *createPredMap(const Graph& graph) {
deba@1699
   157
      return new PredMap(graph);
deba@1699
   158
    }
deba@1699
   159
deba@1723
   160
    /// \brief The type of the matrix map that stores the dists of the nodes.
deba@1699
   161
    ///
deba@1723
   162
    /// The type of the matrix map that stores the dists of the nodes.
deba@1723
   163
    /// It must meet the \ref concept::WriteMatrixMap "WriteMatrixMap" concept.
deba@1699
   164
    ///
deba@1723
   165
    typedef DynamicMatrixMap<Graph, typename Graph::Node, Value> DistMap;
deba@1723
   166
    
deba@1699
   167
    /// \brief Instantiates a DistMap.
deba@1699
   168
    ///
deba@1699
   169
    /// This function instantiates a \ref DistMap. 
deba@1699
   170
    /// \param G is the graph, to which we would like to define the 
deba@1699
   171
    /// \ref DistMap
deba@1699
   172
    static DistMap *createDistMap(const _Graph& graph) {
deba@1699
   173
      return new DistMap(graph);
deba@1699
   174
    }
deba@1699
   175
deba@1699
   176
  };
deba@1699
   177
deba@1754
   178
  /// \brief %Johnson algorithm class.
deba@1699
   179
  ///
deba@1699
   180
  /// \ingroup flowalgs
deba@1754
   181
  /// This class provides an efficient implementation of \c %Johnson 
deba@1699
   182
  /// algorithm. The edge lengths are passed to the algorithm using a
deba@1699
   183
  /// \ref concept::ReadMap "ReadMap", so it is easy to change it to any 
deba@1699
   184
  /// kind of length.
deba@1699
   185
  ///
alpar@1757
   186
  /// The algorithm solves the shortest path problem for each pair
deba@1723
   187
  /// of node when the edges can have negative length but the graph should
deba@1754
   188
  /// not contain cycles with negative sum of length. If we can assume
deba@1723
   189
  /// that all edge is non-negative in the graph then the dijkstra algorithm
deba@1723
   190
  /// should be used from each node.
deba@1723
   191
  ///
deba@1723
   192
  /// The complexity of this algorithm is $O(n^2 * log(n) + n * log(n) * e)$ or
deba@1741
   193
  /// with fibonacci heap O(n^2 * log(n) + n * e). Usually the fibonacci heap
deba@1741
   194
  /// implementation is slower than either binary heap implementation or the 
deba@1741
   195
  /// Floyd-Warshall algorithm. 
deba@1723
   196
  ///
deba@1699
   197
  /// The type of the length is determined by the
deba@1699
   198
  /// \ref concept::ReadMap::Value "Value" of the length map.
deba@1699
   199
  ///
deba@1699
   200
  /// \param _Graph The graph type the algorithm runs on. The default value
deba@1699
   201
  /// is \ref ListGraph. The value of _Graph is not used directly by
deba@1699
   202
  /// Johnson, it is only passed to \ref JohnsonDefaultTraits.
deba@1699
   203
  /// \param _LengthMap This read-only EdgeMap determines the lengths of the
deba@1699
   204
  /// edges. It is read once for each edge, so the map may involve in
deba@1699
   205
  /// relatively time consuming process to compute the edge length if
deba@1699
   206
  /// it is necessary. The default map type is \ref
deba@1699
   207
  /// concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".  The value
deba@1699
   208
  /// of _LengthMap is not used directly by Johnson, it is only passed 
deba@1699
   209
  /// to \ref JohnsonDefaultTraits.  \param _Traits Traits class to set
deba@1699
   210
  /// various data types used by the algorithm.  The default traits
deba@1699
   211
  /// class is \ref JohnsonDefaultTraits
deba@1699
   212
  /// "JohnsonDefaultTraits<_Graph,_LengthMap>".  See \ref
deba@1699
   213
  /// JohnsonDefaultTraits for the documentation of a Johnson traits
deba@1699
   214
  /// class.
deba@1699
   215
  ///
deba@1699
   216
  /// \author Balazs Dezso
deba@1699
   217
deba@1710
   218
#ifdef DOXYGEN
deba@1710
   219
  template <typename _Graph, typename _LengthMap, typename _Traits>
deba@1710
   220
#else
deba@1699
   221
  template <typename _Graph=ListGraph,
deba@1699
   222
	    typename _LengthMap=typename _Graph::template EdgeMap<int>,
deba@1699
   223
	    typename _Traits=JohnsonDefaultTraits<_Graph,_LengthMap> >
deba@1710
   224
#endif
deba@1699
   225
  class Johnson {
deba@1699
   226
  public:
deba@1699
   227
    
deba@1699
   228
    /// \brief \ref Exception for uninitialized parameters.
deba@1699
   229
    ///
deba@1699
   230
    /// This error represents problems in the initialization
deba@1699
   231
    /// of the parameters of the algorithms.
deba@1699
   232
deba@1699
   233
    class UninitializedParameter : public lemon::UninitializedParameter {
deba@1699
   234
    public:
deba@1699
   235
      virtual const char* exceptionName() const {
deba@1699
   236
	return "lemon::Johnson::UninitializedParameter";
deba@1699
   237
      }
deba@1699
   238
    };
deba@1699
   239
deba@1699
   240
    typedef _Traits Traits;
deba@1699
   241
    ///The type of the underlying graph.
deba@1699
   242
    typedef typename _Traits::Graph Graph;
deba@1699
   243
deba@1699
   244
    typedef typename Graph::Node Node;
deba@1699
   245
    typedef typename Graph::NodeIt NodeIt;
deba@1699
   246
    typedef typename Graph::Edge Edge;
deba@1699
   247
    typedef typename Graph::EdgeIt EdgeIt;
deba@1699
   248
    
deba@1699
   249
    /// \brief The type of the length of the edges.
deba@1699
   250
    typedef typename _Traits::LengthMap::Value Value;
deba@1699
   251
    /// \brief The type of the map that stores the edge lengths.
deba@1699
   252
    typedef typename _Traits::LengthMap LengthMap;
deba@1699
   253
    /// \brief The type of the map that stores the last
deba@1699
   254
    /// edges of the shortest paths. The type of the PredMap
deba@1699
   255
    /// is a matrix map for Edges
deba@1699
   256
    typedef typename _Traits::PredMap PredMap;
deba@1699
   257
    /// \brief The type of the map that stores the dists of the nodes.
deba@1699
   258
    /// The type of the DistMap is a matrix map for Values
deba@1699
   259
    typedef typename _Traits::DistMap DistMap;
deba@1699
   260
    /// \brief The operation traits.
deba@1699
   261
    typedef typename _Traits::OperationTraits OperationTraits;
deba@1741
   262
    ///The cross reference type used for the current heap.
deba@1741
   263
    typedef typename _Traits::HeapCrossRef HeapCrossRef;
deba@1741
   264
    ///The heap type used by the dijkstra algorithm.
deba@1741
   265
    typedef typename _Traits::Heap Heap;
deba@1699
   266
  private:
deba@1699
   267
    /// Pointer to the underlying graph.
deba@1699
   268
    const Graph *graph;
deba@1699
   269
    /// Pointer to the length map
deba@1699
   270
    const LengthMap *length;
deba@1699
   271
    ///Pointer to the map of predecessors edges.
deba@1699
   272
    PredMap *_pred;
deba@1699
   273
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
deba@1699
   274
    bool local_pred;
deba@1699
   275
    ///Pointer to the map of distances.
deba@1699
   276
    DistMap *_dist;
deba@1699
   277
    ///Indicates if \ref _dist is locally allocated (\c true) or not.
deba@1699
   278
    bool local_dist;
deba@1741
   279
    ///Pointer to the heap cross references.
deba@1741
   280
    HeapCrossRef *_heap_cross_ref;
deba@1741
   281
    ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
deba@1741
   282
    bool local_heap_cross_ref;
deba@1741
   283
    ///Pointer to the heap.
deba@1741
   284
    Heap *_heap;
deba@1741
   285
    ///Indicates if \ref _heap is locally allocated (\c true) or not.
deba@1741
   286
    bool local_heap;
deba@1699
   287
deba@1699
   288
    /// Creates the maps if necessary.
deba@1699
   289
    void create_maps() {
deba@1699
   290
      if(!_pred) {
deba@1699
   291
	local_pred = true;
deba@1699
   292
	_pred = Traits::createPredMap(*graph);
deba@1699
   293
      }
deba@1699
   294
      if(!_dist) {
deba@1699
   295
	local_dist = true;
deba@1699
   296
	_dist = Traits::createDistMap(*graph);
deba@1699
   297
      }
deba@1741
   298
      if (!_heap_cross_ref) {
deba@1741
   299
	local_heap_cross_ref = true;
deba@1741
   300
	_heap_cross_ref = Traits::createHeapCrossRef(*graph);
deba@1741
   301
      }
deba@1741
   302
      if (!_heap) {
deba@1741
   303
	local_heap = true;
deba@1741
   304
	_heap = Traits::createHeap(*_heap_cross_ref);
deba@1741
   305
      }
deba@1699
   306
    }
deba@1741
   307
deba@1699
   308
  public :
deba@1741
   309
deba@1699
   310
    /// \name Named template parameters
deba@1699
   311
deba@1699
   312
    ///@{
deba@1699
   313
deba@1699
   314
    template <class T>
deba@1699
   315
    struct DefPredMapTraits : public Traits {
deba@1699
   316
      typedef T PredMap;
deba@1699
   317
      static PredMap *createPredMap(const Graph& graph) {
deba@1699
   318
	throw UninitializedParameter();
deba@1699
   319
      }
deba@1699
   320
    };
deba@1699
   321
deba@1699
   322
    /// \brief \ref named-templ-param "Named parameter" for setting PredMap 
deba@1699
   323
    /// type
deba@1699
   324
    /// \ref named-templ-param "Named parameter" for setting PredMap type
deba@1699
   325
    ///
deba@1699
   326
    template <class T>
deba@1710
   327
    struct DefPredMap 
deba@1710
   328
      : public Johnson< Graph, LengthMap, DefPredMapTraits<T> > {
deba@1710
   329
      typedef Johnson< Graph, LengthMap, DefPredMapTraits<T> > Create;
deba@1710
   330
    };
deba@1699
   331
    
deba@1699
   332
    template <class T>
deba@1699
   333
    struct DefDistMapTraits : public Traits {
deba@1699
   334
      typedef T DistMap;
deba@1699
   335
      static DistMap *createDistMap(const Graph& graph) {
deba@1699
   336
	throw UninitializedParameter();
deba@1699
   337
      }
deba@1699
   338
    };
deba@1699
   339
    /// \brief \ref named-templ-param "Named parameter" for setting DistMap 
deba@1699
   340
    /// type
deba@1699
   341
    ///
deba@1699
   342
    /// \ref named-templ-param "Named parameter" for setting DistMap type
deba@1699
   343
    ///
deba@1699
   344
    template <class T>
deba@1710
   345
    struct DefDistMap 
deba@1710
   346
      : public Johnson< Graph, LengthMap, DefDistMapTraits<T> > {
deba@1710
   347
      typedef Johnson< Graph, LengthMap, DefDistMapTraits<T> > Create;
deba@1710
   348
    };
deba@1699
   349
    
deba@1699
   350
    template <class T>
deba@1699
   351
    struct DefOperationTraitsTraits : public Traits {
deba@1699
   352
      typedef T OperationTraits;
deba@1699
   353
    };
deba@1699
   354
    
deba@1699
   355
    /// \brief \ref named-templ-param "Named parameter" for setting 
deba@1699
   356
    /// OperationTraits type
deba@1699
   357
    ///
deba@1710
   358
    /// \ref named-templ-param "Named parameter" for setting 
deba@1710
   359
    /// OperationTraits type
deba@1699
   360
    template <class T>
deba@1710
   361
    struct DefOperationTraits
deba@1710
   362
      : public Johnson< Graph, LengthMap, DefOperationTraitsTraits<T> > {
deba@1710
   363
      typedef Johnson< Graph, LengthMap, DefOperationTraitsTraits<T> > Create;
deba@1710
   364
    };
deba@1741
   365
deba@1741
   366
    template <class H, class CR>
deba@1741
   367
    struct DefHeapTraits : public Traits {
deba@1741
   368
      typedef CR HeapCrossRef;
deba@1741
   369
      typedef H Heap;
deba@1741
   370
      static HeapCrossRef *createHeapCrossRef(const Graph &) {
deba@1741
   371
	throw UninitializedParameter();
deba@1741
   372
      }
deba@1741
   373
      static Heap *createHeap(HeapCrossRef &) 
deba@1741
   374
      {
deba@1741
   375
	throw UninitializedParameter();
deba@1741
   376
      }
deba@1741
   377
    };
deba@1754
   378
    ///\brief \ref named-templ-param "Named parameter" for setting heap and 
deba@1754
   379
    ///cross reference type
deba@1741
   380
deba@1741
   381
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1741
   382
    ///reference type
deba@1741
   383
    ///
deba@1741
   384
    template <class H, class CR = typename Graph::template NodeMap<int> >
deba@1741
   385
    struct DefHeap
deba@1741
   386
      : public Johnson< Graph, LengthMap, DefHeapTraits<H, CR> > { 
deba@1741
   387
      typedef Johnson< Graph, LengthMap, DefHeapTraits<H, CR> > Create;
deba@1741
   388
    };
deba@1741
   389
deba@1741
   390
    template <class H, class CR>
deba@1741
   391
    struct DefStandardHeapTraits : public Traits {
deba@1741
   392
      typedef CR HeapCrossRef;
deba@1741
   393
      typedef H Heap;
deba@1741
   394
      static HeapCrossRef *createHeapCrossRef(const Graph &G) {
deba@1741
   395
	return new HeapCrossRef(G);
deba@1741
   396
      }
deba@1741
   397
      static Heap *createHeap(HeapCrossRef &R) 
deba@1741
   398
      {
deba@1741
   399
	return new Heap(R);
deba@1741
   400
      }
deba@1741
   401
    };
deba@1741
   402
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1741
   403
    ///reference type with automatic allocation
deba@1741
   404
deba@1741
   405
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1741
   406
    ///reference type. It can allocate the heap and the cross reference 
deba@1741
   407
    ///object if the cross reference's constructor waits for the graph as 
deba@1741
   408
    ///parameter and the heap's constructor waits for the cross reference.
deba@1741
   409
    template <class H, class CR = typename Graph::template NodeMap<int> >
deba@1741
   410
    struct DefStandardHeap
deba@1741
   411
      : public Johnson< Graph, LengthMap, DefStandardHeapTraits<H, CR> > { 
deba@1741
   412
      typedef Johnson< Graph, LengthMap, DefStandardHeapTraits<H, CR> > 
deba@1741
   413
      Create;
deba@1741
   414
    };
deba@1699
   415
    
deba@1699
   416
    ///@}
deba@1699
   417
deba@1710
   418
  protected:
deba@1710
   419
deba@1710
   420
    Johnson() {}
deba@1710
   421
deba@1699
   422
  public:      
deba@1741
   423
deba@1741
   424
    typedef Johnson Create;
deba@1699
   425
    
deba@1699
   426
    /// \brief Constructor.
deba@1699
   427
    ///
deba@1699
   428
    /// \param _graph the graph the algorithm will run on.
deba@1699
   429
    /// \param _length the length map used by the algorithm.
deba@1699
   430
    Johnson(const Graph& _graph, const LengthMap& _length) :
deba@1699
   431
      graph(&_graph), length(&_length),
deba@1699
   432
      _pred(0), local_pred(false),
deba@1741
   433
      _dist(0), local_dist(false),
deba@1741
   434
      _heap_cross_ref(0), local_heap_cross_ref(false),
deba@1741
   435
      _heap(0), local_heap(false) {}
deba@1699
   436
    
deba@1699
   437
    ///Destructor.
deba@1699
   438
    ~Johnson() {
deba@1741
   439
      if (local_pred) delete _pred;
deba@1741
   440
      if (local_dist) delete _dist;
deba@1741
   441
      if (local_heap_cross_ref) delete _heap_cross_ref;
deba@1741
   442
      if (local_heap) delete _heap;
deba@1699
   443
    }
deba@1699
   444
deba@1699
   445
    /// \brief Sets the length map.
deba@1699
   446
    ///
deba@1699
   447
    /// Sets the length map.
deba@1699
   448
    /// \return \c (*this)
deba@1699
   449
    Johnson &lengthMap(const LengthMap &m) {
deba@1699
   450
      length = &m;
deba@1699
   451
      return *this;
deba@1699
   452
    }
deba@1699
   453
deba@1699
   454
    /// \brief Sets the map storing the predecessor edges.
deba@1699
   455
    ///
deba@1699
   456
    /// Sets the map storing the predecessor edges.
deba@1699
   457
    /// If you don't use this function before calling \ref run(),
deba@1699
   458
    /// it will allocate one. The destuctor deallocates this
deba@1699
   459
    /// automatically allocated map, of course.
deba@1699
   460
    /// \return \c (*this)
deba@1699
   461
    Johnson &predMap(PredMap &m) {
deba@1699
   462
      if(local_pred) {
deba@1699
   463
	delete _pred;
deba@1699
   464
	local_pred=false;
deba@1699
   465
      }
deba@1699
   466
      _pred = &m;
deba@1699
   467
      return *this;
deba@1699
   468
    }
deba@1699
   469
deba@1699
   470
    /// \brief Sets the map storing the distances calculated by the algorithm.
deba@1699
   471
    ///
deba@1699
   472
    /// Sets the map storing the distances calculated by the algorithm.
deba@1699
   473
    /// If you don't use this function before calling \ref run(),
deba@1699
   474
    /// it will allocate one. The destuctor deallocates this
deba@1699
   475
    /// automatically allocated map, of course.
deba@1699
   476
    /// \return \c (*this)
deba@1699
   477
    Johnson &distMap(DistMap &m) {
deba@1699
   478
      if(local_dist) {
deba@1699
   479
	delete _dist;
deba@1699
   480
	local_dist=false;
deba@1699
   481
      }
deba@1699
   482
      _dist = &m;
deba@1699
   483
      return *this;
deba@1699
   484
    }
deba@1699
   485
deba@1741
   486
  protected:
deba@1741
   487
    
deba@1754
   488
    template <typename PotentialMap>
deba@1754
   489
    void shiftedRun(const PotentialMap& potential) {
deba@1741
   490
      
deba@1747
   491
      typename Graph::template EdgeMap<Value> shiftlen(*graph);
deba@1747
   492
      for (EdgeIt it(*graph);  it != INVALID; ++it) {
deba@1747
   493
      	shiftlen[it] = (*length)[it] 
deba@1754
   494
	  + potential[graph->source(it)] 
deba@1754
   495
	  - potential[graph->target(it)];
deba@1747
   496
      }
deba@1747
   497
      
deba@1747
   498
      typename Dijkstra<Graph, typename Graph::template EdgeMap<Value> >::
deba@1747
   499
	template DefHeap<Heap, HeapCrossRef>::
deba@1747
   500
	Create dijkstra(*graph, shiftlen);
deba@1741
   501
deba@1741
   502
      dijkstra.heap(*_heap, *_heap_cross_ref);
deba@1741
   503
      
deba@1741
   504
      for (NodeIt it(*graph); it != INVALID; ++it) {
deba@1741
   505
	dijkstra.run(it);
deba@1741
   506
	for (NodeIt jt(*graph); jt != INVALID; ++jt) {
deba@1741
   507
	  if (dijkstra.reached(jt)) {
deba@1741
   508
	    _dist->set(it, jt, dijkstra.dist(jt) + 
deba@1754
   509
		       potential[jt] - potential[it]);
deba@1763
   510
	    _pred->set(it, jt, dijkstra.predEdge(jt));
deba@1741
   511
	  } else {
deba@1741
   512
	    _dist->set(it, jt, OperationTraits::infinity());
deba@1741
   513
	    _pred->set(it, jt, INVALID);
deba@1741
   514
	  }
deba@1741
   515
	}
deba@1741
   516
      }
deba@1741
   517
    }
deba@1741
   518
deba@1741
   519
  public:    
deba@1741
   520
deba@1699
   521
    ///\name Execution control
deba@1699
   522
    /// The simplest way to execute the algorithm is to use
deba@1699
   523
    /// one of the member functions called \c run(...).
deba@1699
   524
    /// \n
deba@1699
   525
    /// If you need more control on the execution,
deba@1699
   526
    /// Finally \ref start() will perform the actual path
deba@1699
   527
    /// computation.
deba@1699
   528
deba@1699
   529
    ///@{
deba@1699
   530
deba@1699
   531
    /// \brief Initializes the internal data structures.
deba@1699
   532
    /// 
deba@1699
   533
    /// Initializes the internal data structures.
deba@1699
   534
    void init() {
deba@1699
   535
      create_maps();
deba@1699
   536
    }
deba@1741
   537
deba@1699
   538
    /// \brief Executes the algorithm.
deba@1699
   539
    ///
deba@1699
   540
    /// This method runs the %Johnson algorithm in order to compute 
deba@1699
   541
    /// the shortest path to each node pairs. The algorithm 
deba@1699
   542
    /// computes 
deba@1699
   543
    /// - The shortest path tree for each node.
deba@1699
   544
    /// - The distance between each node pairs.
deba@1699
   545
    void start() {
deba@1710
   546
deba@1754
   547
      typedef typename BelmannFord<Graph, LengthMap>::
deba@1754
   548
      template DefOperationTraits<OperationTraits>::
deba@1754
   549
      template DefPredMap<NullMap<Node, Edge> >::
deba@1754
   550
      Create BelmannFordType;
deba@1754
   551
      
deba@1710
   552
      BelmannFordType belmannford(*graph, *length);
deba@1710
   553
deba@1710
   554
      NullMap<Node, Edge> predMap;
deba@1710
   555
deba@1710
   556
      belmannford.predMap(predMap);
deba@1699
   557
      
deba@1710
   558
      belmannford.init(OperationTraits::zero());
deba@1699
   559
      belmannford.start();
deba@1699
   560
deba@1754
   561
      shiftedRun(belmannford.distMap());
deba@1699
   562
    }
deba@1741
   563
deba@1754
   564
    /// \brief Executes the algorithm and checks the negatvie cycles.
deba@1741
   565
    ///
deba@1741
   566
    /// This method runs the %Johnson algorithm in order to compute 
deba@1741
   567
    /// the shortest path to each node pairs. If the graph contains
deba@1754
   568
    /// negative cycle it gives back false. The algorithm 
deba@1741
   569
    /// computes 
deba@1741
   570
    /// - The shortest path tree for each node.
deba@1741
   571
    /// - The distance between each node pairs.
deba@1741
   572
    bool checkedStart() {
deba@1754
   573
      
deba@1754
   574
      typedef typename BelmannFord<Graph, LengthMap>::
deba@1754
   575
      template DefOperationTraits<OperationTraits>::
deba@1754
   576
      template DefPredMap<NullMap<Node, Edge> >::
deba@1754
   577
      Create BelmannFordType;
deba@1741
   578
deba@1741
   579
      BelmannFordType belmannford(*graph, *length);
deba@1741
   580
deba@1741
   581
      NullMap<Node, Edge> predMap;
deba@1741
   582
deba@1741
   583
      belmannford.predMap(predMap);
deba@1741
   584
      
deba@1741
   585
      belmannford.init(OperationTraits::zero());
deba@1741
   586
      if (!belmannford.checkedStart()) return false;
deba@1741
   587
deba@1754
   588
      shiftedRun(belmannford.distMap());
deba@1741
   589
      return true;
deba@1741
   590
    }
deba@1741
   591
deba@1699
   592
    
deba@1699
   593
    /// \brief Runs %Johnson algorithm.
deba@1699
   594
    ///    
deba@1699
   595
    /// This method runs the %Johnson algorithm from a each node
deba@1699
   596
    /// in order to compute the shortest path to each node pairs. 
deba@1699
   597
    /// The algorithm computes
deba@1699
   598
    /// - The shortest path tree for each node.
deba@1699
   599
    /// - The distance between each node pairs.
deba@1699
   600
    ///
deba@1699
   601
    /// \note d.run(s) is just a shortcut of the following code.
deba@1699
   602
    /// \code
deba@1699
   603
    ///  d.init();
deba@1699
   604
    ///  d.start();
deba@1699
   605
    /// \endcode
deba@1699
   606
    void run() {
deba@1699
   607
      init();
deba@1699
   608
      start();
deba@1699
   609
    }
deba@1699
   610
    
deba@1699
   611
    ///@}
deba@1699
   612
deba@1699
   613
    /// \name Query Functions
deba@1699
   614
    /// The result of the %Johnson algorithm can be obtained using these
deba@1699
   615
    /// functions.\n
deba@1699
   616
    /// Before the use of these functions,
deba@1699
   617
    /// either run() or start() must be called.
deba@1699
   618
    
deba@1699
   619
    ///@{
deba@1699
   620
deba@1699
   621
    /// \brief Copies the shortest path to \c t into \c p
deba@1699
   622
    ///    
deba@1699
   623
    /// This function copies the shortest path to \c t into \c p.
deba@1699
   624
    /// If it \c t is a source itself or unreachable, then it does not
deba@1699
   625
    /// alter \c p.
deba@1699
   626
    /// \return Returns \c true if a path to \c t was actually copied to \c p,
deba@1699
   627
    /// \c false otherwise.
deba@1699
   628
    /// \sa DirPath
deba@1699
   629
    template <typename Path>
deba@1699
   630
    bool getPath(Path &p, Node source, Node target) {
deba@1699
   631
      if (connected(source, target)) {
deba@1699
   632
	p.clear();
deba@1699
   633
	typename Path::Builder b(target);
deba@1763
   634
	for(b.setStartNode(target); predEdge(source, target) != INVALID;
deba@1699
   635
	    target = predNode(target)) {
deba@1763
   636
	  b.pushFront(predEdge(source, target));
deba@1699
   637
	}
deba@1699
   638
	b.commit();
deba@1699
   639
	return true;
deba@1699
   640
      }
deba@1699
   641
      return false;
deba@1699
   642
    }
deba@1699
   643
	  
deba@1699
   644
    /// \brief The distance between two nodes.
deba@1699
   645
    ///
deba@1699
   646
    /// Returns the distance between two nodes.
deba@1699
   647
    /// \pre \ref run() must be called before using this function.
deba@1699
   648
    /// \warning If node \c v in unreachable from the root the return value
deba@1699
   649
    /// of this funcion is undefined.
deba@1699
   650
    Value dist(Node source, Node target) const { 
deba@1699
   651
      return (*_dist)(source, target); 
deba@1699
   652
    }
deba@1699
   653
deba@1699
   654
    /// \brief Returns the 'previous edge' of the shortest path tree.
deba@1699
   655
    ///
deba@1699
   656
    /// For the node \c node it returns the 'previous edge' of the shortest 
deba@1699
   657
    /// path tree to direction of the node \c root 
deba@1699
   658
    /// i.e. it returns the last edge of a shortest path from the node \c root 
deba@1699
   659
    /// to \c node. It is \ref INVALID if \c node is unreachable from the root
deba@1699
   660
    /// or if \c node=root. The shortest path tree used here is equal to the 
deba@1699
   661
    /// shortest path tree used in \ref predNode(). 
deba@1699
   662
    /// \pre \ref run() must be called before using this function.
deba@1763
   663
    Edge predEdge(Node root, Node node) const { 
deba@1699
   664
      return (*_pred)(root, node); 
deba@1699
   665
    }
deba@1699
   666
deba@1699
   667
    /// \brief Returns the 'previous node' of the shortest path tree.
deba@1699
   668
    ///
deba@1699
   669
    /// For a node \c node it returns the 'previous node' of the shortest path 
deba@1699
   670
    /// tree to direction of the node \c root, i.e. it returns the last but 
deba@1699
   671
    /// one node from a shortest path from the \c root to \c node. It is 
deba@1699
   672
    /// INVALID if \c node is unreachable from the root or if \c node=root. 
deba@1699
   673
    /// The shortest path tree used here is equal to the 
deba@1763
   674
    /// shortest path tree used in \ref predEdge().  
deba@1699
   675
    /// \pre \ref run() must be called before using this function.
deba@1699
   676
    Node predNode(Node root, Node node) const { 
deba@1699
   677
      return (*_pred)(root, node) == INVALID ? 
deba@1699
   678
      INVALID : graph->source((*_pred)(root, node)); 
deba@1699
   679
    }
deba@1699
   680
    
deba@1699
   681
    /// \brief Returns a reference to the matrix node map of distances.
deba@1699
   682
    ///
deba@1699
   683
    /// Returns a reference to the matrix node map of distances. 
deba@1699
   684
    ///
deba@1699
   685
    /// \pre \ref run() must be called before using this function.
deba@1699
   686
    const DistMap &distMap() const { return *_dist;}
deba@1699
   687
 
deba@1699
   688
    /// \brief Returns a reference to the shortest path tree map.
deba@1699
   689
    ///
deba@1699
   690
    /// Returns a reference to the matrix node map of the edges of the
deba@1699
   691
    /// shortest path tree.
deba@1699
   692
    /// \pre \ref run() must be called before using this function.
deba@1699
   693
    const PredMap &predMap() const { return *_pred;}
deba@1699
   694
 
deba@1699
   695
    /// \brief Checks if a node is reachable from the root.
deba@1699
   696
    ///
deba@1699
   697
    /// Returns \c true if \c v is reachable from the root.
deba@1699
   698
    /// \pre \ref run() must be called before using this function.
deba@1699
   699
    ///
deba@1699
   700
    bool connected(Node source, Node target) { 
deba@1699
   701
      return (*_dist)(source, target) != OperationTraits::infinity(); 
deba@1699
   702
    }
deba@1699
   703
    
deba@1699
   704
    ///@}
deba@1699
   705
  };
deba@1699
   706
 
deba@1699
   707
} //END OF NAMESPACE LEMON
deba@1699
   708
deba@1699
   709
#endif