COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/include/dijkstra.h @ 431:79a5641f2dbc

Last change on this file since 431:79a5641f2dbc was 430:60e4627e8c74, checked in by Alpar Juttner, 20 years ago

Many new modules (groups) in the documentation.

File size: 6.2 KB
Line 
1// -*- C++ -*-
2#ifndef HUGO_DIJKSTRA_H
3#define HUGO_DIJKSTRA_H
4
5///ingroup galgs
6///\file
7///\brief Dijkstra algorithm.
8
9#include <bin_heap.h>
10#include <invalid.h>
11
12namespace hugo {
13
14/// \addtogroup galgs
15/// @{
16
17  ///%Dijkstra algorithm class.
18
19  ///This class provides an efficient implementation of %Dijkstra algorithm.
20  ///The edge lengths are passed to the algorithm using a
21  ///\ref ReadMapSkeleton "readable map",
22  ///so it is easy to change it to any kind of length.
23  ///
24  ///The type of the length is determined by the \c ValueType of the length map.
25  ///
26  ///It is also possible to change the underlying priority heap.
27  ///
28  ///\param Graph The graph type the algorithm runs on.
29  ///\param LengthMap This read-only
30  ///EdgeMap
31  ///determines the
32  ///lengths of the edges. It is read once for each edge, so the map
33  ///may involve in relatively time consuming process to compute the edge
34  ///length if it is necessary. The default map type is
35  ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
36  ///\param Heap The heap type used by the %Dijkstra
37  ///algorithm. The default
38  ///is using \ref BinHeap "binary heap".
39 
40#ifdef DOXYGEN
41  template <typename Graph,
42            typename LengthMap,
43            typename Heap>
44#else
45  template <typename Graph,
46            typename LengthMap=typename Graph::EdgeMap<int>,
47            template <class,class,class> class Heap = BinHeap >
48#endif
49  class Dijkstra{
50  public:
51    typedef typename Graph::Node Node;
52    typedef typename Graph::NodeIt NodeIt;
53    typedef typename Graph::Edge Edge;
54    typedef typename Graph::OutEdgeIt OutEdgeIt;
55   
56    typedef typename LengthMap::ValueType ValueType;
57    typedef typename Graph::NodeMap<Edge> PredMap;
58    typedef typename Graph::NodeMap<Node> PredNodeMap;
59    typedef typename Graph::NodeMap<ValueType> DistMap;
60
61  private:
62    const Graph& G;
63    const LengthMap& length;
64    PredMap predecessor;
65    PredNodeMap pred_node;
66    DistMap distance;
67   
68  public :
69   
70    Dijkstra(Graph& _G, LengthMap& _length) :
71      G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
72   
73    void run(Node s);
74   
75    ///The distance of a node from the root.
76
77    ///Returns the distance of a node from the root.
78    ///\pre \ref run() must be called before using this function.
79    ///\warning If node \c v in unreachable from the root the return value
80    ///of this funcion is undefined.
81    ValueType dist(Node v) const { return distance[v]; }
82
83    ///Returns the previous edge of the shortest path tree.
84
85    ///For a node \c v it returns the previous edge of the shortest path tree,
86    ///i.e. it returns the last edge from a shortest path from the root to \c
87    ///v. It is INVALID if \c v is unreachable from the root or if \c v=s. The
88    ///shortest path tree used here is equal to the shortest path tree used in
89    ///\ref predNode(Node v).  \pre \ref run() must be called before using
90    ///this function.
91    Edge pred(Node v) const { return predecessor[v]; }
92
93    ///Returns the previous node of the shortest path tree.
94
95    ///For a node \c v it returns the previous node of the shortest path tree,
96    ///i.e. it returns the last but one node from a shortest path from the
97    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
98    ///\c v=s. The shortest path tree used here is equal to the shortest path
99    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
100    ///using this function.
101    Node predNode(Node v) const { return pred_node[v]; }
102   
103    ///Returns a reference to the NodeMap of distances.
104
105    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
106    ///be called before using this function.
107    const DistMap &distMap() const { return distance;}
108 
109    ///Returns a reference to the shortest path tree map.
110
111    ///Returns a reference to the NodeMap of the edges of the
112    ///shortest path tree.
113    ///\pre \ref run() must be called before using this function.
114    const PredMap &predMap() const { return predecessor;}
115 
116    ///Returns a reference to the map of nodes of shortest paths.
117
118    ///Returns a reference to the NodeMap of the last but one nodes of the
119    ///shortest path tree.
120    ///\pre \ref run() must be called before using this function.
121    const PredNodeMap &predNodeMap() const { return pred_node;}
122
123    ///Checks if a node is reachable from the root.
124
125    ///Returns \c true if \c v is reachable from the root.
126    ///\warning the root node is reported to be unreached!
127    ///\todo Is this what we want?
128    ///\pre \ref run() must be called before using this function.
129    ///
130    bool reached(Node v) { return G.valid(predecessor[v]); }
131   
132  };
133 
134
135  // **********************************************************************
136  //  IMPLEMENTATIONS
137  // **********************************************************************
138
139  ///Runs %Dijkstra algorithm from node the root.
140
141  ///This method runs the %Dijkstra algorithm from a root node \c s
142  ///in order to
143  ///compute the
144  ///shortest path to each node. The algorithm computes
145  ///- The shortest path tree.
146  ///- The distance of each node from the root.
147  template <typename Graph, typename LengthMap,
148            template<class,class,class> class Heap >
149  void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
150   
151    NodeIt u;
152    for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
153      predecessor.set(u,INVALID);
154      pred_node.set(u,INVALID);
155    }
156   
157    typename Graph::NodeMap<int> heap_map(G,-1);
158   
159    Heap<Node,ValueType,typename Graph::NodeMap<int> > heap(heap_map);
160   
161    heap.push(s,0);
162   
163      while ( !heap.empty() ) {
164       
165        Node v=heap.top();
166        ValueType oldvalue=heap[v];
167        heap.pop();
168        distance.set(v, oldvalue);
169       
170        { //FIXME this bracket is for e to be local
171          OutEdgeIt e;
172        for(G.first(e, v);
173            G.valid(e); G.next(e)) {
174          Node w=G.bNode(e);
175         
176          switch(heap.state(w)) {
177          case heap.PRE_HEAP:
178            heap.push(w,oldvalue+length[e]);
179            predecessor.set(w,e);
180            pred_node.set(w,v);
181            break;
182          case heap.IN_HEAP:
183            if ( oldvalue+length[e] < heap[w] ) {
184              heap.decrease(w, oldvalue+length[e]);
185              predecessor.set(w,e);
186              pred_node.set(w,v);
187            }
188            break;
189          case heap.POST_HEAP:
190            break;
191          }
192        }
193      } //FIXME tis bracket
194      }
195  }
196
197/// @}
198 
199} //END OF NAMESPACE HUGO
200
201#endif
202
203
Note: See TracBrowser for help on using the repository browser.