Ut struktura. Elso valtozat.
3 *template <Graph, T, Heap=FibHeap, LengthMap=Graph::EdgeMap<T> >
7 *Dijkstra(Graph G, LengthMap length)
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.
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.
21 *bool reached(Node v) : After run(s) was run, it is true iff v is
26 #ifndef HUGO_DIJKSTRA_H
27 #define HUGO_DIJKSTRA_H
34 //Alpar: Changed the order of the parameters
36 ///Dijkstra algorithm class.
38 ///This class provides an efficient implementation of Dijkstra algorithm.
39 ///The edge lengths are passed to the algorithm using a
40 ///\ref ReadMapSkeleton "readable map",
41 ///so it is easy to change it to any kind of length.
43 ///The type of the length is determined by the \c ValueType of the length map.
45 ///It is also posible to change the underlying priority heap.
47 ///\param Graph The graph type the algorithm runs on.
48 ///\param LengthMap This read-only EdgeMap determines the
49 ///lengths of the edges. It is read once for each edge, so the map
50 ///may involve in relatively time consuming process to compute the edge
51 ///length if it is necessary.
52 ///\param Heap The heap type used by the Dijkstra
53 ///algorithm. The default
54 ///is using \ref BinHeap "binary heap".
55 template <typename Graph,
56 typename LengthMap=typename Graph::EdgeMap<int>,
57 typename Heap=BinHeap<typename Graph::Node,
58 typename LengthMap::ValueType,
59 typename Graph::NodeMap<int> > >
62 typedef typename LengthMap::ValueType ValueType;
63 typedef typename Graph::NodeMap<Edge> PredMap;
64 typedef typename Graph::NodeMap<Node> PredNodeMap;
65 typedef typename Graph::NodeMap<ValueType> DistMap;
68 typedef typename Graph::Node Node;
69 typedef typename Graph::NodeIt NodeIt;
70 typedef typename Graph::Edge Edge;
71 typedef typename Graph::OutEdgeIt OutEdgeIt;
74 const LengthMap& length;
77 PredNodeMap pred_node;
81 // typename Graph::NodeMap<bool> reach;
82 // //typename Graph::NodeMap<int> reach;
87 The distance of the nodes is 0.
89 Dijkstra(Graph& _G, LengthMap& _length) :
90 G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
95 ///The distance of a node from the source.
97 ///Returns the distance of a node from the source.
98 ///\pre \ref run() must be called before using this function.
99 ///\warning If node \c v in unreachable from \c s the return value
100 ///of this funcion is undefined.
101 ValueType dist(Node v) const { return distance[v]; }
102 ///Returns the edges of the shortest path tree.
104 ///For a node \c v it returns the last edge of the shortest path
105 ///from \c s to \c v or INVALID if \c v is unreachable from \c s.
106 ///\pre \ref run() must be called before using this function.
107 Edge pred(Node v) const { return predecessor[v]; }
108 ///Returns the nodes of the shortest paths.
110 ///For a node \c v it returns the last but one node of the shortest path
111 ///from \c s to \c v or INVALID if \c v is unreachable from \c s.
112 ///\pre \ref run() must be called before using this function.
113 Node predNode(Node v) const { return pred_node[v]; }
115 ///Returns a reference to the NodeMap of distances.
117 ///\pre \ref run() must be called before using this function.
119 const DistMap &distMap() const { return distance;}
120 ///Returns a reference to the shortest path tree map.
122 ///Returns a reference to the NodeMap of the edges of the
123 ///shortest path tree.
124 ///\pre \ref run() must be called before using this function.
125 const PredMap &predMap() const { return predecessor;}
126 ///Returns a reference to the map of nodes of shortest paths.
128 ///Returns a reference to the NodeMap of the last but one nodes of the
130 ///\pre \ref run() must be called before using this function.
131 const PredNodeMap &predNodeMap() const { return pred_node;}
133 // bool reached(Node v) { return reach[v]; }
135 ///Chech if a node is reachable from \c s.
137 ///Returns \c true if \c v is reachable from \c s.
138 ///\warning \c s is reported to be unreached!
139 ///\todo Is this what we want?
140 ///\pre \ref run() must be called before using this function.
142 bool reached(Node v) { return G.valid(predecessor[v]); }
147 // **********************************************************************
149 // **********************************************************************
151 ///Runs Dijkstra algorithm from node \c s.
153 ///This method runs the Dijkstra algorithm from node \c s in order to
155 ///shortest path to each node. The algorithm computes
156 ///- The shortest path tree.
157 ///- The distance of each node.
158 template <typename Graph, typename LengthMap, typename Heap >
159 void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
162 for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
163 predecessor.set(u,INVALID);
164 pred_node.set(u,INVALID);
165 // If a node is unreacheable, then why should be the dist=0?
166 // distance.set(u,0);
167 // reach.set(u,false);
170 //We don't need it at all.
172 // typename Graph::NodeMap<bool> scanned(G,false);
173 // //typename Graph::NodeMap<int> scanned(G,false);
174 typename Graph::NodeMap<int> heap_map(G,-1);
179 // reach.set(s, true);
181 while ( !heap.empty() ) {
184 ValueType oldvalue=heap[v];
186 distance.set(v, oldvalue);
188 for(OutEdgeIt e(G,v); G.valid(e); G.next(e)) {
191 switch(heap.state(w)) {
193 // reach.set(w,true);
194 heap.push(w,oldvalue+length[e]);
195 predecessor.set(w,e);
199 if ( oldvalue+length[e] < heap[w] ) {
200 heap.decrease(w, oldvalue+length[e]);
201 predecessor.set(w,e);
205 case Heap::POST_HEAP:
212 } //END OF NAMESPACE HUGO