COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/include/dijkstra.h @ 258:94bafec4f56f

Last change on this file since 258:94bafec4f56f was 258:94bafec4f56f, checked in by Mihaly Barasz, 20 years ago

bin_heap.hh atnevezese

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