1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/work/jacint/dijkstra.hh Fri Jan 30 14:56:11 2004 +0000
1.3 @@ -0,0 +1,192 @@
1.4 +/*
1.5 + *dijkstra
1.6 + *by jacint
1.7 + *Performs Dijkstra's algorithm from node s.
1.8 + *
1.9 + *Constructor:
1.10 + *
1.11 + *dijkstra(graph_type& G, node_iterator s, edge_property_vector& distance)
1.12 + *
1.13 + *
1.14 + *
1.15 + *Member functions:
1.16 + *
1.17 + *void run()
1.18 + *
1.19 + * The following function should be used after run() was already run.
1.20 + *
1.21 + *
1.22 + *T dist(node_iterator v) : returns the distance from s to v.
1.23 + * It is 0 if v is not reachable from s.
1.24 + *
1.25 + *
1.26 + *edge_iterator pred(node_iterator v)
1.27 + * Returns the last edge of a shortest s-v path.
1.28 + * Returns an invalid iterator if v=s or v is not
1.29 + * reachable from s.
1.30 + *
1.31 + *
1.32 + *bool reach(node_iterator v) : true if v is reachable from s
1.33 + *
1.34 + *
1.35 + *
1.36 + *
1.37 + *
1.38 + *Problems:
1.39 + *
1.40 + *Heap implementation is needed, because the priority queue of stl
1.41 + *does not have a mathod for key-decrease, so we had to use here a
1.42 + *g\'any solution.
1.43 + *
1.44 + *The implementation of infinity would be desirable, see after line 100.
1.45 + */
1.46 +
1.47 +#ifndef DIJKSTRA_HH
1.48 +#define DIJKSTRA_HH
1.49 +
1.50 +#include <queue>
1.51 +#include <algorithm>
1.52 +
1.53 +#include <marci_graph_traits.hh>
1.54 +#include <marci_property_vector.hh>
1.55 +
1.56 +
1.57 +namespace std {
1.58 + namespace marci {
1.59 +
1.60 +
1.61 +
1.62 +
1.63 +
1.64 + template <typename graph_type, typename T>
1.65 + class dijkstra{
1.66 + typedef typename graph_traits<graph_type>::node_iterator node_iterator;
1.67 + typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
1.68 + typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
1.69 + typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
1.70 + typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
1.71 +
1.72 +
1.73 + graph_type& G;
1.74 + node_iterator s;
1.75 + node_property_vector<graph_type, edge_iterator> predecessor;
1.76 + node_property_vector<graph_type, T> distance;
1.77 + edge_property_vector<graph_type, T> length;
1.78 + node_property_vector<graph_type, bool> reached;
1.79 +
1.80 + public :
1.81 +
1.82 + /*
1.83 + The distance of all the nodes is 0.
1.84 + */
1.85 + dijkstra(graph_type& _G, node_iterator _s, edge_property_vector<graph_type, T>& _length) :
1.86 + G(_G), s(_s), predecessor(G, 0), distance(G, 0), length(_length), reached(G, false) { }
1.87 +
1.88 +
1.89 +
1.90 + /*By Misi.*/
1.91 + struct node_dist_comp
1.92 + {
1.93 + node_property_vector<graph_type, T> &d;
1.94 + node_dist_comp(node_property_vector<graph_type, T> &_d) : d(_d) {}
1.95 +
1.96 + bool operator()(const node_iterator& u, const node_iterator& v) const
1.97 + { return d.get(u) < d.get(v); }
1.98 + };
1.99 +
1.100 +
1.101 +
1.102 + void run() {
1.103 +
1.104 + node_property_vector<graph_type, bool> scanned(G, false);
1.105 + std::priority_queue<node_iterator, vector<node_iterator>, node_dist_comp>
1.106 + heap(( node_dist_comp(distance) ));
1.107 +
1.108 + heap.push(s);
1.109 + reached.put(s, true);
1.110 +
1.111 + while (!heap.empty()) {
1.112 +
1.113 + node_iterator v=heap.top();
1.114 + heap.pop();
1.115 +
1.116 +
1.117 + if (!scanned.get(v)) {
1.118 +
1.119 + for(out_edge_iterator e=G.first_out_edge(v); e.valid(); ++e) {
1.120 + node_iterator w=G.head(e);
1.121 +
1.122 + if (!scanned.get(w)) {
1.123 + if (!reached.get(w)) {
1.124 + reached.put(w,true);
1.125 + distance.put(w, distance.get(v)-length.get(e));
1.126 + predecessor.put(w,e);
1.127 + } else if (distance.get(v)-length.get(e)>distance.get(w)) {
1.128 + distance.put(w, distance.get(v)-length.get(e));
1.129 + predecessor.put(w,e);
1.130 + }
1.131 +
1.132 + heap.push(w);
1.133 +
1.134 + }
1.135 +
1.136 + }
1.137 + scanned.put(v,true);
1.138 +
1.139 + } // if (!scanned.get(v))
1.140 +
1.141 +
1.142 +
1.143 + } // while (!heap.empty())
1.144 +
1.145 +
1.146 + } //void run()
1.147 +
1.148 +
1.149 +
1.150 +
1.151 +
1.152 + /*
1.153 + *Returns the distance of the node v.
1.154 + *It is 0 for the root and for the nodes not
1.155 + *reachable form the root.
1.156 + */
1.157 + T dist(node_iterator v) {
1.158 + return -distance.get(v);
1.159 + }
1.160 +
1.161 +
1.162 +
1.163 + /*
1.164 + * Returns the last edge of a shortest s-v path.
1.165 + * Returns an invalid iterator if v=root or v is not
1.166 + * reachable from the root.
1.167 + */
1.168 + edge_iterator pred(node_iterator v) {
1.169 + if (v!=s) { return predecessor.get(v);}
1.170 + else {return edge_iterator();}
1.171 + }
1.172 +
1.173 +
1.174 +
1.175 + bool reach(node_iterator v) {
1.176 + return reached.get(v);
1.177 + }
1.178 +
1.179 +
1.180 +
1.181 +
1.182 +
1.183 +
1.184 +
1.185 +
1.186 +
1.187 + };// class dijkstra
1.188 +
1.189 +
1.190 +
1.191 + } // namespace marci
1.192 +}
1.193 +#endif //DIJKSTRA_HH
1.194 +
1.195 +