src/work/bfs_iterator.h
author marci
Tue, 23 Mar 2004 13:31:02 +0000
changeset 238 ad3bdd78f4f6
child 243 a85fd87460e3
permissions -rw-r--r--
.
marci@174
     1
// -*- c++ -*-
marci@174
     2
#ifndef BFS_ITERATOR_H
marci@174
     3
#define BFS_ITERATOR_H
marci@174
     4
marci@174
     5
#include <queue>
marci@174
     6
#include <stack>
marci@174
     7
#include <utility>
marci@174
     8
#include <graph_wrapper.h>
marci@174
     9
marci@174
    10
namespace hugo {
marci@174
    11
marci@174
    12
  template <typename Graph>
marci@174
    13
  struct bfs {
marci@174
    14
    typedef typename Graph::Node Node;
marci@174
    15
    typedef typename Graph::Edge Edge;
marci@174
    16
    typedef typename Graph::NodeIt NodeIt;
marci@174
    17
    typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@174
    18
    Graph& G;
marci@174
    19
    Node s;
marci@174
    20
    typename Graph::NodeMap<bool> reached;
marci@174
    21
    typename Graph::NodeMap<Edge> pred;
marci@174
    22
    typename Graph::NodeMap<int> dist;
marci@174
    23
    std::queue<Node> bfs_queue;
marci@174
    24
    bfs(Graph& _G, Node _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) { 
marci@174
    25
      bfs_queue.push(s); 
marci@174
    26
      for(NodeIt i=G.template first<NodeIt>(); i.valid(); ++i) 
marci@174
    27
	reached.set(i, false);
marci@174
    28
      reached.set(s, true);
marci@174
    29
      dist.set(s, 0); 
marci@174
    30
    }
marci@174
    31
    
marci@174
    32
    void run() {
marci@174
    33
      while (!bfs_queue.empty()) {
marci@174
    34
	Node v=bfs_queue.front();
marci@174
    35
	OutEdgeIt e=G.template first<OutEdgeIt>(v);
marci@174
    36
	bfs_queue.pop();
marci@174
    37
	for( ; e.valid(); ++e) {
marci@174
    38
	  Node w=G.bNode(e);
marci@174
    39
	  std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl;
marci@174
    40
	  if (!reached.get(w)) {
marci@174
    41
	    std::cout << G.id(w) << " is newly reached :-)" << std::endl;
marci@174
    42
	    bfs_queue.push(w);
marci@174
    43
	    dist.set(w, dist.get(v)+1);
marci@174
    44
	    pred.set(w, e);
marci@174
    45
	    reached.set(w, true);
marci@174
    46
	  } else {
marci@174
    47
	    std::cout << G.id(w) << " is already reached" << std::endl;
marci@174
    48
	  }
marci@174
    49
	}
marci@174
    50
      }
marci@174
    51
    }
marci@174
    52
  };
marci@174
    53
marci@174
    54
//   template <typename Graph> 
marci@174
    55
//   struct bfs_visitor {
marci@174
    56
//     typedef typename Graph::Node Node;
marci@174
    57
//     typedef typename Graph::Edge Edge;
marci@174
    58
//     typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@174
    59
//     Graph& G;
marci@174
    60
//     bfs_visitor(Graph& _G) : G(_G) { }
marci@174
    61
//     void at_previously_reached(OutEdgeIt& e) { 
marci@174
    62
//       //Node v=G.aNode(e);
marci@174
    63
//       Node w=G.bNode(e);
marci@174
    64
//       std::cout << G.id(w) << " is already reached" << std::endl;
marci@174
    65
//    }
marci@174
    66
//     void at_newly_reached(OutEdgeIt& e) { 
marci@174
    67
//       //Node v=G.aNode(e);
marci@174
    68
//       Node w=G.bNode(e);
marci@174
    69
//       std::cout << G.id(w) << " is newly reached :-)" << std::endl;
marci@174
    70
//     }
marci@174
    71
//   };
marci@174
    72
marci@174
    73
//   template <typename Graph, typename ReachedMap, typename visitor_type>
marci@174
    74
//   struct bfs_iterator {
marci@174
    75
//     typedef typename Graph::Node Node;
marci@174
    76
//     typedef typename Graph::Edge Edge;
marci@174
    77
//     typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@174
    78
//     Graph& G;
marci@174
    79
//     std::queue<OutEdgeIt>& bfs_queue;
marci@174
    80
//     ReachedMap& reached;
marci@174
    81
//     visitor_type& visitor;
marci@174
    82
//     void process() {
marci@174
    83
//       while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@174
    84
//       if (bfs_queue.empty()) return;
marci@174
    85
//       OutEdgeIt e=bfs_queue.front();
marci@174
    86
//       //Node v=G.aNode(e);
marci@174
    87
//       Node w=G.bNode(e);
marci@174
    88
//       if (!reached.get(w)) {
marci@174
    89
// 	visitor.at_newly_reached(e);
marci@174
    90
// 	bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@174
    91
// 	reached.set(w, true);
marci@174
    92
//       } else {
marci@174
    93
// 	visitor.at_previously_reached(e);
marci@174
    94
//       }
marci@174
    95
//     }
marci@174
    96
//     bfs_iterator(Graph& _G, std::queue<OutEdgeIt>& _bfs_queue, ReachedMap& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) { 
marci@174
    97
//       //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@174
    98
//       valid();
marci@174
    99
//     }
marci@174
   100
//     bfs_iterator<Graph, ReachedMap, visitor_type>& operator++() { 
marci@174
   101
//       //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@174
   102
//       //if (bfs_queue.empty()) return *this;
marci@174
   103
//       if (!valid()) return *this;
marci@174
   104
//       ++(bfs_queue.front());
marci@174
   105
//       //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@174
   106
//       valid();
marci@174
   107
//       return *this;
marci@174
   108
//     }
marci@174
   109
//     //void next() { 
marci@174
   110
//     //  while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@174
   111
//     //  if (bfs_queue.empty()) return;
marci@174
   112
//     //  ++(bfs_queue.front());
marci@174
   113
//     //  while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@174
   114
//     //}
marci@174
   115
//     bool valid() { 
marci@174
   116
//       while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@174
   117
//       if (bfs_queue.empty()) return false; else return true;
marci@174
   118
//     }
marci@174
   119
//     //bool finished() { 
marci@174
   120
//     //  while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@174
   121
//     //  if (bfs_queue.empty()) return true; else return false;
marci@174
   122
//     //}
marci@174
   123
//     operator Edge () { return bfs_queue.front(); }
marci@174
   124
marci@174
   125
//   };
marci@174
   126
marci@174
   127
//   template <typename Graph, typename ReachedMap>
marci@174
   128
//   struct bfs_iterator1 {
marci@174
   129
//     typedef typename Graph::Node Node;
marci@174
   130
//     typedef typename Graph::Edge Edge;
marci@174
   131
//     typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@174
   132
//     Graph& G;
marci@174
   133
//     std::queue<OutEdgeIt>& bfs_queue;
marci@174
   134
//     ReachedMap& reached;
marci@174
   135
//     bool _newly_reached;
marci@174
   136
//     bfs_iterator1(Graph& _G, std::queue<OutEdgeIt>& _bfs_queue, ReachedMap& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@174
   137
//       valid();
marci@174
   138
//       if (!bfs_queue.empty() && bfs_queue.front().valid()) { 
marci@174
   139
// 	OutEdgeIt e=bfs_queue.front();
marci@174
   140
// 	Node w=G.bNode(e);
marci@174
   141
// 	if (!reached.get(w)) {
marci@174
   142
// 	  bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@174
   143
// 	  reached.set(w, true);
marci@174
   144
// 	  _newly_reached=true;
marci@174
   145
// 	} else {
marci@174
   146
// 	  _newly_reached=false;
marci@174
   147
// 	}
marci@174
   148
//       }
marci@174
   149
//     }
marci@174
   150
//     bfs_iterator1<Graph, ReachedMap>& operator++() { 
marci@174
   151
//       if (!valid()) return *this;
marci@174
   152
//       ++(bfs_queue.front());
marci@174
   153
//       valid();
marci@174
   154
//       if (!bfs_queue.empty() && bfs_queue.front().valid()) { 
marci@174
   155
// 	OutEdgeIt e=bfs_queue.front();
marci@174
   156
// 	Node w=G.bNode(e);
marci@174
   157
// 	if (!reached.get(w)) {
marci@174
   158
// 	  bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@174
   159
// 	  reached.set(w, true);
marci@174
   160
// 	  _newly_reached=true;
marci@174
   161
// 	} else {
marci@174
   162
// 	  _newly_reached=false;
marci@174
   163
// 	}
marci@174
   164
//       }
marci@174
   165
//       return *this;
marci@174
   166
//     }
marci@174
   167
//     bool valid() { 
marci@174
   168
//       while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@174
   169
//       if (bfs_queue.empty()) return false; else return true;
marci@174
   170
//     }
marci@174
   171
//     operator OutEdgeIt() { return bfs_queue.front(); }
marci@174
   172
//     //ize
marci@174
   173
//     bool newly_reached() { return _newly_reached; }
marci@174
   174
marci@174
   175
//   };
marci@174
   176
marci@174
   177
//   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@174
   178
//   struct BfsIterator {
marci@174
   179
//     typedef typename Graph::Node Node;
marci@174
   180
//     Graph& G;
marci@174
   181
//     std::queue<OutEdgeIt>& bfs_queue;
marci@174
   182
//     ReachedMap& reached;
marci@174
   183
//     bool b_node_newly_reached;
marci@174
   184
//     OutEdgeIt actual_edge;
marci@174
   185
//     BfsIterator(Graph& _G, 
marci@174
   186
// 		std::queue<OutEdgeIt>& _bfs_queue, 
marci@174
   187
// 		ReachedMap& _reached) : 
marci@174
   188
//       G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@174
   189
//       actual_edge=bfs_queue.front();
marci@174
   190
//       if (actual_edge.valid()) { 
marci@174
   191
// 	Node w=G.bNode(actual_edge);
marci@174
   192
// 	if (!reached.get(w)) {
marci@174
   193
// 	  bfs_queue.push(G.firstOutEdge(w));
marci@174
   194
// 	  reached.set(w, true);
marci@174
   195
// 	  b_node_newly_reached=true;
marci@174
   196
// 	} else {
marci@174
   197
// 	  b_node_newly_reached=false;
marci@174
   198
// 	}
marci@174
   199
//       }
marci@174
   200
//     }
marci@174
   201
//     BfsIterator<Graph, OutEdgeIt, ReachedMap>& 
marci@174
   202
//     operator++() { 
marci@174
   203
//       if (bfs_queue.front().valid()) { 
marci@174
   204
// 	++(bfs_queue.front());
marci@174
   205
// 	actual_edge=bfs_queue.front();
marci@174
   206
// 	if (actual_edge.valid()) {
marci@174
   207
// 	  Node w=G.bNode(actual_edge);
marci@174
   208
// 	  if (!reached.get(w)) {
marci@174
   209
// 	    bfs_queue.push(G.firstOutEdge(w));
marci@174
   210
// 	    reached.set(w, true);
marci@174
   211
// 	    b_node_newly_reached=true;
marci@174
   212
// 	  } else {
marci@174
   213
// 	    b_node_newly_reached=false;
marci@174
   214
// 	  }
marci@174
   215
// 	}
marci@174
   216
//       } else {
marci@174
   217
// 	bfs_queue.pop(); 
marci@174
   218
// 	actual_edge=bfs_queue.front();
marci@174
   219
// 	if (actual_edge.valid()) {
marci@174
   220
// 	  Node w=G.bNode(actual_edge);
marci@174
   221
// 	  if (!reached.get(w)) {
marci@174
   222
// 	    bfs_queue.push(G.firstOutEdge(w));
marci@174
   223
// 	    reached.set(w, true);
marci@174
   224
// 	    b_node_newly_reached=true;
marci@174
   225
// 	  } else {
marci@174
   226
// 	    b_node_newly_reached=false;
marci@174
   227
// 	  }
marci@174
   228
// 	}
marci@174
   229
//       }
marci@174
   230
//       return *this;
marci@174
   231
//     }
marci@174
   232
//     bool finished() { return bfs_queue.empty(); }
marci@174
   233
//     operator OutEdgeIt () { return actual_edge; }
marci@174
   234
//     bool bNodeIsNewlyReached() { return b_node_newly_reached; }
marci@174
   235
//     bool aNodeIsExamined() { return !(actual_edge.valid()); }
marci@174
   236
//   };
marci@174
   237
marci@174
   238
marci@174
   239
//   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@174
   240
//   struct DfsIterator {
marci@174
   241
//     typedef typename Graph::Node Node;
marci@174
   242
//     Graph& G;
marci@174
   243
//     std::stack<OutEdgeIt>& bfs_queue;
marci@174
   244
//     ReachedMap& reached;
marci@174
   245
//     bool b_node_newly_reached;
marci@174
   246
//     OutEdgeIt actual_edge;
marci@174
   247
//     DfsIterator(Graph& _G, 
marci@174
   248
// 		std::stack<OutEdgeIt>& _bfs_queue, 
marci@174
   249
// 		ReachedMap& _reached) : 
marci@174
   250
//       G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@174
   251
//       actual_edge=bfs_queue.top();
marci@174
   252
//       if (actual_edge.valid()) { 
marci@174
   253
// 	Node w=G.bNode(actual_edge);
marci@174
   254
// 	if (!reached.get(w)) {
marci@174
   255
// 	  bfs_queue.push(G.firstOutEdge(w));
marci@174
   256
// 	  reached.set(w, true);
marci@174
   257
// 	  b_node_newly_reached=true;
marci@174
   258
// 	} else {
marci@174
   259
// 	  ++(bfs_queue.top());
marci@174
   260
// 	  b_node_newly_reached=false;
marci@174
   261
// 	}
marci@174
   262
//       } else {
marci@174
   263
// 	bfs_queue.pop();
marci@174
   264
//       }
marci@174
   265
//     }
marci@174
   266
//     DfsIterator<Graph, OutEdgeIt, ReachedMap>& 
marci@174
   267
//     operator++() { 
marci@174
   268
//       actual_edge=bfs_queue.top();
marci@174
   269
//       if (actual_edge.valid()) { 
marci@174
   270
// 	Node w=G.bNode(actual_edge);
marci@174
   271
// 	if (!reached.get(w)) {
marci@174
   272
// 	  bfs_queue.push(G.firstOutEdge(w));
marci@174
   273
// 	  reached.set(w, true);
marci@174
   274
// 	  b_node_newly_reached=true;
marci@174
   275
// 	} else {
marci@174
   276
// 	  ++(bfs_queue.top());
marci@174
   277
// 	  b_node_newly_reached=false;
marci@174
   278
// 	}
marci@174
   279
//       } else {
marci@174
   280
// 	bfs_queue.pop();
marci@174
   281
//       }
marci@174
   282
//       return *this;
marci@174
   283
//     }
marci@174
   284
//     bool finished() { return bfs_queue.empty(); }
marci@174
   285
//     operator OutEdgeIt () { return actual_edge; }
marci@174
   286
//     bool bNodeIsNewlyReached() { return b_node_newly_reached; }
marci@174
   287
//     bool aNodeIsExamined() { return !(actual_edge.valid()); }
marci@174
   288
//   };
marci@174
   289
marci@174
   290
//   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@174
   291
//   struct BfsIterator1 {
marci@174
   292
//     typedef typename Graph::Node Node;
marci@174
   293
//     Graph& G;
marci@174
   294
//     std::queue<OutEdgeIt>& bfs_queue;
marci@174
   295
//     ReachedMap& reached;
marci@174
   296
//     bool b_node_newly_reached;
marci@174
   297
//     OutEdgeIt actual_edge;
marci@174
   298
//     BfsIterator1(Graph& _G, 
marci@174
   299
// 		std::queue<OutEdgeIt>& _bfs_queue, 
marci@174
   300
// 		ReachedMap& _reached) : 
marci@174
   301
//       G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@174
   302
//       actual_edge=bfs_queue.front();
marci@174
   303
//       if (actual_edge.valid()) { 
marci@174
   304
//       	Node w=G.bNode(actual_edge);
marci@174
   305
// 	if (!reached.get(w)) {
marci@174
   306
// 	  bfs_queue.push(OutEdgeIt(G, w));
marci@174
   307
// 	  reached.set(w, true);
marci@174
   308
// 	  b_node_newly_reached=true;
marci@174
   309
// 	} else {
marci@174
   310
// 	  b_node_newly_reached=false;
marci@174
   311
// 	}
marci@174
   312
//       }
marci@174
   313
//     }
marci@174
   314
//     void next() { 
marci@174
   315
//       if (bfs_queue.front().valid()) { 
marci@174
   316
// 	++(bfs_queue.front());
marci@174
   317
// 	actual_edge=bfs_queue.front();
marci@174
   318
// 	if (actual_edge.valid()) {
marci@174
   319
// 	  Node w=G.bNode(actual_edge);
marci@174
   320
// 	  if (!reached.get(w)) {
marci@174
   321
// 	    bfs_queue.push(OutEdgeIt(G, w));
marci@174
   322
// 	    reached.set(w, true);
marci@174
   323
// 	    b_node_newly_reached=true;
marci@174
   324
// 	  } else {
marci@174
   325
// 	    b_node_newly_reached=false;
marci@174
   326
// 	  }
marci@174
   327
// 	}
marci@174
   328
//       } else {
marci@174
   329
// 	bfs_queue.pop(); 
marci@174
   330
// 	actual_edge=bfs_queue.front();
marci@174
   331
// 	if (actual_edge.valid()) {
marci@174
   332
// 	  Node w=G.bNode(actual_edge);
marci@174
   333
// 	  if (!reached.get(w)) {
marci@174
   334
// 	    bfs_queue.push(OutEdgeIt(G, w));
marci@174
   335
// 	    reached.set(w, true);
marci@174
   336
// 	    b_node_newly_reached=true;
marci@174
   337
// 	  } else {
marci@174
   338
// 	    b_node_newly_reached=false;
marci@174
   339
// 	  }
marci@174
   340
// 	}
marci@174
   341
//       }
marci@174
   342
//       //return *this;
marci@174
   343
//     }
marci@174
   344
//     bool finished() { return bfs_queue.empty(); }
marci@174
   345
//     operator OutEdgeIt () { return actual_edge; }
marci@174
   346
//     bool bNodeIsNewlyReached() { return b_node_newly_reached; }
marci@174
   347
//     bool aNodeIsExamined() { return !(actual_edge.valid()); }
marci@174
   348
//   };
marci@174
   349
marci@174
   350
marci@174
   351
//   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@174
   352
//   struct DfsIterator1 {
marci@174
   353
//     typedef typename Graph::Node Node;
marci@174
   354
//     Graph& G;
marci@174
   355
//     std::stack<OutEdgeIt>& bfs_queue;
marci@174
   356
//     ReachedMap& reached;
marci@174
   357
//     bool b_node_newly_reached;
marci@174
   358
//     OutEdgeIt actual_edge;
marci@174
   359
//     DfsIterator1(Graph& _G, 
marci@174
   360
// 		std::stack<OutEdgeIt>& _bfs_queue, 
marci@174
   361
// 		ReachedMap& _reached) : 
marci@174
   362
//       G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@174
   363
//       //actual_edge=bfs_queue.top();
marci@174
   364
//       //if (actual_edge.valid()) { 
marci@174
   365
//       //	Node w=G.bNode(actual_edge);
marci@174
   366
//       //if (!reached.get(w)) {
marci@174
   367
//       //  bfs_queue.push(OutEdgeIt(G, w));
marci@174
   368
//       //  reached.set(w, true);
marci@174
   369
//       //  b_node_newly_reached=true;
marci@174
   370
//       //} else {
marci@174
   371
//       //  ++(bfs_queue.top());
marci@174
   372
//       //  b_node_newly_reached=false;
marci@174
   373
//       //}
marci@174
   374
//       //} else {
marci@174
   375
//       //	bfs_queue.pop();
marci@174
   376
//       //}
marci@174
   377
//     }
marci@174
   378
//     void next() { 
marci@174
   379
//       actual_edge=bfs_queue.top();
marci@174
   380
//       if (actual_edge.valid()) { 
marci@174
   381
// 	Node w=G.bNode(actual_edge);
marci@174
   382
// 	if (!reached.get(w)) {
marci@174
   383
// 	  bfs_queue.push(OutEdgeIt(G, w));
marci@174
   384
// 	  reached.set(w, true);
marci@174
   385
// 	  b_node_newly_reached=true;
marci@174
   386
// 	} else {
marci@174
   387
// 	  ++(bfs_queue.top());
marci@174
   388
// 	  b_node_newly_reached=false;
marci@174
   389
// 	}
marci@174
   390
//       } else {
marci@174
   391
// 	bfs_queue.pop();
marci@174
   392
//       }
marci@174
   393
//       //return *this;
marci@174
   394
//     }
marci@174
   395
//     bool finished() { return bfs_queue.empty(); }
marci@174
   396
//     operator OutEdgeIt () { return actual_edge; }
marci@174
   397
//     bool bNodeIsNewlyReached() { return b_node_newly_reached; }
marci@174
   398
//     bool aNodeIsLeaved() { return !(actual_edge.valid()); }
marci@174
   399
//   };
marci@174
   400
marci@174
   401
//   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@174
   402
//   class BfsIterator2 {
marci@174
   403
//     typedef typename Graph::Node Node;
marci@174
   404
//     const Graph& G;
marci@174
   405
//     std::queue<OutEdgeIt> bfs_queue;
marci@174
   406
//     ReachedMap reached;
marci@174
   407
//     bool b_node_newly_reached;
marci@174
   408
//     OutEdgeIt actual_edge;
marci@174
   409
//   public:
marci@174
   410
//     BfsIterator2(const Graph& _G) : G(_G), reached(G, false) { }
marci@174
   411
//     void pushAndSetReached(Node s) { 
marci@174
   412
//       reached.set(s, true);
marci@174
   413
//       if (bfs_queue.empty()) {
marci@174
   414
// 	bfs_queue.push(G.template first<OutEdgeIt>(s));
marci@174
   415
// 	actual_edge=bfs_queue.front();
marci@174
   416
// 	if (actual_edge.valid()) { 
marci@174
   417
// 	  Node w=G.bNode(actual_edge);
marci@174
   418
// 	  if (!reached.get(w)) {
marci@174
   419
// 	    bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@174
   420
// 	    reached.set(w, true);
marci@174
   421
// 	    b_node_newly_reached=true;
marci@174
   422
// 	  } else {
marci@174
   423
// 	    b_node_newly_reached=false;
marci@174
   424
// 	  }
marci@174
   425
// 	} //else {
marci@174
   426
// 	//}
marci@174
   427
//       } else {
marci@174
   428
// 	bfs_queue.push(G.template first<OutEdgeIt>(s));
marci@174
   429
//       }
marci@174
   430
//     }
marci@174
   431
//     BfsIterator2<Graph, OutEdgeIt, ReachedMap>& 
marci@174
   432
//     operator++() { 
marci@174
   433
//       if (bfs_queue.front().valid()) { 
marci@174
   434
// 	++(bfs_queue.front());
marci@174
   435
// 	actual_edge=bfs_queue.front();
marci@174
   436
// 	if (actual_edge.valid()) {
marci@174
   437
// 	  Node w=G.bNode(actual_edge);
marci@174
   438
// 	  if (!reached.get(w)) {
marci@174
   439
// 	    bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@174
   440
// 	    reached.set(w, true);
marci@174
   441
// 	    b_node_newly_reached=true;
marci@174
   442
// 	  } else {
marci@174
   443
// 	    b_node_newly_reached=false;
marci@174
   444
// 	  }
marci@174
   445
// 	}
marci@174
   446
//       } else {
marci@174
   447
// 	bfs_queue.pop(); 
marci@174
   448
// 	if (!bfs_queue.empty()) {
marci@174
   449
// 	  actual_edge=bfs_queue.front();
marci@174
   450
// 	  if (actual_edge.valid()) {
marci@174
   451
// 	    Node w=G.bNode(actual_edge);
marci@174
   452
// 	    if (!reached.get(w)) {
marci@174
   453
// 	      bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@174
   454
// 	      reached.set(w, true);
marci@174
   455
// 	      b_node_newly_reached=true;
marci@174
   456
// 	    } else {
marci@174
   457
// 	      b_node_newly_reached=false;
marci@174
   458
// 	    }
marci@174
   459
// 	  }
marci@174
   460
// 	}
marci@174
   461
//       }
marci@174
   462
//       return *this;
marci@174
   463
//     }
marci@174
   464
//     bool finished() const { return bfs_queue.empty(); }
marci@174
   465
//     operator OutEdgeIt () const { return actual_edge; }
marci@174
   466
//     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@174
   467
//     bool isANodeExamined() const { return !(actual_edge.valid()); }
marci@174
   468
//     const ReachedMap& getReachedMap() const { return reached; }
marci@174
   469
//     const std::queue<OutEdgeIt>& getBfsQueue() const { return bfs_queue; }
marci@174
   470
//  };
marci@174
   471
marci@174
   472
marci@174
   473
//   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@174
   474
//   class BfsIterator3 {
marci@174
   475
//     typedef typename Graph::Node Node;
marci@174
   476
//     const Graph& G;
marci@174
   477
//     std::queue< std::pair<Node, OutEdgeIt> > bfs_queue;
marci@174
   478
//     ReachedMap reached;
marci@174
   479
//     bool b_node_newly_reached;
marci@174
   480
//     OutEdgeIt actual_edge;
marci@174
   481
//   public:
marci@174
   482
//     BfsIterator3(const Graph& _G) : G(_G), reached(G, false) { }
marci@174
   483
//     void pushAndSetReached(Node s) { 
marci@174
   484
//       reached.set(s, true);
marci@174
   485
//       if (bfs_queue.empty()) {
marci@174
   486
// 	bfs_queue.push(std::pair<Node, OutEdgeIt>(s, G.template first<OutEdgeIt>(s)));
marci@174
   487
// 	actual_edge=bfs_queue.front().second;
marci@174
   488
// 	if (actual_edge.valid()) { 
marci@174
   489
// 	  Node w=G.bNode(actual_edge);
marci@174
   490
// 	  if (!reached.get(w)) {
marci@174
   491
// 	    bfs_queue.push(std::pair<Node, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
marci@174
   492
// 	    reached.set(w, true);
marci@174
   493
// 	    b_node_newly_reached=true;
marci@174
   494
// 	  } else {
marci@174
   495
// 	    b_node_newly_reached=false;
marci@174
   496
// 	  }
marci@174
   497
// 	} //else {
marci@174
   498
// 	//}
marci@174
   499
//       } else {
marci@174
   500
// 	bfs_queue.push(std::pair<Node, OutEdgeIt>(s, G.template first<OutEdgeIt>(s)));
marci@174
   501
//       }
marci@174
   502
//     }
marci@174
   503
//     BfsIterator3<Graph, OutEdgeIt, ReachedMap>& 
marci@174
   504
//     operator++() { 
marci@174
   505
//       if (bfs_queue.front().second.valid()) { 
marci@174
   506
// 	++(bfs_queue.front().second);
marci@174
   507
// 	actual_edge=bfs_queue.front().second;
marci@174
   508
// 	if (actual_edge.valid()) {
marci@174
   509
// 	  Node w=G.bNode(actual_edge);
marci@174
   510
// 	  if (!reached.get(w)) {
marci@174
   511
// 	    bfs_queue.push(std::pair<Node, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
marci@174
   512
// 	    reached.set(w, true);
marci@174
   513
// 	    b_node_newly_reached=true;
marci@174
   514
// 	  } else {
marci@174
   515
// 	    b_node_newly_reached=false;
marci@174
   516
// 	  }
marci@174
   517
// 	}
marci@174
   518
//       } else {
marci@174
   519
// 	bfs_queue.pop(); 
marci@174
   520
// 	if (!bfs_queue.empty()) {
marci@174
   521
// 	  actual_edge=bfs_queue.front().second;
marci@174
   522
// 	  if (actual_edge.valid()) {
marci@174
   523
// 	    Node w=G.bNode(actual_edge);
marci@174
   524
// 	    if (!reached.get(w)) {
marci@174
   525
// 	      bfs_queue.push(std::pair<Node, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
marci@174
   526
// 	      reached.set(w, true);
marci@174
   527
// 	      b_node_newly_reached=true;
marci@174
   528
// 	    } else {
marci@174
   529
// 	      b_node_newly_reached=false;
marci@174
   530
// 	    }
marci@174
   531
// 	  }
marci@174
   532
// 	}
marci@174
   533
//       }
marci@174
   534
//       return *this;
marci@174
   535
//     }
marci@174
   536
//     bool finished() const { return bfs_queue.empty(); }
marci@174
   537
//     operator OutEdgeIt () const { return actual_edge; }
marci@174
   538
//     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@174
   539
//     bool isANodeExamined() const { return !(actual_edge.valid()); }
marci@174
   540
//     Node aNode() const { return bfs_queue.front().first; }
marci@174
   541
//     Node bNode() const { return G.bNode(actual_edge); }
marci@174
   542
//     const ReachedMap& getReachedMap() const { return reached; }
marci@174
   543
//     //const std::queue< std::pair<Node, OutEdgeIt> >& getBfsQueue() const { return bfs_queue; }
marci@174
   544
//  };
marci@174
   545
marci@174
   546
marci@174
   547
  template <typename Graph, typename OutEdgeIt, 
marci@174
   548
	    typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
marci@174
   549
  class BfsIterator4 {
marci@174
   550
    typedef typename Graph::Node Node;
marci@174
   551
    const Graph& G;
marci@174
   552
    std::queue<Node> bfs_queue;
marci@174
   553
    ReachedMap& reached;
marci@174
   554
    bool b_node_newly_reached;
marci@174
   555
    OutEdgeIt actual_edge;
marci@174
   556
    bool own_reached_map;
marci@174
   557
  public:
marci@174
   558
    BfsIterator4(const Graph& _G, ReachedMap& _reached) : 
marci@174
   559
      G(_G), reached(_reached), 
marci@174
   560
      own_reached_map(false) { }
marci@174
   561
    BfsIterator4(const Graph& _G) : 
marci@174
   562
      G(_G), reached(*(new ReachedMap(G /*, false*/))), 
marci@174
   563
      own_reached_map(true) { }
marci@174
   564
    ~BfsIterator4() { if (own_reached_map) delete &reached; }
marci@174
   565
    void pushAndSetReached(Node s) { 
marci@174
   566
      //std::cout << "mimi" << &reached << std::endl;
marci@174
   567
      reached.set(s, true);
marci@174
   568
      //std::cout << "mumus" << std::endl;
marci@174
   569
      if (bfs_queue.empty()) {
marci@174
   570
	//std::cout << "bibi1" << std::endl;
marci@174
   571
	bfs_queue.push(s);
marci@174
   572
	//std::cout << "zizi" << std::endl;
marci@174
   573
	G./*getF*/first(actual_edge, s);
marci@174
   574
	//std::cout << "kiki" << std::endl;
marci@174
   575
	if (G.valid(actual_edge)/*.valid()*/) { 
marci@174
   576
	  Node w=G.bNode(actual_edge);
marci@174
   577
	  if (!reached.get(w)) {
marci@174
   578
	    bfs_queue.push(w);
marci@174
   579
	    reached.set(w, true);
marci@174
   580
	    b_node_newly_reached=true;
marci@174
   581
	  } else {
marci@174
   582
	    b_node_newly_reached=false;
marci@174
   583
	  }
marci@174
   584
	} 
marci@174
   585
      } else {
marci@174
   586
	//std::cout << "bibi2" << std::endl;
marci@174
   587
	bfs_queue.push(s);
marci@174
   588
      }
marci@174
   589
    }
marci@174
   590
    BfsIterator4<Graph, OutEdgeIt, ReachedMap>& 
marci@174
   591
    operator++() { 
marci@174
   592
      if (G.valid(actual_edge)/*.valid()*/) { 
marci@174
   593
	/*++*/G.next(actual_edge);
marci@174
   594
	if (G.valid(actual_edge)/*.valid()*/) {
marci@174
   595
	  Node w=G.bNode(actual_edge);
marci@174
   596
	  if (!reached.get(w)) {
marci@174
   597
	    bfs_queue.push(w);
marci@174
   598
	    reached.set(w, true);
marci@174
   599
	    b_node_newly_reached=true;
marci@174
   600
	  } else {
marci@174
   601
	    b_node_newly_reached=false;
marci@174
   602
	  }
marci@174
   603
	}
marci@174
   604
      } else {
marci@174
   605
	bfs_queue.pop(); 
marci@174
   606
	if (!bfs_queue.empty()) {
marci@174
   607
	  G./*getF*/first(actual_edge, bfs_queue.front());
marci@174
   608
	  if (G.valid(actual_edge)/*.valid()*/) {
marci@174
   609
	    Node w=G.bNode(actual_edge);
marci@174
   610
	    if (!reached.get(w)) {
marci@174
   611
	      bfs_queue.push(w);
marci@174
   612
	      reached.set(w, true);
marci@174
   613
	      b_node_newly_reached=true;
marci@174
   614
	    } else {
marci@174
   615
	      b_node_newly_reached=false;
marci@174
   616
	    }
marci@174
   617
	  }
marci@174
   618
	}
marci@174
   619
      }
marci@174
   620
      return *this;
marci@174
   621
    }
marci@174
   622
    bool finished() const { return bfs_queue.empty(); }
marci@174
   623
    operator OutEdgeIt () const { return actual_edge; }
marci@174
   624
    bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@174
   625
    bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
marci@174
   626
    Node aNode() const { return bfs_queue.front(); }
marci@174
   627
    Node bNode() const { return G.bNode(actual_edge); }
marci@174
   628
    const ReachedMap& getReachedMap() const { return reached; }
marci@174
   629
    const std::queue<Node>& getBfsQueue() const { return bfs_queue; }
marci@174
   630
 };  
marci@174
   631
marci@174
   632
marci@174
   633
  template <typename GraphWrapper, /*typename OutEdgeIt,*/ 
marci@174
   634
	    typename ReachedMap/*=typename GraphWrapper::NodeMap<bool>*/ >
marci@174
   635
  class BfsIterator5 {
marci@174
   636
    typedef typename GraphWrapper::Node Node;
marci@174
   637
    typedef typename GraphWrapper::OutEdgeIt OutEdgeIt;
marci@174
   638
    GraphWrapper G;
marci@174
   639
    std::queue<Node> bfs_queue;
marci@174
   640
    ReachedMap& reached;
marci@174
   641
    bool b_node_newly_reached;
marci@174
   642
    OutEdgeIt actual_edge;
marci@174
   643
    bool own_reached_map;
marci@174
   644
  public:
marci@174
   645
    BfsIterator5(const GraphWrapper& _G, ReachedMap& _reached) : 
marci@174
   646
      G(_G), reached(_reached), 
marci@174
   647
      own_reached_map(false) { }
marci@174
   648
    BfsIterator5(const GraphWrapper& _G) : 
marci@174
   649
      G(_G), reached(*(new ReachedMap(G /*, false*/))), 
marci@174
   650
      own_reached_map(true) { }
marci@174
   651
//     BfsIterator5(const typename GraphWrapper::BaseGraph& _G, 
marci@174
   652
// 		 ReachedMap& _reached) : 
marci@174
   653
//       G(_G), reached(_reached), 
marci@174
   654
//       own_reached_map(false) { }
marci@174
   655
//     BfsIterator5(const typename GraphWrapper::BaseGraph& _G) : 
marci@174
   656
//       G(_G), reached(*(new ReachedMap(G /*, false*/))), 
marci@174
   657
//       own_reached_map(true) { }
marci@174
   658
    ~BfsIterator5() { if (own_reached_map) delete &reached; }
marci@174
   659
    void pushAndSetReached(Node s) { 
marci@174
   660
      reached.set(s, true);
marci@174
   661
      if (bfs_queue.empty()) {
marci@174
   662
	bfs_queue.push(s);
marci@174
   663
	G./*getF*/first(actual_edge, s);
marci@174
   664
	if (G.valid(actual_edge)/*.valid()*/) { 
marci@174
   665
	  Node w=G.bNode(actual_edge);
marci@174
   666
	  if (!reached.get(w)) {
marci@174
   667
	    bfs_queue.push(w);
marci@174
   668
	    reached.set(w, true);
marci@174
   669
	    b_node_newly_reached=true;
marci@174
   670
	  } else {
marci@174
   671
	    b_node_newly_reached=false;
marci@174
   672
	  }
marci@174
   673
	} 
marci@174
   674
      } else {
marci@174
   675
	bfs_queue.push(s);
marci@174
   676
      }
marci@174
   677
    }
marci@174
   678
    BfsIterator5<GraphWrapper, /*OutEdgeIt,*/ ReachedMap>& 
marci@174
   679
    operator++() { 
marci@174
   680
      if (G.valid(actual_edge)/*.valid()*/) { 
marci@174
   681
	/*++*/G.next(actual_edge);
marci@174
   682
	if (G.valid(actual_edge)/*.valid()*/) {
marci@174
   683
	  Node w=G.bNode(actual_edge);
marci@174
   684
	  if (!reached.get(w)) {
marci@174
   685
	    bfs_queue.push(w);
marci@174
   686
	    reached.set(w, true);
marci@174
   687
	    b_node_newly_reached=true;
marci@174
   688
	  } else {
marci@174
   689
	    b_node_newly_reached=false;
marci@174
   690
	  }
marci@174
   691
	}
marci@174
   692
      } else {
marci@174
   693
	bfs_queue.pop(); 
marci@174
   694
	if (!bfs_queue.empty()) {
marci@174
   695
	  G./*getF*/first(actual_edge, bfs_queue.front());
marci@174
   696
	  if (G.valid(actual_edge)/*.valid()*/) {
marci@174
   697
	    Node w=G.bNode(actual_edge);
marci@174
   698
	    if (!reached.get(w)) {
marci@174
   699
	      bfs_queue.push(w);
marci@174
   700
	      reached.set(w, true);
marci@174
   701
	      b_node_newly_reached=true;
marci@174
   702
	    } else {
marci@174
   703
	      b_node_newly_reached=false;
marci@174
   704
	    }
marci@174
   705
	  }
marci@174
   706
	}
marci@174
   707
      }
marci@174
   708
      return *this;
marci@174
   709
    }
marci@174
   710
    bool finished() const { return bfs_queue.empty(); }
marci@174
   711
    operator OutEdgeIt () const { return actual_edge; }
marci@174
   712
    bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@174
   713
    bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
marci@174
   714
    Node aNode() const { return bfs_queue.front(); }
marci@174
   715
    Node bNode() const { return G.bNode(actual_edge); }
marci@174
   716
    const ReachedMap& getReachedMap() const { return reached; }
marci@174
   717
    const std::queue<Node>& getBfsQueue() const { return bfs_queue; }
marci@174
   718
  };  
marci@174
   719
marci@174
   720
  template <typename Graph, typename OutEdgeIt, 
marci@174
   721
	    typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
marci@174
   722
  class DfsIterator4 {
marci@174
   723
    typedef typename Graph::Node Node;
marci@174
   724
    const Graph& G;
marci@174
   725
    std::stack<OutEdgeIt> dfs_stack;
marci@174
   726
    bool b_node_newly_reached;
marci@174
   727
    OutEdgeIt actual_edge;
marci@174
   728
    Node actual_node;
marci@174
   729
    ReachedMap& reached;
marci@174
   730
    bool own_reached_map;
marci@174
   731
  public:
marci@174
   732
    DfsIterator4(const Graph& _G, ReachedMap& _reached) : 
marci@174
   733
      G(_G), reached(_reached), 
marci@174
   734
      own_reached_map(false) { }
marci@174
   735
    DfsIterator4(const Graph& _G) : 
marci@174
   736
      G(_G), reached(*(new ReachedMap(G /*, false*/))), 
marci@174
   737
      own_reached_map(true) { }
marci@174
   738
    ~DfsIterator4() { if (own_reached_map) delete &reached; }
marci@174
   739
    void pushAndSetReached(Node s) { 
marci@174
   740
      actual_node=s;
marci@174
   741
      reached.set(s, true);
marci@174
   742
      dfs_stack.push(G.template first<OutEdgeIt>(s)); 
marci@174
   743
    }
marci@174
   744
    DfsIterator4<Graph, OutEdgeIt, ReachedMap>& 
marci@174
   745
    operator++() { 
marci@174
   746
      actual_edge=dfs_stack.top();
marci@174
   747
      //actual_node=G.aNode(actual_edge);
marci@174
   748
      if (G.valid(actual_edge)/*.valid()*/) { 
marci@174
   749
	Node w=G.bNode(actual_edge);
marci@174
   750
	actual_node=w;
marci@174
   751
	if (!reached.get(w)) {
marci@174
   752
	  dfs_stack.push(G.template first<OutEdgeIt>(w));
marci@174
   753
	  reached.set(w, true);
marci@174
   754
	  b_node_newly_reached=true;
marci@174
   755
	} else {
marci@174
   756
	  actual_node=G.aNode(actual_edge);
marci@174
   757
	  /*++*/G.next(dfs_stack.top());
marci@174
   758
	  b_node_newly_reached=false;
marci@174
   759
	}
marci@174
   760
      } else {
marci@174
   761
	//actual_node=G.aNode(dfs_stack.top());
marci@174
   762
	dfs_stack.pop();
marci@174
   763
      }
marci@174
   764
      return *this;
marci@174
   765
    }
marci@174
   766
    bool finished() const { return dfs_stack.empty(); }
marci@174
   767
    operator OutEdgeIt () const { return actual_edge; }
marci@174
   768
    bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@174
   769
    bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
marci@174
   770
    Node aNode() const { return actual_node; /*FIXME*/}
marci@174
   771
    Node bNode() const { return G.bNode(actual_edge); }
marci@174
   772
    const ReachedMap& getReachedMap() const { return reached; }
marci@174
   773
    const std::stack<OutEdgeIt>& getDfsStack() const { return dfs_stack; }
marci@174
   774
  };
marci@174
   775
marci@174
   776
  template <typename GraphWrapper, /*typename OutEdgeIt,*/ 
marci@174
   777
	    typename ReachedMap/*=typename GraphWrapper::NodeMap<bool>*/ >
marci@174
   778
  class DfsIterator5 {
marci@174
   779
    typedef typename GraphWrapper::Node Node;
marci@174
   780
    typedef typename GraphWrapper::OutEdgeIt OutEdgeIt;
marci@174
   781
    GraphWrapper G;
marci@174
   782
    std::stack<OutEdgeIt> dfs_stack;
marci@174
   783
    bool b_node_newly_reached;
marci@174
   784
    OutEdgeIt actual_edge;
marci@174
   785
    Node actual_node;
marci@174
   786
    ReachedMap& reached;
marci@174
   787
    bool own_reached_map;
marci@174
   788
  public:
marci@174
   789
    DfsIterator5(const GraphWrapper& _G, ReachedMap& _reached) : 
marci@174
   790
      G(_G), reached(_reached), 
marci@174
   791
      own_reached_map(false) { }
marci@174
   792
    DfsIterator5(const GraphWrapper& _G) : 
marci@174
   793
      G(_G), reached(*(new ReachedMap(G /*, false*/))), 
marci@174
   794
      own_reached_map(true) { }
marci@174
   795
    ~DfsIterator5() { if (own_reached_map) delete &reached; }
marci@174
   796
    void pushAndSetReached(Node s) { 
marci@174
   797
      actual_node=s;
marci@174
   798
      reached.set(s, true);
marci@174
   799
      dfs_stack.push(G.template first<OutEdgeIt>(s)); 
marci@174
   800
    }
marci@174
   801
    DfsIterator5<GraphWrapper, /*OutEdgeIt,*/ ReachedMap>& 
marci@174
   802
    operator++() { 
marci@174
   803
      actual_edge=dfs_stack.top();
marci@174
   804
      //actual_node=G.aNode(actual_edge);
marci@174
   805
      if (G.valid(actual_edge)/*.valid()*/) { 
marci@174
   806
	Node w=G.bNode(actual_edge);
marci@174
   807
	actual_node=w;
marci@174
   808
	if (!reached.get(w)) {
marci@174
   809
	  dfs_stack.push(G.template first<OutEdgeIt>(w));
marci@174
   810
	  reached.set(w, true);
marci@174
   811
	  b_node_newly_reached=true;
marci@174
   812
	} else {
marci@174
   813
	  actual_node=G.aNode(actual_edge);
marci@174
   814
	  /*++*/G.next(dfs_stack.top());
marci@174
   815
	  b_node_newly_reached=false;
marci@174
   816
	}
marci@174
   817
      } else {
marci@174
   818
	//actual_node=G.aNode(dfs_stack.top());
marci@174
   819
	dfs_stack.pop();
marci@174
   820
      }
marci@174
   821
      return *this;
marci@174
   822
    }
marci@174
   823
    bool finished() const { return dfs_stack.empty(); }
marci@174
   824
    operator OutEdgeIt () const { return actual_edge; }
marci@174
   825
    bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@174
   826
    bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
marci@174
   827
    Node aNode() const { return actual_node; /*FIXME*/}
marci@174
   828
    Node bNode() const { return G.bNode(actual_edge); }
marci@174
   829
    const ReachedMap& getReachedMap() const { return reached; }
marci@174
   830
    const std::stack<OutEdgeIt>& getDfsStack() const { return dfs_stack; }
marci@174
   831
  };
marci@174
   832
marci@174
   833
marci@174
   834
marci@174
   835
} // namespace hugo
marci@174
   836
marci@174
   837
#endif //BFS_ITERATOR_H