src/work/athos/reverse_bfs.hh
author jacint
Sun, 22 Feb 2004 12:17:16 +0000
changeset 113 cf7b01232d86
parent 77 69b2d279c8f0
child 120 576f55fec89e
permissions -rw-r--r--
*** empty log message ***
athos@77
     1
/*
athos@77
     2
reverse_bfs
athos@77
     3
by jacint
athos@77
     4
Performs a bfs on the out edges. It does not count predecessors, 
athos@77
     5
only the distances, but one can easily modify it to know the pred as well.
athos@77
     6
athos@77
     7
Constructor: 
athos@77
     8
athos@77
     9
reverse_bfs(graph_type& G, node_iterator t)
athos@77
    10
athos@77
    11
athos@77
    12
athos@77
    13
Member functions:
athos@77
    14
athos@77
    15
void run(): runs a reverse bfs from t
athos@77
    16
athos@77
    17
  The following function should be used after run() was already run.
athos@77
    18
athos@77
    19
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
    20
athos@77
    21
*/
athos@77
    22
#ifndef REVERSE_BFS_HH
athos@77
    23
#define REVERSE_BFS_HH
athos@77
    24
athos@77
    25
#include <queue>
athos@77
    26
athos@77
    27
//#include <marci_list_graph.hh>
athos@77
    28
//#include <marci_property_vector.hh>
athos@77
    29
athos@77
    30
athos@77
    31
athos@77
    32
namespace  marci {
athos@77
    33
athos@77
    34
  template <typename graph_type>
athos@77
    35
  class reverse_bfs {
athos@77
    36
athos@77
    37
    typedef typename graph_type::NodeIt NodeIt;
athos@77
    38
    typedef typename graph_type::EdgeIt EdgeIt;
athos@77
    39
    typedef typename graph_type::EachNodeIt EachNodeIt;
athos@77
    40
    typedef typename graph_type::EachEdgeIt EachEdgeIt;
athos@77
    41
    typedef typename graph_type::OutEdgeIt OutEdgeIt;
athos@77
    42
    typedef typename graph_type::InEdgeIt InEdgeIt;
athos@77
    43
    typedef typename graph_type::SymEdgeIt SymEdgeIt;
athos@77
    44
athos@77
    45
athos@77
    46
athos@77
    47
    graph_type& G;
athos@77
    48
    NodeIt t;
athos@77
    49
//    node_property_vector<graph_type, edge_iterator> pred;
athos@77
    50
    //node_property_vector<graph_type, int>
athos@77
    51
    typename graph_type::NodeMap<int> distance;
athos@77
    52
    
athos@77
    53
athos@77
    54
  public :
athos@77
    55
athos@77
    56
    /*
athos@77
    57
      The distance of the nodes is n, except t for which it is 0.
athos@77
    58
    */
athos@77
    59
    reverse_bfs(graph_type& _G, NodeIt _t) : 
athos@77
    60
      G(_G), t(_t), 
athos@77
    61
      distance(G, G.nodeNum()) {
athos@77
    62
      distance.set(t,0);
athos@77
    63
    }
athos@77
    64
    
athos@77
    65
    void run() {
athos@77
    66
athos@77
    67
      //node_property_vector<graph_type, bool> 
athos@77
    68
      typename graph_type::NodeMap<bool> reached(G, false); 
athos@77
    69
      reached.set(t, true);
athos@77
    70
athos@77
    71
      std::queue<NodeIt> bfs_queue;
athos@77
    72
      bfs_queue.push(t);
athos@77
    73
athos@77
    74
      while (!bfs_queue.empty()) {
athos@77
    75
athos@77
    76
        NodeIt v=bfs_queue.front();	
athos@77
    77
	bfs_queue.pop();
athos@77
    78
athos@77
    79
	for(InEdgeIt e=G.template first<InEdgeIt>(v); e.valid(); ++e) {
athos@77
    80
	  NodeIt w=G.tail(e);
athos@77
    81
	  if (!reached.get(w)) {
athos@77
    82
	    bfs_queue.push(w);
athos@77
    83
	    distance.set(w, distance.get(v)+1);
athos@77
    84
	    reached.set(w, true);
athos@77
    85
	  }
athos@77
    86
	}
athos@77
    87
      }
athos@77
    88
    }
athos@77
    89
athos@77
    90
athos@77
    91
athos@77
    92
    int dist(NodeIt v) {
athos@77
    93
      return distance.get(v);
athos@77
    94
    }
athos@77
    95
athos@77
    96
athos@77
    97
  };
athos@77
    98
alpar@105
    99
} // namespace hugo
athos@77
   100
athos@77
   101
#endif //REVERSE_BFS_HH
athos@77
   102
athos@77
   103