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@440: * Copyright (C) 2003-2009 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. kpeter@559: template alpar@100: struct DijkstraDefaultOperationTraits { kpeter@559: /// \e kpeter@559: typedef V Value; 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: 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@559: ///\tparam LEN The type of the length map. kpeter@559: 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. kpeter@716: ///It must conform to the \ref concepts::ReadMap "ReadMap" concept. kpeter@559: typedef LEN LengthMap; kpeter@716: ///The type of the arc lengths. kpeter@559: typedef typename LEN::Value Value; kpeter@244: kpeter@503: /// 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@503: ///Instantiates a \c 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@503: ///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@559: typedef BinHeap > Heap; kpeter@503: ///Instantiates a \c 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. kpeter@716: ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. kpeter@244: typedef typename Digraph::template NodeMap PredMap; kpeter@503: ///Instantiates a \c PredMap. alpar@209: kpeter@503: ///This function instantiates a \ref PredMap. kpeter@244: ///\param g is the digraph, to which we would like to define the kpeter@503: ///\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. kpeter@716: ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. alpar@100: ///By default it is a NullMap. alpar@100: typedef NullMap ProcessedMap; kpeter@503: ///Instantiates a \c ProcessedMap. alpar@209: kpeter@503: ///This function instantiates a \ref ProcessedMap. alpar@100: ///\param g is the digraph, to which kpeter@503: ///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. kpeter@716: ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. kpeter@559: typedef typename Digraph::template NodeMap DistMap; kpeter@503: ///Instantiates a \c DistMap. alpar@209: kpeter@503: ///This function instantiates a \ref DistMap. kpeter@244: ///\param g is the digraph, to which we would like to define kpeter@503: ///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: /// kpeter@716: ///The %Dijkstra algorithm solves the single-source shortest path problem kpeter@716: ///when all arc lengths are non-negative. If there are negative lengths, kpeter@716: ///the BellmanFord algorithm should be used instead. kpeter@716: /// 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@405: ///The default type is \ref ListDigraph. kpeter@559: ///\tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies kpeter@405: ///the lengths of the arcs. kpeter@405: ///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@405: ///concepts::Digraph::ArcMap "GR::ArcMap". alpar@100: #ifdef DOXYGEN kpeter@559: template alpar@100: #else alpar@100: template , kpeter@559: typename TR=DijkstraDefaultTraits > alpar@100: #endif alpar@100: class Dijkstra { alpar@100: public: alpar@100: kpeter@244: ///The type of the digraph the algorithm runs on. alpar@100: typedef typename TR::Digraph Digraph; alpar@209: kpeter@716: ///The type of the arc lengths. 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@503: ///\brief The \ref DijkstraDefaultOperationTraits "operation traits class" kpeter@503: ///of the algorithm. alpar@100: typedef typename TR::OperationTraits OperationTraits; kpeter@244: kpeter@405: ///The \ref DijkstraDefaultTraits "traits class" of the algorithm. 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. kpeter@503: 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: kpeter@584: ///\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: { deba@290: LEMON_ASSERT(false, "PredMap is not initialized"); deba@290: return 0; // ignore warnings alpar@100: } alpar@100: }; kpeter@244: ///\brief \ref named-templ-param "Named parameter" for setting kpeter@503: ///\c PredMap type. alpar@100: /// kpeter@244: ///\ref named-templ-param "Named parameter" for setting kpeter@503: ///\c PredMap type. kpeter@716: ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. 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: { deba@290: LEMON_ASSERT(false, "DistMap is not initialized"); deba@290: return 0; // ignore warnings alpar@100: } alpar@100: }; kpeter@244: ///\brief \ref named-templ-param "Named parameter" for setting kpeter@503: ///\c DistMap type. alpar@100: /// kpeter@244: ///\ref named-templ-param "Named parameter" for setting kpeter@503: ///\c DistMap type. kpeter@716: ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. 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: { deba@290: LEMON_ASSERT(false, "ProcessedMap is not initialized"); deba@290: return 0; // ignore warnings alpar@100: } alpar@100: }; kpeter@244: ///\brief \ref named-templ-param "Named parameter" for setting kpeter@503: ///\c ProcessedMap type. alpar@100: /// kpeter@244: ///\ref named-templ-param "Named parameter" for setting kpeter@503: ///\c ProcessedMap type. kpeter@716: ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. 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@503: ///\c ProcessedMap type to be Digraph::NodeMap. alpar@100: /// kpeter@244: ///\ref named-templ-param "Named parameter" for setting kpeter@503: ///\c 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 &) { deba@290: LEMON_ASSERT(false, "HeapCrossRef is not initialized"); deba@290: return 0; // ignore warnings alpar@100: } alpar@209: static Heap *createHeap(HeapCrossRef &) alpar@100: { deba@290: LEMON_ASSERT(false, "Heap is not initialized"); deba@290: return 0; // ignore warnings alpar@100: } alpar@100: }; alpar@100: ///\brief \ref named-templ-param "Named parameter" for setting kpeter@405: ///heap and cross reference types alpar@100: /// alpar@209: ///\ref named-templ-param "Named parameter" for setting heap and cross kpeter@405: ///reference types. If this named parameter is used, then external kpeter@405: ///heap and cross reference objects must be passed to the algorithm kpeter@405: ///using the \ref heap() function before calling \ref run(Node) "run()" kpeter@405: ///or \ref init(). kpeter@405: ///\sa SetStandardHeap 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 kpeter@405: ///heap and cross reference types with automatic allocation alpar@100: /// alpar@209: ///\ref named-templ-param "Named parameter" for setting heap and cross kpeter@405: ///reference types with automatic allocation. kpeter@405: ///They should have standard constructor interfaces to be able to kpeter@405: ///automatically created by the algorithm (i.e. the digraph should be kpeter@405: ///passed to the constructor of the cross reference and the cross kpeter@405: ///reference should be passed to the constructor of the heap). kpeter@405: ///However external heap and cross reference objects could also be kpeter@405: ///passed to the algorithm using the \ref heap() function before kpeter@405: ///calling \ref run(Node) "run()" or \ref init(). kpeter@405: ///\sa SetHeap 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@313: ///\c OperationTraits type alpar@100: /// kpeter@244: ///\ref named-templ-param "Named parameter" for setting kpeter@503: ///\c OperationTraits type. kpeter@716: /// For more information see \ref DijkstraDefaultOperationTraits. 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@503: ///\param g The digraph the algorithm runs on. kpeter@503: ///\param length The length map used by the algorithm. kpeter@503: Dijkstra(const Digraph& g, const LengthMap& length) : kpeter@503: 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: { kpeter@503: _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. kpeter@405: ///If you don't use this function before calling \ref run(Node) "run()" kpeter@405: ///or \ref init(), an instance will be allocated automatically. kpeter@405: ///The destructor deallocates this automatically allocated map, kpeter@405: ///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. kpeter@405: ///If you don't use this function before calling \ref run(Node) "run()" kpeter@405: ///or \ref init(), an instance will be allocated automatically. kpeter@405: ///The destructor deallocates this automatically allocated map, kpeter@405: ///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@405: ///If you don't use this function before calling \ref run(Node) "run()" kpeter@405: ///or \ref init(), an instance will be allocated automatically. kpeter@405: ///The destructor deallocates this automatically allocated map, kpeter@405: ///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. kpeter@405: ///If you don't use this function before calling \ref run(Node) "run()" kpeter@405: ///or \ref init(), heap and cross reference instances will be kpeter@405: ///allocated automatically. kpeter@405: ///The destructor deallocates these automatically allocated objects, kpeter@405: ///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: kpeter@405: ///\name Execution Control kpeter@405: ///The simplest way to execute the %Dijkstra algorithm is to use kpeter@405: ///one of the member functions called \ref run(Node) "run()".\n kpeter@713: ///If you need better control on the execution, you have to call kpeter@713: ///\ref init() first, then you can add several source nodes with kpeter@405: ///\ref addSource(). Finally the actual path computation can be kpeter@405: ///performed with one of the \ref start() functions. alpar@100: alpar@100: ///@{ alpar@100: kpeter@405: ///\brief Initializes the internal data structures. kpeter@405: /// alpar@100: ///Initializes the internal data structures. 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: kpeter@503: _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: { kpeter@503: 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: kpeter@405: ///Returns \c false if there are nodes to be processed. kpeter@405: kpeter@405: ///Returns \c false if there are nodes to be processed kpeter@405: ///in the priority heap. kpeter@244: bool emptyQueue() const { return _heap->empty(); } kpeter@244: kpeter@405: ///Returns the number of the nodes to be processed. alpar@100: kpeter@405: ///Returns the number of the nodes to be processed kpeter@405: ///in the priority heap. 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@286: ///Executes the algorithm until the given target node is processed. kpeter@244: kpeter@286: ///Executes the algorithm until the given target node is processed. alpar@100: /// alpar@100: ///This method runs the %Dijkstra algorithm from the root node(s) kpeter@286: ///in order to compute the shortest path to \c t. alpar@100: /// kpeter@244: ///The algorithm computes kpeter@286: ///- the shortest path to \c t, kpeter@286: ///- the distance of \c t 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. kpeter@286: void start(Node t) alpar@100: { kpeter@286: while ( !_heap->empty() && _heap->top()!=t ) processNextNode(); kpeter@286: if ( !_heap->empty() ) { kpeter@286: finalizeNodeData(_heap->top(),_heap->prio()); kpeter@286: _heap->pop(); kpeter@286: } 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@286: ///Runs the algorithm from the given source 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@286: ///in order to compute the shortest path to node \c t kpeter@286: ///(it stops searching when \c t is processed). alpar@100: /// kpeter@286: ///\return \c true if \c t is reachable form \c s. 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 kpeter@286: bool run(Node s,Node t) { alpar@100: init(); alpar@100: addSource(s); alpar@100: start(t); kpeter@286: return (*_heap_cross_ref)[t] == Heap::POST_HEAP; alpar@100: } alpar@209: alpar@100: ///@} alpar@100: alpar@100: ///\name Query Functions kpeter@405: ///The results of the %Dijkstra algorithm can be obtained using these alpar@100: ///functions.\n kpeter@716: ///Either \ref run(Node) "run()" or \ref init() should be called kpeter@405: ///before using them. alpar@209: alpar@100: ///@{ alpar@100: kpeter@716: ///The shortest path to the given node. alpar@209: kpeter@716: ///Returns the shortest path to the given node from the root(s). kpeter@244: /// kpeter@405: ///\warning \c t should be reached from the root(s). kpeter@244: /// kpeter@405: ///\pre Either \ref run(Node) "run()" or \ref init() kpeter@405: ///must be called before using this function. kpeter@244: Path path(Node t) const { return Path(*G, *_pred, t); } alpar@100: kpeter@716: ///The distance of the given node from the root(s). alpar@100: kpeter@716: ///Returns the distance of the given node from the root(s). kpeter@244: /// kpeter@405: ///\warning If node \c v is not reached from the root(s), then kpeter@244: ///the return value of this function is undefined. kpeter@244: /// kpeter@405: ///\pre Either \ref run(Node) "run()" or \ref init() kpeter@405: ///must be called before using this function. alpar@100: Value dist(Node v) const { return (*_dist)[v]; } alpar@100: kpeter@716: ///\brief Returns the 'previous arc' of the shortest path tree for kpeter@716: ///the given node. kpeter@716: /// 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@405: ///shortest path from a root to \c v. It is \c INVALID if \c v kpeter@405: ///is not reached 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@716: ///tree used in \ref predNode() and \ref predMap(). kpeter@244: /// kpeter@405: ///\pre Either \ref run(Node) "run()" or \ref init() kpeter@405: ///must be called before using this function. alpar@100: Arc predArc(Node v) const { return (*_pred)[v]; } alpar@100: kpeter@716: ///\brief Returns the 'previous node' of the shortest path tree for kpeter@716: ///the given node. kpeter@716: /// 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@716: ///of a shortest path from a root to \c v. It is \c INVALID kpeter@405: ///if \c v is not reached 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@716: ///tree used in \ref predArc() and \ref predMap(). kpeter@244: /// kpeter@405: ///\pre Either \ref run(Node) "run()" or \ref init() kpeter@405: ///must be called before 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@405: ///\pre Either \ref run(Node) "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@716: ///arcs, which form the shortest path tree (forest). kpeter@244: /// kpeter@405: ///\pre Either \ref run(Node) "run()" or \ref init() kpeter@244: ///must be called before using this function. alpar@100: const PredMap &predMap() const { return *_pred;} alpar@209: kpeter@716: ///Checks if the given node is reached from the root(s). alpar@100: kpeter@405: ///Returns \c true if \c v is reached from the root(s). kpeter@405: /// kpeter@405: ///\pre Either \ref run(Node) "run()" or \ref init() 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@405: /// kpeter@405: ///\pre Either \ref run(Node) "run()" or \ref init() 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@716: ///The current distance of the given node from the root(s). kpeter@244: kpeter@716: ///Returns the current distance of the given node from the root(s). kpeter@244: ///It may be decreased in the following processes. kpeter@405: /// kpeter@405: ///\pre Either \ref run(Node) "run()" or \ref init() kpeter@286: ///must be called before using this function and kpeter@286: ///node \c v must be reached but not necessarily processed. kpeter@286: Value currentDist(Node v) const { kpeter@286: return processed(v) ? (*_dist)[v] : (*_heap)[v]; kpeter@286: } 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@559: ///\tparam LEN The type of the length map. kpeter@559: 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. kpeter@716: ///It must conform to the \ref concepts::ReadMap "ReadMap" concept. kpeter@559: typedef LEN LengthMap; kpeter@716: ///The type of the arc lengths. kpeter@559: typedef typename LEN::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. kpeter@716: ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. kpeter@278: typedef typename Digraph::template NodeMap PredMap; kpeter@301: ///Instantiates a PredMap. alpar@209: kpeter@301: ///This function instantiates a PredMap. kpeter@244: ///\param g is the digraph, to which we would like to define the kpeter@301: ///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. kpeter@716: ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. alpar@100: ///By default it is a NullMap. alpar@100: typedef NullMap ProcessedMap; kpeter@301: ///Instantiates a ProcessedMap. alpar@209: kpeter@301: ///This function instantiates a ProcessedMap. alpar@100: ///\param g is the digraph, to which kpeter@301: ///we would like to define the 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. kpeter@716: ///It must conform to the \ref concepts::WriteMap "WriteMap" concept. kpeter@559: typedef typename Digraph::template NodeMap DistMap; kpeter@301: ///Instantiates a DistMap. alpar@209: kpeter@301: ///This function instantiates a DistMap. alpar@210: ///\param g is the digraph, to which we would like to define kpeter@301: ///the 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@716: ///It must conform to the \ref concepts::Path "Path" concept. kpeter@278: typedef lemon::Path Path; alpar@100: }; alpar@209: kpeter@313: /// Default traits class used by DijkstraWizard alpar@100: kpeter@716: /// Default traits class used by DijkstraWizard. kpeter@716: /// \tparam GR The type of the digraph. kpeter@716: /// \tparam LEN The type of the length map. kpeter@559: template kpeter@559: class DijkstraWizardBase : public DijkstraWizardDefaultTraits alpar@100: { kpeter@559: 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@559: DijkstraWizardBase(const GR &g,const LEN &l) : alpar@209: _g(reinterpret_cast(const_cast(&g))), kpeter@559: _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@405: /// It does not have own \ref run(Node) "run()" method, it uses the kpeter@405: /// functions 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: 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: typedef typename TR::LengthMap LengthMap; alpar@100: typedef typename LengthMap::Value Value; alpar@100: typedef typename TR::PredMap PredMap; alpar@100: typedef typename TR::DistMap DistMap; kpeter@244: typedef typename TR::ProcessedMap ProcessedMap; kpeter@278: typedef typename TR::Path Path; 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: { 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: 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@716: kpeter@716: ///\brief \ref named-templ-param "Named parameter" for setting kpeter@716: ///the predecessor map. alpar@100: /// kpeter@716: ///\ref named-templ-param "Named parameter" function for setting kpeter@716: ///the map that stores the predecessor arcs of the nodes. 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@716: kpeter@716: ///\brief \ref named-templ-param "Named parameter" for setting kpeter@716: ///the distance map. kpeter@278: /// kpeter@716: ///\ref named-templ-param "Named parameter" function for setting kpeter@716: ///the map that stores the distances of the nodes calculated kpeter@716: ///by the algorithm. 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@716: kpeter@716: ///\brief \ref named-func-param "Named parameter" for setting kpeter@716: ///the processed map. kpeter@244: /// kpeter@716: ///\ref named-templ-param "Named parameter" function for setting kpeter@716: ///the map that indicates which nodes are processed. 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@716: 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 kpeter@405: ///\warning Don't forget to put the \ref DijkstraWizard::run(Node) "run()" alpar@100: ///to the end of the parameter list. alpar@100: ///\sa DijkstraWizard alpar@100: ///\sa Dijkstra kpeter@559: template kpeter@559: DijkstraWizard > kpeter@559: dijkstra(const GR &digraph, const LEN &length) alpar@100: { kpeter@559: return DijkstraWizard >(digraph,length); alpar@100: } alpar@100: alpar@100: } //END OF NAMESPACE LEMON alpar@100: alpar@100: #endif