jacint@50: /* jacint@50: reverse_bfs jacint@50: by jacint jacint@78: 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@78: reverse_bfs(graph_type& G, NodeIt 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@78: int dist(NodeIt 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@78: #include jacint@50: jacint@50: jacint@50: jacint@50: namespace marci { jacint@50: jacint@50: template jacint@50: class reverse_bfs { jacint@78: typedef typename graph_traits::NodeIt NodeIt; jacint@78: //typedef typename graph_traits::EdgeIt EdgeIt; jacint@78: typedef typename graph_traits::EachNodeIt EachNodeIt; jacint@78: typedef typename graph_traits::InEdgeIt InEdgeIt; jacint@50: jacint@50: jacint@50: graph_type& G; jacint@78: NodeIt t; jacint@78: // NodeMap pred; jacint@78: NodeMap distance; jacint@50: jacint@50: jacint@50: public : jacint@50: jacint@50: /* jacint@78: The distance of the Nodes is n, except t for which it is 0. jacint@50: */ jacint@78: reverse_bfs(graph_type& _G, NodeIt _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@78: NodeMap reached(G, false); jacint@50: reached.put(t, true); jacint@50: jacint@78: std::queue bfs_queue; jacint@50: bfs_queue.push(t); jacint@50: jacint@50: while (!bfs_queue.empty()) { jacint@50: jacint@78: NodeIt v=bfs_queue.front(); jacint@50: bfs_queue.pop(); jacint@50: jacint@78: for(InEdgeIt e=G.template first(v); e.valid(); ++e) { jacint@78: NodeIt 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@78: int dist(NodeIt 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: