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