# HG changeset patch # User marci # Date 1077119908 0 # Node ID f26897fb91fdd285e8b6c6585911b09cb4a42365 # Parent ba20e7ab1baaad2e2a4ae02795a11b47bfc8bac2 dfs iterator: DfsIterator4 improved version diff -r ba20e7ab1baa -r f26897fb91fd src/work/bfs_iterator.hh --- a/src/work/bfs_iterator.hh Wed Feb 18 14:43:01 2004 +0000 +++ b/src/work/bfs_iterator.hh Wed Feb 18 15:58:28 2004 +0000 @@ -283,7 +283,7 @@ bool finished() { return bfs_queue.empty(); } operator OutEdgeIt () { return actual_edge; } bool bNodeIsNewlyReached() { return b_node_newly_reached; } - bool aNodeIsLeaved() { return !(actual_edge.valid()); } + bool aNodeIsExamined() { return !(actual_edge.valid()); } }; template @@ -614,6 +614,72 @@ }; + template + struct DfsIterator4 { + typedef typename Graph::NodeIt NodeIt; + const Graph& G; + std::stack dfs_stack; + //ReachedMap& reached; + bool b_node_newly_reached; + OutEdgeIt actual_edge; + NodeIt actual_node; + ReachedMap reached; + DfsIterator4(const Graph& _G + /*, std::stack& _bfs_queue, + ReachedMap& _reached*/) : + G(_G), reached(G, false) /*, bfs_queue(_bfs_queue), reached(_reached)*/ { + //actual_edge=bfs_queue.top(); + //if (actual_edge.valid()) { + // NodeIt w=G.bNode(actual_edge); + //if (!reached.get(w)) { + // bfs_queue.push(OutEdgeIt(G, w)); + // reached.set(w, true); + // b_node_newly_reached=true; + //} else { + // ++(bfs_queue.top()); + // b_node_newly_reached=false; + //} + //} else { + // bfs_queue.pop(); + //} + } + void pushAndSetReached(NodeIt s) { + reached.set(s, true); + dfs_stack.push(G.template first(s)); + } + DfsIterator4& + operator++() { + actual_edge=dfs_stack.top(); + //actual_node=G.aNode(actual_edge); + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + actual_node=w; + if (!reached.get(w)) { + dfs_stack.push(G.template first(w)); + reached.set(w, true); + b_node_newly_reached=true; + } else { + ++(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 actual_node; } + NodeIt bNode() const { return G.bNode(actual_edge); } + const ReachedMap& getReachedMap() const { return reached; } + const std::stack& getDfsStack() const { return dfs_stack; } + }; + + + template class BfsIterator5 { typedef typename GraphWrapper::NodeIt NodeIt; diff -r ba20e7ab1baa -r f26897fb91fd src/work/iterator_bfs_demo.cc --- a/src/work/iterator_bfs_demo.cc Wed Feb 18 14:43:01 2004 +0000 +++ b/src/work/iterator_bfs_demo.cc Wed Feb 18 15:58:28 2004 +0000 @@ -79,6 +79,37 @@ } } + { + std::cout << "iterator dfs demo 4 ..." << std::endl; + DfsIterator4< ListGraph, ListGraph::OutEdgeIt, ListGraph::NodeMap > dfs(G); + dfs.pushAndSetReached(s); + while (!dfs.finished()) { + ++dfs; + if (OutEdgeIt(dfs).valid()) { + std::cout << "OutEdgeIt: " << dfs; + std::cout << " aNode: " << G.aNode(dfs); + std::cout << " bNode: " << G.bNode(dfs) << " "; + } else { + std::cout << "OutEdgeIt: " << "invalid"; + std::cout << " aNode: " << dfs.aNode(); + std::cout << " bNode: " << "invalid" << " "; + } + if (dfs.isBNodeNewlyReached()) { + std::cout << "bNodeIsNewlyReached "; + } else { + std::cout << "bNodeIsNotNewlyReached "; + } + if (dfs.isANodeExamined()) { + std::cout << "aNodeIsExamined "; + } else { + std::cout << "aNodeIsNotExamined "; + } + std::cout< CTGW; CTGW wG(G); diff -r ba20e7ab1baa -r f26897fb91fd src/work/makefile --- a/src/work/makefile Wed Feb 18 14:43:01 2004 +0000 +++ b/src/work/makefile Wed Feb 18 15:58:28 2004 +0000 @@ -1,7 +1,7 @@ INCLUDEDIRS=-I../include -I. -I./jacint CXXFLAGS=-g -O -Wall $(INCLUDEDIRS) -ansi -pedantic -BINARIES = bfsdemo bfsdemo2 bin_heap_demo marci_graph_demo iterator_bfs_dfs_demo graphdemo proba +BINARIES = bin_heap_demo marci_graph_demo iterator_bfs_dfs_demo graphdemo bfsdemo bfsdemo2 # Hat, ez elismerem, hogy nagyon ronda, de mukodik minden altalam # ismert rendszeren :-) (Misi)