1.1 --- a/src/work/jacint/dijkstra.hh Mon Mar 01 16:32:50 2004 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,192 +0,0 @@
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, NodeIt s, EdgeMap& 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(NodeIt 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 - *EdgeIt pred(NodeIt 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(NodeIt 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 <marciMap.hh>
1.55 -
1.56 -
1.57 -namespace std {
1.58 - namespace hugo {
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>::NodeIt NodeIt;
1.67 - typedef typename graph_traits<graph_type>::EdgeIt EdgeIt;
1.68 - typedef typename graph_traits<graph_type>::EachNodeIt EachNodeIt;
1.69 - typedef typename graph_traits<graph_type>::InEdgeIt InEdgeIt;
1.70 - typedef typename graph_traits<graph_type>::OutEdgeIt OutEdgeIt;
1.71 -
1.72 -
1.73 - graph_type& G;
1.74 - NodeIt s;
1.75 - NodeMap<graph_type, EdgeIt> predecessor;
1.76 - NodeMap<graph_type, T> distance;
1.77 - EdgeMap<graph_type, T> length;
1.78 - NodeMap<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, NodeIt _s, EdgeMap<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 - NodeMap<graph_type, T> &d;
1.94 - Node_dist_comp(NodeMap<graph_type, T> &_d) : d(_d) {}
1.95 -
1.96 - bool operator()(const NodeIt& u, const NodeIt& 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 - NodeMap<graph_type, bool> scanned(G, false);
1.105 - std::priority_queue<NodeIt, vector<NodeIt>, 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 - NodeIt v=heap.top();
1.114 - heap.pop();
1.115 -
1.116 -
1.117 - if (!scanned.get(v)) {
1.118 -
1.119 - for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
1.120 - NodeIt 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(NodeIt 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 - EdgeIt pred(NodeIt v) {
1.169 - if (v!=s) { return predecessor.get(v);}
1.170 - else {return EdgeIt();}
1.171 - }
1.172 -
1.173 -
1.174 -
1.175 - bool reach(NodeIt 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 hugo
1.192 -}
1.193 -#endif //DIJKSTRA_HH
1.194 -
1.195 -