COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/alpar/dijkstra/dijkstra.h @ 229:ae5f9ca94be7

Last change on this file since 229:ae5f9ca94be7 was 229:ae5f9ca94be7, checked in by Alpar Juttner, 21 years ago

DocFix?

File size: 6.6 KB
RevLine 
[222]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>
[228]30#include <bin_heap.hh>
[222]31#include <invalid.h>
32
33namespace hugo {
34 
35  //Alpar: Changed the order of the parameters
[224]36 
[229]37  ///%Dijkstra algorithm class.
[224]38
[229]39  ///This class provides an efficient implementation of %Dijkstra algorithm.
[224]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.
[229]53  ///\param Heap The heap type used by the %Dijkstra
[224]54  ///algorithm. The default
55  ///is using \ref BinHeap "binary heap".
[222]56  template <typename Graph,
57            typename LengthMap=typename Graph::EdgeMap<int>,
[228]58            typename Heap=BinHeap <typename Graph::Node,
59                                   typename LengthMap::ValueType,
60                                   typename Graph::NodeMap<int> > >
[222]61  class Dijkstra{
62  public:
[228]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   
[222]68    typedef typename LengthMap::ValueType ValueType;
[224]69    typedef typename Graph::NodeMap<Edge> PredMap;
70    typedef typename Graph::NodeMap<Node> PredNodeMap;
71    typedef typename Graph::NodeMap<ValueType> DistMap;
[222]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   
[224]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.
[229]100    ///\warning If node \c v in unreachable from the source the return value
[224]101    ///of this funcion is undefined.
[222]102    ValueType dist(Node v) const { return distance[v]; }
[224]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
[229]106    ///from the source to \c v or INVALID if \c v is unreachable
107    ///from the source.
[224]108    ///\pre \ref run() must be called before using this function.
[222]109    Edge pred(Node v) const { return predecessor[v]; }
[224]110    ///Returns the nodes of the shortest paths.
111
112    ///For a node \c v it returns the last but one node of the shortest path
[229]113    ///from the source to \c v or INVALID if \c v is unreachable
114    ///from the source.
[224]115    ///\pre \ref run() must be called before using this function.
[222]116    Node predNode(Node v) const { return pred_node[v]; }
117   
[224]118    ///Returns a reference to the NodeMap of distances.
119
120    ///\pre \ref run() must be called before using this function.
121    ///
[222]122    const DistMap &distMap() const { return distance;}
[224]123    ///Returns a reference to the shortest path tree map.
124
125    ///Returns a reference to the NodeMap of the edges of the
126    ///shortest path tree.
127    ///\pre \ref run() must be called before using this function.
[222]128    const PredMap &predMap() const { return predecessor;}
[224]129    ///Returns a reference to the map of nodes of  shortest paths.
130
131    ///Returns a reference to the NodeMap of the last but one nodes of the
132    ///shortest paths.
133    ///\pre \ref run() must be called before using this function.
[222]134    const PredNodeMap &predNodeMap() const { return pred_node;}
135
136    //    bool reached(Node v) { return reach[v]; }
[224]137
[229]138    ///Chechs if a node is reachable from the source.
[224]139
[229]140    ///Returns \c true if \c v is reachable from the source.
141    ///\warning the source node is reported to be unreached!
[224]142    ///\todo Is this what we want?
143    ///\pre \ref run() must be called before using this function.
[222]144    ///
145    bool reached(Node v) { return G.valid(predecessor[v]); }
146   
147  };
148 
149
[224]150  // **********************************************************************
151  //  IMPLEMENTATIONS
152  // **********************************************************************
[222]153
[229]154  ///Runs %Dijkstra algorithm from node the source.
[224]155
[229]156  ///This method runs the %Dijkstra algorithm from a source node \c s
157  ///in order to
[224]158  ///compute the
159  ///shortest path to each node. The algorithm computes
160  ///- The shortest path tree.
[229]161  ///- The distance of each node from the source.
[222]162  template <typename Graph, typename LengthMap, typename Heap >
163  void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
164   
165    NodeIt u;
166    for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
167      predecessor.set(u,INVALID);
[224]168      pred_node.set(u,INVALID);
[222]169      // If a node is unreacheable, then why should be the dist=0?
170      // distance.set(u,0);
171      //      reach.set(u,false);
172    }
173   
174    //We don't need it at all.
175    //     //FIXME:
176    //     typename Graph::NodeMap<bool> scanned(G,false);
177    //     //typename Graph::NodeMap<int> scanned(G,false);
178    typename Graph::NodeMap<int> heap_map(G,-1);
179   
180    Heap heap(heap_map);
181   
182    heap.push(s,0);
183    //    reach.set(s, true);
184   
185      while ( !heap.empty() ) {
186       
187        Node v=heap.top();
188        ValueType oldvalue=heap[v];
189        heap.pop();
190        distance.set(v, oldvalue);
191       
192        for(OutEdgeIt e(G,v); G.valid(e); G.next(e)) {
193          Node w=G.head(e);
194         
195          switch(heap.state(w)) {
196          case Heap::PRE_HEAP:
197            //      reach.set(w,true);
198            heap.push(w,oldvalue+length[e]);
199            predecessor.set(w,e);
200            pred_node.set(w,v);
201            break;
202          case Heap::IN_HEAP:
203            if ( oldvalue+length[e] < heap[w] ) {
204              heap.decrease(w, oldvalue+length[e]);
205              predecessor.set(w,e);
206              pred_node.set(w,v);
207            }
208            break;
209          case Heap::POST_HEAP:
210            break;
211          }
212        }
213      }
214  }
215 
216} //END OF NAMESPACE HUGO
217
218#endif
219
220
Note: See TracBrowser for help on using the repository browser.