Index: src/work/bfs_iterator.hh
===================================================================
--- src/work/bfs_iterator.hh	(revision 144)
+++ src/work/bfs_iterator.hh	(revision 148)
@@ -545,5 +545,5 @@
 
   template <typename Graph, typename OutEdgeIt, 
-	    typename ReachedMap=typename Graph::NodeMap<bool> >
+	    typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
   class BfsIterator4 {
     typedef typename Graph::NodeIt NodeIt;
@@ -567,5 +567,5 @@
 	bfs_queue.push(s);
 	G.getFirst(actual_edge, s);
-	if (actual_edge.valid()) { 
+	if (G.valid(actual_edge)/*.valid()*/) { 
 	  NodeIt w=G.bNode(actual_edge);
 	  if (!reached.get(w)) {
@@ -583,7 +583,7 @@
     BfsIterator4<Graph, OutEdgeIt, ReachedMap>& 
     operator++() { 
-      if (actual_edge.valid()) { 
-	++actual_edge;
-	if (actual_edge.valid()) {
+      if (G.valid(actual_edge)/*.valid()*/) { 
+	/*++*/G.next(actual_edge);
+	if (G.valid(actual_edge)/*.valid()*/) {
 	  NodeIt w=G.bNode(actual_edge);
 	  if (!reached.get(w)) {
@@ -599,5 +599,5 @@
 	if (!bfs_queue.empty()) {
 	  G.getFirst(actual_edge, bfs_queue.front());
-	  if (actual_edge.valid()) {
+	  if (G.valid(actual_edge)/*.valid()*/) {
 	    NodeIt w=G.bNode(actual_edge);
 	    if (!reached.get(w)) {
@@ -616,5 +616,5 @@
     operator OutEdgeIt () const { return actual_edge; }
     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
-    bool isANodeExamined() const { return !(actual_edge.valid()); }
+    bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
     NodeIt aNode() const { return bfs_queue.front(); }
     NodeIt bNode() const { return G.bNode(actual_edge); }
@@ -623,6 +623,86 @@
  };  
 
+
+  template <typename GraphWrapper, typename OutEdgeIt, 
+	    typename ReachedMap/*=typename GraphWrapper::NodeMap<bool>*/ >
+  class BfsIterator5 {
+    typedef typename GraphWrapper::NodeIt NodeIt;
+    GraphWrapper G;
+    std::queue<NodeIt> bfs_queue;
+    ReachedMap& reached;
+    bool b_node_newly_reached;
+    OutEdgeIt actual_edge;
+    bool own_reached_map;
+  public:
+    BfsIterator5(const GraphWrapper& _G, ReachedMap& _reached) : 
+      G(_G), reached(_reached), 
+      own_reached_map(false) { }
+    BfsIterator5(const GraphWrapper& _G) : 
+      G(_G), reached(*(new ReachedMap(G /*, false*/))), 
+      own_reached_map(true) { }
+    ~BfsIterator5() { if (own_reached_map) delete &reached; }
+    void pushAndSetReached(NodeIt s) { 
+      reached.set(s, true);
+      if (bfs_queue.empty()) {
+	bfs_queue.push(s);
+	G.getFirst(actual_edge, s);
+	if (G.valid(actual_edge)/*.valid()*/) { 
+	  NodeIt w=G.bNode(actual_edge);
+	  if (!reached.get(w)) {
+	    bfs_queue.push(w);
+	    reached.set(w, true);
+	    b_node_newly_reached=true;
+	  } else {
+	    b_node_newly_reached=false;
+	  }
+	} 
+      } else {
+	bfs_queue.push(s);
+      }
+    }
+    BfsIterator5<GraphWrapper, OutEdgeIt, ReachedMap>& 
+    operator++() { 
+      if (G.valid(actual_edge)/*.valid()*/) { 
+	/*++*/G.next(actual_edge);
+	if (G.valid(actual_edge)/*.valid()*/) {
+	  NodeIt w=G.bNode(actual_edge);
+	  if (!reached.get(w)) {
+	    bfs_queue.push(w);
+	    reached.set(w, true);
+	    b_node_newly_reached=true;
+	  } else {
+	    b_node_newly_reached=false;
+	  }
+	}
+      } else {
+	bfs_queue.pop(); 
+	if (!bfs_queue.empty()) {
+	  G.getFirst(actual_edge, bfs_queue.front());
+	  if (G.valid(actual_edge)/*.valid()*/) {
+	    NodeIt w=G.bNode(actual_edge);
+	    if (!reached.get(w)) {
+	      bfs_queue.push(w);
+	      reached.set(w, true);
+	      b_node_newly_reached=true;
+	    } else {
+	      b_node_newly_reached=false;
+	    }
+	  }
+	}
+      }
+      return *this;
+    }
+    bool finished() const { return bfs_queue.empty(); }
+    operator OutEdgeIt () const { return actual_edge; }
+    bool isBNodeNewlyReached() const { return b_node_newly_reached; }
+    bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
+    NodeIt aNode() const { return bfs_queue.front(); }
+    NodeIt bNode() const { return G.bNode(actual_edge); }
+    const ReachedMap& getReachedMap() const { return reached; }
+    const std::queue<NodeIt>& getBfsQueue() const { return bfs_queue; }
+  };  
+
   template <typename Graph, typename OutEdgeIt, 
-	    typename ReachedMap=typename Graph::NodeMap<bool> >
+	    typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
   class DfsIterator4 {
     typedef typename Graph::NodeIt NodeIt;
@@ -651,5 +731,5 @@
       actual_edge=dfs_stack.top();
       //actual_node=G.aNode(actual_edge);
-      if (actual_edge.valid()) { 
+      if (G.valid(actual_edge)/*.valid()*/) { 
 	NodeIt w=G.bNode(actual_edge);
 	actual_node=w;
@@ -660,5 +740,5 @@
 	} else {
 	  actual_node=G.aNode(actual_edge);
-	  ++(dfs_stack.top());
+	  /*++*/G.next(dfs_stack.top());
 	  b_node_newly_reached=false;
 	}
@@ -672,5 +752,5 @@
     operator OutEdgeIt () const { return actual_edge; }
     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
-    bool isANodeExamined() const { return !(actual_edge.valid()); }
+    bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
     NodeIt aNode() const { return actual_node; /*FIXME*/}
     NodeIt bNode() const { return G.bNode(actual_edge); }
@@ -679,78 +759,59 @@
   };
 
-
-
-  template <typename GraphWrapper, typename ReachedMap>
-  class BfsIterator5 {
+  template <typename GraphWrapper, typename OutEdgeIt, 
+	    typename ReachedMap/*=typename GraphWrapper::NodeMap<bool>*/ >
+  class DfsIterator5 {
     typedef typename GraphWrapper::NodeIt NodeIt;
-    typedef typename GraphWrapper::OutEdgeIt OutEdgeIt;
-    GraphWrapper gw;
-    std::queue<NodeIt> bfs_queue;
-    ReachedMap reached;
-    bool b_node_newly_reached;
-    OutEdgeIt actual_edge;
+    GraphWrapper G;
+    std::stack<OutEdgeIt> dfs_stack;
+    bool b_node_newly_reached;
+    OutEdgeIt actual_edge;
+    NodeIt actual_node;
+    ReachedMap& reached;
+    bool own_reached_map;
   public:
-    BfsIterator5(GraphWrapper _gw) : 
-      gw(_gw), reached(_gw.getGraph()) { }
+    DfsIterator5(const GraphWrapper& _G, ReachedMap& _reached) : 
+      G(_G), reached(_reached), 
+      own_reached_map(false) { }
+    DfsIterator5(const GraphWrapper& _G) : 
+      G(_G), reached(*(new ReachedMap(G /*, false*/))), 
+      own_reached_map(true) { }
+    ~DfsIterator5() { if (own_reached_map) delete &reached; }
     void pushAndSetReached(NodeIt s) { 
+      actual_node=s;
       reached.set(s, true);
-      if (bfs_queue.empty()) {
-	bfs_queue.push(s);
-	gw.getFirst(actual_edge, s);
-	if (actual_edge.valid()) { 
-	  NodeIt w=gw.bNode(actual_edge);
-	  if (!reached.get(w)) {
-	    bfs_queue.push(w);
-	    reached.set(w, true);
-	    b_node_newly_reached=true;
-	  } else {
-	    b_node_newly_reached=false;
-	  }
-	} 
-      } else {
-	bfs_queue.push(s);
-      }
-    }
-    BfsIterator5<GraphWrapper, ReachedMap>& 
+      dfs_stack.push(G.template first<OutEdgeIt>(s)); 
+    }
+    DfsIterator5<GraphWrapper, OutEdgeIt, ReachedMap>& 
     operator++() { 
-      if (actual_edge.valid()) { 
-	++actual_edge;
-	if (actual_edge.valid()) {
-	  NodeIt w=gw.bNode(actual_edge);
-	  if (!reached.get(w)) {
-	    bfs_queue.push(w);
-	    reached.set(w, true);
-	    b_node_newly_reached=true;
-	  } else {
-	    b_node_newly_reached=false;
-	  }
-	}
-      } else {
-	bfs_queue.pop(); 
-	if (!bfs_queue.empty()) {
-	  gw.getFirst(actual_edge, bfs_queue.front());
-	  if (actual_edge.valid()) {
-	    NodeIt w=gw.bNode(actual_edge);
-	    if (!reached.get(w)) {
-	      bfs_queue.push(w);
-	      reached.set(w, true);
-	      b_node_newly_reached=true;
-	    } else {
-	      b_node_newly_reached=false;
-	    }
-	  }
-	}
-      }
-      return *this;
-    }
-    bool finished() const { return bfs_queue.empty(); }
+      actual_edge=dfs_stack.top();
+      //actual_node=G.aNode(actual_edge);
+      if (G.valid(actual_edge)/*.valid()*/) { 
+	NodeIt w=G.bNode(actual_edge);
+	actual_node=w;
+	if (!reached.get(w)) {
+	  dfs_stack.push(G.template first<OutEdgeIt>(w));
+	  reached.set(w, true);
+	  b_node_newly_reached=true;
+	} else {
+	  actual_node=G.aNode(actual_edge);
+	  /*++*/G.next(dfs_stack.top());
+	  b_node_newly_reached=false;
+	}
+      } else {
+	//actual_node=G.aNode(dfs_stack.top());
+	dfs_stack.pop();
+      }
+      return *this;
+    }
+    bool finished() const { return dfs_stack.empty(); }
     operator OutEdgeIt () const { return actual_edge; }
     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
-    bool isANodeExamined() const { return !(actual_edge.valid()); }
-    NodeIt aNode() const { return bfs_queue.front(); }
-    NodeIt bNode() const { return gw.bNode(actual_edge); }
+    bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
+    NodeIt aNode() const { return actual_node; /*FIXME*/}
+    NodeIt bNode() const { return G.bNode(actual_edge); }
     const ReachedMap& getReachedMap() const { return reached; }
-    const std::queue<NodeIt>& getBfsQueue() const { return bfs_queue; }
- };
+    const std::stack<OutEdgeIt>& getDfsStack() const { return dfs_stack; }
+  };
 
 
