src/work/reverse_bfs.hh
author klao
Tue, 27 Jan 2004 19:16:38 +0000
changeset 37 e0e41f9e2be5
parent 22 0286c68fc680
permissions -rw-r--r--
Generikus binaris kupac implementacio.
Alap demo file mukodesenek bemutatasahoz.
jacint@22
     1
/*
jacint@22
     2
reverse_bfs
jacint@22
     3
by jacint
jacint@22
     4
Performs a bfs on the out edges. It does not count predecessors, 
jacint@22
     5
only the distances, but one can easily modify it to know the pred as well.
jacint@22
     6
jacint@22
     7
Constructor: 
jacint@22
     8
jacint@22
     9
reverse_bfs(graph_type& G, node_iterator t)
jacint@22
    10
jacint@22
    11
jacint@22
    12
jacint@22
    13
Member functions:
jacint@22
    14
jacint@22
    15
void run(): runs a reverse bfs from t
jacint@22
    16
jacint@22
    17
  The following function should be used after run() was already run.
jacint@22
    18
jacint@22
    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.
jacint@22
    20
jacint@22
    21
*/
jacint@22
    22
#ifndef REVERSE_BFS_HH
jacint@22
    23
#define REVERSE_BFS_HH
jacint@22
    24
jacint@22
    25
#include <queue>
jacint@22
    26
jacint@22
    27
#include <marci_graph_traits.hh>
jacint@22
    28
#include <marci_property_vector.hh>
jacint@22
    29
jacint@22
    30
jacint@22
    31
jacint@22
    32
namespace  marci {
jacint@22
    33
jacint@22
    34
  template <typename graph_type>
jacint@22
    35
  class reverse_bfs {
jacint@22
    36
    typedef typename graph_traits<graph_type>::node_iterator node_iterator;
jacint@22
    37
    //typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
jacint@22
    38
    typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
jacint@22
    39
    typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
jacint@22
    40
jacint@22
    41
jacint@22
    42
    graph_type& G;
jacint@22
    43
    node_iterator t;
jacint@22
    44
//    node_property_vector<graph_type, edge_iterator> pred;
jacint@22
    45
    node_property_vector<graph_type, int> distance;
jacint@22
    46
    
jacint@22
    47
jacint@22
    48
  public :
jacint@22
    49
jacint@22
    50
    /*
jacint@22
    51
      The distance of the nodes is n, except t for which it is 0.
jacint@22
    52
    */
jacint@22
    53
    reverse_bfs(graph_type& _G, node_iterator _t) : G(_G), t(_t), distance(G, number_of(G.first_node())) {
jacint@22
    54
      distance.put(t,0);
jacint@22
    55
    }
jacint@22
    56
    
jacint@22
    57
    void run() {
jacint@22
    58
jacint@22
    59
      node_property_vector<graph_type, bool> reached(G, false); 
jacint@22
    60
      reached.put(t, true);
jacint@22
    61
jacint@22
    62
      std::queue<node_iterator> bfs_queue;
jacint@22
    63
      bfs_queue.push(t);
jacint@22
    64
jacint@22
    65
      while (!bfs_queue.empty()) {
jacint@22
    66
jacint@22
    67
        node_iterator v=bfs_queue.front();	
jacint@22
    68
	bfs_queue.pop();
jacint@22
    69
jacint@30
    70
	for(in_edge_iterator e=G.first_in_edge(v); e.valid(); ++e) {
jacint@22
    71
	  node_iterator w=G.tail(e);
jacint@22
    72
	  if (!reached.get(w)) {
jacint@22
    73
	    bfs_queue.push(w);
jacint@22
    74
	    distance.put(w, distance.get(v)+1);
jacint@22
    75
	    reached.put(w, true);
jacint@22
    76
	  }
jacint@22
    77
	}
jacint@22
    78
      }
jacint@22
    79
    }
jacint@22
    80
jacint@22
    81
jacint@22
    82
jacint@22
    83
    int dist(node_iterator v) {
jacint@22
    84
      return distance.get(v);
jacint@22
    85
    }
jacint@22
    86
jacint@22
    87
jacint@22
    88
  };
jacint@22
    89
jacint@22
    90
} // namespace marci
jacint@22
    91
jacint@22
    92
#endif //REVERSE_BFS_HH
jacint@22
    93
jacint@22
    94