src/work/marci_bfs.hh
author klao
Tue, 27 Jan 2004 19:16:38 +0000
changeset 37 e0e41f9e2be5
parent 11 33a84426c221
child 107 8d62f0072ff0
permissions -rw-r--r--
Generikus binaris kupac implementacio.
Alap demo file mukodesenek bemutatasahoz.
marci@9
     1
#ifndef MARCI_BFS_HH
marci@9
     2
#define MARCI_BFS_HH
marci@9
     3
marci@9
     4
#include <queue>
marci@9
     5
marci@9
     6
#include <marci_property_vector.hh>
marci@9
     7
marci@9
     8
namespace marci {
marci@9
     9
marci@9
    10
  template <typename graph_type>
marci@9
    11
  struct bfs {
marci@19
    12
    typedef typename graph_type::node_iterator node_iterator;
marci@19
    13
    typedef typename graph_type::edge_iterator edge_iterator;
marci@19
    14
    typedef typename graph_type::each_node_iterator each_node_iterator;
marci@19
    15
    typedef typename graph_type::out_edge_iterator out_edge_iterator;
marci@9
    16
    graph_type& G;
marci@9
    17
    node_iterator s;
marci@9
    18
    node_property_vector<graph_type, bool> reached;
marci@9
    19
    node_property_vector<graph_type, edge_iterator> pred;
marci@9
    20
    node_property_vector<graph_type, int> dist;
marci@9
    21
    std::queue<node_iterator> bfs_queue;
marci@9
    22
    bfs(graph_type& _G, node_iterator _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) { 
marci@9
    23
      bfs_queue.push(s); 
marci@19
    24
      for(each_node_iterator i=G.first_node(); i.valid(); ++i) 
marci@9
    25
	reached.put(i, false);
marci@9
    26
      reached.put(s, true);
marci@9
    27
      dist.put(s, 0); 
marci@9
    28
    }
marci@9
    29
    
marci@9
    30
    void run() {
marci@9
    31
      while (!bfs_queue.empty()) {
marci@9
    32
	node_iterator v=bfs_queue.front();
marci@9
    33
	out_edge_iterator e=G.first_out_edge(v);
marci@9
    34
	bfs_queue.pop();
marci@19
    35
	for( ; e.valid(); ++e) {
marci@9
    36
	  node_iterator w=G.head(e);
marci@9
    37
	  std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl;
marci@9
    38
	  if (!reached.get(w)) {
marci@9
    39
	    std::cout << G.id(w) << " is newly reached :-)" << std::endl;
marci@9
    40
	    bfs_queue.push(w);
marci@9
    41
	    dist.put(w, dist.get(v)+1);
marci@9
    42
	    pred.put(w, e);
marci@9
    43
	    reached.put(w, true);
marci@9
    44
	  } else {
marci@9
    45
	    std::cout << G.id(w) << " is already reached" << std::endl;
marci@9
    46
	  }
marci@9
    47
	}
marci@9
    48
      }
marci@9
    49
    }
marci@9
    50
  };
marci@9
    51
marci@9
    52
  template <typename graph_type> 
marci@9
    53
  struct bfs_visitor {
marci@19
    54
    typedef typename graph_type::node_iterator node_iterator;
marci@19
    55
    typedef typename graph_type::edge_iterator edge_iterator;
marci@19
    56
    typedef typename graph_type::out_edge_iterator out_edge_iterator;
marci@9
    57
    graph_type& G;
marci@9
    58
    bfs_visitor(graph_type& _G) : G(_G) { }
marci@9
    59
    void at_previously_reached(out_edge_iterator& e) { 
marci@9
    60
      //node_iterator v=G.tail(e);
marci@9
    61
      node_iterator w=G.head(e);
marci@9
    62
      std::cout << G.id(w) << " is already reached" << std::endl;
marci@9
    63
   }
marci@9
    64
    void at_newly_reached(out_edge_iterator& e) { 
marci@9
    65
      //node_iterator v=G.tail(e);
marci@9
    66
      node_iterator w=G.head(e);
marci@9
    67
      std::cout << G.id(w) << " is newly reached :-)" << std::endl;
marci@9
    68
    }
marci@9
    69
  };
marci@9
    70
marci@9
    71
  template <typename graph_type, typename reached_type, typename visitor_type>
marci@9
    72
  struct bfs_iterator {
marci@19
    73
    typedef typename graph_type::node_iterator node_iterator;
marci@19
    74
    typedef typename graph_type::edge_iterator edge_iterator;
marci@19
    75
    typedef typename graph_type::out_edge_iterator out_edge_iterator;
marci@9
    76
    graph_type& G;
marci@9
    77
    std::queue<out_edge_iterator>& bfs_queue;
marci@9
    78
    reached_type& reached;
marci@9
    79
    visitor_type& visitor;
marci@9
    80
    void process() {
marci@19
    81
      while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@9
    82
      if (bfs_queue.empty()) return;
marci@9
    83
      out_edge_iterator e=bfs_queue.front();
marci@9
    84
      //node_iterator v=G.tail(e);
marci@9
    85
      node_iterator w=G.head(e);
marci@9
    86
      if (!reached.get(w)) {
marci@9
    87
	visitor.at_newly_reached(e);
marci@9
    88
	bfs_queue.push(G.first_out_edge(w));
marci@9
    89
	reached.put(w, true);
marci@9
    90
      } else {
marci@9
    91
	visitor.at_previously_reached(e);
marci@9
    92
      }
marci@9
    93
    }
marci@9
    94
    bfs_iterator(graph_type& _G, std::queue<out_edge_iterator>& _bfs_queue, reached_type& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) { 
marci@19
    95
      //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@19
    96
      valid();
marci@9
    97
    }
marci@9
    98
    bfs_iterator<graph_type, reached_type, visitor_type>& operator++() { 
marci@19
    99
      //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@9
   100
      //if (bfs_queue.empty()) return *this;
marci@19
   101
      if (!valid()) return *this;
marci@9
   102
      ++(bfs_queue.front());
marci@19
   103
      //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@19
   104
      valid();
marci@9
   105
      return *this;
marci@9
   106
    }
marci@9
   107
    //void next() { 
marci@19
   108
    //  while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@9
   109
    //  if (bfs_queue.empty()) return;
marci@9
   110
    //  ++(bfs_queue.front());
marci@19
   111
    //  while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@9
   112
    //}
marci@19
   113
    bool valid() { 
marci@19
   114
      while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@9
   115
      if (bfs_queue.empty()) return false; else return true;
marci@9
   116
    }
marci@9
   117
    //bool finished() { 
marci@19
   118
    //  while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@9
   119
    //  if (bfs_queue.empty()) return true; else return false;
marci@9
   120
    //}
marci@9
   121
    operator edge_iterator () { return bfs_queue.front(); }
marci@9
   122
marci@9
   123
  };
marci@9
   124
marci@11
   125
  template <typename graph_type, typename reached_type>
marci@11
   126
  struct bfs_iterator1 {
marci@19
   127
    typedef typename graph_type::node_iterator node_iterator;
marci@19
   128
    typedef typename graph_type::edge_iterator edge_iterator;
marci@19
   129
    typedef typename graph_type::out_edge_iterator out_edge_iterator;
marci@11
   130
    graph_type& G;
marci@11
   131
    std::queue<out_edge_iterator>& bfs_queue;
marci@11
   132
    reached_type& reached;
marci@19
   133
    bool _newly_reached;
marci@11
   134
    bfs_iterator1(graph_type& _G, std::queue<out_edge_iterator>& _bfs_queue, reached_type& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@19
   135
      valid();
marci@19
   136
      if (!bfs_queue.empty() && bfs_queue.front().valid()) { 
marci@11
   137
	out_edge_iterator e=bfs_queue.front();
marci@11
   138
	node_iterator w=G.head(e);
marci@11
   139
	if (!reached.get(w)) {
marci@11
   140
	  bfs_queue.push(G.first_out_edge(w));
marci@11
   141
	  reached.put(w, true);
marci@19
   142
	  _newly_reached=true;
marci@11
   143
	} else {
marci@19
   144
	  _newly_reached=false;
marci@11
   145
	}
marci@11
   146
      }
marci@11
   147
    }
marci@11
   148
    bfs_iterator1<graph_type, reached_type>& operator++() { 
marci@19
   149
      if (!valid()) return *this;
marci@11
   150
      ++(bfs_queue.front());
marci@19
   151
      valid();
marci@19
   152
      if (!bfs_queue.empty() && bfs_queue.front().valid()) { 
marci@11
   153
	out_edge_iterator e=bfs_queue.front();
marci@11
   154
	node_iterator w=G.head(e);
marci@11
   155
	if (!reached.get(w)) {
marci@11
   156
	  bfs_queue.push(G.first_out_edge(w));
marci@11
   157
	  reached.put(w, true);
marci@19
   158
	  _newly_reached=true;
marci@11
   159
	} else {
marci@19
   160
	  _newly_reached=false;
marci@11
   161
	}
marci@11
   162
      }
marci@11
   163
      return *this;
marci@11
   164
    }
marci@19
   165
    bool valid() { 
marci@19
   166
      while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@11
   167
      if (bfs_queue.empty()) return false; else return true;
marci@11
   168
    }
marci@11
   169
    operator edge_iterator () { return bfs_queue.front(); }
marci@19
   170
    bool newly_reached() { return _newly_reached; }
marci@11
   171
marci@11
   172
  };
marci@9
   173
marci@9
   174
} // namespace marci
marci@9
   175
marci@9
   176
#endif //MARCI_BFS_HH