src/work/marci/bfs_iterator.h
author ladanyi
Thu, 06 May 2004 13:48:04 +0000
changeset 542 69bde1d90c04
parent 415 679e64913c5e
child 560 5adcef1d7bcc
permissions -rw-r--r--
Set up automake environment.
marci@301
     1
// -*- c++ -*-
marci@301
     2
#ifndef HUGO_BFS_ITERATOR_H
marci@301
     3
#define HUGO_BFS_ITERATOR_H
marci@301
     4
marci@301
     5
#include <queue>
marci@301
     6
#include <stack>
marci@301
     7
#include <utility>
marci@301
     8
marci@301
     9
namespace hugo {
marci@301
    10
marci@303
    11
  template <typename Graph, /*typename OutEdgeIt,*/ 
marci@303
    12
	    typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
marci@360
    13
  class BfsIterator {
marci@303
    14
  protected:
marci@303
    15
    typedef typename Graph::Node Node;
marci@303
    16
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@303
    17
    const Graph* graph;
marci@301
    18
    std::queue<Node> bfs_queue;
marci@301
    19
    ReachedMap& reached;
marci@301
    20
    bool b_node_newly_reached;
marci@301
    21
    OutEdgeIt actual_edge;
marci@301
    22
    bool own_reached_map;
marci@301
    23
  public:
marci@360
    24
    BfsIterator(const Graph& _graph, ReachedMap& _reached) : 
marci@303
    25
      graph(&_graph), reached(_reached), 
marci@301
    26
      own_reached_map(false) { }
marci@360
    27
    BfsIterator(const Graph& _graph) : 
marci@303
    28
      graph(&_graph), reached(*(new ReachedMap(*graph /*, false*/))), 
marci@301
    29
      own_reached_map(true) { }
marci@360
    30
    ~BfsIterator() { if (own_reached_map) delete &reached; }
marci@448
    31
    /// This method markes s reached.
marci@448
    32
    /// If the queue is empty, then s is pushed in the bfs queue 
marci@448
    33
    /// and the first OutEdgeIt is processed.
marci@448
    34
    /// If the queue is not empty, then s is simply pushed.
marci@301
    35
    void pushAndSetReached(Node s) { 
marci@301
    36
      reached.set(s, true);
marci@301
    37
      if (bfs_queue.empty()) {
marci@301
    38
	bfs_queue.push(s);
marci@303
    39
	graph->first(actual_edge, s);
marci@303
    40
	if (graph->valid(actual_edge)) { 
marci@303
    41
	  Node w=graph->bNode(actual_edge);
marci@303
    42
	  if (!reached[w]) {
marci@301
    43
	    bfs_queue.push(w);
marci@301
    44
	    reached.set(w, true);
marci@301
    45
	    b_node_newly_reached=true;
marci@301
    46
	  } else {
marci@301
    47
	    b_node_newly_reached=false;
marci@301
    48
	  }
marci@301
    49
	} 
marci@301
    50
      } else {
marci@301
    51
	bfs_queue.push(s);
marci@301
    52
      }
marci@301
    53
    }
marci@448
    54
    /// As \c BfsIterator<Graph, ReachedMap> works as an edge-iterator, 
marci@448
    55
    /// its \c operator++() iterates on the edges in a bfs order.
marci@360
    56
    BfsIterator<Graph, /*OutEdgeIt,*/ ReachedMap>& 
marci@301
    57
    operator++() { 
marci@303
    58
      if (graph->valid(actual_edge)) { 
marci@303
    59
	graph->next(actual_edge);
marci@303
    60
	if (graph->valid(actual_edge)) {
marci@303
    61
	  Node w=graph->bNode(actual_edge);
marci@303
    62
	  if (!reached[w]) {
marci@301
    63
	    bfs_queue.push(w);
marci@301
    64
	    reached.set(w, true);
marci@301
    65
	    b_node_newly_reached=true;
marci@301
    66
	  } else {
marci@301
    67
	    b_node_newly_reached=false;
marci@301
    68
	  }
marci@301
    69
	}
marci@301
    70
      } else {
marci@301
    71
	bfs_queue.pop(); 
marci@301
    72
	if (!bfs_queue.empty()) {
marci@303
    73
	  graph->first(actual_edge, bfs_queue.front());
marci@303
    74
	  if (graph->valid(actual_edge)) {
marci@303
    75
	    Node w=graph->bNode(actual_edge);
marci@303
    76
	    if (!reached[w]) {
marci@301
    77
	      bfs_queue.push(w);
marci@301
    78
	      reached.set(w, true);
marci@301
    79
	      b_node_newly_reached=true;
marci@301
    80
	    } else {
marci@301
    81
	      b_node_newly_reached=false;
marci@301
    82
	    }
marci@301
    83
	  }
marci@301
    84
	}
marci@301
    85
      }
marci@301
    86
      return *this;
marci@301
    87
    }
marci@301
    88
    bool finished() const { return bfs_queue.empty(); }
marci@448
    89
    /// The conversion operator makes for converting the bfs-iterator 
marci@448
    90
    /// to an \c out-edge-iterator.
marci@409
    91
    operator OutEdgeIt() const { return actual_edge; }
marci@301
    92
    bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@303
    93
    bool isANodeExamined() const { return !(graph->valid(actual_edge)); }
marci@301
    94
    Node aNode() const { return bfs_queue.front(); }
marci@303
    95
    Node bNode() const { return graph->bNode(actual_edge); }
marci@301
    96
    const ReachedMap& getReachedMap() const { return reached; }
marci@301
    97
    const std::queue<Node>& getBfsQueue() const { return bfs_queue; }
marci@301
    98
  };  
marci@301
    99
marci@409
   100
  /// Bfs searches from s for the nodes wich are not marked in 
marci@448
   101
  /// \c reached_map
marci@448
   102
  /// Reached is a read-write bool-map, Pred is a write-nodemap 
marci@448
   103
  /// and dist is an rw-nodemap, have to be.
marci@409
   104
  template <typename Graph, 
marci@409
   105
	    typename ReachedMap=typename Graph::template NodeMap<bool>, 
marci@409
   106
	    typename PredMap
marci@409
   107
	    =typename Graph::template NodeMap<typename Graph::Edge>, 
marci@409
   108
	    typename DistMap=typename Graph::template NodeMap<int> > 
marci@409
   109
  class Bfs : public BfsIterator<Graph, ReachedMap> {
marci@409
   110
    typedef BfsIterator<Graph, ReachedMap> Parent;
marci@409
   111
  protected:
marci@409
   112
    typedef typename Parent::Node Node;
marci@409
   113
    PredMap& pred;
marci@409
   114
    DistMap& dist;
marci@409
   115
  public:
marci@409
   116
    Bfs<Graph, ReachedMap, PredMap, DistMap>(const Graph& _graph, ReachedMap& _reached, PredMap& _pred, DistMap& _dist) : BfsIterator<Graph, ReachedMap>(_graph, _reached), pred(&_pred), dist(&_dist) { }
marci@448
   117
    /// s is marked to be reached and pushed in the bfs queue.
marci@448
   118
    /// If the queue is empty, then the first out-edge is processed.
marci@448
   119
    /// If s was not marked previously, then 
marci@448
   120
    /// in addition its pred is set to be INVALID, and dist to 0. 
marci@448
   121
    /// if s was marked previuosly, then it is simply pushed.
marci@409
   122
    void push(Node s) { 
marci@409
   123
      if (this->reached[s]) {
marci@409
   124
	Parent::pushAndSetReached(s);
marci@409
   125
      } else {
marci@409
   126
	Parent::pushAndSetReached(s);
marci@409
   127
	pred.set(s, INVALID);
marci@409
   128
	dist.set(s, 0);
marci@409
   129
      }
marci@409
   130
    }
marci@448
   131
    /// A bfs is processed from s.
marci@409
   132
    void run(Node s) {
marci@409
   133
      push(s);
marci@409
   134
      while (!this->finished()) this->operator++();
marci@409
   135
    }
marci@409
   136
    Bfs<Graph, ReachedMap, PredMap, DistMap> operator++() {
marci@409
   137
      Parent::operator++();
marci@415
   138
      if (this->graph->valid(this->actual_edge) && this->b_node_newly_reached) 
marci@415
   139
      {
marci@415
   140
	pred.set(this->bNode(), this->actual_edge);
marci@415
   141
	dist.set(this->bNode(), dist[this->aNode()]);
marci@409
   142
      }
marci@409
   143
      return *this;
marci@409
   144
    }
marci@409
   145
    const PredMap& getPredMap() const { return pred; }
marci@409
   146
    const DistMap& getDistMap() const { return dist; }
marci@409
   147
  };
marci@409
   148
marci@303
   149
  template <typename Graph, /*typename OutEdgeIt,*/ 
marci@303
   150
	    typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
marci@360
   151
  class DfsIterator {
marci@303
   152
  protected:
marci@303
   153
    typedef typename Graph::Node Node;
marci@303
   154
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@303
   155
    const Graph* graph;
marci@301
   156
    std::stack<OutEdgeIt> dfs_stack;
marci@301
   157
    bool b_node_newly_reached;
marci@301
   158
    OutEdgeIt actual_edge;
marci@301
   159
    Node actual_node;
marci@301
   160
    ReachedMap& reached;
marci@301
   161
    bool own_reached_map;
marci@301
   162
  public:
marci@360
   163
    DfsIterator(const Graph& _graph, ReachedMap& _reached) : 
marci@303
   164
      graph(&_graph), reached(_reached), 
marci@301
   165
      own_reached_map(false) { }
marci@360
   166
    DfsIterator(const Graph& _graph) : 
marci@303
   167
      graph(&_graph), reached(*(new ReachedMap(*graph /*, false*/))), 
marci@301
   168
      own_reached_map(true) { }
marci@360
   169
    ~DfsIterator() { if (own_reached_map) delete &reached; }
marci@301
   170
    void pushAndSetReached(Node s) { 
marci@301
   171
      actual_node=s;
marci@301
   172
      reached.set(s, true);
marci@301
   173
      OutEdgeIt e;
marci@303
   174
      graph->first(e, s);
marci@301
   175
      dfs_stack.push(e); 
marci@301
   176
    }
marci@360
   177
    DfsIterator<Graph, /*OutEdgeIt,*/ ReachedMap>& 
marci@301
   178
    operator++() { 
marci@301
   179
      actual_edge=dfs_stack.top();
marci@301
   180
      //actual_node=G.aNode(actual_edge);
marci@303
   181
      if (graph->valid(actual_edge)/*.valid()*/) { 
marci@303
   182
	Node w=graph->bNode(actual_edge);
marci@301
   183
	actual_node=w;
marci@303
   184
	if (!reached[w]) {
marci@301
   185
	  OutEdgeIt e;
marci@303
   186
	  graph->first(e, w);
marci@301
   187
	  dfs_stack.push(e);
marci@301
   188
	  reached.set(w, true);
marci@301
   189
	  b_node_newly_reached=true;
marci@301
   190
	} else {
marci@303
   191
	  actual_node=graph->aNode(actual_edge);
marci@303
   192
	  graph->next(dfs_stack.top());
marci@301
   193
	  b_node_newly_reached=false;
marci@301
   194
	}
marci@301
   195
      } else {
marci@301
   196
	//actual_node=G.aNode(dfs_stack.top());
marci@301
   197
	dfs_stack.pop();
marci@301
   198
      }
marci@301
   199
      return *this;
marci@301
   200
    }
marci@301
   201
    bool finished() const { return dfs_stack.empty(); }
marci@409
   202
    operator OutEdgeIt() const { return actual_edge; }
marci@301
   203
    bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@303
   204
    bool isANodeExamined() const { return !(graph->valid(actual_edge)); }
marci@301
   205
    Node aNode() const { return actual_node; /*FIXME*/}
marci@389
   206
    Node bNode() const { return graph->bNode(actual_edge); }
marci@301
   207
    const ReachedMap& getReachedMap() const { return reached; }
marci@301
   208
    const std::stack<OutEdgeIt>& getDfsStack() const { return dfs_stack; }
marci@301
   209
  };
marci@301
   210
marci@448
   211
  /// Dfs searches from s for the nodes wich are not marked in 
marci@448
   212
  /// \c reached_map
marci@448
   213
  /// Reached is a read-write bool-map, Pred is a write-nodemap, have to be.
marci@448
   214
  template <typename Graph, 
marci@448
   215
	    typename ReachedMap=typename Graph::template NodeMap<bool>, 
marci@448
   216
	    typename PredMap
marci@448
   217
	    =typename Graph::template NodeMap<typename Graph::Edge> > 
marci@448
   218
  class Dfs : public DfsIterator<Graph, ReachedMap> {
marci@448
   219
    typedef DfsIterator<Graph, ReachedMap> Parent;
marci@448
   220
  protected:
marci@448
   221
    typedef typename Parent::Node Node;
marci@448
   222
    PredMap& pred;
marci@448
   223
  public:
marci@448
   224
    Dfs<Graph, ReachedMap, PredMap>(const Graph& _graph, ReachedMap& _reached, PredMap& _pred) : DfsIterator<Graph, ReachedMap>(_graph, _reached), pred(&_pred) { }
marci@448
   225
    /// s is marked to be reached and pushed in the bfs queue.
marci@448
   226
    /// If the queue is empty, then the first out-edge is processed.
marci@448
   227
    /// If s was not marked previously, then 
marci@448
   228
    /// in addition its pred is set to be INVALID. 
marci@448
   229
    /// if s was marked previuosly, then it is simply pushed.
marci@448
   230
    void push(Node s) { 
marci@448
   231
      if (this->reached[s]) {
marci@448
   232
	Parent::pushAndSetReached(s);
marci@448
   233
      } else {
marci@448
   234
	Parent::pushAndSetReached(s);
marci@448
   235
	pred.set(s, INVALID);
marci@448
   236
      }
marci@448
   237
    }
marci@448
   238
    /// A bfs is processed from s.
marci@448
   239
    void run(Node s) {
marci@448
   240
      push(s);
marci@448
   241
      while (!this->finished()) this->operator++();
marci@448
   242
    }
marci@448
   243
    Dfs<Graph, ReachedMap, PredMap> operator++() {
marci@448
   244
      Parent::operator++();
marci@448
   245
      if (this->graph->valid(this->actual_edge) && this->b_node_newly_reached) 
marci@448
   246
      {
marci@448
   247
	pred.set(this->bNode(), this->actual_edge);
marci@448
   248
      }
marci@448
   249
      return *this;
marci@448
   250
    }
marci@448
   251
    const PredMap& getPredMap() const { return pred; }
marci@448
   252
  };
marci@448
   253
marci@448
   254
marci@301
   255
} // namespace hugo
marci@301
   256
marci@301
   257
#endif //HUGO_BFS_ITERATOR_H