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