jacint@33: /* jacint@33: *dijkstra jacint@33: *by jacint jacint@33: *Performs Dijkstra's algorithm from node s. jacint@33: * jacint@33: *Constructor: jacint@33: * jacint@33: *dijkstra(graph_type& G, node_iterator s, edge_property_vector& distance) jacint@33: * jacint@33: * jacint@33: * jacint@33: *Member functions: jacint@33: * jacint@33: *void run() jacint@33: * jacint@33: * The following function should be used after run() was already run. jacint@33: * jacint@33: * jacint@33: *T dist(node_iterator v) : returns the distance from s to v. jacint@33: * It is 0 if v is not reachable from s. jacint@33: * jacint@33: * jacint@33: *edge_iterator pred(node_iterator v) jacint@33: * Returns the last edge of a shortest s-v path. jacint@33: * Returns an invalid iterator if v=s or v is not jacint@33: * reachable from s. jacint@33: * jacint@33: * jacint@33: *bool reach(node_iterator v) : true if v is reachable from s jacint@33: * jacint@33: * jacint@33: * jacint@33: * jacint@33: * jacint@33: *Problems: jacint@33: * jacint@33: *Heap implementation is needed, because the priority queue of stl jacint@33: *does not have a mathod for key-decrease, so we had to use here a jacint@33: *g\'any solution. jacint@33: * jacint@33: *The implementation of infinity would be desirable, see after line 100. jacint@33: */ jacint@33: jacint@33: #ifndef DIJKSTRA_HH jacint@33: #define DIJKSTRA_HH jacint@33: jacint@33: #include jacint@33: #include jacint@33: jacint@33: #include jacint@33: #include jacint@33: jacint@33: jacint@33: namespace std { jacint@33: namespace marci { jacint@33: jacint@33: jacint@33: jacint@33: jacint@33: jacint@33: template jacint@33: class dijkstra{ jacint@33: typedef typename graph_traits::node_iterator node_iterator; jacint@33: typedef typename graph_traits::edge_iterator edge_iterator; jacint@33: typedef typename graph_traits::each_node_iterator each_node_iterator; jacint@33: typedef typename graph_traits::in_edge_iterator in_edge_iterator; jacint@33: typedef typename graph_traits::out_edge_iterator out_edge_iterator; jacint@33: jacint@33: jacint@33: graph_type& G; jacint@33: node_iterator s; jacint@33: node_property_vector predecessor; jacint@33: node_property_vector distance; jacint@33: edge_property_vector length; jacint@33: node_property_vector reached; jacint@33: jacint@33: public : jacint@33: jacint@33: /* jacint@33: The distance of all the nodes is 0. jacint@33: */ jacint@33: dijkstra(graph_type& _G, node_iterator _s, edge_property_vector& _length) : jacint@33: G(_G), s(_s), predecessor(G, 0), distance(G, 0), length(_length), reached(G, false) { } jacint@33: jacint@33: jacint@33: jacint@33: /*By Misi.*/ jacint@33: struct node_dist_comp jacint@33: { jacint@33: node_property_vector &d; jacint@33: node_dist_comp(node_property_vector &_d) : d(_d) {} jacint@33: jacint@33: bool operator()(const node_iterator& u, const node_iterator& v) const jacint@33: { return d.get(u) < d.get(v); } jacint@33: }; jacint@33: jacint@33: jacint@33: jacint@33: void run() { jacint@33: jacint@33: node_property_vector scanned(G, false); jacint@33: std::priority_queue, node_dist_comp> jacint@33: heap(( node_dist_comp(distance) )); jacint@33: jacint@33: heap.push(s); jacint@33: reached.put(s, true); jacint@33: jacint@33: while (!heap.empty()) { jacint@33: jacint@33: node_iterator v=heap.top(); jacint@33: heap.pop(); jacint@33: jacint@33: jacint@33: if (!scanned.get(v)) { jacint@33: jacint@33: for(out_edge_iterator e=G.first_out_edge(v); e.valid(); ++e) { jacint@33: node_iterator w=G.head(e); jacint@33: jacint@33: if (!scanned.get(w)) { jacint@33: if (!reached.get(w)) { jacint@33: reached.put(w,true); jacint@33: distance.put(w, distance.get(v)-length.get(e)); jacint@33: predecessor.put(w,e); jacint@33: } else if (distance.get(v)-length.get(e)>distance.get(w)) { jacint@33: distance.put(w, distance.get(v)-length.get(e)); jacint@33: predecessor.put(w,e); jacint@33: } jacint@33: jacint@33: heap.push(w); jacint@33: jacint@33: } jacint@33: jacint@33: } jacint@33: scanned.put(v,true); jacint@33: jacint@33: } // if (!scanned.get(v)) jacint@33: jacint@33: jacint@33: jacint@33: } // while (!heap.empty()) jacint@33: jacint@33: jacint@33: } //void run() jacint@33: jacint@33: jacint@33: jacint@33: jacint@33: jacint@33: /* jacint@33: *Returns the distance of the node v. jacint@33: *It is 0 for the root and for the nodes not jacint@33: *reachable form the root. jacint@33: */ jacint@33: T dist(node_iterator v) { jacint@33: return -distance.get(v); jacint@33: } jacint@33: jacint@33: jacint@33: jacint@33: /* jacint@33: * Returns the last edge of a shortest s-v path. jacint@33: * Returns an invalid iterator if v=root or v is not jacint@33: * reachable from the root. jacint@33: */ jacint@33: edge_iterator pred(node_iterator v) { jacint@33: if (v!=s) { return predecessor.get(v);} jacint@33: else {return edge_iterator();} jacint@33: } jacint@33: jacint@33: jacint@33: jacint@33: bool reach(node_iterator v) { jacint@33: return reached.get(v); jacint@33: } jacint@33: jacint@33: jacint@33: jacint@33: jacint@33: jacint@33: jacint@33: jacint@33: jacint@33: jacint@33: };// class dijkstra jacint@33: jacint@33: jacint@33: jacint@33: } // namespace marci jacint@33: } jacint@33: #endif //DIJKSTRA_HH jacint@33: jacint@33: