Elk?sz?lt a boundingbox oszt?ly (boundingbox.h) ?s hozz? a tesztprogi.
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
30 #include <bin_heap.hh>
35 //Alpar: Changed the order of the parameters
37 ///%Dijkstra algorithm class.
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.
44 ///The type of the length is determined by the \c ValueType of the length map.
46 ///It is also posible to change the underlying priority heap.
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> > >
63 typedef typename Graph::Node Node;
64 typedef typename Graph::NodeIt NodeIt;
65 typedef typename Graph::Edge Edge;
66 typedef typename Graph::OutEdgeIt OutEdgeIt;
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;
75 const LengthMap& length;
78 PredNodeMap pred_node;
82 // typename Graph::NodeMap<bool> reach;
83 // //typename Graph::NodeMap<int> reach;
88 The distance of the nodes is 0.
90 Dijkstra(Graph& _G, LengthMap& _length) :
91 G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
96 ///The distance of a node from the source.
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 the source 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.
105 ///For a node \c v it returns the last edge of the shortest path
106 ///from the source to \c v or INVALID if \c v is unreachable
108 ///\pre \ref run() must be called before using this function.
109 Edge pred(Node v) const { return predecessor[v]; }
110 ///Returns the nodes of the shortest paths.
112 ///For a node \c v it returns the last but one node of the shortest path
113 ///from the source to \c v or INVALID if \c v is unreachable
115 ///\pre \ref run() must be called before using this function.
116 Node predNode(Node v) const { return pred_node[v]; }
118 ///Returns a reference to the NodeMap of distances.
120 ///\pre \ref run() must be called before using this function.
122 const DistMap &distMap() const { return distance;}
123 ///Returns a reference to the shortest path tree map.
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.
128 const PredMap &predMap() const { return predecessor;}
129 ///Returns a reference to the map of nodes of shortest paths.
131 ///Returns a reference to the NodeMap of the last but one nodes of the
133 ///\pre \ref run() must be called before using this function.
134 const PredNodeMap &predNodeMap() const { return pred_node;}
136 // bool reached(Node v) { return reach[v]; }
138 ///Chechs if a node is reachable from the source.
140 ///Returns \c true if \c v is reachable from the source.
141 ///\warning the source node is reported to be unreached!
142 ///\todo Is this what we want?
143 ///\pre \ref run() must be called before using this function.
145 bool reached(Node v) { return G.valid(predecessor[v]); }
150 // **********************************************************************
152 // **********************************************************************
154 ///Runs %Dijkstra algorithm from node the source.
156 ///This method runs the %Dijkstra algorithm from a source node \c s
159 ///shortest path to each node. The algorithm computes
160 ///- The shortest path tree.
161 ///- The distance of each node from the source.
162 template <typename Graph, typename LengthMap, typename Heap >
163 void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
166 for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
167 predecessor.set(u,INVALID);
168 pred_node.set(u,INVALID);
169 // If a node is unreacheable, then why should be the dist=0?
170 // distance.set(u,0);
171 // reach.set(u,false);
174 //We don't need it at all.
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);
183 // reach.set(s, true);
185 while ( !heap.empty() ) {
188 ValueType oldvalue=heap[v];
190 distance.set(v, oldvalue);
192 for(OutEdgeIt e(G,v); G.valid(e); G.next(e)) {
195 switch(heap.state(w)) {
197 // reach.set(w,true);
198 heap.push(w,oldvalue+length[e]);
199 predecessor.set(w,e);
203 if ( oldvalue+length[e] < heap[w] ) {
204 heap.decrease(w, oldvalue+length[e]);
205 predecessor.set(w,e);
209 case Heap::POST_HEAP:
216 } //END OF NAMESPACE HUGO