jacint@50: /* jacint@50: reverse_bfs jacint@50: by jacint jacint@50: Performs a bfs on the out edges. It does not count predecessors, jacint@50: only the distances, but one can easily modify it to know the pred as well. jacint@50: jacint@50: Constructor: jacint@50: jacint@50: reverse_bfs(graph_type& G, node_iterator t) jacint@50: jacint@50: jacint@50: jacint@50: Member functions: jacint@50: jacint@50: void run(): runs a reverse bfs from t jacint@50: jacint@50: The following function should be used after run() was already run. jacint@50: jacint@50: 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. jacint@50: jacint@50: */ jacint@50: #ifndef REVERSE_BFS_HH jacint@50: #define REVERSE_BFS_HH jacint@50: jacint@50: #include jacint@50: jacint@50: #include jacint@50: #include jacint@50: jacint@50: jacint@50: jacint@50: namespace marci { jacint@50: jacint@50: template jacint@50: class reverse_bfs { 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: jacint@50: jacint@50: graph_type& G; jacint@50: node_iterator t; jacint@50: // node_property_vector pred; jacint@50: node_property_vector distance; jacint@50: jacint@50: jacint@50: public : jacint@50: jacint@50: /* jacint@50: The distance of the nodes is n, except t for which it is 0. jacint@50: */ jacint@50: reverse_bfs(graph_type& _G, node_iterator _t) : G(_G), t(_t), distance(G, number_of(G.first_node())) { jacint@50: distance.put(t,0); jacint@50: } jacint@50: jacint@50: void run() { jacint@50: jacint@50: node_property_vector reached(G, false); jacint@50: reached.put(t, true); jacint@50: jacint@50: std::queue bfs_queue; jacint@50: bfs_queue.push(t); jacint@50: jacint@50: while (!bfs_queue.empty()) { jacint@50: jacint@50: node_iterator v=bfs_queue.front(); jacint@50: bfs_queue.pop(); jacint@50: jacint@50: for(in_edge_iterator e=G.first_in_edge(v); e.valid(); ++e) { jacint@50: node_iterator w=G.tail(e); jacint@50: if (!reached.get(w)) { jacint@50: bfs_queue.push(w); jacint@50: distance.put(w, distance.get(v)+1); jacint@50: reached.put(w, true); jacint@50: } jacint@50: } jacint@50: } jacint@50: } jacint@50: jacint@50: jacint@50: jacint@50: int dist(node_iterator v) { jacint@50: return distance.get(v); jacint@50: } jacint@50: jacint@50: jacint@50: }; jacint@50: jacint@50: } // namespace marci jacint@50: jacint@50: #endif //REVERSE_BFS_HH jacint@50: jacint@50: