diff -r 181b37336b29 -r 0286c68fc680 src/work/reverse_bfs.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/reverse_bfs.hh Tue Jan 20 21:27:28 2004 +0000 @@ -0,0 +1,94 @@ +/* +reverse_bfs +by jacint +Performs a bfs on the out edges. It does not count predecessors, +only the distances, but one can easily modify it to know the pred as well. + +Constructor: + +reverse_bfs(graph_type& G, node_iterator t) + + + +Member functions: + +void run(): runs a reverse bfs from t + + The following function should be used after run() was already run. + +int dist(node_iterator v) : returns the distance from v to t. It is the number of nodes if t is not reachable from v. + +*/ +#ifndef REVERSE_BFS_HH +#define REVERSE_BFS_HH + +#include + +#include +#include + + + +namespace marci { + + template + class reverse_bfs { + typedef typename graph_traits::node_iterator node_iterator; + //typedef typename graph_traits::edge_iterator edge_iterator; + typedef typename graph_traits::each_node_iterator each_node_iterator; + typedef typename graph_traits::in_edge_iterator in_edge_iterator; + + + graph_type& G; + node_iterator t; +// node_property_vector pred; + node_property_vector distance; + + + public : + + /* + The distance of the nodes is n, except t for which it is 0. + */ + reverse_bfs(graph_type& _G, node_iterator _t) : G(_G), t(_t), distance(G, number_of(G.first_node())) { + distance.put(t,0); + } + + void run() { + + node_property_vector reached(G, false); + reached.put(t, true); + + std::queue bfs_queue; + bfs_queue.push(t); + + while (!bfs_queue.empty()) { + + node_iterator v=bfs_queue.front(); + bfs_queue.pop(); + + for(in_edge_iterator e=G.first_in_edge(v); e.is_valid(); ++e) { + node_iterator w=G.tail(e); + if (!reached.get(w)) { + bfs_queue.push(w); + distance.put(w, distance.get(v)+1); + reached.put(w, true); + } + } + } + } + + + + int dist(node_iterator v) { + return distance.get(v); + } + + + }; + +} // namespace marci + +#endif //REVERSE_BFS_HH + +