lemon/johnson.h
author deba
Tue, 17 Oct 2006 10:50:57 +0000
changeset 2247 269a0dcee70b
parent 2151 38ec4a930c05
child 2260 4274224f8a7d
permissions -rw-r--r--
Update the Path concept
Concept check for paths

DirPath renamed to Path
The interface updated to the new lemon interface
Make difference between the empty path and the path from one node
Builder interface have not been changed
// I wanted but there was not accordance about it

UPath is removed
It was a buggy implementation, it could not iterate on the
nodes in the right order
Right way to use undirected paths => path of edges in undirected graphs

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