# HG changeset patch # User marci # Date 1083083228 0 # Node ID 510c53fd06cdf862e5d645683a118dc9eaf74956 # Parent 9c997ebe4aff8df9ddd3752fa8aba1720d9698c5 bfs, dfs, bfsiterator, dfsiterator for alpar's sake of being much more standardized. diff -r 9c997ebe4aff -r 510c53fd06cd src/work/marci/bfs_iterator.h --- a/src/work/marci/bfs_iterator.h Tue Apr 27 14:17:13 2004 +0000 +++ b/src/work/marci/bfs_iterator.h Tue Apr 27 16:27:08 2004 +0000 @@ -28,9 +28,10 @@ graph(&_graph), reached(*(new ReachedMap(*graph /*, false*/))), own_reached_map(true) { } ~BfsIterator() { if (own_reached_map) delete &reached; } - //s is marcked reached. - //if the queue is empty, then the its is pushed ant the first OutEdgeIt is processe. - //is the queue is not empty, then is it pushed. + /// This method markes s reached. + /// If the queue is empty, then s is pushed in the bfs queue + /// and the first OutEdgeIt is processed. + /// If the queue is not empty, then s is simply pushed. void pushAndSetReached(Node s) { reached.set(s, true); if (bfs_queue.empty()) { @@ -50,6 +51,8 @@ bfs_queue.push(s); } } + /// As \c BfsIterator works as an edge-iterator, + /// its \c operator++() iterates on the edges in a bfs order. BfsIterator& operator++() { if (graph->valid(actual_edge)) { @@ -83,6 +86,8 @@ return *this; } bool finished() const { return bfs_queue.empty(); } + /// The conversion operator makes for converting the bfs-iterator + /// to an \c out-edge-iterator. operator OutEdgeIt() const { return actual_edge; } bool isBNodeNewlyReached() const { return b_node_newly_reached; } bool isANodeExamined() const { return !(graph->valid(actual_edge)); } @@ -93,7 +98,9 @@ }; /// Bfs searches from s for the nodes wich are not marked in - /// reachedmap + /// \c reached_map + /// Reached is a read-write bool-map, Pred is a write-nodemap + /// and dist is an rw-nodemap, have to be. template , typename PredMap @@ -107,11 +114,11 @@ DistMap& dist; public: Bfs(const Graph& _graph, ReachedMap& _reached, PredMap& _pred, DistMap& _dist) : BfsIterator(_graph, _reached), pred(&_pred), dist(&_dist) { } - //s is marked to be reached and pushed in the bfs queue. - //if the queue is empty, then the first out-edge is processed - //If s was not marked previously, then - //in addition its pred is set to be INVALID, and dist to 0. - //if s was marked previuosly, then it is simply pushed. + /// s is marked to be reached and pushed in the bfs queue. + /// If the queue is empty, then the first out-edge is processed. + /// If s was not marked previously, then + /// in addition its pred is set to be INVALID, and dist to 0. + /// if s was marked previuosly, then it is simply pushed. void push(Node s) { if (this->reached[s]) { Parent::pushAndSetReached(s); @@ -121,6 +128,7 @@ dist.set(s, 0); } } + /// A bfs is processed from s. void run(Node s) { push(s); while (!this->finished()) this->operator++(); @@ -200,6 +208,50 @@ const std::stack& getDfsStack() const { return dfs_stack; } }; + /// Dfs searches from s for the nodes wich are not marked in + /// \c reached_map + /// Reached is a read-write bool-map, Pred is a write-nodemap, have to be. + template , + typename PredMap + =typename Graph::template NodeMap > + class Dfs : public DfsIterator { + typedef DfsIterator Parent; + protected: + typedef typename Parent::Node Node; + PredMap& pred; + public: + Dfs(const Graph& _graph, ReachedMap& _reached, PredMap& _pred) : DfsIterator(_graph, _reached), pred(&_pred) { } + /// s is marked to be reached and pushed in the bfs queue. + /// If the queue is empty, then the first out-edge is processed. + /// If s was not marked previously, then + /// in addition its pred is set to be INVALID. + /// if s was marked previuosly, then it is simply pushed. + void push(Node s) { + if (this->reached[s]) { + Parent::pushAndSetReached(s); + } else { + Parent::pushAndSetReached(s); + pred.set(s, INVALID); + } + } + /// A bfs is processed from s. + void run(Node s) { + push(s); + while (!this->finished()) this->operator++(); + } + Dfs operator++() { + Parent::operator++(); + if (this->graph->valid(this->actual_edge) && this->b_node_newly_reached) + { + pred.set(this->bNode(), this->actual_edge); + } + return *this; + } + const PredMap& getPredMap() const { return pred; } + }; + + } // namespace hugo #endif //HUGO_BFS_ITERATOR_H