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