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