4 *Performs Dijkstra's algorithm from node s.
8 *dijkstra(graph_type& G, node_iterator s, edge_property_vector& distance)
16 * The following function should be used after run() was already run.
19 *T dist(node_iterator v) : returns the distance from s to v.
20 * It is 0 if v is not reachable from s.
23 *edge_iterator pred(node_iterator 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(node_iterator 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 <marci_property_vector.hh>
61 template <typename graph_type, typename T>
63 typedef typename graph_traits<graph_type>::node_iterator node_iterator;
64 typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
65 typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
66 typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
67 typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
72 node_property_vector<graph_type, edge_iterator> predecessor;
73 node_property_vector<graph_type, T> distance;
74 edge_property_vector<graph_type, T> length;
75 node_property_vector<graph_type, bool> reached;
80 The distance of all the nodes is 0.
82 dijkstra(graph_type& _G, node_iterator _s, edge_property_vector<graph_type, T>& _length) :
83 G(_G), s(_s), predecessor(G, 0), distance(G, 0), length(_length), reached(G, false) { }
90 node_property_vector<graph_type, T> &d;
91 node_dist_comp(node_property_vector<graph_type, T> &_d) : d(_d) {}
93 bool operator()(const node_iterator& u, const node_iterator& v) const
94 { return d.get(u) < d.get(v); }
101 node_property_vector<graph_type, bool> scanned(G, false);
102 std::priority_queue<node_iterator, vector<node_iterator>, node_dist_comp>
103 heap(( node_dist_comp(distance) ));
106 reached.put(s, true);
108 while (!heap.empty()) {
110 node_iterator v=heap.top();
114 if (!scanned.get(v)) {
116 for(out_edge_iterator e=G.first_out_edge(v); e.valid(); ++e) {
117 node_iterator w=G.head(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.
154 T dist(node_iterator v) {
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 edge_iterator pred(node_iterator v) {
166 if (v!=s) { return predecessor.get(v);}
167 else {return edge_iterator();}
172 bool reach(node_iterator v) {
173 return reached.get(v);