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