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