alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*- alpar@100: * alpar@209: * This file is a part of LEMON, a generic C++ optimization library. alpar@100: * alpar@100: * Copyright (C) 2003-2008 alpar@100: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@100: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@100: * alpar@100: * Permission to use, modify and distribute this software is granted alpar@100: * provided that this copyright notice appears in all copies. For alpar@100: * precise terms see the accompanying LICENSE file. alpar@100: * alpar@100: * This software is provided "AS IS" with no warranty of any kind, alpar@100: * express or implied, and with no claim as to its suitability for any alpar@100: * purpose. alpar@100: * alpar@100: */ alpar@100: alpar@100: #ifndef LEMON_DIJKSTRA_H alpar@100: #define LEMON_DIJKSTRA_H alpar@100: alpar@100: ///\ingroup shortest_path alpar@100: ///\file alpar@100: ///\brief Dijkstra algorithm. alpar@100: alpar@184: #include kpeter@169: #include alpar@100: #include alpar@100: #include deba@220: #include alpar@100: #include alpar@100: #include kpeter@278: #include alpar@100: alpar@100: namespace lemon { alpar@100: kpeter@244: /// \brief Default operation traits for the Dijkstra algorithm class. alpar@209: /// kpeter@244: /// This operation traits class defines all computational operations and kpeter@244: /// constants which are used in the Dijkstra algorithm. alpar@100: template alpar@100: struct DijkstraDefaultOperationTraits { alpar@100: /// \brief Gives back the zero value of the type. alpar@100: static Value zero() { alpar@100: return static_cast(0); alpar@100: } alpar@100: /// \brief Gives back the sum of the given two elements. alpar@100: static Value plus(const Value& left, const Value& right) { alpar@100: return left + right; alpar@100: } kpeter@244: /// \brief Gives back true only if the first value is less than the second. alpar@100: static bool less(const Value& left, const Value& right) { alpar@100: return left < right; alpar@100: } alpar@100: }; alpar@100: kpeter@244: /// \brief Widest path operation traits for the Dijkstra algorithm class. alpar@209: /// kpeter@244: /// This operation traits class defines all computational operations and kpeter@244: /// constants which are used in the Dijkstra algorithm for widest path kpeter@244: /// computation. kpeter@244: /// kpeter@244: /// \see DijkstraDefaultOperationTraits alpar@100: template alpar@100: struct DijkstraWidestPathOperationTraits { alpar@100: /// \brief Gives back the maximum value of the type. alpar@100: static Value zero() { alpar@100: return std::numeric_limits::max(); alpar@100: } alpar@100: /// \brief Gives back the minimum of the given two elements. alpar@100: static Value plus(const Value& left, const Value& right) { alpar@100: return std::min(left, right); alpar@100: } kpeter@244: /// \brief Gives back true only if the first value is less than the second. alpar@100: static bool less(const Value& left, const Value& right) { alpar@100: return left < right; alpar@100: } alpar@100: }; alpar@209: alpar@100: ///Default traits class of Dijkstra class. alpar@100: alpar@100: ///Default traits class of Dijkstra class. kpeter@244: ///\tparam GR The type of the digraph. kpeter@244: ///\tparam LM The type of the length map. alpar@100: template alpar@100: struct DijkstraDefaultTraits alpar@100: { kpeter@244: ///The type of the digraph the algorithm runs on. alpar@100: typedef GR Digraph; kpeter@244: alpar@100: ///The type of the map that stores the arc lengths. alpar@100: alpar@100: ///The type of the map that stores the arc lengths. alpar@100: ///It must meet the \ref concepts::ReadMap "ReadMap" concept. alpar@100: typedef LM LengthMap; kpeter@244: ///The type of the length of the arcs. alpar@100: typedef typename LM::Value Value; kpeter@244: alpar@100: /// Operation traits for Dijkstra algorithm. alpar@100: kpeter@244: /// This class defines the operations that are used in the algorithm. alpar@100: /// \see DijkstraDefaultOperationTraits alpar@100: typedef DijkstraDefaultOperationTraits OperationTraits; alpar@100: kpeter@244: /// The cross reference type used by the heap. alpar@100: kpeter@244: /// The cross reference type used by the heap. alpar@100: /// Usually it is \c Digraph::NodeMap. alpar@100: typedef typename Digraph::template NodeMap HeapCrossRef; kpeter@244: ///Instantiates a \ref HeapCrossRef. alpar@100: kpeter@244: ///This function instantiates a \ref HeapCrossRef. kpeter@244: /// \param g is the digraph, to which we would like to define the kpeter@244: /// \ref HeapCrossRef. kpeter@244: static HeapCrossRef *createHeapCrossRef(const Digraph &g) alpar@100: { kpeter@244: return new HeapCrossRef(g); alpar@100: } alpar@209: kpeter@244: ///The heap type used by the Dijkstra algorithm. alpar@100: kpeter@244: ///The heap type used by the Dijkstra algorithm. alpar@100: /// alpar@100: ///\sa BinHeap alpar@100: ///\sa Dijkstra alpar@100: typedef BinHeap > Heap; kpeter@244: ///Instantiates a \ref Heap. alpar@100: kpeter@244: ///This function instantiates a \ref Heap. kpeter@244: static Heap *createHeap(HeapCrossRef& r) alpar@100: { kpeter@244: return new Heap(r); alpar@100: } alpar@100: kpeter@244: ///\brief The type of the map that stores the predecessor alpar@100: ///arcs of the shortest paths. alpar@209: /// kpeter@244: ///The type of the map that stores the predecessor alpar@100: ///arcs of the shortest paths. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. kpeter@244: typedef typename Digraph::template NodeMap PredMap; kpeter@244: ///Instantiates a \ref PredMap. alpar@209: kpeter@244: ///This function instantiates a \ref PredMap. kpeter@244: ///\param g is the digraph, to which we would like to define the kpeter@244: ///\ref PredMap. kpeter@244: static PredMap *createPredMap(const Digraph &g) alpar@100: { kpeter@244: return new PredMap(g); alpar@100: } alpar@100: kpeter@244: ///The type of the map that indicates which nodes are processed. alpar@209: kpeter@244: ///The type of the map that indicates which nodes are processed. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. alpar@100: ///By default it is a NullMap. alpar@100: typedef NullMap ProcessedMap; kpeter@244: ///Instantiates a \ref ProcessedMap. alpar@209: kpeter@244: ///This function instantiates a \ref ProcessedMap. alpar@100: ///\param g is the digraph, to which kpeter@244: ///we would like to define the \ref ProcessedMap alpar@100: #ifdef DOXYGEN kpeter@244: static ProcessedMap *createProcessedMap(const Digraph &g) alpar@100: #else kpeter@244: static ProcessedMap *createProcessedMap(const Digraph &) alpar@100: #endif alpar@100: { alpar@100: return new ProcessedMap(); alpar@100: } alpar@209: kpeter@244: ///The type of the map that stores the distances of the nodes. kpeter@244: kpeter@244: ///The type of the map that stores the distances of the nodes. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. alpar@100: typedef typename Digraph::template NodeMap DistMap; kpeter@244: ///Instantiates a \ref DistMap. alpar@209: alpar@209: ///This function instantiates a \ref DistMap. kpeter@244: ///\param g is the digraph, to which we would like to define alpar@210: ///the \ref DistMap kpeter@244: static DistMap *createDistMap(const Digraph &g) alpar@100: { kpeter@244: return new DistMap(g); alpar@100: } alpar@100: }; alpar@209: alpar@100: ///%Dijkstra algorithm class. alpar@209: alpar@100: /// \ingroup shortest_path kpeter@244: ///This class provides an efficient implementation of the %Dijkstra algorithm. kpeter@244: /// alpar@100: ///The arc lengths are passed to the algorithm using a alpar@100: ///\ref concepts::ReadMap "ReadMap", alpar@100: ///so it is easy to change it to any kind of length. alpar@100: ///The type of the length is determined by the alpar@100: ///\ref concepts::ReadMap::Value "Value" of the length map. alpar@100: ///It is also possible to change the underlying priority heap. alpar@100: /// kpeter@278: ///There is also a \ref dijkstra() "function-type interface" for the kpeter@244: ///%Dijkstra algorithm, which is convenient in the simplier cases and kpeter@244: ///it can be used easier. kpeter@244: /// kpeter@244: ///\tparam GR The type of the digraph the algorithm runs on. kpeter@244: ///The default value is \ref ListDigraph. kpeter@244: ///The value of GR is not used directly by \ref Dijkstra, it is only kpeter@244: ///passed to \ref DijkstraDefaultTraits. kpeter@244: ///\tparam LM A readable arc map that determines the lengths of the alpar@100: ///arcs. It is read once for each arc, so the map may involve in kpeter@244: ///relatively time consuming process to compute the arc lengths if alpar@100: ///it is necessary. The default map type is \ref kpeter@244: ///concepts::Digraph::ArcMap "Digraph::ArcMap". kpeter@244: ///The value of LM is not used directly by \ref Dijkstra, it is only kpeter@244: ///passed to \ref DijkstraDefaultTraits. kpeter@244: ///\tparam TR Traits class to set various data types used by the algorithm. kpeter@244: ///The default traits class is \ref DijkstraDefaultTraits kpeter@244: ///"DijkstraDefaultTraits". See \ref DijkstraDefaultTraits kpeter@244: ///for the documentation of a Dijkstra traits class. alpar@100: #ifdef DOXYGEN alpar@100: template alpar@100: #else alpar@100: template , alpar@209: typename TR=DijkstraDefaultTraits > alpar@100: #endif alpar@100: class Dijkstra { alpar@100: public: kpeter@244: ///\ref Exception for uninitialized parameters. kpeter@244: kpeter@244: ///This error represents problems in the initialization of the kpeter@244: ///parameters of the algorithm. alpar@100: class UninitializedParameter : public lemon::UninitializedParameter { alpar@100: public: alpar@100: virtual const char* what() const throw() { alpar@209: return "lemon::Dijkstra::UninitializedParameter"; alpar@100: } alpar@100: }; alpar@100: kpeter@244: ///The type of the digraph the algorithm runs on. alpar@100: typedef typename TR::Digraph Digraph; alpar@209: alpar@100: ///The type of the length of the arcs. alpar@100: typedef typename TR::LengthMap::Value Value; alpar@100: ///The type of the map that stores the arc lengths. alpar@100: typedef typename TR::LengthMap LengthMap; kpeter@244: ///\brief The type of the map that stores the predecessor arcs of the kpeter@244: ///shortest paths. alpar@100: typedef typename TR::PredMap PredMap; kpeter@244: ///The type of the map that stores the distances of the nodes. kpeter@244: typedef typename TR::DistMap DistMap; kpeter@244: ///The type of the map that indicates which nodes are processed. alpar@100: typedef typename TR::ProcessedMap ProcessedMap; kpeter@244: ///The type of the paths. kpeter@244: typedef PredMapPath Path; alpar@100: ///The cross reference type used for the current heap. alpar@100: typedef typename TR::HeapCrossRef HeapCrossRef; kpeter@244: ///The heap type used by the algorithm. alpar@100: typedef typename TR::Heap Heap; kpeter@244: ///The operation traits class. alpar@100: typedef typename TR::OperationTraits OperationTraits; kpeter@244: kpeter@244: ///The traits class. kpeter@244: typedef TR Traits; kpeter@244: alpar@100: private: kpeter@244: kpeter@244: typedef typename Digraph::Node Node; kpeter@244: typedef typename Digraph::NodeIt NodeIt; kpeter@244: typedef typename Digraph::Arc Arc; kpeter@244: typedef typename Digraph::OutArcIt OutArcIt; kpeter@244: kpeter@244: //Pointer to the underlying digraph. alpar@100: const Digraph *G; kpeter@244: //Pointer to the length map. alpar@100: const LengthMap *length; kpeter@244: //Pointer to the map of predecessors arcs. alpar@100: PredMap *_pred; kpeter@244: //Indicates if _pred is locally allocated (true) or not. alpar@100: bool local_pred; kpeter@244: //Pointer to the map of distances. alpar@100: DistMap *_dist; kpeter@244: //Indicates if _dist is locally allocated (true) or not. alpar@100: bool local_dist; kpeter@244: //Pointer to the map of processed status of the nodes. alpar@100: ProcessedMap *_processed; kpeter@244: //Indicates if _processed is locally allocated (true) or not. alpar@100: bool local_processed; kpeter@244: //Pointer to the heap cross references. alpar@100: HeapCrossRef *_heap_cross_ref; kpeter@244: //Indicates if _heap_cross_ref is locally allocated (true) or not. alpar@100: bool local_heap_cross_ref; kpeter@244: //Pointer to the heap. alpar@100: Heap *_heap; kpeter@244: //Indicates if _heap is locally allocated (true) or not. alpar@100: bool local_heap; alpar@100: alpar@280: //Creates the maps if necessary. alpar@209: void create_maps() alpar@100: { alpar@100: if(!_pred) { alpar@209: local_pred = true; alpar@209: _pred = Traits::createPredMap(*G); alpar@100: } alpar@100: if(!_dist) { alpar@209: local_dist = true; alpar@209: _dist = Traits::createDistMap(*G); alpar@100: } alpar@100: if(!_processed) { alpar@209: local_processed = true; alpar@209: _processed = Traits::createProcessedMap(*G); alpar@100: } alpar@100: if (!_heap_cross_ref) { alpar@209: local_heap_cross_ref = true; alpar@209: _heap_cross_ref = Traits::createHeapCrossRef(*G); alpar@100: } alpar@100: if (!_heap) { alpar@209: local_heap = true; alpar@209: _heap = Traits::createHeap(*_heap_cross_ref); alpar@100: } alpar@100: } alpar@209: kpeter@244: public: alpar@100: alpar@100: typedef Dijkstra Create; alpar@209: alpar@100: ///\name Named template parameters alpar@100: alpar@100: ///@{ alpar@100: alpar@100: template kpeter@257: struct SetPredMapTraits : public Traits { alpar@100: typedef T PredMap; alpar@100: static PredMap *createPredMap(const Digraph &) alpar@100: { alpar@209: throw UninitializedParameter(); alpar@100: } alpar@100: }; kpeter@244: ///\brief \ref named-templ-param "Named parameter" for setting kpeter@244: ///\ref PredMap type. alpar@100: /// kpeter@244: ///\ref named-templ-param "Named parameter" for setting kpeter@244: ///\ref PredMap type. alpar@100: template kpeter@257: struct SetPredMap kpeter@257: : public Dijkstra< Digraph, LengthMap, SetPredMapTraits > { kpeter@257: typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits > Create; alpar@100: }; alpar@209: alpar@100: template kpeter@257: struct SetDistMapTraits : public Traits { alpar@100: typedef T DistMap; alpar@100: static DistMap *createDistMap(const Digraph &) alpar@100: { alpar@209: throw UninitializedParameter(); alpar@100: } alpar@100: }; kpeter@244: ///\brief \ref named-templ-param "Named parameter" for setting kpeter@244: ///\ref DistMap type. alpar@100: /// kpeter@244: ///\ref named-templ-param "Named parameter" for setting kpeter@244: ///\ref DistMap type. alpar@100: template kpeter@257: struct SetDistMap kpeter@257: : public Dijkstra< Digraph, LengthMap, SetDistMapTraits > { kpeter@257: typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits > Create; alpar@100: }; alpar@209: alpar@100: template kpeter@257: struct SetProcessedMapTraits : public Traits { alpar@100: typedef T ProcessedMap; kpeter@244: static ProcessedMap *createProcessedMap(const Digraph &) alpar@100: { alpar@209: throw UninitializedParameter(); alpar@100: } alpar@100: }; kpeter@244: ///\brief \ref named-templ-param "Named parameter" for setting kpeter@244: ///\ref ProcessedMap type. alpar@100: /// kpeter@244: ///\ref named-templ-param "Named parameter" for setting kpeter@244: ///\ref ProcessedMap type. alpar@100: template kpeter@257: struct SetProcessedMap kpeter@257: : public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits > { kpeter@257: typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits > Create; alpar@100: }; alpar@209: kpeter@257: struct SetStandardProcessedMapTraits : public Traits { alpar@100: typedef typename Digraph::template NodeMap ProcessedMap; kpeter@244: static ProcessedMap *createProcessedMap(const Digraph &g) alpar@100: { kpeter@244: return new ProcessedMap(g); alpar@100: } alpar@100: }; kpeter@244: ///\brief \ref named-templ-param "Named parameter" for setting kpeter@244: ///\ref ProcessedMap type to be Digraph::NodeMap. alpar@100: /// kpeter@244: ///\ref named-templ-param "Named parameter" for setting kpeter@244: ///\ref ProcessedMap type to be Digraph::NodeMap. kpeter@244: ///If you don't set it explicitly, it will be automatically allocated. kpeter@257: struct SetStandardProcessedMap kpeter@257: : public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > { kpeter@257: typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > alpar@210: Create; alpar@100: }; alpar@100: alpar@100: template kpeter@257: struct SetHeapTraits : public Traits { alpar@100: typedef CR HeapCrossRef; alpar@100: typedef H Heap; alpar@100: static HeapCrossRef *createHeapCrossRef(const Digraph &) { alpar@209: throw UninitializedParameter(); alpar@100: } alpar@209: static Heap *createHeap(HeapCrossRef &) alpar@100: { alpar@209: throw UninitializedParameter(); alpar@100: } alpar@100: }; alpar@100: ///\brief \ref named-templ-param "Named parameter" for setting alpar@100: ///heap and cross reference type alpar@100: /// alpar@209: ///\ref named-templ-param "Named parameter" for setting heap and cross kpeter@244: ///reference type. alpar@100: template > kpeter@257: struct SetHeap kpeter@257: : public Dijkstra< Digraph, LengthMap, SetHeapTraits > { kpeter@257: typedef Dijkstra< Digraph, LengthMap, SetHeapTraits > Create; alpar@100: }; alpar@100: alpar@100: template kpeter@257: struct SetStandardHeapTraits : public Traits { alpar@100: typedef CR HeapCrossRef; alpar@100: typedef H Heap; alpar@100: static HeapCrossRef *createHeapCrossRef(const Digraph &G) { alpar@209: return new HeapCrossRef(G); alpar@100: } alpar@209: static Heap *createHeap(HeapCrossRef &R) alpar@100: { alpar@209: return new Heap(R); alpar@100: } alpar@100: }; alpar@100: ///\brief \ref named-templ-param "Named parameter" for setting alpar@100: ///heap and cross reference type with automatic allocation alpar@100: /// alpar@209: ///\ref named-templ-param "Named parameter" for setting heap and cross alpar@209: ///reference type. It can allocate the heap and the cross reference alpar@209: ///object if the cross reference's constructor waits for the digraph as alpar@100: ///parameter and the heap's constructor waits for the cross reference. alpar@100: template > kpeter@257: struct SetStandardHeap kpeter@257: : public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits > { kpeter@257: typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits > alpar@100: Create; alpar@100: }; alpar@100: alpar@100: template kpeter@257: struct SetOperationTraitsTraits : public Traits { alpar@100: typedef T OperationTraits; alpar@100: }; alpar@209: alpar@209: /// \brief \ref named-templ-param "Named parameter" for setting kpeter@244: ///\ref OperationTraits type alpar@100: /// kpeter@244: ///\ref named-templ-param "Named parameter" for setting kpeter@244: ///\ref OperationTraits type. alpar@100: template kpeter@257: struct SetOperationTraits kpeter@257: : public Dijkstra > { kpeter@257: typedef Dijkstra > alpar@100: Create; alpar@100: }; alpar@209: alpar@100: ///@} alpar@100: alpar@100: protected: alpar@100: alpar@100: Dijkstra() {} alpar@100: alpar@209: public: alpar@209: alpar@100: ///Constructor. alpar@209: kpeter@244: ///Constructor. kpeter@244: ///\param _g The digraph the algorithm runs on. kpeter@244: ///\param _length The length map used by the algorithm. kpeter@244: Dijkstra(const Digraph& _g, const LengthMap& _length) : kpeter@244: G(&_g), length(&_length), alpar@100: _pred(NULL), local_pred(false), alpar@100: _dist(NULL), local_dist(false), alpar@100: _processed(NULL), local_processed(false), alpar@100: _heap_cross_ref(NULL), local_heap_cross_ref(false), alpar@100: _heap(NULL), local_heap(false) alpar@100: { } alpar@209: alpar@100: ///Destructor. alpar@209: ~Dijkstra() alpar@100: { alpar@100: if(local_pred) delete _pred; alpar@100: if(local_dist) delete _dist; alpar@100: if(local_processed) delete _processed; alpar@100: if(local_heap_cross_ref) delete _heap_cross_ref; alpar@100: if(local_heap) delete _heap; alpar@100: } alpar@100: alpar@100: ///Sets the length map. alpar@100: alpar@100: ///Sets the length map. alpar@100: ///\return (*this) alpar@209: Dijkstra &lengthMap(const LengthMap &m) alpar@100: { alpar@100: length = &m; alpar@100: return *this; alpar@100: } alpar@100: kpeter@244: ///Sets the map that stores the predecessor arcs. alpar@100: kpeter@244: ///Sets the map that stores the predecessor arcs. alpar@100: ///If you don't use this function before calling \ref run(), kpeter@244: ///it will allocate one. The destructor deallocates this alpar@100: ///automatically allocated map, of course. alpar@100: ///\return (*this) alpar@209: Dijkstra &predMap(PredMap &m) alpar@100: { alpar@100: if(local_pred) { alpar@209: delete _pred; alpar@209: local_pred=false; alpar@100: } alpar@100: _pred = &m; alpar@100: return *this; alpar@100: } alpar@100: kpeter@244: ///Sets the map that indicates which nodes are processed. alpar@100: kpeter@244: ///Sets the map that indicates which nodes are processed. alpar@100: ///If you don't use this function before calling \ref run(), kpeter@244: ///it will allocate one. The destructor deallocates this kpeter@244: ///automatically allocated map, of course. kpeter@244: ///\return (*this) kpeter@244: Dijkstra &processedMap(ProcessedMap &m) kpeter@244: { kpeter@244: if(local_processed) { kpeter@244: delete _processed; kpeter@244: local_processed=false; kpeter@244: } kpeter@244: _processed = &m; kpeter@244: return *this; kpeter@244: } kpeter@244: kpeter@244: ///Sets the map that stores the distances of the nodes. kpeter@244: kpeter@244: ///Sets the map that stores the distances of the nodes calculated by the kpeter@244: ///algorithm. kpeter@244: ///If you don't use this function before calling \ref run(), kpeter@244: ///it will allocate one. The destructor deallocates this alpar@100: ///automatically allocated map, of course. alpar@100: ///\return (*this) alpar@209: Dijkstra &distMap(DistMap &m) alpar@100: { alpar@100: if(local_dist) { alpar@209: delete _dist; alpar@209: local_dist=false; alpar@100: } alpar@100: _dist = &m; alpar@100: return *this; alpar@100: } alpar@100: alpar@100: ///Sets the heap and the cross reference used by algorithm. alpar@100: alpar@100: ///Sets the heap and the cross reference used by algorithm. alpar@100: ///If you don't use this function before calling \ref run(), kpeter@244: ///it will allocate one. The destructor deallocates this alpar@100: ///automatically allocated heap and cross reference, of course. alpar@100: ///\return (*this) alpar@100: Dijkstra &heap(Heap& hp, HeapCrossRef &cr) alpar@100: { alpar@100: if(local_heap_cross_ref) { alpar@209: delete _heap_cross_ref; alpar@209: local_heap_cross_ref=false; alpar@100: } alpar@100: _heap_cross_ref = &cr; alpar@100: if(local_heap) { alpar@209: delete _heap; alpar@209: local_heap=false; alpar@100: } alpar@100: _heap = &hp; alpar@100: return *this; alpar@100: } alpar@100: alpar@100: private: kpeter@244: alpar@100: void finalizeNodeData(Node v,Value dst) alpar@100: { alpar@100: _processed->set(v,true); alpar@100: _dist->set(v, dst); alpar@100: } alpar@100: alpar@100: public: alpar@100: alpar@100: ///\name Execution control kpeter@244: ///The simplest way to execute the algorithm is to use one of the kpeter@244: ///member functions called \ref lemon::Dijkstra::run() "run()". alpar@100: ///\n kpeter@244: ///If you need more control on the execution, first you must call kpeter@244: ///\ref lemon::Dijkstra::init() "init()", then you can add several kpeter@244: ///source nodes with \ref lemon::Dijkstra::addSource() "addSource()". kpeter@244: ///Finally \ref lemon::Dijkstra::start() "start()" will perform the kpeter@244: ///actual path computation. alpar@100: alpar@100: ///@{ alpar@100: alpar@100: ///Initializes the internal data structures. alpar@100: alpar@100: ///Initializes the internal data structures. alpar@100: /// alpar@100: void init() alpar@100: { alpar@100: create_maps(); alpar@100: _heap->clear(); alpar@100: for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { alpar@209: _pred->set(u,INVALID); alpar@209: _processed->set(u,false); alpar@209: _heap_cross_ref->set(u,Heap::PRE_HEAP); alpar@100: } alpar@100: } alpar@209: alpar@100: ///Adds a new source node. alpar@100: alpar@100: ///Adds a new source node to the priority heap. alpar@100: ///The optional second parameter is the initial distance of the node. alpar@100: /// kpeter@244: ///The function checks if the node has already been added to the heap and alpar@100: ///it is pushed to the heap only if either it was not in the heap alpar@100: ///or the shortest path found till then is shorter than \c dst. alpar@100: void addSource(Node s,Value dst=OperationTraits::zero()) alpar@100: { alpar@100: if(_heap->state(s) != Heap::IN_HEAP) { alpar@209: _heap->push(s,dst); alpar@100: } else if(OperationTraits::less((*_heap)[s], dst)) { alpar@209: _heap->set(s,dst); alpar@209: _pred->set(s,INVALID); alpar@100: } alpar@100: } alpar@209: alpar@100: ///Processes the next node in the priority heap alpar@100: alpar@100: ///Processes the next node in the priority heap. alpar@100: /// alpar@100: ///\return The processed node. alpar@100: /// kpeter@244: ///\warning The priority heap must not be empty. alpar@100: Node processNextNode() alpar@100: { alpar@209: Node v=_heap->top(); alpar@100: Value oldvalue=_heap->prio(); alpar@100: _heap->pop(); alpar@100: finalizeNodeData(v,oldvalue); alpar@209: alpar@100: for(OutArcIt e(*G,v); e!=INVALID; ++e) { alpar@209: Node w=G->target(e); alpar@209: switch(_heap->state(w)) { alpar@209: case Heap::PRE_HEAP: alpar@209: _heap->push(w,OperationTraits::plus(oldvalue, (*length)[e])); alpar@209: _pred->set(w,e); alpar@209: break; alpar@209: case Heap::IN_HEAP: alpar@209: { alpar@209: Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]); alpar@209: if ( OperationTraits::less(newvalue, (*_heap)[w]) ) { alpar@209: _heap->decrease(w, newvalue); alpar@209: _pred->set(w,e); alpar@209: } alpar@209: } alpar@209: break; alpar@209: case Heap::POST_HEAP: alpar@209: break; alpar@209: } alpar@100: } alpar@100: return v; alpar@100: } alpar@100: kpeter@244: ///The next node to be processed. alpar@209: kpeter@244: ///Returns the next node to be processed or \c INVALID if the kpeter@244: ///priority heap is empty. kpeter@244: Node nextNode() const alpar@209: { alpar@100: return !_heap->empty()?_heap->top():INVALID; alpar@100: } alpar@209: alpar@100: ///\brief Returns \c false if there are nodes kpeter@244: ///to be processed. alpar@100: /// alpar@100: ///Returns \c false if there are nodes kpeter@244: ///to be processed in the priority heap. kpeter@244: bool emptyQueue() const { return _heap->empty(); } kpeter@244: alpar@100: ///Returns the number of the nodes to be processed in the priority heap alpar@100: kpeter@244: ///Returns the number of the nodes to be processed in the priority heap. alpar@100: /// kpeter@244: int queueSize() const { return _heap->size(); } alpar@209: alpar@100: ///Executes the algorithm. alpar@100: alpar@100: ///Executes the algorithm. alpar@100: /// kpeter@244: ///This method runs the %Dijkstra algorithm from the root node(s) kpeter@244: ///in order to compute the shortest path to each node. kpeter@244: /// kpeter@244: ///The algorithm computes kpeter@244: ///- the shortest path tree (forest), kpeter@244: ///- the distance of each node from the root(s). kpeter@244: /// kpeter@244: ///\pre init() must be called and at least one root node should be kpeter@244: ///added with addSource() before using this function. kpeter@244: /// kpeter@244: ///\note d.start() is just a shortcut of the following code. kpeter@244: ///\code kpeter@244: /// while ( !d.emptyQueue() ) { kpeter@244: /// d.processNextNode(); kpeter@244: /// } kpeter@244: ///\endcode kpeter@244: void start() kpeter@244: { kpeter@244: while ( !emptyQueue() ) processNextNode(); kpeter@244: } kpeter@244: kpeter@244: ///Executes the algorithm until the given target node is reached. kpeter@244: kpeter@244: ///Executes the algorithm until the given target node is reached. alpar@100: /// alpar@100: ///This method runs the %Dijkstra algorithm from the root node(s) kpeter@244: ///in order to compute the shortest path to \c dest. alpar@100: /// kpeter@244: ///The algorithm computes kpeter@244: ///- the shortest path to \c dest, kpeter@244: ///- the distance of \c dest from the root(s). alpar@100: /// kpeter@244: ///\pre init() must be called and at least one root node should be kpeter@244: ///added with addSource() before using this function. alpar@100: void start(Node dest) alpar@100: { alpar@100: while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); alpar@100: if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); alpar@100: } alpar@209: alpar@100: ///Executes the algorithm until a condition is met. alpar@100: alpar@100: ///Executes the algorithm until a condition is met. alpar@100: /// kpeter@244: ///This method runs the %Dijkstra algorithm from the root node(s) in kpeter@244: ///order to compute the shortest path to a node \c v with kpeter@244: /// nm[v] true, if such a node can be found. alpar@100: /// kpeter@244: ///\param nm A \c bool (or convertible) node map. The algorithm alpar@100: ///will stop when it reaches a node \c v with nm[v] true. alpar@100: /// alpar@100: ///\return The reached node \c v with nm[v] true or alpar@100: ///\c INVALID if no such node was found. kpeter@244: /// kpeter@244: ///\pre init() must be called and at least one root node should be kpeter@244: ///added with addSource() before using this function. alpar@100: template alpar@100: Node start(const NodeBoolMap &nm) alpar@100: { alpar@100: while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); alpar@100: if ( _heap->empty() ) return INVALID; alpar@100: finalizeNodeData(_heap->top(),_heap->prio()); alpar@100: return _heap->top(); alpar@100: } alpar@209: kpeter@244: ///Runs the algorithm from the given node. alpar@209: kpeter@244: ///This method runs the %Dijkstra algorithm from node \c s kpeter@244: ///in order to compute the shortest path to each node. alpar@100: /// kpeter@244: ///The algorithm computes kpeter@244: ///- the shortest path tree, kpeter@244: ///- the distance of each node from the root. kpeter@244: /// kpeter@244: ///\note d.run(s) is just a shortcut of the following code. alpar@100: ///\code alpar@100: /// d.init(); alpar@100: /// d.addSource(s); alpar@100: /// d.start(); alpar@100: ///\endcode alpar@100: void run(Node s) { alpar@100: init(); alpar@100: addSource(s); alpar@100: start(); alpar@100: } alpar@209: alpar@100: ///Finds the shortest path between \c s and \c t. alpar@209: kpeter@244: ///This method runs the %Dijkstra algorithm from node \c s kpeter@244: ///in order to compute the shortest path to \c t. alpar@100: /// kpeter@244: ///\return The length of the shortest s--t path, kpeter@244: ///if \c t is reachable form \c s, \c 0 otherwise. kpeter@244: /// kpeter@244: ///\note Apart from the return value, d.run(s,t) is just a kpeter@244: ///shortcut of the following code. alpar@100: ///\code alpar@100: /// d.init(); alpar@100: /// d.addSource(s); alpar@100: /// d.start(t); alpar@100: ///\endcode alpar@100: Value run(Node s,Node t) { alpar@100: init(); alpar@100: addSource(s); alpar@100: start(t); alpar@100: return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t]; alpar@100: } alpar@209: alpar@100: ///@} alpar@100: alpar@100: ///\name Query Functions alpar@100: ///The result of the %Dijkstra algorithm can be obtained using these alpar@100: ///functions.\n kpeter@244: ///Either \ref lemon::Dijkstra::run() "run()" or kpeter@244: ///\ref lemon::Dijkstra::start() "start()" must be called before kpeter@244: ///using them. alpar@209: alpar@100: ///@{ alpar@100: kpeter@244: ///The shortest path to a node. alpar@209: kpeter@244: ///Returns the shortest path to a node. kpeter@244: /// kpeter@244: ///\warning \c t should be reachable from the root(s). kpeter@244: /// kpeter@244: ///\pre Either \ref run() or \ref start() must be called before kpeter@244: ///using this function. kpeter@244: Path path(Node t) const { return Path(*G, *_pred, t); } alpar@100: kpeter@244: ///The distance of a node from the root(s). alpar@100: kpeter@244: ///Returns the distance of a node from the root(s). kpeter@244: /// kpeter@244: ///\warning If node \c v is not reachable from the root(s), then kpeter@244: ///the return value of this function is undefined. kpeter@244: /// kpeter@244: ///\pre Either \ref run() or \ref start() must be called before kpeter@244: ///using this function. alpar@100: Value dist(Node v) const { return (*_dist)[v]; } alpar@100: kpeter@244: ///Returns the 'previous arc' of the shortest path tree for a node. alpar@100: kpeter@244: ///This function returns the 'previous arc' of the shortest path kpeter@244: ///tree for the node \c v, i.e. it returns the last arc of a kpeter@244: ///shortest path from the root(s) to \c v. It is \c INVALID if \c v kpeter@244: ///is not reachable from the root(s) or if \c v is a root. kpeter@244: /// kpeter@244: ///The shortest path tree used here is equal to the shortest path kpeter@244: ///tree used in \ref predNode(). kpeter@244: /// kpeter@244: ///\pre Either \ref run() or \ref start() must be called before kpeter@244: ///using this function. alpar@100: Arc predArc(Node v) const { return (*_pred)[v]; } alpar@100: kpeter@244: ///Returns the 'previous node' of the shortest path tree for a node. alpar@100: kpeter@244: ///This function returns the 'previous node' of the shortest path kpeter@244: ///tree for the node \c v, i.e. it returns the last but one node kpeter@244: ///from a shortest path from the root(s) to \c v. It is \c INVALID kpeter@244: ///if \c v is not reachable from the root(s) or if \c v is a root. kpeter@244: /// kpeter@244: ///The shortest path tree used here is equal to the shortest path kpeter@244: ///tree used in \ref predArc(). kpeter@244: /// kpeter@244: ///\pre Either \ref run() or \ref start() must be called before alpar@100: ///using this function. alpar@100: Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: alpar@209: G->source((*_pred)[v]); } alpar@209: kpeter@244: ///\brief Returns a const reference to the node map that stores the kpeter@244: ///distances of the nodes. kpeter@244: /// kpeter@244: ///Returns a const reference to the node map that stores the distances kpeter@244: ///of the nodes calculated by the algorithm. kpeter@244: /// kpeter@244: ///\pre Either \ref run() or \ref init() kpeter@244: ///must be called before using this function. alpar@100: const DistMap &distMap() const { return *_dist;} alpar@209: kpeter@244: ///\brief Returns a const reference to the node map that stores the kpeter@244: ///predecessor arcs. kpeter@244: /// kpeter@244: ///Returns a const reference to the node map that stores the predecessor kpeter@244: ///arcs, which form the shortest path tree. kpeter@244: /// kpeter@244: ///\pre Either \ref run() or \ref init() kpeter@244: ///must be called before using this function. alpar@100: const PredMap &predMap() const { return *_pred;} alpar@209: kpeter@244: ///Checks if a node is reachable from the root(s). alpar@100: kpeter@244: ///Returns \c true if \c v is reachable from the root(s). kpeter@244: ///\pre Either \ref run() or \ref start() kpeter@244: ///must be called before using this function. kpeter@244: bool reached(Node v) const { return (*_heap_cross_ref)[v] != kpeter@244: Heap::PRE_HEAP; } alpar@100: alpar@100: ///Checks if a node is processed. alpar@100: alpar@100: ///Returns \c true if \c v is processed, i.e. the shortest alpar@100: ///path to \c v has already found. kpeter@244: ///\pre Either \ref run() or \ref start() kpeter@244: ///must be called before using this function. kpeter@244: bool processed(Node v) const { return (*_heap_cross_ref)[v] == kpeter@244: Heap::POST_HEAP; } kpeter@244: kpeter@244: ///The current distance of a node from the root(s). kpeter@244: kpeter@244: ///Returns the current distance of a node from the root(s). kpeter@244: ///It may be decreased in the following processes. kpeter@244: ///\pre \c v should be reached but not processed. kpeter@244: Value currentDist(Node v) const { return (*_heap)[v]; } alpar@209: alpar@100: ///@} alpar@100: }; alpar@100: alpar@100: kpeter@244: ///Default traits class of dijkstra() function. alpar@100: kpeter@244: ///Default traits class of dijkstra() function. kpeter@244: ///\tparam GR The type of the digraph. kpeter@244: ///\tparam LM The type of the length map. alpar@100: template alpar@100: struct DijkstraWizardDefaultTraits alpar@100: { kpeter@244: ///The type of the digraph the algorithm runs on. alpar@100: typedef GR Digraph; alpar@100: ///The type of the map that stores the arc lengths. alpar@100: alpar@100: ///The type of the map that stores the arc lengths. alpar@100: ///It must meet the \ref concepts::ReadMap "ReadMap" concept. alpar@100: typedef LM LengthMap; kpeter@244: ///The type of the length of the arcs. alpar@100: typedef typename LM::Value Value; kpeter@244: alpar@100: /// Operation traits for Dijkstra algorithm. alpar@100: kpeter@244: /// This class defines the operations that are used in the algorithm. alpar@100: /// \see DijkstraDefaultOperationTraits alpar@100: typedef DijkstraDefaultOperationTraits OperationTraits; alpar@100: kpeter@244: /// The cross reference type used by the heap. alpar@100: kpeter@244: /// The cross reference type used by the heap. alpar@100: /// Usually it is \c Digraph::NodeMap. alpar@100: typedef typename Digraph::template NodeMap HeapCrossRef; kpeter@244: ///Instantiates a \ref HeapCrossRef. alpar@100: alpar@209: ///This function instantiates a \ref HeapCrossRef. kpeter@244: /// \param g is the digraph, to which we would like to define the alpar@100: /// HeapCrossRef. kpeter@244: static HeapCrossRef *createHeapCrossRef(const Digraph &g) alpar@100: { kpeter@244: return new HeapCrossRef(g); alpar@100: } alpar@209: kpeter@244: ///The heap type used by the Dijkstra algorithm. alpar@100: kpeter@244: ///The heap type used by the Dijkstra algorithm. alpar@100: /// alpar@100: ///\sa BinHeap alpar@100: ///\sa Dijkstra kpeter@244: typedef BinHeap, alpar@209: std::less > Heap; alpar@100: kpeter@244: ///Instantiates a \ref Heap. kpeter@244: kpeter@244: ///This function instantiates a \ref Heap. kpeter@244: /// \param r is the HeapCrossRef which is used. kpeter@244: static Heap *createHeap(HeapCrossRef& r) alpar@100: { kpeter@244: return new Heap(r); alpar@100: } alpar@100: kpeter@244: ///\brief The type of the map that stores the predecessor alpar@100: ///arcs of the shortest paths. alpar@209: /// kpeter@244: ///The type of the map that stores the predecessor alpar@100: ///arcs of the shortest paths. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. kpeter@278: typedef typename Digraph::template NodeMap PredMap; kpeter@244: ///Instantiates a \ref PredMap. alpar@209: alpar@209: ///This function instantiates a \ref PredMap. kpeter@244: ///\param g is the digraph, to which we would like to define the kpeter@244: ///\ref PredMap. kpeter@244: static PredMap *createPredMap(const Digraph &g) alpar@100: { kpeter@278: return new PredMap(g); alpar@100: } alpar@209: kpeter@244: ///The type of the map that indicates which nodes are processed. kpeter@244: kpeter@244: ///The type of the map that indicates which nodes are processed. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. alpar@100: ///By default it is a NullMap. alpar@100: typedef NullMap ProcessedMap; kpeter@244: ///Instantiates a \ref ProcessedMap. alpar@209: alpar@209: ///This function instantiates a \ref ProcessedMap. alpar@100: ///\param g is the digraph, to which kpeter@244: ///we would like to define the \ref ProcessedMap. alpar@100: #ifdef DOXYGEN kpeter@244: static ProcessedMap *createProcessedMap(const Digraph &g) alpar@100: #else kpeter@244: static ProcessedMap *createProcessedMap(const Digraph &) alpar@100: #endif alpar@100: { alpar@100: return new ProcessedMap(); alpar@100: } alpar@209: kpeter@244: ///The type of the map that stores the distances of the nodes. kpeter@244: kpeter@244: ///The type of the map that stores the distances of the nodes. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. kpeter@278: typedef typename Digraph::template NodeMap DistMap; kpeter@244: ///Instantiates a \ref DistMap. alpar@209: alpar@209: ///This function instantiates a \ref DistMap. alpar@210: ///\param g is the digraph, to which we would like to define alpar@210: ///the \ref DistMap kpeter@244: static DistMap *createDistMap(const Digraph &g) alpar@100: { kpeter@278: return new DistMap(g); alpar@100: } kpeter@278: kpeter@278: ///The type of the shortest paths. kpeter@278: kpeter@278: ///The type of the shortest paths. kpeter@278: ///It must meet the \ref concepts::Path "Path" concept. kpeter@278: typedef lemon::Path Path; alpar@100: }; alpar@209: kpeter@244: /// Default traits class used by \ref DijkstraWizard alpar@100: alpar@100: /// To make it easier to use Dijkstra algorithm kpeter@244: /// we have created a wizard class. alpar@100: /// This \ref DijkstraWizard class needs default traits, kpeter@244: /// as well as the \ref Dijkstra class. alpar@100: /// The \ref DijkstraWizardBase is a class to be the default traits of the alpar@100: /// \ref DijkstraWizard class. alpar@100: template alpar@100: class DijkstraWizardBase : public DijkstraWizardDefaultTraits alpar@100: { alpar@100: typedef DijkstraWizardDefaultTraits Base; alpar@100: protected: kpeter@244: //The type of the nodes in the digraph. alpar@100: typedef typename Base::Digraph::Node Node; alpar@100: kpeter@244: //Pointer to the digraph the algorithm runs on. alpar@100: void *_g; kpeter@278: //Pointer to the length map. alpar@100: void *_length; kpeter@251: //Pointer to the map of processed nodes. kpeter@251: void *_processed; kpeter@244: //Pointer to the map of predecessors arcs. alpar@100: void *_pred; kpeter@244: //Pointer to the map of distances. alpar@100: void *_dist; kpeter@278: //Pointer to the shortest path to the target node. kpeter@278: void *_path; kpeter@278: //Pointer to the distance of the target node. kpeter@278: void *_di; alpar@100: kpeter@244: public: alpar@100: /// Constructor. alpar@209: alpar@100: /// This constructor does not require parameters, therefore it initiates kpeter@278: /// all of the attributes to \c 0. kpeter@251: DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), kpeter@278: _dist(0), _path(0), _di(0) {} alpar@100: alpar@100: /// Constructor. alpar@209: kpeter@278: /// This constructor requires two parameters, kpeter@278: /// others are initiated to \c 0. kpeter@244: /// \param g The digraph the algorithm runs on. kpeter@244: /// \param l The length map. kpeter@278: DijkstraWizardBase(const GR &g,const LM &l) : alpar@209: _g(reinterpret_cast(const_cast(&g))), alpar@209: _length(reinterpret_cast(const_cast(&l))), kpeter@278: _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} alpar@100: alpar@100: }; alpar@209: kpeter@278: /// Auxiliary class for the function-type interface of Dijkstra algorithm. alpar@100: kpeter@278: /// This auxiliary class is created to implement the kpeter@278: /// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. kpeter@278: /// It does not have own \ref run() method, it uses the functions kpeter@278: /// and features of the plain \ref Dijkstra. alpar@100: /// kpeter@278: /// This class should only be used through the \ref dijkstra() function, kpeter@278: /// which makes it easier to use the algorithm. alpar@100: template alpar@100: class DijkstraWizard : public TR alpar@100: { alpar@100: typedef TR Base; alpar@100: kpeter@244: ///The type of the digraph the algorithm runs on. alpar@100: typedef typename TR::Digraph Digraph; kpeter@244: alpar@100: typedef typename Digraph::Node Node; alpar@100: typedef typename Digraph::NodeIt NodeIt; alpar@100: typedef typename Digraph::Arc Arc; alpar@100: typedef typename Digraph::OutArcIt OutArcIt; alpar@209: alpar@100: ///The type of the map that stores the arc lengths. alpar@100: typedef typename TR::LengthMap LengthMap; alpar@100: ///The type of the length of the arcs. alpar@100: typedef typename LengthMap::Value Value; kpeter@244: ///\brief The type of the map that stores the predecessor alpar@100: ///arcs of the shortest paths. alpar@100: typedef typename TR::PredMap PredMap; kpeter@244: ///The type of the map that stores the distances of the nodes. alpar@100: typedef typename TR::DistMap DistMap; kpeter@244: ///The type of the map that indicates which nodes are processed. kpeter@244: typedef typename TR::ProcessedMap ProcessedMap; kpeter@278: ///The type of the shortest paths kpeter@278: typedef typename TR::Path Path; alpar@100: ///The heap type used by the dijkstra algorithm. alpar@100: typedef typename TR::Heap Heap; kpeter@244: alpar@100: public: kpeter@244: alpar@100: /// Constructor. alpar@100: DijkstraWizard() : TR() {} alpar@100: alpar@100: /// Constructor that requires parameters. alpar@100: alpar@100: /// Constructor that requires parameters. alpar@100: /// These parameters will be the default values for the traits class. kpeter@278: /// \param g The digraph the algorithm runs on. kpeter@278: /// \param l The length map. kpeter@278: DijkstraWizard(const Digraph &g, const LengthMap &l) : kpeter@278: TR(g,l) {} alpar@100: alpar@100: ///Copy constructor alpar@100: DijkstraWizard(const TR &b) : TR(b) {} alpar@100: alpar@100: ~DijkstraWizard() {} alpar@100: kpeter@278: ///Runs Dijkstra algorithm from the given source node. alpar@209: kpeter@278: ///This method runs %Dijkstra algorithm from the given source node kpeter@278: ///in order to compute the shortest path to each node. kpeter@278: void run(Node s) alpar@100: { kpeter@278: if (s==INVALID) throw UninitializedParameter(); alpar@209: Dijkstra kpeter@278: dijk(*reinterpret_cast(Base::_g), kpeter@278: *reinterpret_cast(Base::_length)); kpeter@278: if (Base::_pred) kpeter@278: dijk.predMap(*reinterpret_cast(Base::_pred)); kpeter@278: if (Base::_dist) kpeter@278: dijk.distMap(*reinterpret_cast(Base::_dist)); kpeter@278: if (Base::_processed) kpeter@278: dijk.processedMap(*reinterpret_cast(Base::_processed)); kpeter@278: dijk.run(s); alpar@100: } alpar@100: kpeter@278: ///Finds the shortest path between \c s and \c t. alpar@100: kpeter@278: ///This method runs the %Dijkstra algorithm from node \c s kpeter@278: ///in order to compute the shortest path to node \c t kpeter@278: ///(it stops searching when \c t is processed). kpeter@278: /// kpeter@278: ///\return \c true if \c t is reachable form \c s. kpeter@278: bool run(Node s, Node t) alpar@100: { kpeter@278: if (s==INVALID || t==INVALID) throw UninitializedParameter(); kpeter@278: Dijkstra kpeter@278: dijk(*reinterpret_cast(Base::_g), kpeter@278: *reinterpret_cast(Base::_length)); kpeter@278: if (Base::_pred) kpeter@278: dijk.predMap(*reinterpret_cast(Base::_pred)); kpeter@278: if (Base::_dist) kpeter@278: dijk.distMap(*reinterpret_cast(Base::_dist)); kpeter@278: if (Base::_processed) kpeter@278: dijk.processedMap(*reinterpret_cast(Base::_processed)); kpeter@278: dijk.run(s,t); kpeter@278: if (Base::_path) kpeter@278: *reinterpret_cast(Base::_path) = dijk.path(t); kpeter@278: if (Base::_di) kpeter@278: *reinterpret_cast(Base::_di) = dijk.dist(t); kpeter@278: return dijk.reached(t); kpeter@244: } kpeter@244: alpar@100: template kpeter@257: struct SetPredMapBase : public Base { alpar@100: typedef T PredMap; alpar@100: static PredMap *createPredMap(const Digraph &) { return 0; }; kpeter@257: SetPredMapBase(const TR &b) : TR(b) {} alpar@100: }; kpeter@278: ///\brief \ref named-func-param "Named parameter" kpeter@244: ///for setting \ref PredMap object. alpar@100: /// kpeter@278: ///\ref named-func-param "Named parameter" kpeter@244: ///for setting \ref PredMap object. alpar@100: template kpeter@257: DijkstraWizard > predMap(const T &t) alpar@100: { alpar@100: Base::_pred=reinterpret_cast(const_cast(&t)); kpeter@257: return DijkstraWizard >(*this); alpar@100: } alpar@209: alpar@100: template kpeter@278: struct SetDistMapBase : public Base { kpeter@278: typedef T DistMap; kpeter@278: static DistMap *createDistMap(const Digraph &) { return 0; }; kpeter@278: SetDistMapBase(const TR &b) : TR(b) {} kpeter@278: }; kpeter@278: ///\brief \ref named-func-param "Named parameter" kpeter@278: ///for setting \ref DistMap object. kpeter@278: /// kpeter@278: ///\ref named-func-param "Named parameter" kpeter@278: ///for setting \ref DistMap object. kpeter@278: template kpeter@278: DijkstraWizard > distMap(const T &t) kpeter@278: { kpeter@278: Base::_dist=reinterpret_cast(const_cast(&t)); kpeter@278: return DijkstraWizard >(*this); kpeter@278: } kpeter@278: kpeter@278: template kpeter@257: struct SetProcessedMapBase : public Base { kpeter@244: typedef T ProcessedMap; kpeter@244: static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; kpeter@257: SetProcessedMapBase(const TR &b) : TR(b) {} kpeter@244: }; kpeter@278: ///\brief \ref named-func-param "Named parameter" kpeter@244: ///for setting \ref ProcessedMap object. kpeter@244: /// kpeter@278: /// \ref named-func-param "Named parameter" kpeter@244: ///for setting \ref ProcessedMap object. kpeter@244: template kpeter@257: DijkstraWizard > processedMap(const T &t) kpeter@244: { kpeter@244: Base::_processed=reinterpret_cast(const_cast(&t)); kpeter@257: return DijkstraWizard >(*this); kpeter@244: } kpeter@244: kpeter@244: template kpeter@278: struct SetPathBase : public Base { kpeter@278: typedef T Path; kpeter@278: SetPathBase(const TR &b) : TR(b) {} alpar@100: }; kpeter@278: ///\brief \ref named-func-param "Named parameter" kpeter@278: ///for getting the shortest path to the target node. alpar@100: /// kpeter@278: ///\ref named-func-param "Named parameter" kpeter@278: ///for getting the shortest path to the target node. alpar@100: template kpeter@278: DijkstraWizard > path(const T &t) alpar@100: { kpeter@278: Base::_path=reinterpret_cast(const_cast(&t)); kpeter@278: return DijkstraWizard >(*this); kpeter@278: } kpeter@278: kpeter@278: ///\brief \ref named-func-param "Named parameter" kpeter@278: ///for getting the distance of the target node. kpeter@278: /// kpeter@278: ///\ref named-func-param "Named parameter" kpeter@278: ///for getting the distance of the target node. kpeter@278: DijkstraWizard dist(const Value &d) kpeter@278: { kpeter@278: Base::_di=reinterpret_cast(const_cast(&d)); kpeter@278: return *this; alpar@100: } alpar@209: alpar@100: }; alpar@209: kpeter@278: ///Function-type interface for Dijkstra algorithm. alpar@100: alpar@100: /// \ingroup shortest_path kpeter@278: ///Function-type interface for Dijkstra algorithm. alpar@100: /// kpeter@278: ///This function also has several \ref named-func-param "named parameters", alpar@100: ///they are declared as the members of class \ref DijkstraWizard. kpeter@278: ///The following examples show how to use these parameters. alpar@100: ///\code kpeter@278: /// // Compute shortest path from node s to each node kpeter@278: /// dijkstra(g,length).predMap(preds).distMap(dists).run(s); kpeter@278: /// kpeter@278: /// // Compute shortest path from s to t kpeter@278: /// bool reached = dijkstra(g,length).path(p).dist(d).run(s,t); alpar@100: ///\endcode alpar@100: ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" alpar@100: ///to the end of the parameter list. alpar@100: ///\sa DijkstraWizard alpar@100: ///\sa Dijkstra alpar@100: template alpar@100: DijkstraWizard > kpeter@278: dijkstra(const GR &digraph, const LM &length) alpar@100: { kpeter@278: return DijkstraWizard >(digraph,length); alpar@100: } alpar@100: alpar@100: } //END OF NAMESPACE LEMON alpar@100: alpar@100: #endif