deba@1699: /* -*- C++ -*- deba@1699: * alpar@1956: * This file is a part of LEMON, a generic C++ optimization library alpar@1956: * alpar@1956: * Copyright (C) 2003-2006 alpar@1956: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@1699: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@1699: * deba@1699: * Permission to use, modify and distribute this software is granted deba@1699: * provided that this copyright notice appears in all copies. For deba@1699: * precise terms see the accompanying LICENSE file. deba@1699: * deba@1699: * This software is provided "AS IS" with no warranty of any kind, deba@1699: * express or implied, and with no claim as to its suitability for any deba@1699: * purpose. deba@1699: * deba@1699: */ deba@1699: deba@1699: #ifndef LEMON_JOHNSON_H deba@1699: #define LEMON_JOHNSON_H deba@1699: deba@2376: ///\ingroup shortest_path deba@1699: /// \file deba@1699: /// \brief Johnson algorithm. deba@1699: /// deba@1699: deba@1699: #include deba@1699: #include deba@1699: #include deba@1864: #include deba@2335: #include deba@1993: #include deba@1699: #include deba@1699: #include deba@1723: #include deba@1699: deba@1699: #include deba@1699: deba@1699: namespace lemon { deba@1699: deba@1699: /// \brief Default OperationTraits for the Johnson algorithm class. deba@1699: /// deba@1699: /// It defines all computational operations and constants which are deba@1699: /// used in the Floyd-Warshall algorithm. The default implementation deba@1699: /// is based on the numeric_limits class. If the numeric type does not deba@1699: /// have infinity value then the maximum value is used as extremal deba@1699: /// infinity value. deba@1699: template < deba@1699: typename Value, deba@1699: bool has_infinity = std::numeric_limits::has_infinity> deba@1699: struct JohnsonDefaultOperationTraits { deba@1699: /// \brief Gives back the zero value of the type. deba@1699: static Value zero() { deba@1699: return static_cast(0); deba@1699: } deba@1699: /// \brief Gives back the positive infinity value of the type. deba@1699: static Value infinity() { deba@1699: return std::numeric_limits::infinity(); deba@1699: } deba@1699: /// \brief Gives back the sum of the given two elements. deba@1699: static Value plus(const Value& left, const Value& right) { deba@1699: return left + right; deba@1699: } deba@1699: /// \brief Gives back true only if the first value less than the second. deba@1699: static bool less(const Value& left, const Value& right) { deba@1699: return left < right; deba@1699: } deba@1699: }; deba@1699: deba@1699: template deba@1699: struct JohnsonDefaultOperationTraits { deba@1699: static Value zero() { deba@1699: return static_cast(0); deba@1699: } deba@1699: static Value infinity() { deba@1699: return std::numeric_limits::max(); deba@1699: } deba@1699: static Value plus(const Value& left, const Value& right) { deba@1699: if (left == infinity() || right == infinity()) return infinity(); deba@1699: return left + right; deba@1699: } deba@1699: static bool less(const Value& left, const Value& right) { deba@1699: return left < right; deba@1699: } deba@1699: }; deba@1699: deba@1699: /// \brief Default traits class of Johnson class. deba@1699: /// deba@1699: /// Default traits class of Johnson class. deba@1699: /// \param _Graph Graph type. deba@1699: /// \param _LegthMap Type of length map. deba@1699: template deba@1699: struct JohnsonDefaultTraits { deba@1699: /// The graph type the algorithm runs on. deba@1699: typedef _Graph Graph; deba@1699: deba@1699: /// \brief The type of the map that stores the edge lengths. deba@1699: /// deba@1699: /// The type of the map that stores the edge lengths. alpar@2260: /// It must meet the \ref concepts::ReadMap "ReadMap" concept. deba@1699: typedef _LengthMap LengthMap; deba@1699: deba@1699: // The type of the length of the edges. deba@1699: typedef typename _LengthMap::Value Value; deba@1699: deba@1864: /// \brief Operation traits for bellman-ford algorithm. deba@1699: /// deba@1699: /// It defines the infinity type on the given Value type deba@1699: /// and the used operation. deba@1699: /// \see JohnsonDefaultOperationTraits deba@1699: typedef JohnsonDefaultOperationTraits OperationTraits; deba@1741: deba@1741: /// The cross reference type used by heap. deba@1741: deba@1741: /// The cross reference type used by heap. deba@1741: /// Usually it is \c Graph::NodeMap. deba@1741: typedef typename Graph::template NodeMap HeapCrossRef; deba@1741: deba@1741: ///Instantiates a HeapCrossRef. deba@1741: deba@1741: ///This function instantiates a \ref HeapCrossRef. deba@1741: /// \param graph is the graph, to which we would like to define the deba@1741: /// HeapCrossRef. deba@1741: static HeapCrossRef *createHeapCrossRef(const Graph& graph) { deba@1741: return new HeapCrossRef(graph); deba@1741: } deba@1741: deba@1741: ///The heap type used by Dijkstra algorithm. deba@1741: deba@1741: ///The heap type used by Dijkstra algorithm. deba@1741: /// deba@1741: ///\sa BinHeap deba@1741: ///\sa Dijkstra mqrelly@2263: typedef BinHeap > Heap; deba@1741: deba@1741: ///Instantiates a Heap. deba@1741: deba@1741: ///This function instantiates a \ref Heap. deba@1741: /// \param crossRef The cross reference for the heap. deba@1741: static Heap *createHeap(HeapCrossRef& crossRef) { deba@1741: return new Heap(crossRef); deba@1741: } deba@1699: deba@1723: /// \brief The type of the matrix map that stores the last edges of the deba@1699: /// shortest paths. deba@1699: /// deba@1723: /// The type of the map that stores the last edges of the shortest paths. deba@1699: /// It must be a matrix map with \c Graph::Edge value type. deba@1699: /// deba@1723: typedef DynamicMatrixMap PredMap; deba@1699: deba@1699: /// \brief Instantiates a PredMap. deba@1699: /// deba@1699: /// This function instantiates a \ref PredMap. alpar@1953: /// \param graph is the graph, to which we would like to define the PredMap. deba@1699: /// \todo The graph alone may be insufficient for the initialization deba@1741: static PredMap *createPredMap(const Graph& graph) { deba@1699: return new PredMap(graph); deba@1699: } deba@1699: deba@1723: /// \brief The type of the matrix map that stores the dists of the nodes. deba@1699: /// deba@1723: /// The type of the matrix map that stores the dists of the nodes. alpar@2260: /// It must meet the \ref concepts::WriteMatrixMap "WriteMatrixMap" concept. deba@1699: /// deba@1723: typedef DynamicMatrixMap DistMap; deba@1723: deba@1699: /// \brief Instantiates a DistMap. deba@1699: /// deba@1699: /// This function instantiates a \ref DistMap. alpar@1953: /// \param graph is the graph, to which we would like to define the deba@1699: /// \ref DistMap deba@1699: static DistMap *createDistMap(const _Graph& graph) { deba@1699: return new DistMap(graph); deba@1699: } deba@1699: deba@1699: }; deba@1699: deba@1754: /// \brief %Johnson algorithm class. deba@1699: /// deba@2376: /// \ingroup shortest_path deba@1754: /// This class provides an efficient implementation of \c %Johnson deba@1699: /// algorithm. The edge lengths are passed to the algorithm using a alpar@2260: /// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any deba@1699: /// kind of length. deba@1699: /// alpar@1757: /// The algorithm solves the shortest path problem for each pair deba@1723: /// of node when the edges can have negative length but the graph should deba@1754: /// not contain cycles with negative sum of length. If we can assume deba@1723: /// that all edge is non-negative in the graph then the dijkstra algorithm deba@1723: /// should be used from each node. deba@1723: /// deba@2042: /// The complexity of this algorithm is \f$ O(n^2\log(n)+n\log(n)e) \f$ or deba@2042: /// with fibonacci heap \f$ O(n^2\log(n)+ne) \f$. Usually the fibonacci heap deba@1741: /// implementation is slower than either binary heap implementation or the deba@1741: /// Floyd-Warshall algorithm. deba@1723: /// deba@1699: /// The type of the length is determined by the alpar@2260: /// \ref concepts::ReadMap::Value "Value" of the length map. deba@1699: /// deba@1699: /// \param _Graph The graph type the algorithm runs on. The default value deba@1699: /// is \ref ListGraph. The value of _Graph is not used directly by deba@1699: /// Johnson, it is only passed to \ref JohnsonDefaultTraits. deba@1699: /// \param _LengthMap This read-only EdgeMap determines the lengths of the deba@1699: /// edges. It is read once for each edge, so the map may involve in deba@1699: /// relatively time consuming process to compute the edge length if deba@1699: /// it is necessary. The default map type is \ref alpar@2260: /// concepts::Graph::EdgeMap "Graph::EdgeMap". The value deba@1699: /// of _LengthMap is not used directly by Johnson, it is only passed deba@1699: /// to \ref JohnsonDefaultTraits. \param _Traits Traits class to set deba@1699: /// various data types used by the algorithm. The default traits deba@1699: /// class is \ref JohnsonDefaultTraits deba@1699: /// "JohnsonDefaultTraits<_Graph,_LengthMap>". See \ref deba@1699: /// JohnsonDefaultTraits for the documentation of a Johnson traits deba@1699: /// class. deba@1699: /// deba@1699: /// \author Balazs Dezso deba@1699: deba@1710: #ifdef DOXYGEN deba@1710: template deba@1710: #else deba@1699: template , deba@1699: typename _Traits=JohnsonDefaultTraits<_Graph,_LengthMap> > deba@1710: #endif deba@1699: class Johnson { deba@1699: public: deba@1699: deba@1699: /// \brief \ref Exception for uninitialized parameters. deba@1699: /// deba@1699: /// This error represents problems in the initialization deba@1699: /// of the parameters of the algorithms. deba@1699: deba@1699: class UninitializedParameter : public lemon::UninitializedParameter { deba@1699: public: alpar@2151: virtual const char* what() const throw() { deba@1699: return "lemon::Johnson::UninitializedParameter"; deba@1699: } deba@1699: }; deba@1699: deba@1699: typedef _Traits Traits; deba@1699: ///The type of the underlying graph. deba@1699: typedef typename _Traits::Graph Graph; deba@1699: deba@1699: typedef typename Graph::Node Node; deba@1699: typedef typename Graph::NodeIt NodeIt; deba@1699: typedef typename Graph::Edge Edge; deba@1699: typedef typename Graph::EdgeIt EdgeIt; deba@1699: deba@1699: /// \brief The type of the length of the edges. deba@1699: typedef typename _Traits::LengthMap::Value Value; deba@1699: /// \brief The type of the map that stores the edge lengths. deba@1699: typedef typename _Traits::LengthMap LengthMap; deba@1699: /// \brief The type of the map that stores the last deba@1699: /// edges of the shortest paths. The type of the PredMap deba@1699: /// is a matrix map for Edges deba@1699: typedef typename _Traits::PredMap PredMap; deba@1699: /// \brief The type of the map that stores the dists of the nodes. deba@1699: /// The type of the DistMap is a matrix map for Values deba@1699: typedef typename _Traits::DistMap DistMap; deba@1699: /// \brief The operation traits. deba@1699: typedef typename _Traits::OperationTraits OperationTraits; deba@1741: ///The cross reference type used for the current heap. deba@1741: typedef typename _Traits::HeapCrossRef HeapCrossRef; deba@1741: ///The heap type used by the dijkstra algorithm. deba@1741: typedef typename _Traits::Heap Heap; deba@1699: private: deba@1699: /// Pointer to the underlying graph. deba@1699: const Graph *graph; deba@1699: /// Pointer to the length map deba@1699: const LengthMap *length; deba@1699: ///Pointer to the map of predecessors edges. deba@1699: PredMap *_pred; deba@1699: ///Indicates if \ref _pred is locally allocated (\c true) or not. deba@1699: bool local_pred; deba@1699: ///Pointer to the map of distances. deba@1699: DistMap *_dist; deba@1699: ///Indicates if \ref _dist is locally allocated (\c true) or not. deba@1699: bool local_dist; deba@1741: ///Pointer to the heap cross references. deba@1741: HeapCrossRef *_heap_cross_ref; deba@1741: ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not. deba@1741: bool local_heap_cross_ref; deba@1741: ///Pointer to the heap. deba@1741: Heap *_heap; deba@1741: ///Indicates if \ref _heap is locally allocated (\c true) or not. deba@1741: bool local_heap; deba@1699: deba@1699: /// Creates the maps if necessary. deba@1699: void create_maps() { deba@1699: if(!_pred) { deba@1699: local_pred = true; deba@1699: _pred = Traits::createPredMap(*graph); deba@1699: } deba@1699: if(!_dist) { deba@1699: local_dist = true; deba@1699: _dist = Traits::createDistMap(*graph); deba@1699: } deba@1741: if (!_heap_cross_ref) { deba@1741: local_heap_cross_ref = true; deba@1741: _heap_cross_ref = Traits::createHeapCrossRef(*graph); deba@1741: } deba@1741: if (!_heap) { deba@1741: local_heap = true; deba@1741: _heap = Traits::createHeap(*_heap_cross_ref); deba@1741: } deba@1699: } deba@1741: deba@1699: public : deba@1741: deba@1699: /// \name Named template parameters deba@1699: deba@1699: ///@{ deba@1699: deba@1699: template deba@1699: struct DefPredMapTraits : public Traits { deba@1699: typedef T PredMap; deba@1699: static PredMap *createPredMap(const Graph& graph) { deba@1699: throw UninitializedParameter(); deba@1699: } deba@1699: }; deba@1699: deba@1699: /// \brief \ref named-templ-param "Named parameter" for setting PredMap deba@1699: /// type deba@1699: /// \ref named-templ-param "Named parameter" for setting PredMap type deba@1699: /// deba@1699: template deba@1710: struct DefPredMap deba@1710: : public Johnson< Graph, LengthMap, DefPredMapTraits > { deba@1710: typedef Johnson< Graph, LengthMap, DefPredMapTraits > Create; deba@1710: }; deba@1699: deba@1699: template deba@1699: struct DefDistMapTraits : public Traits { deba@1699: typedef T DistMap; deba@1699: static DistMap *createDistMap(const Graph& graph) { deba@1699: throw UninitializedParameter(); deba@1699: } deba@1699: }; deba@1699: /// \brief \ref named-templ-param "Named parameter" for setting DistMap deba@1699: /// type deba@1699: /// deba@1699: /// \ref named-templ-param "Named parameter" for setting DistMap type deba@1699: /// deba@1699: template deba@1710: struct DefDistMap deba@1710: : public Johnson< Graph, LengthMap, DefDistMapTraits > { deba@1710: typedef Johnson< Graph, LengthMap, DefDistMapTraits > Create; deba@1710: }; deba@1699: deba@1699: template deba@1699: struct DefOperationTraitsTraits : public Traits { deba@1699: typedef T OperationTraits; deba@1699: }; deba@1699: deba@1699: /// \brief \ref named-templ-param "Named parameter" for setting deba@1699: /// OperationTraits type deba@1699: /// deba@1710: /// \ref named-templ-param "Named parameter" for setting deba@1710: /// OperationTraits type deba@1699: template deba@1710: struct DefOperationTraits deba@1710: : public Johnson< Graph, LengthMap, DefOperationTraitsTraits > { deba@1710: typedef Johnson< Graph, LengthMap, DefOperationTraitsTraits > Create; deba@1710: }; deba@1741: deba@1741: template deba@1741: struct DefHeapTraits : public Traits { deba@1741: typedef CR HeapCrossRef; deba@1741: typedef H Heap; deba@1741: static HeapCrossRef *createHeapCrossRef(const Graph &) { deba@1741: throw UninitializedParameter(); deba@1741: } deba@1741: static Heap *createHeap(HeapCrossRef &) deba@1741: { deba@1741: throw UninitializedParameter(); deba@1741: } deba@1741: }; deba@1754: ///\brief \ref named-templ-param "Named parameter" for setting heap and deba@1754: ///cross reference type deba@2230: /// deba@1741: ///\ref named-templ-param "Named parameter" for setting heap and cross deba@1741: ///reference type deba@1741: /// deba@1741: template > deba@1741: struct DefHeap deba@1741: : public Johnson< Graph, LengthMap, DefHeapTraits > { deba@1741: typedef Johnson< Graph, LengthMap, DefHeapTraits > Create; deba@1741: }; deba@1741: deba@1741: template deba@1741: struct DefStandardHeapTraits : public Traits { deba@1741: typedef CR HeapCrossRef; deba@1741: typedef H Heap; deba@1741: static HeapCrossRef *createHeapCrossRef(const Graph &G) { deba@1741: return new HeapCrossRef(G); deba@1741: } deba@1741: static Heap *createHeap(HeapCrossRef &R) deba@1741: { deba@1741: return new Heap(R); deba@1741: } deba@1741: }; deba@2230: ///\brief \ref named-templ-param "Named parameter" for setting deba@2230: ///heap and cross reference type with automatic allocation deba@2230: /// deba@1741: ///\ref named-templ-param "Named parameter" for setting heap and cross deba@1741: ///reference type. It can allocate the heap and the cross reference deba@1741: ///object if the cross reference's constructor waits for the graph as deba@1741: ///parameter and the heap's constructor waits for the cross reference. deba@1741: template > deba@1741: struct DefStandardHeap deba@1741: : public Johnson< Graph, LengthMap, DefStandardHeapTraits > { deba@1741: typedef Johnson< Graph, LengthMap, DefStandardHeapTraits > deba@1741: Create; deba@1741: }; deba@1699: deba@1699: ///@} deba@1699: deba@1710: protected: deba@1710: deba@1710: Johnson() {} deba@1710: deba@1699: public: deba@1741: deba@1741: typedef Johnson Create; deba@1699: deba@1699: /// \brief Constructor. deba@1699: /// deba@1699: /// \param _graph the graph the algorithm will run on. deba@1699: /// \param _length the length map used by the algorithm. deba@1699: Johnson(const Graph& _graph, const LengthMap& _length) : deba@1699: graph(&_graph), length(&_length), deba@1699: _pred(0), local_pred(false), deba@1741: _dist(0), local_dist(false), deba@1741: _heap_cross_ref(0), local_heap_cross_ref(false), deba@1741: _heap(0), local_heap(false) {} deba@1699: deba@1699: ///Destructor. deba@1699: ~Johnson() { deba@1741: if (local_pred) delete _pred; deba@1741: if (local_dist) delete _dist; deba@1741: if (local_heap_cross_ref) delete _heap_cross_ref; deba@1741: if (local_heap) delete _heap; deba@1699: } deba@1699: deba@1699: /// \brief Sets the length map. deba@1699: /// deba@1699: /// Sets the length map. deba@1699: /// \return \c (*this) deba@1699: Johnson &lengthMap(const LengthMap &m) { deba@1699: length = &m; deba@1699: return *this; deba@1699: } deba@1699: deba@1699: /// \brief Sets the map storing the predecessor edges. deba@1699: /// deba@1699: /// Sets the map storing the predecessor edges. deba@1699: /// If you don't use this function before calling \ref run(), deba@1699: /// it will allocate one. The destuctor deallocates this deba@1699: /// automatically allocated map, of course. deba@1699: /// \return \c (*this) deba@1699: Johnson &predMap(PredMap &m) { deba@1699: if(local_pred) { deba@1699: delete _pred; deba@1699: local_pred=false; deba@1699: } deba@1699: _pred = &m; deba@1699: return *this; deba@1699: } deba@1699: deba@1699: /// \brief Sets the map storing the distances calculated by the algorithm. deba@1699: /// deba@1699: /// Sets the map storing the distances calculated by the algorithm. deba@1699: /// If you don't use this function before calling \ref run(), deba@1699: /// it will allocate one. The destuctor deallocates this deba@1699: /// automatically allocated map, of course. deba@1699: /// \return \c (*this) deba@1699: Johnson &distMap(DistMap &m) { deba@1699: if(local_dist) { deba@1699: delete _dist; deba@1699: local_dist=false; deba@1699: } deba@1699: _dist = &m; deba@1699: return *this; deba@1699: } deba@1699: deba@1916: public: deba@1916: deba@1916: ///\name Execution control deba@1916: /// The simplest way to execute the algorithm is to use deba@1916: /// one of the member functions called \c run(...). deba@1916: /// \n deba@1916: /// If you need more control on the execution, deba@1916: /// Finally \ref start() will perform the actual path deba@1916: /// computation. deba@1916: deba@1916: ///@{ deba@1916: deba@1916: /// \brief Initializes the internal data structures. deba@1916: /// deba@1916: /// Initializes the internal data structures. deba@1916: void init() { deba@1916: create_maps(); deba@1916: } deba@1916: deba@1916: /// \brief Executes the algorithm with own potential map. deba@1916: /// deba@1916: /// This method runs the %Johnson algorithm in order to compute deba@1916: /// the shortest path to each node pairs. The potential map deba@1916: /// can be given for this algorithm which usually calculated deba@1916: /// by the Bellman-Ford algorithm. If the graph does not have deba@1916: /// negative length edge then this start function can be used deba@1916: /// with constMap(0) parameter to omit the running time of deba@1916: /// the Bellman-Ford. deba@1916: /// The algorithm computes deba@1916: /// - The shortest path tree for each node. deba@1916: /// - The distance between each node pairs. deba@1754: template deba@1916: void shiftedStart(const PotentialMap& potential) { deba@1747: typename Graph::template EdgeMap shiftlen(*graph); deba@1747: for (EdgeIt it(*graph); it != INVALID; ++it) { deba@1747: shiftlen[it] = (*length)[it] deba@1754: + potential[graph->source(it)] deba@1754: - potential[graph->target(it)]; deba@1747: } deba@1747: deba@1747: typename Dijkstra >:: deba@1747: template DefHeap:: deba@1747: Create dijkstra(*graph, shiftlen); deba@1741: deba@1741: dijkstra.heap(*_heap, *_heap_cross_ref); deba@1741: deba@1741: for (NodeIt it(*graph); it != INVALID; ++it) { deba@1741: dijkstra.run(it); deba@1741: for (NodeIt jt(*graph); jt != INVALID; ++jt) { deba@1741: if (dijkstra.reached(jt)) { deba@1741: _dist->set(it, jt, dijkstra.dist(jt) + deba@1754: potential[jt] - potential[it]); deba@1763: _pred->set(it, jt, dijkstra.predEdge(jt)); deba@1741: } else { deba@1741: _dist->set(it, jt, OperationTraits::infinity()); deba@1741: _pred->set(it, jt, INVALID); deba@1741: } deba@1741: } deba@1741: } deba@1741: } deba@1741: deba@1699: /// \brief Executes the algorithm. deba@1699: /// deba@1699: /// This method runs the %Johnson algorithm in order to compute deba@1699: /// the shortest path to each node pairs. The algorithm deba@1699: /// computes deba@1699: /// - The shortest path tree for each node. deba@1699: /// - The distance between each node pairs. deba@1699: void start() { deba@1710: deba@1864: typedef typename BellmanFord:: deba@1754: template DefOperationTraits:: deba@1754: template DefPredMap >:: deba@1864: Create BellmanFordType; deba@1754: deba@1864: BellmanFordType bellmanford(*graph, *length); deba@1710: deba@1710: NullMap predMap; deba@1710: deba@1864: bellmanford.predMap(predMap); deba@1699: deba@1864: bellmanford.init(OperationTraits::zero()); deba@1864: bellmanford.start(); deba@1699: deba@1916: shiftedStart(bellmanford.distMap()); deba@1699: } deba@1741: deba@1754: /// \brief Executes the algorithm and checks the negatvie cycles. deba@1741: /// deba@1741: /// This method runs the %Johnson algorithm in order to compute deba@1741: /// the shortest path to each node pairs. If the graph contains deba@1754: /// negative cycle it gives back false. The algorithm deba@1741: /// computes deba@1741: /// - The shortest path tree for each node. deba@1741: /// - The distance between each node pairs. deba@1741: bool checkedStart() { deba@1754: deba@1864: typedef typename BellmanFord:: deba@1754: template DefOperationTraits:: deba@1754: template DefPredMap >:: deba@1864: Create BellmanFordType; deba@1741: deba@1864: BellmanFordType bellmanford(*graph, *length); deba@1741: deba@1741: NullMap predMap; deba@1741: deba@1864: bellmanford.predMap(predMap); deba@1741: deba@1864: bellmanford.init(OperationTraits::zero()); deba@1864: if (!bellmanford.checkedStart()) return false; deba@1741: deba@1916: shiftedStart(bellmanford.distMap()); deba@1741: return true; deba@1741: } deba@1741: deba@1699: deba@1699: /// \brief Runs %Johnson algorithm. deba@1699: /// deba@1699: /// This method runs the %Johnson algorithm from a each node deba@1699: /// in order to compute the shortest path to each node pairs. deba@1699: /// The algorithm computes deba@1699: /// - The shortest path tree for each node. deba@1699: /// - The distance between each node pairs. deba@1699: /// deba@1699: /// \note d.run(s) is just a shortcut of the following code. alpar@1946: ///\code deba@1699: /// d.init(); deba@1699: /// d.start(); alpar@1946: ///\endcode deba@1699: void run() { deba@1699: init(); deba@1699: start(); deba@1699: } deba@1699: deba@1699: ///@} deba@1699: deba@1699: /// \name Query Functions deba@1699: /// The result of the %Johnson algorithm can be obtained using these deba@1699: /// functions.\n deba@1699: /// Before the use of these functions, deba@1699: /// either run() or start() must be called. deba@1699: deba@1699: ///@{ deba@1699: deba@2335: typedef PredMatrixMapPath Path; deba@2335: deba@2335: ///Gives back the shortest path. deba@2335: deba@2335: ///Gives back the shortest path. deba@2335: ///\pre The \c t should be reachable from the \c t. deba@2335: Path path(Node s, Node t) deba@2335: { deba@2335: return Path(*graph, *_pred, s, t); deba@1699: } deba@1699: deba@1699: /// \brief The distance between two nodes. deba@1699: /// deba@1699: /// Returns the distance between two nodes. deba@1699: /// \pre \ref run() must be called before using this function. deba@1699: /// \warning If node \c v in unreachable from the root the return value deba@1699: /// of this funcion is undefined. deba@1699: Value dist(Node source, Node target) const { deba@1699: return (*_dist)(source, target); deba@1699: } deba@1699: deba@1699: /// \brief Returns the 'previous edge' of the shortest path tree. deba@1699: /// deba@1699: /// For the node \c node it returns the 'previous edge' of the shortest deba@1699: /// path tree to direction of the node \c root deba@1699: /// i.e. it returns the last edge of a shortest path from the node \c root deba@1699: /// to \c node. It is \ref INVALID if \c node is unreachable from the root deba@1699: /// or if \c node=root. The shortest path tree used here is equal to the deba@1699: /// shortest path tree used in \ref predNode(). deba@1699: /// \pre \ref run() must be called before using this function. deba@1763: Edge predEdge(Node root, Node node) const { deba@1699: return (*_pred)(root, node); deba@1699: } deba@1699: deba@1699: /// \brief Returns the 'previous node' of the shortest path tree. deba@1699: /// deba@1699: /// For a node \c node it returns the 'previous node' of the shortest path deba@1699: /// tree to direction of the node \c root, i.e. it returns the last but deba@1699: /// one node from a shortest path from the \c root to \c node. It is deba@1699: /// INVALID if \c node is unreachable from the root or if \c node=root. deba@1699: /// The shortest path tree used here is equal to the deba@1763: /// shortest path tree used in \ref predEdge(). deba@1699: /// \pre \ref run() must be called before using this function. deba@1699: Node predNode(Node root, Node node) const { deba@1699: return (*_pred)(root, node) == INVALID ? deba@1699: INVALID : graph->source((*_pred)(root, node)); deba@1699: } deba@1699: deba@1699: /// \brief Returns a reference to the matrix node map of distances. deba@1699: /// deba@1699: /// Returns a reference to the matrix node map of distances. deba@1699: /// deba@1699: /// \pre \ref run() must be called before using this function. deba@1699: const DistMap &distMap() const { return *_dist;} deba@1699: deba@1699: /// \brief Returns a reference to the shortest path tree map. deba@1699: /// deba@1699: /// Returns a reference to the matrix node map of the edges of the deba@1699: /// shortest path tree. deba@1699: /// \pre \ref run() must be called before using this function. deba@1699: const PredMap &predMap() const { return *_pred;} deba@1699: deba@1699: /// \brief Checks if a node is reachable from the root. deba@1699: /// deba@1699: /// Returns \c true if \c v is reachable from the root. deba@1699: /// \pre \ref run() must be called before using this function. deba@1699: /// deba@1699: bool connected(Node source, Node target) { deba@1699: return (*_dist)(source, target) != OperationTraits::infinity(); deba@1699: } deba@1699: deba@1699: ///@} deba@1699: }; deba@1699: deba@1699: } //END OF NAMESPACE LEMON deba@1699: deba@1699: #endif