alpar@100: /* -*- C++ -*- alpar@100: * alpar@100: * 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@100: alpar@100: #include alpar@100: #include alpar@100: #include alpar@100: #include alpar@100: #include alpar@100: #include alpar@100: alpar@100: alpar@100: namespace lemon { alpar@100: alpar@100: /// \brief Default OperationTraits for the Dijkstra algorithm class. alpar@100: /// alpar@100: /// It defines all computational operations and constants which are alpar@100: /// 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: } alpar@100: /// \brief Gives back true only if the first value 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: alpar@100: /// \brief Widest path OperationTraits for the Dijkstra algorithm class. alpar@100: /// alpar@100: /// It defines all computational operations and constants which are alpar@100: /// used in the Dijkstra algorithm for widest path computation. 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: } alpar@100: /// \brief Gives back true only if the first value 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: alpar@100: ///Default traits class of Dijkstra class. alpar@100: alpar@100: ///Default traits class of Dijkstra class. alpar@100: ///\param GR Digraph type. alpar@100: ///\param LM Type of length map. alpar@100: template alpar@100: struct DijkstraDefaultTraits alpar@100: { alpar@100: ///The digraph type 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; alpar@100: //The type of the length of the arcs. alpar@100: typedef typename LM::Value Value; alpar@100: /// Operation traits for Dijkstra algorithm. alpar@100: alpar@100: /// It defines the used operation by the algorithm. alpar@100: /// \see DijkstraDefaultOperationTraits alpar@100: typedef DijkstraDefaultOperationTraits OperationTraits; alpar@100: /// The cross reference type used by heap. alpar@100: alpar@100: alpar@100: /// The cross reference type used by heap. alpar@100: /// Usually it is \c Digraph::NodeMap. alpar@100: typedef typename Digraph::template NodeMap HeapCrossRef; alpar@100: ///Instantiates a HeapCrossRef. alpar@100: alpar@100: ///This function instantiates a \c HeapCrossRef. alpar@100: /// \param G is the digraph, to which we would like to define the alpar@100: /// HeapCrossRef. alpar@100: static HeapCrossRef *createHeapCrossRef(const GR &G) alpar@100: { alpar@100: return new HeapCrossRef(G); alpar@100: } alpar@100: alpar@100: ///The heap type used by Dijkstra algorithm. alpar@100: alpar@100: ///The heap type used by Dijkstra algorithm. alpar@100: /// alpar@100: ///\sa BinHeap alpar@100: ///\sa Dijkstra alpar@100: typedef BinHeap > Heap; alpar@100: alpar@100: static Heap *createHeap(HeapCrossRef& R) alpar@100: { alpar@100: return new Heap(R); alpar@100: } alpar@100: alpar@100: ///\brief The type of the map that stores the last alpar@100: ///arcs of the shortest paths. alpar@100: /// alpar@100: ///The type of the map that stores the last alpar@100: ///arcs of the shortest paths. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. alpar@100: /// alpar@100: typedef typename Digraph::template NodeMap PredMap; alpar@100: ///Instantiates a PredMap. alpar@100: alpar@100: ///This function instantiates a \c PredMap. alpar@100: ///\param G is the digraph, to which we would like to define the PredMap. alpar@100: ///\todo The digraph alone may be insufficient for the initialization alpar@100: static PredMap *createPredMap(const GR &G) alpar@100: { alpar@100: return new PredMap(G); alpar@100: } alpar@100: alpar@100: ///The type of the map that stores whether a nodes is processed. alpar@100: alpar@100: ///The type of the map that stores whether a nodes is processed. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. alpar@100: ///By default it is a NullMap. alpar@100: ///\todo If it is set to a real map, alpar@100: ///Dijkstra::processed() should read this. alpar@100: ///\todo named parameter to set this type, function to read and write. alpar@100: typedef NullMap ProcessedMap; alpar@100: ///Instantiates a ProcessedMap. alpar@100: alpar@100: ///This function instantiates a \c ProcessedMap. alpar@100: ///\param g is the digraph, to which alpar@100: ///we would like to define the \c ProcessedMap alpar@100: #ifdef DOXYGEN alpar@100: static ProcessedMap *createProcessedMap(const GR &g) alpar@100: #else alpar@100: static ProcessedMap *createProcessedMap(const GR &) alpar@100: #endif alpar@100: { alpar@100: return new ProcessedMap(); alpar@100: } alpar@100: ///The type of the map that stores the dists of the nodes. alpar@100: alpar@100: ///The type of the map that stores the dists of the nodes. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. alpar@100: /// alpar@100: typedef typename Digraph::template NodeMap DistMap; alpar@100: ///Instantiates a DistMap. alpar@100: alpar@100: ///This function instantiates a \ref DistMap. alpar@100: ///\param G is the digraph, to which we would like to define the \ref DistMap alpar@100: static DistMap *createDistMap(const GR &G) alpar@100: { alpar@100: return new DistMap(G); alpar@100: } alpar@100: }; alpar@100: alpar@100: ///%Dijkstra algorithm class. alpar@100: alpar@100: /// \ingroup shortest_path alpar@100: ///This class provides an efficient implementation of %Dijkstra algorithm. 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: /// alpar@100: ///The type of the length is determined by the alpar@100: ///\ref concepts::ReadMap::Value "Value" of the length map. alpar@100: /// alpar@100: ///It is also possible to change the underlying priority heap. alpar@100: /// alpar@100: ///\param GR The digraph type the algorithm runs on. The default value alpar@100: ///is \ref ListDigraph. The value of GR is not used directly by alpar@100: ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits. alpar@100: ///\param LM This read-only ArcMap determines the lengths of the alpar@100: ///arcs. It is read once for each arc, so the map may involve in alpar@100: ///relatively time consuming process to compute the arc length if alpar@100: ///it is necessary. The default map type is \ref alpar@100: ///concepts::Digraph::ArcMap "Digraph::ArcMap". The value alpar@100: ///of LM is not used directly by Dijkstra, it is only passed to \ref alpar@100: ///DijkstraDefaultTraits. \param TR Traits class to set alpar@100: ///various data types used by the algorithm. The default traits alpar@100: ///class is \ref DijkstraDefaultTraits alpar@100: ///"DijkstraDefaultTraits". See \ref alpar@100: ///DijkstraDefaultTraits for the documentation of a Dijkstra traits alpar@100: ///class. alpar@100: /// alpar@100: ///\author Jacint Szabo and Alpar Juttner alpar@100: alpar@100: #ifdef DOXYGEN alpar@100: template alpar@100: #else alpar@100: template , alpar@100: typename TR=DijkstraDefaultTraits > alpar@100: #endif alpar@100: class Dijkstra { alpar@100: public: alpar@100: /** alpar@100: * \brief \ref Exception for uninitialized parameters. alpar@100: * alpar@100: * This error represents problems in the initialization alpar@100: * of the parameters of the algorithms. alpar@100: */ alpar@100: class UninitializedParameter : public lemon::UninitializedParameter { alpar@100: public: alpar@100: virtual const char* what() const throw() { alpar@100: return "lemon::Dijkstra::UninitializedParameter"; alpar@100: } alpar@100: }; alpar@100: alpar@100: typedef TR Traits; alpar@100: ///The type of the underlying digraph. alpar@100: typedef typename TR::Digraph Digraph; alpar@100: ///\e alpar@100: typedef typename Digraph::Node Node; alpar@100: ///\e alpar@100: typedef typename Digraph::NodeIt NodeIt; alpar@100: ///\e alpar@100: typedef typename Digraph::Arc Arc; alpar@100: ///\e alpar@100: typedef typename Digraph::OutArcIt OutArcIt; alpar@100: 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; alpar@100: ///\brief The type of the map that stores the last alpar@100: ///arcs of the shortest paths. alpar@100: typedef typename TR::PredMap PredMap; alpar@100: ///The type of the map indicating if a node is processed. alpar@100: typedef typename TR::ProcessedMap ProcessedMap; alpar@100: ///The type of the map that stores the dists of the nodes. alpar@100: typedef typename TR::DistMap DistMap; alpar@100: ///The cross reference type used for the current heap. alpar@100: typedef typename TR::HeapCrossRef HeapCrossRef; alpar@100: ///The heap type used by the dijkstra algorithm. alpar@100: typedef typename TR::Heap Heap; alpar@100: ///The operation traits. alpar@100: typedef typename TR::OperationTraits OperationTraits; alpar@100: private: alpar@100: /// Pointer to the underlying digraph. alpar@100: const Digraph *G; alpar@100: /// Pointer to the length map alpar@100: const LengthMap *length; alpar@100: ///Pointer to the map of predecessors arcs. alpar@100: PredMap *_pred; alpar@100: ///Indicates if \ref _pred is locally allocated (\c true) or not. alpar@100: bool local_pred; alpar@100: ///Pointer to the map of distances. alpar@100: DistMap *_dist; alpar@100: ///Indicates if \ref _dist is locally allocated (\c true) or not. alpar@100: bool local_dist; alpar@100: ///Pointer to the map of processed status of the nodes. alpar@100: ProcessedMap *_processed; alpar@100: ///Indicates if \ref _processed is locally allocated (\c true) or not. alpar@100: bool local_processed; alpar@100: ///Pointer to the heap cross references. alpar@100: HeapCrossRef *_heap_cross_ref; alpar@100: ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not. alpar@100: bool local_heap_cross_ref; alpar@100: ///Pointer to the heap. alpar@100: Heap *_heap; alpar@100: ///Indicates if \ref _heap is locally allocated (\c true) or not. alpar@100: bool local_heap; alpar@100: alpar@100: ///Creates the maps if necessary. alpar@100: alpar@100: ///\todo Better memory allocation (instead of new). alpar@100: void create_maps() alpar@100: { alpar@100: if(!_pred) { alpar@100: local_pred = true; alpar@100: _pred = Traits::createPredMap(*G); alpar@100: } alpar@100: if(!_dist) { alpar@100: local_dist = true; alpar@100: _dist = Traits::createDistMap(*G); alpar@100: } alpar@100: if(!_processed) { alpar@100: local_processed = true; alpar@100: _processed = Traits::createProcessedMap(*G); alpar@100: } alpar@100: if (!_heap_cross_ref) { alpar@100: local_heap_cross_ref = true; alpar@100: _heap_cross_ref = Traits::createHeapCrossRef(*G); alpar@100: } alpar@100: if (!_heap) { alpar@100: local_heap = true; alpar@100: _heap = Traits::createHeap(*_heap_cross_ref); alpar@100: } alpar@100: } alpar@100: alpar@100: public : alpar@100: alpar@100: typedef Dijkstra Create; alpar@100: alpar@100: ///\name Named template parameters alpar@100: alpar@100: ///@{ alpar@100: alpar@100: template alpar@100: struct DefPredMapTraits : public Traits { alpar@100: typedef T PredMap; alpar@100: static PredMap *createPredMap(const Digraph &) alpar@100: { alpar@100: throw UninitializedParameter(); alpar@100: } alpar@100: }; alpar@100: ///\ref named-templ-param "Named parameter" for setting PredMap type alpar@100: alpar@100: ///\ref named-templ-param "Named parameter" for setting PredMap type alpar@100: /// alpar@100: template alpar@100: struct DefPredMap alpar@100: : public Dijkstra< Digraph, LengthMap, DefPredMapTraits > { alpar@100: typedef Dijkstra< Digraph, LengthMap, DefPredMapTraits > Create; alpar@100: }; alpar@100: alpar@100: template alpar@100: struct DefDistMapTraits : public Traits { alpar@100: typedef T DistMap; alpar@100: static DistMap *createDistMap(const Digraph &) alpar@100: { alpar@100: throw UninitializedParameter(); alpar@100: } alpar@100: }; alpar@100: ///\ref named-templ-param "Named parameter" for setting DistMap type alpar@100: alpar@100: ///\ref named-templ-param "Named parameter" for setting DistMap type alpar@100: /// alpar@100: template alpar@100: struct DefDistMap alpar@100: : public Dijkstra< Digraph, LengthMap, DefDistMapTraits > { alpar@100: typedef Dijkstra< Digraph, LengthMap, DefDistMapTraits > Create; alpar@100: }; alpar@100: alpar@100: template alpar@100: struct DefProcessedMapTraits : public Traits { alpar@100: typedef T ProcessedMap; alpar@100: static ProcessedMap *createProcessedMap(const Digraph &G) alpar@100: { alpar@100: throw UninitializedParameter(); alpar@100: } alpar@100: }; alpar@100: ///\ref named-templ-param "Named parameter" for setting ProcessedMap type alpar@100: alpar@100: ///\ref named-templ-param "Named parameter" for setting ProcessedMap type alpar@100: /// alpar@100: template alpar@100: struct DefProcessedMap alpar@100: : public Dijkstra< Digraph, LengthMap, DefProcessedMapTraits > { alpar@100: typedef Dijkstra< Digraph, LengthMap, DefProcessedMapTraits > Create; alpar@100: }; alpar@100: alpar@100: struct DefDigraphProcessedMapTraits : public Traits { alpar@100: typedef typename Digraph::template NodeMap ProcessedMap; alpar@100: static ProcessedMap *createProcessedMap(const Digraph &G) alpar@100: { alpar@100: return new ProcessedMap(G); alpar@100: } alpar@100: }; alpar@100: ///\brief \ref named-templ-param "Named parameter" alpar@100: ///for setting the ProcessedMap type to be Digraph::NodeMap. alpar@100: /// alpar@100: ///\ref named-templ-param "Named parameter" alpar@100: ///for setting the ProcessedMap type to be Digraph::NodeMap. alpar@100: ///If you don't set it explicitely, it will be automatically allocated. alpar@100: template alpar@100: struct DefProcessedMapToBeDefaultMap alpar@100: : public Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> { alpar@100: typedef Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> Create; alpar@100: }; alpar@100: alpar@100: template alpar@100: struct DefHeapTraits : public Traits { alpar@100: typedef CR HeapCrossRef; alpar@100: typedef H Heap; alpar@100: static HeapCrossRef *createHeapCrossRef(const Digraph &) { alpar@100: throw UninitializedParameter(); alpar@100: } alpar@100: static Heap *createHeap(HeapCrossRef &) alpar@100: { alpar@100: 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@100: ///\ref named-templ-param "Named parameter" for setting heap and cross alpar@100: ///reference type alpar@100: /// alpar@100: template > alpar@100: struct DefHeap alpar@100: : public Dijkstra< Digraph, LengthMap, DefHeapTraits > { alpar@100: typedef Dijkstra< Digraph, LengthMap, DefHeapTraits > Create; alpar@100: }; alpar@100: alpar@100: template alpar@100: struct DefStandardHeapTraits : public Traits { alpar@100: typedef CR HeapCrossRef; alpar@100: typedef H Heap; alpar@100: static HeapCrossRef *createHeapCrossRef(const Digraph &G) { alpar@100: return new HeapCrossRef(G); alpar@100: } alpar@100: static Heap *createHeap(HeapCrossRef &R) alpar@100: { alpar@100: 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@100: ///\ref named-templ-param "Named parameter" for setting heap and cross alpar@100: ///reference type. It can allocate the heap and the cross reference alpar@100: ///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 > alpar@100: struct DefStandardHeap alpar@100: : public Dijkstra< Digraph, LengthMap, DefStandardHeapTraits > { alpar@100: typedef Dijkstra< Digraph, LengthMap, DefStandardHeapTraits > alpar@100: Create; alpar@100: }; alpar@100: alpar@100: template alpar@100: struct DefOperationTraitsTraits : public Traits { alpar@100: typedef T OperationTraits; alpar@100: }; alpar@100: alpar@100: /// \brief \ref named-templ-param "Named parameter" for setting alpar@100: /// OperationTraits type alpar@100: /// alpar@100: /// \ref named-templ-param "Named parameter" for setting OperationTraits alpar@100: /// type alpar@100: template alpar@100: struct DefOperationTraits alpar@100: : public Dijkstra > { alpar@100: typedef Dijkstra > alpar@100: Create; alpar@100: }; alpar@100: alpar@100: ///@} alpar@100: alpar@100: alpar@100: protected: alpar@100: alpar@100: Dijkstra() {} alpar@100: alpar@100: public: alpar@100: alpar@100: ///Constructor. alpar@100: alpar@100: ///\param _G the digraph the algorithm will run on. alpar@100: ///\param _length the length map used by the algorithm. alpar@100: Dijkstra(const Digraph& _G, const LengthMap& _length) : alpar@100: 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@100: alpar@100: ///Destructor. alpar@100: ~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@100: Dijkstra &lengthMap(const LengthMap &m) alpar@100: { alpar@100: length = &m; alpar@100: return *this; alpar@100: } alpar@100: alpar@100: ///Sets the map storing the predecessor arcs. alpar@100: alpar@100: ///Sets the map storing the predecessor arcs. alpar@100: ///If you don't use this function before calling \ref run(), alpar@100: ///it will allocate one. The destuctor deallocates this alpar@100: ///automatically allocated map, of course. alpar@100: ///\return (*this) alpar@100: Dijkstra &predMap(PredMap &m) alpar@100: { alpar@100: if(local_pred) { alpar@100: delete _pred; alpar@100: local_pred=false; alpar@100: } alpar@100: _pred = &m; alpar@100: return *this; alpar@100: } alpar@100: alpar@100: ///Sets the map storing the distances calculated by the algorithm. alpar@100: alpar@100: ///Sets the map storing the distances calculated by the algorithm. alpar@100: ///If you don't use this function before calling \ref run(), alpar@100: ///it will allocate one. The destuctor deallocates this alpar@100: ///automatically allocated map, of course. alpar@100: ///\return (*this) alpar@100: Dijkstra &distMap(DistMap &m) alpar@100: { alpar@100: if(local_dist) { alpar@100: delete _dist; alpar@100: 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(), alpar@100: ///it will allocate one. The destuctor 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@100: delete _heap_cross_ref; alpar@100: local_heap_cross_ref=false; alpar@100: } alpar@100: _heap_cross_ref = &cr; alpar@100: if(local_heap) { alpar@100: delete _heap; alpar@100: local_heap=false; alpar@100: } alpar@100: _heap = &hp; alpar@100: return *this; alpar@100: } alpar@100: alpar@100: private: 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: typedef PredMapPath Path; alpar@100: alpar@100: ///\name Execution control alpar@100: ///The simplest way to execute the algorithm is to use alpar@100: ///one of the member functions called \c run(...). alpar@100: ///\n alpar@100: ///If you need more control on the execution, alpar@100: ///first you must call \ref init(), then you can add several source nodes alpar@100: ///with \ref addSource(). alpar@100: ///Finally \ref start() will perform the actual path alpar@100: ///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@100: _pred->set(u,INVALID); alpar@100: _processed->set(u,false); alpar@100: _heap_cross_ref->set(u,Heap::PRE_HEAP); alpar@100: } alpar@100: } alpar@100: alpar@100: ///Adds a new source node. alpar@100: alpar@100: ///Adds a new source node to the priority heap. alpar@100: /// alpar@100: ///The optional second parameter is the initial distance of the node. alpar@100: /// alpar@100: ///It 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@100: _heap->push(s,dst); alpar@100: } else if(OperationTraits::less((*_heap)[s], dst)) { alpar@100: _heap->set(s,dst); alpar@100: _pred->set(s,INVALID); alpar@100: } alpar@100: } alpar@100: 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: /// alpar@100: ///\warning The priority heap must not be empty! alpar@100: Node processNextNode() alpar@100: { alpar@100: Node v=_heap->top(); alpar@100: Value oldvalue=_heap->prio(); alpar@100: _heap->pop(); alpar@100: finalizeNodeData(v,oldvalue); alpar@100: alpar@100: for(OutArcIt e(*G,v); e!=INVALID; ++e) { alpar@100: Node w=G->target(e); alpar@100: switch(_heap->state(w)) { alpar@100: case Heap::PRE_HEAP: alpar@100: _heap->push(w,OperationTraits::plus(oldvalue, (*length)[e])); alpar@100: _pred->set(w,e); alpar@100: break; alpar@100: case Heap::IN_HEAP: alpar@100: { alpar@100: Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]); alpar@100: if ( OperationTraits::less(newvalue, (*_heap)[w]) ) { alpar@100: _heap->decrease(w, newvalue); alpar@100: _pred->set(w,e); alpar@100: } alpar@100: } alpar@100: break; alpar@100: case Heap::POST_HEAP: alpar@100: break; alpar@100: } alpar@100: } alpar@100: return v; alpar@100: } alpar@100: alpar@100: ///Next node to be processed. alpar@100: alpar@100: ///Next node to be processed. alpar@100: /// alpar@100: ///\return The next node to be processed or INVALID if the priority heap alpar@100: /// is empty. alpar@100: Node nextNode() alpar@100: { alpar@100: return !_heap->empty()?_heap->top():INVALID; alpar@100: } alpar@100: alpar@100: ///\brief Returns \c false if there are nodes alpar@100: ///to be processed in the priority heap alpar@100: /// alpar@100: ///Returns \c false if there are nodes alpar@100: ///to be processed in the priority heap alpar@100: bool emptyQueue() { return _heap->empty(); } alpar@100: ///Returns the number of the nodes to be processed in the priority heap alpar@100: alpar@100: ///Returns the number of the nodes to be processed in the priority heap alpar@100: /// alpar@100: int queueSize() { return _heap->size(); } alpar@100: alpar@100: ///Executes the algorithm. alpar@100: alpar@100: ///Executes the algorithm. alpar@100: /// alpar@100: ///\pre init() must be called and at least one node should be added alpar@100: ///with addSource() before using this function. alpar@100: /// alpar@100: ///This method runs the %Dijkstra algorithm from the root node(s) alpar@100: ///in order to alpar@100: ///compute the alpar@100: ///shortest path to each node. The algorithm computes alpar@100: ///- The shortest path tree. alpar@100: ///- The distance of each node from the root(s). alpar@100: /// alpar@100: void start() alpar@100: { alpar@100: while ( !_heap->empty() ) processNextNode(); alpar@100: } alpar@100: alpar@100: ///Executes the algorithm until \c dest is reached. alpar@100: alpar@100: ///Executes the algorithm until \c dest is reached. alpar@100: /// alpar@100: ///\pre init() must be called and at least one node should be added alpar@100: ///with addSource() before using this function. alpar@100: /// alpar@100: ///This method runs the %Dijkstra algorithm from the root node(s) alpar@100: ///in order to alpar@100: ///compute the alpar@100: ///shortest path to \c dest. The algorithm computes alpar@100: ///- The shortest path to \c dest. alpar@100: ///- The distance of \c dest from the root(s). alpar@100: /// 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@100: alpar@100: ///Executes the algorithm until a condition is met. alpar@100: alpar@100: ///Executes the algorithm until a condition is met. alpar@100: /// alpar@100: ///\pre init() must be called and at least one node should be added alpar@100: ///with addSource() before using this function. alpar@100: /// alpar@100: ///\param nm must be a 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. 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@100: alpar@100: ///Runs %Dijkstra algorithm from node \c s. alpar@100: alpar@100: ///This method runs the %Dijkstra algorithm from a root node \c s alpar@100: ///in order to alpar@100: ///compute the alpar@100: ///shortest path to each node. The algorithm computes alpar@100: ///- The shortest path tree. alpar@100: ///- The distance of each node from the root. alpar@100: /// alpar@100: ///\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@100: alpar@100: ///Finds the shortest path between \c s and \c t. alpar@100: alpar@100: ///Finds the shortest path between \c s and \c t. alpar@100: /// alpar@100: ///\return The length of the shortest s---t path if there exists one, alpar@100: ///0 otherwise. alpar@100: ///\note Apart from the return value, d.run(s) is alpar@100: ///just a 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@100: 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 alpar@100: ///Before the use of these functions, alpar@100: ///either run() or start() must be called. alpar@100: alpar@100: ///@{ alpar@100: alpar@100: ///Gives back the shortest path. alpar@100: alpar@100: ///Gives back the shortest path. alpar@100: ///\pre The \c t should be reachable from the source. alpar@100: Path path(Node t) alpar@100: { alpar@100: return Path(*G, *_pred, t); alpar@100: } alpar@100: alpar@100: ///The distance of a node from the root. alpar@100: alpar@100: ///Returns the distance of a node from the root. alpar@100: ///\pre \ref run() must be called before using this function. alpar@100: ///\warning If node \c v in unreachable from the root the return value alpar@100: ///of this funcion is undefined. alpar@100: Value dist(Node v) const { return (*_dist)[v]; } alpar@100: alpar@100: ///The current distance of a node from the root. alpar@100: alpar@100: ///Returns the current distance of a node from the root. alpar@100: ///It may be decreased in the following processes. alpar@100: ///\pre \c node should be reached but not processed alpar@100: Value currentDist(Node v) const { return (*_heap)[v]; } alpar@100: alpar@100: ///Returns the 'previous arc' of the shortest path tree. alpar@100: alpar@100: ///For a node \c v it returns the 'previous arc' of the shortest path tree, alpar@100: ///i.e. it returns the last arc of a shortest path from the root to \c alpar@100: ///v. It is \ref INVALID alpar@100: ///if \c v is unreachable from the root or if \c v=s. The alpar@100: ///shortest path tree used here is equal to the shortest path tree used in alpar@100: ///\ref predNode(). \pre \ref run() must be called before using alpar@100: ///this function. alpar@100: Arc predArc(Node v) const { return (*_pred)[v]; } alpar@100: alpar@100: ///Returns the 'previous node' of the shortest path tree. alpar@100: alpar@100: ///For a node \c v it returns the 'previous node' of the shortest path tree, alpar@100: ///i.e. it returns the last but one node from a shortest path from the alpar@100: ///root to \c /v. It is INVALID if \c v is unreachable from the root or if alpar@100: ///\c v=s. The shortest path tree used here is equal to the shortest path alpar@100: ///tree used in \ref predArc(). \pre \ref run() must be called before alpar@100: ///using this function. alpar@100: Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: alpar@100: G->source((*_pred)[v]); } alpar@100: alpar@100: ///Returns a reference to the NodeMap of distances. alpar@100: alpar@100: ///Returns a reference to the NodeMap of distances. \pre \ref run() must alpar@100: ///be called before using this function. alpar@100: const DistMap &distMap() const { return *_dist;} alpar@100: alpar@100: ///Returns a reference to the shortest path tree map. alpar@100: alpar@100: ///Returns a reference to the NodeMap of the arcs of the alpar@100: ///shortest path tree. alpar@100: ///\pre \ref run() must be called before using this function. alpar@100: const PredMap &predMap() const { return *_pred;} alpar@100: alpar@100: ///Checks if a node is reachable from the root. alpar@100: alpar@100: ///Returns \c true if \c v is reachable from the root. alpar@100: ///\warning The source nodes are inditated as unreached. alpar@100: ///\pre \ref run() must be called before using this function. alpar@100: /// alpar@100: bool reached(Node v) { return (*_heap_cross_ref)[v] != 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. alpar@100: ///\pre \ref run() must be called before using this function. alpar@100: /// alpar@100: bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; } alpar@100: alpar@100: ///@} alpar@100: }; alpar@100: alpar@100: alpar@100: alpar@100: alpar@100: alpar@100: ///Default traits class of Dijkstra function. alpar@100: alpar@100: ///Default traits class of Dijkstra function. alpar@100: ///\param GR Digraph type. alpar@100: ///\param LM Type of length map. alpar@100: template alpar@100: struct DijkstraWizardDefaultTraits alpar@100: { alpar@100: ///The digraph type 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; alpar@100: //The type of the length of the arcs. alpar@100: typedef typename LM::Value Value; alpar@100: /// Operation traits for Dijkstra algorithm. alpar@100: alpar@100: /// It defines the used operation by the algorithm. alpar@100: /// \see DijkstraDefaultOperationTraits alpar@100: typedef DijkstraDefaultOperationTraits OperationTraits; alpar@100: ///The heap type used by Dijkstra algorithm. alpar@100: alpar@100: /// The cross reference type used by heap. alpar@100: alpar@100: /// The cross reference type used by heap. alpar@100: /// Usually it is \c Digraph::NodeMap. alpar@100: typedef typename Digraph::template NodeMap HeapCrossRef; alpar@100: ///Instantiates a HeapCrossRef. alpar@100: alpar@100: ///This function instantiates a \ref HeapCrossRef. alpar@100: /// \param G is the digraph, to which we would like to define the alpar@100: /// HeapCrossRef. alpar@100: /// \todo The digraph alone may be insufficient for the initialization alpar@100: static HeapCrossRef *createHeapCrossRef(const GR &G) alpar@100: { alpar@100: return new HeapCrossRef(G); alpar@100: } alpar@100: alpar@100: ///The heap type used by Dijkstra algorithm. alpar@100: alpar@100: ///The heap type used by Dijkstra algorithm. alpar@100: /// alpar@100: ///\sa BinHeap alpar@100: ///\sa Dijkstra alpar@100: typedef BinHeap, alpar@100: std::less > Heap; alpar@100: alpar@100: static Heap *createHeap(HeapCrossRef& R) alpar@100: { alpar@100: return new Heap(R); alpar@100: } alpar@100: alpar@100: ///\brief The type of the map that stores the last alpar@100: ///arcs of the shortest paths. alpar@100: /// alpar@100: ///The type of the map that stores the last alpar@100: ///arcs of the shortest paths. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. alpar@100: /// alpar@100: typedef NullMap PredMap; alpar@100: ///Instantiates a PredMap. alpar@100: alpar@100: ///This function instantiates a \ref PredMap. alpar@100: ///\param g is the digraph, to which we would like to define the PredMap. alpar@100: ///\todo The digraph alone may be insufficient for the initialization alpar@100: #ifdef DOXYGEN alpar@100: static PredMap *createPredMap(const GR &g) alpar@100: #else alpar@100: static PredMap *createPredMap(const GR &) alpar@100: #endif alpar@100: { alpar@100: return new PredMap(); alpar@100: } alpar@100: ///The type of the map that stores whether a nodes is processed. alpar@100: alpar@100: ///The type of the map that stores whether a nodes is processed. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. alpar@100: ///By default it is a NullMap. alpar@100: ///\todo If it is set to a real map, alpar@100: ///Dijkstra::processed() should read this. alpar@100: ///\todo named parameter to set this type, function to read and write. alpar@100: typedef NullMap ProcessedMap; alpar@100: ///Instantiates a ProcessedMap. alpar@100: alpar@100: ///This function instantiates a \ref ProcessedMap. alpar@100: ///\param g is the digraph, to which alpar@100: ///we would like to define the \ref ProcessedMap alpar@100: #ifdef DOXYGEN alpar@100: static ProcessedMap *createProcessedMap(const GR &g) alpar@100: #else alpar@100: static ProcessedMap *createProcessedMap(const GR &) alpar@100: #endif alpar@100: { alpar@100: return new ProcessedMap(); alpar@100: } alpar@100: ///The type of the map that stores the dists of the nodes. alpar@100: alpar@100: ///The type of the map that stores the dists of the nodes. alpar@100: ///It must meet the \ref concepts::WriteMap "WriteMap" concept. alpar@100: /// alpar@100: typedef NullMap DistMap; alpar@100: ///Instantiates a DistMap. alpar@100: alpar@100: ///This function instantiates a \ref DistMap. alpar@100: ///\param g is the digraph, to which we would like to define the \ref DistMap alpar@100: #ifdef DOXYGEN alpar@100: static DistMap *createDistMap(const GR &g) alpar@100: #else alpar@100: static DistMap *createDistMap(const GR &) alpar@100: #endif alpar@100: { alpar@100: return new DistMap(); alpar@100: } alpar@100: }; alpar@100: alpar@100: /// Default traits used by \ref DijkstraWizard alpar@100: alpar@100: /// To make it easier to use Dijkstra algorithm alpar@100: ///we have created a wizard class. alpar@100: /// This \ref DijkstraWizard class needs default traits, alpar@100: ///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: /// \todo More named parameters are required... alpar@100: template alpar@100: class DijkstraWizardBase : public DijkstraWizardDefaultTraits alpar@100: { alpar@100: alpar@100: typedef DijkstraWizardDefaultTraits Base; alpar@100: protected: alpar@100: /// Type of the nodes in the digraph. alpar@100: typedef typename Base::Digraph::Node Node; alpar@100: alpar@100: /// Pointer to the underlying digraph. alpar@100: void *_g; alpar@100: /// Pointer to the length map alpar@100: void *_length; alpar@100: ///Pointer to the map of predecessors arcs. alpar@100: void *_pred; alpar@100: ///Pointer to the map of distances. alpar@100: void *_dist; alpar@100: ///Pointer to the source node. alpar@100: Node _source; alpar@100: alpar@100: public: alpar@100: /// Constructor. alpar@100: alpar@100: /// This constructor does not require parameters, therefore it initiates alpar@100: /// all of the attributes to default values (0, INVALID). alpar@100: DijkstraWizardBase() : _g(0), _length(0), _pred(0), alpar@100: _dist(0), _source(INVALID) {} alpar@100: alpar@100: /// Constructor. alpar@100: alpar@100: /// This constructor requires some parameters, alpar@100: /// listed in the parameters list. alpar@100: /// Others are initiated to 0. alpar@100: /// \param g is the initial value of \ref _g alpar@100: /// \param l is the initial value of \ref _length alpar@100: /// \param s is the initial value of \ref _source alpar@100: DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : alpar@100: _g(reinterpret_cast(const_cast(&g))), alpar@100: _length(reinterpret_cast(const_cast(&l))), alpar@100: _pred(0), _dist(0), _source(s) {} alpar@100: alpar@100: }; alpar@100: alpar@100: /// A class to make the usage of Dijkstra algorithm easier alpar@100: alpar@100: /// This class is created to make it easier to use Dijkstra algorithm. alpar@100: /// It uses the functions and features of the plain \ref Dijkstra, alpar@100: /// but it is much simpler to use it. alpar@100: /// alpar@100: /// Simplicity means that the way to change the types defined alpar@100: /// in the traits class is based on functions that returns the new class alpar@100: /// and not on templatable built-in classes. alpar@100: /// When using the plain \ref Dijkstra alpar@100: /// the new class with the modified type comes from alpar@100: /// the original class by using the :: alpar@100: /// operator. In the case of \ref DijkstraWizard only alpar@100: /// a function have to be called and it will alpar@100: /// return the needed class. alpar@100: /// alpar@100: /// It does not have own \ref run method. When its \ref run method is called alpar@100: /// it initiates a plain \ref Dijkstra class, and calls the \ref alpar@100: /// Dijkstra::run method of it. alpar@100: template alpar@100: class DijkstraWizard : public TR alpar@100: { alpar@100: typedef TR Base; alpar@100: alpar@100: ///The type of the underlying digraph. alpar@100: typedef typename TR::Digraph Digraph; alpar@100: //\e alpar@100: typedef typename Digraph::Node Node; alpar@100: //\e alpar@100: typedef typename Digraph::NodeIt NodeIt; alpar@100: //\e alpar@100: typedef typename Digraph::Arc Arc; alpar@100: //\e alpar@100: typedef typename Digraph::OutArcIt OutArcIt; alpar@100: 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; alpar@100: ///\brief The type of the map that stores the last alpar@100: ///arcs of the shortest paths. alpar@100: typedef typename TR::PredMap PredMap; alpar@100: ///The type of the map that stores the dists of the nodes. alpar@100: typedef typename TR::DistMap DistMap; alpar@100: ///The heap type used by the dijkstra algorithm. alpar@100: typedef typename TR::Heap Heap; alpar@100: public: 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. alpar@100: DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) : alpar@100: TR(g,l,s) {} alpar@100: alpar@100: ///Copy constructor alpar@100: DijkstraWizard(const TR &b) : TR(b) {} alpar@100: alpar@100: ~DijkstraWizard() {} alpar@100: alpar@100: ///Runs Dijkstra algorithm from a given node. alpar@100: alpar@100: ///Runs Dijkstra algorithm from a given node. alpar@100: ///The node can be given by the \ref source function. alpar@100: void run() alpar@100: { alpar@100: if(Base::_source==INVALID) throw UninitializedParameter(); alpar@100: Dijkstra alpar@100: dij(*reinterpret_cast(Base::_g), alpar@100: *reinterpret_cast(Base::_length)); alpar@100: if(Base::_pred) dij.predMap(*reinterpret_cast(Base::_pred)); alpar@100: if(Base::_dist) dij.distMap(*reinterpret_cast(Base::_dist)); alpar@100: dij.run(Base::_source); alpar@100: } alpar@100: alpar@100: ///Runs Dijkstra algorithm from the given node. alpar@100: alpar@100: ///Runs Dijkstra algorithm from the given node. alpar@100: ///\param s is the given source. alpar@100: void run(Node s) alpar@100: { alpar@100: Base::_source=s; alpar@100: run(); alpar@100: } alpar@100: alpar@100: template alpar@100: struct DefPredMapBase : public Base { alpar@100: typedef T PredMap; alpar@100: static PredMap *createPredMap(const Digraph &) { return 0; }; alpar@100: DefPredMapBase(const TR &b) : TR(b) {} alpar@100: }; alpar@100: alpar@100: ///\brief \ref named-templ-param "Named parameter" alpar@100: ///function for setting PredMap type alpar@100: /// alpar@100: /// \ref named-templ-param "Named parameter" alpar@100: ///function for setting PredMap type alpar@100: /// alpar@100: template alpar@100: DijkstraWizard > predMap(const T &t) alpar@100: { alpar@100: Base::_pred=reinterpret_cast(const_cast(&t)); alpar@100: return DijkstraWizard >(*this); alpar@100: } alpar@100: alpar@100: template alpar@100: struct DefDistMapBase : public Base { alpar@100: typedef T DistMap; alpar@100: static DistMap *createDistMap(const Digraph &) { return 0; }; alpar@100: DefDistMapBase(const TR &b) : TR(b) {} alpar@100: }; alpar@100: alpar@100: ///\brief \ref named-templ-param "Named parameter" alpar@100: ///function for setting DistMap type alpar@100: /// alpar@100: /// \ref named-templ-param "Named parameter" alpar@100: ///function for setting DistMap type alpar@100: /// alpar@100: template alpar@100: DijkstraWizard > distMap(const T &t) alpar@100: { alpar@100: Base::_dist=reinterpret_cast(const_cast(&t)); alpar@100: return DijkstraWizard >(*this); alpar@100: } alpar@100: alpar@100: /// Sets the source node, from which the Dijkstra algorithm runs. alpar@100: alpar@100: /// Sets the source node, from which the Dijkstra algorithm runs. alpar@100: /// \param s is the source node. alpar@100: DijkstraWizard &source(Node s) alpar@100: { alpar@100: Base::_source=s; alpar@100: return *this; alpar@100: } alpar@100: alpar@100: }; alpar@100: alpar@100: ///Function type interface for Dijkstra algorithm. alpar@100: alpar@100: /// \ingroup shortest_path alpar@100: ///Function type interface for Dijkstra algorithm. alpar@100: /// alpar@100: ///This function also has several alpar@100: ///\ref named-templ-func-param "named parameters", alpar@100: ///they are declared as the members of class \ref DijkstraWizard. alpar@100: ///The following alpar@100: ///example shows how to use these parameters. alpar@100: ///\code alpar@100: /// dijkstra(g,length,source).predMap(preds).run(); 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 > alpar@100: dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) alpar@100: { alpar@100: return DijkstraWizard >(g,l,s); alpar@100: } alpar@100: alpar@100: } //END OF NAMESPACE LEMON alpar@100: alpar@100: #endif