.
4 *Performs Dijkstra's algorithm from Node s.
8 *dijkstra(graph_type& G, NodeIt s, EdgeMap& distance)
16 * The following function should be used after run() was already run.
19 *T dist(NodeIt v) : returns the distance from s to v.
20 * It is 0 if v is not reachable from s.
23 *EdgeIt pred(NodeIt v)
24 * Returns the last Edge of a shortest s-v path.
25 * Returns an invalid iterator if v=s or v is not
29 *bool reach(NodeIt v) : true if v is reachable from s
37 *Heap implementation is needed, because the priority queue of stl
38 *does not have a mathod for key-decrease, so we had to use here a
41 *The implementation of infinity would be desirable, see after line 100.
50 #include <marci_graph_traits.hh>
51 #include <marciMap.hh>
61 template <typename graph_type, typename T>
63 typedef typename graph_traits<graph_type>::NodeIt NodeIt;
64 typedef typename graph_traits<graph_type>::EdgeIt EdgeIt;
65 typedef typename graph_traits<graph_type>::EachNodeIt EachNodeIt;
66 typedef typename graph_traits<graph_type>::InEdgeIt InEdgeIt;
67 typedef typename graph_traits<graph_type>::OutEdgeIt OutEdgeIt;
72 NodeMap<graph_type, EdgeIt> predecessor;
73 NodeMap<graph_type, T> distance;
74 EdgeMap<graph_type, T> length;
75 NodeMap<graph_type, bool> reached;
80 The distance of all the Nodes is 0.
82 dijkstra(graph_type& _G, NodeIt _s, EdgeMap<graph_type, T>& _length) :
83 G(_G), s(_s), predecessor(G, 0), distance(G, 0), length(_length), reached(G, false) { }
90 NodeMap<graph_type, T> &d;
91 Node_dist_comp(NodeMap<graph_type, T> &_d) : d(_d) {}
93 bool operator()(const NodeIt& u, const NodeIt& v) const
94 { return d.get(u) < d.get(v); }
101 NodeMap<graph_type, bool> scanned(G, false);
102 std::priority_queue<NodeIt, vector<NodeIt>, Node_dist_comp>
103 heap(( Node_dist_comp(distance) ));
106 reached.put(s, true);
108 while (!heap.empty()) {
114 if (!scanned.get(v)) {
116 for(OutEdgeIt e=G.template first<OutEdgeIt>(v); e.valid(); ++e) {
119 if (!scanned.get(w)) {
120 if (!reached.get(w)) {
122 distance.put(w, distance.get(v)-length.get(e));
123 predecessor.put(w,e);
124 } else if (distance.get(v)-length.get(e)>distance.get(w)) {
125 distance.put(w, distance.get(v)-length.get(e));
126 predecessor.put(w,e);
136 } // if (!scanned.get(v))
140 } // while (!heap.empty())
150 *Returns the distance of the Node v.
151 *It is 0 for the root and for the Nodes not
152 *reachable form the root.
155 return -distance.get(v);
161 * Returns the last Edge of a shortest s-v path.
162 * Returns an invalid iterator if v=root or v is not
163 * reachable from the root.
165 EdgeIt pred(NodeIt v) {
166 if (v!=s) { return predecessor.get(v);}
167 else {return EdgeIt();}
172 bool reach(NodeIt v) {
173 return reached.get(v);