COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/alpar/dijkstra/dijkstra.h @ 228:1d5f4cd0342c

Last change on this file since 228:1d5f4cd0342c was 228:1d5f4cd0342c, checked in by Alpar Juttner, 20 years ago

Bugfix

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