# HG changeset patch # User marci # Date 1081003291 0 # Node ID 19f3943521ab5d330f32f3dfedb47b34248c5b80 # Parent be43902fadb70ce7e0558b239bd1b99e90960dde takaritas diff -r be43902fadb7 -r 19f3943521ab src/work/bfs_iterator.hh --- a/src/work/bfs_iterator.hh Sat Apr 03 14:22:33 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,836 +0,0 @@ -#ifndef BFS_ITERATOR_HH -#define BFS_ITERATOR_HH - -#include -#include -#include -#include - -namespace hugo { - - template - struct bfs { - typedef typename Graph::NodeIt NodeIt; - typedef typename Graph::EdgeIt EdgeIt; - typedef typename Graph::EachNodeIt EachNodeIt; - typedef typename Graph::OutEdgeIt OutEdgeIt; - Graph& G; - NodeIt s; - typename Graph::NodeMap reached; - typename Graph::NodeMap pred; - typename Graph::NodeMap dist; - std::queue bfs_queue; - bfs(Graph& _G, NodeIt _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) { - bfs_queue.push(s); - for(EachNodeIt i=G.template first(); i.valid(); ++i) - reached.set(i, false); - reached.set(s, true); - dist.set(s, 0); - } - - void run() { - while (!bfs_queue.empty()) { - NodeIt v=bfs_queue.front(); - OutEdgeIt e=G.template first(v); - bfs_queue.pop(); - for( ; e.valid(); ++e) { - NodeIt w=G.bNode(e); - std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl; - if (!reached.get(w)) { - std::cout << G.id(w) << " is newly reached :-)" << std::endl; - bfs_queue.push(w); - dist.set(w, dist.get(v)+1); - pred.set(w, e); - reached.set(w, true); - } else { - std::cout << G.id(w) << " is already reached" << std::endl; - } - } - } - } - }; - - template - struct bfs_visitor { - typedef typename Graph::NodeIt NodeIt; - typedef typename Graph::EdgeIt EdgeIt; - typedef typename Graph::OutEdgeIt OutEdgeIt; - Graph& G; - bfs_visitor(Graph& _G) : G(_G) { } - void at_previously_reached(OutEdgeIt& e) { - //NodeIt v=G.aNode(e); - NodeIt w=G.bNode(e); - std::cout << G.id(w) << " is already reached" << std::endl; - } - void at_newly_reached(OutEdgeIt& e) { - //NodeIt v=G.aNode(e); - NodeIt w=G.bNode(e); - std::cout << G.id(w) << " is newly reached :-)" << std::endl; - } - }; - - template - struct bfs_iterator { - typedef typename Graph::NodeIt NodeIt; - typedef typename Graph::EdgeIt EdgeIt; - typedef typename Graph::OutEdgeIt OutEdgeIt; - Graph& G; - std::queue& bfs_queue; - ReachedMap& reached; - visitor_type& visitor; - void process() { - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - if (bfs_queue.empty()) return; - OutEdgeIt e=bfs_queue.front(); - //NodeIt v=G.aNode(e); - NodeIt w=G.bNode(e); - if (!reached.get(w)) { - visitor.at_newly_reached(e); - bfs_queue.push(G.template first(w)); - reached.set(w, true); - } else { - visitor.at_previously_reached(e); - } - } - bfs_iterator(Graph& _G, std::queue& _bfs_queue, ReachedMap& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) { - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - valid(); - } - bfs_iterator& operator++() { - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - //if (bfs_queue.empty()) return *this; - if (!valid()) return *this; - ++(bfs_queue.front()); - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - valid(); - return *this; - } - //void next() { - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - // if (bfs_queue.empty()) return; - // ++(bfs_queue.front()); - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - //} - bool valid() { - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - if (bfs_queue.empty()) return false; else return true; - } - //bool finished() { - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - // if (bfs_queue.empty()) return true; else return false; - //} - operator EdgeIt () { return bfs_queue.front(); } - - }; - - template - struct bfs_iterator1 { - typedef typename Graph::NodeIt NodeIt; - typedef typename Graph::EdgeIt EdgeIt; - typedef typename Graph::OutEdgeIt OutEdgeIt; - Graph& G; - std::queue& bfs_queue; - ReachedMap& reached; - bool _newly_reached; - bfs_iterator1(Graph& _G, std::queue& _bfs_queue, ReachedMap& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) { - valid(); - if (!bfs_queue.empty() && bfs_queue.front().valid()) { - OutEdgeIt e=bfs_queue.front(); - NodeIt w=G.bNode(e); - if (!reached.get(w)) { - bfs_queue.push(G.template first(w)); - reached.set(w, true); - _newly_reached=true; - } else { - _newly_reached=false; - } - } - } - bfs_iterator1& operator++() { - if (!valid()) return *this; - ++(bfs_queue.front()); - valid(); - if (!bfs_queue.empty() && bfs_queue.front().valid()) { - OutEdgeIt e=bfs_queue.front(); - NodeIt w=G.bNode(e); - if (!reached.get(w)) { - bfs_queue.push(G.template first(w)); - reached.set(w, true); - _newly_reached=true; - } else { - _newly_reached=false; - } - } - return *this; - } - bool valid() { - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - if (bfs_queue.empty()) return false; else return true; - } - operator OutEdgeIt() { return bfs_queue.front(); } - //ize - bool newly_reached() { return _newly_reached; } - - }; - - template - struct BfsIterator { - typedef typename Graph::NodeIt NodeIt; - Graph& G; - std::queue& bfs_queue; - ReachedMap& reached; - bool b_node_newly_reached; - OutEdgeIt actual_edge; - BfsIterator(Graph& _G, - std::queue& _bfs_queue, - ReachedMap& _reached) : - G(_G), bfs_queue(_bfs_queue), reached(_reached) { - actual_edge=bfs_queue.front(); - if (actual_edge.valid()) { - NodeIt w=G.bNode(actual_edge); - if (!reached.get(w)) { - bfs_queue.push(G.firstOutEdge(w)); - reached.set(w, true); - b_node_newly_reached=true; - } else { - b_node_newly_reached=false; - } - } - } - BfsIterator& - operator++() { - if (bfs_queue.front().valid()) { - ++(bfs_queue.front()); - actual_edge=bfs_queue.front(); - if (actual_edge.valid()) { - NodeIt w=G.bNode(actual_edge); - if (!reached.get(w)) { - bfs_queue.push(G.firstOutEdge(w)); - reached.set(w, true); - b_node_newly_reached=true; - } else { - b_node_newly_reached=false; - } - } - } else { - bfs_queue.pop(); - actual_edge=bfs_queue.front(); - if (actual_edge.valid()) { - NodeIt w=G.bNode(actual_edge); - if (!reached.get(w)) { - bfs_queue.push(G.firstOutEdge(w)); - reached.set(w, true); - b_node_newly_reached=true; - } else { - b_node_newly_reached=false; - } - } - } - return *this; - } - bool finished() { return bfs_queue.empty(); } - operator OutEdgeIt () { return actual_edge; } - bool bNodeIsNewlyReached() { return b_node_newly_reached; } - bool aNodeIsExamined() { return !(actual_edge.valid()); } - }; - - - template - struct DfsIterator { - typedef typename Graph::NodeIt NodeIt; - Graph& G; - std::stack& bfs_queue; - ReachedMap& reached; - bool b_node_newly_reached; - OutEdgeIt actual_edge; - DfsIterator(Graph& _G, - std::stack& _bfs_queue, - ReachedMap& _reached) : - G(_G), 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(G.firstOutEdge(w)); - reached.set(w, true); - b_node_newly_reached=true; - } else { - ++(bfs_queue.top()); - b_node_newly_reached=false; - } - } else { - bfs_queue.pop(); - } - } - DfsIterator& - operator++() { - actual_edge=bfs_queue.top(); - if (actual_edge.valid()) { - NodeIt w=G.bNode(actual_edge); - if (!reached.get(w)) { - bfs_queue.push(G.firstOutEdge(w)); - reached.set(w, true); - b_node_newly_reached=true; - } else { - ++(bfs_queue.top()); - b_node_newly_reached=false; - } - } else { - bfs_queue.pop(); - } - return *this; - } - bool finished() { return bfs_queue.empty(); } - operator OutEdgeIt () { return actual_edge; } - bool bNodeIsNewlyReached() { return b_node_newly_reached; } - bool aNodeIsExamined() { return !(actual_edge.valid()); } - }; - - template - struct BfsIterator1 { - typedef typename Graph::NodeIt NodeIt; - Graph& G; - std::queue& bfs_queue; - ReachedMap& reached; - bool b_node_newly_reached; - OutEdgeIt actual_edge; - BfsIterator1(Graph& _G, - std::queue& _bfs_queue, - ReachedMap& _reached) : - G(_G), bfs_queue(_bfs_queue), reached(_reached) { - actual_edge=bfs_queue.front(); - 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 { - b_node_newly_reached=false; - } - } - } - void next() { - if (bfs_queue.front().valid()) { - ++(bfs_queue.front()); - actual_edge=bfs_queue.front(); - 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 { - b_node_newly_reached=false; - } - } - } else { - bfs_queue.pop(); - actual_edge=bfs_queue.front(); - 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 { - b_node_newly_reached=false; - } - } - } - //return *this; - } - bool finished() { return bfs_queue.empty(); } - operator OutEdgeIt () { return actual_edge; } - bool bNodeIsNewlyReached() { return b_node_newly_reached; } - bool aNodeIsExamined() { return !(actual_edge.valid()); } - }; - - - template - struct DfsIterator1 { - typedef typename Graph::NodeIt NodeIt; - Graph& G; - std::stack& bfs_queue; - ReachedMap& reached; - bool b_node_newly_reached; - OutEdgeIt actual_edge; - DfsIterator1(Graph& _G, - std::stack& _bfs_queue, - ReachedMap& _reached) : - G(_G), 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 next() { - 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(); - } - //return *this; - } - bool finished() { return bfs_queue.empty(); } - operator OutEdgeIt () { return actual_edge; } - bool bNodeIsNewlyReached() { return b_node_newly_reached; } - bool aNodeIsLeaved() { return !(actual_edge.valid()); } - }; - - template - class BfsIterator2 { - typedef typename Graph::NodeIt NodeIt; - const Graph& G; - std::queue bfs_queue; - ReachedMap reached; - bool b_node_newly_reached; - OutEdgeIt actual_edge; - public: - BfsIterator2(const Graph& _G) : G(_G), reached(G, false) { } - void pushAndSetReached(NodeIt s) { - reached.set(s, true); - if (bfs_queue.empty()) { - bfs_queue.push(G.template first(s)); - actual_edge=bfs_queue.front(); - if (actual_edge.valid()) { - NodeIt w=G.bNode(actual_edge); - if (!reached.get(w)) { - bfs_queue.push(G.template first(w)); - reached.set(w, true); - b_node_newly_reached=true; - } else { - b_node_newly_reached=false; - } - } //else { - //} - } else { - bfs_queue.push(G.template first(s)); - } - } - BfsIterator2& - operator++() { - if (bfs_queue.front().valid()) { - ++(bfs_queue.front()); - actual_edge=bfs_queue.front(); - if (actual_edge.valid()) { - NodeIt w=G.bNode(actual_edge); - if (!reached.get(w)) { - bfs_queue.push(G.template first(w)); - reached.set(w, true); - b_node_newly_reached=true; - } else { - b_node_newly_reached=false; - } - } - } else { - bfs_queue.pop(); - if (!bfs_queue.empty()) { - actual_edge=bfs_queue.front(); - if (actual_edge.valid()) { - NodeIt w=G.bNode(actual_edge); - if (!reached.get(w)) { - bfs_queue.push(G.template first(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 !(actual_edge.valid()); } - const ReachedMap& getReachedMap() const { return reached; } - const std::queue& getBfsQueue() const { return bfs_queue; } - }; - - - template - class BfsIterator3 { - typedef typename Graph::NodeIt NodeIt; - const Graph& G; - std::queue< std::pair > bfs_queue; - ReachedMap reached; - bool b_node_newly_reached; - OutEdgeIt actual_edge; - public: - BfsIterator3(const Graph& _G) : G(_G), reached(G, false) { } - void pushAndSetReached(NodeIt s) { - reached.set(s, true); - if (bfs_queue.empty()) { - bfs_queue.push(std::pair(s, G.template first(s))); - actual_edge=bfs_queue.front().second; - if (actual_edge.valid()) { - NodeIt w=G.bNode(actual_edge); - if (!reached.get(w)) { - bfs_queue.push(std::pair(w, G.template first(w))); - reached.set(w, true); - b_node_newly_reached=true; - } else { - b_node_newly_reached=false; - } - } //else { - //} - } else { - bfs_queue.push(std::pair(s, G.template first(s))); - } - } - BfsIterator3& - operator++() { - if (bfs_queue.front().second.valid()) { - ++(bfs_queue.front().second); - actual_edge=bfs_queue.front().second; - if (actual_edge.valid()) { - NodeIt w=G.bNode(actual_edge); - if (!reached.get(w)) { - bfs_queue.push(std::pair(w, G.template first(w))); - reached.set(w, true); - b_node_newly_reached=true; - } else { - b_node_newly_reached=false; - } - } - } else { - bfs_queue.pop(); - if (!bfs_queue.empty()) { - actual_edge=bfs_queue.front().second; - if (actual_edge.valid()) { - NodeIt w=G.bNode(actual_edge); - if (!reached.get(w)) { - bfs_queue.push(std::pair(w, G.template first(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 !(actual_edge.valid()); } - NodeIt aNode() const { return bfs_queue.front().first; } - NodeIt bNode() const { return G.bNode(actual_edge); } - const ReachedMap& getReachedMap() const { return reached; } - //const std::queue< std::pair >& getBfsQueue() const { return bfs_queue; } - }; - - - template */ > - class BfsIterator4 { - typedef typename Graph::NodeIt NodeIt; - const Graph& G; - std::queue bfs_queue; - ReachedMap& reached; - bool b_node_newly_reached; - OutEdgeIt actual_edge; - bool own_reached_map; - public: - BfsIterator4(const Graph& _G, ReachedMap& _reached) : - G(_G), reached(_reached), - own_reached_map(false) { } - BfsIterator4(const Graph& _G) : - G(_G), reached(*(new ReachedMap(G /*, false*/))), - own_reached_map(true) { } - ~BfsIterator4() { if (own_reached_map) delete &reached; } - void pushAndSetReached(NodeIt s) { - //std::cout << "mimi" << &reached << std::endl; - reached.set(s, true); - //std::cout << "mumus" << std::endl; - if (bfs_queue.empty()) { - //std::cout << "bibi1" << std::endl; - bfs_queue.push(s); - //std::cout << "zizi" << std::endl; - G.getFirst(actual_edge, s); - //std::cout << "kiki" << std::endl; - 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 { - //std::cout << "bibi2" << std::endl; - bfs_queue.push(s); - } - } - BfsIterator4& - 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& getBfsQueue() const { return bfs_queue; } - }; - - - template */ > - class BfsIterator5 { - typedef typename GraphWrapper::NodeIt NodeIt; - typedef typename GraphWrapper::OutEdgeIt OutEdgeIt; - GraphWrapper G; - std::queue 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(const typename GraphWrapper::BaseGraph& _G, -// ReachedMap& _reached) : -// G(_G), reached(_reached), -// own_reached_map(false) { } -// BfsIterator5(const typename GraphWrapper::BaseGraph& _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& - 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& getBfsQueue() const { return bfs_queue; } - }; - - template */ > - class DfsIterator4 { - typedef typename Graph::NodeIt NodeIt; - const Graph& G; - std::stack dfs_stack; - bool b_node_newly_reached; - OutEdgeIt actual_edge; - NodeIt actual_node; - ReachedMap& reached; - bool own_reached_map; - public: - DfsIterator4(const Graph& _G, ReachedMap& _reached) : - G(_G), reached(_reached), - own_reached_map(false) { } - DfsIterator4(const Graph& _G) : - G(_G), reached(*(new ReachedMap(G /*, false*/))), - own_reached_map(true) { } - ~DfsIterator4() { if (own_reached_map) delete &reached; } - void pushAndSetReached(NodeIt s) { - actual_node=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 (G.valid(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 { - 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 !(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::stack& getDfsStack() const { return dfs_stack; } - }; - - template */ > - class DfsIterator5 { - typedef typename GraphWrapper::NodeIt NodeIt; - typedef typename GraphWrapper::OutEdgeIt OutEdgeIt; - GraphWrapper G; - std::stack dfs_stack; - bool b_node_newly_reached; - OutEdgeIt actual_edge; - NodeIt actual_node; - ReachedMap& reached; - bool own_reached_map; - public: - 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); - dfs_stack.push(G.template first(s)); - } - DfsIterator5& - operator++() { - 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(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 !(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::stack& getDfsStack() const { return dfs_stack; } - }; - - - -} // namespace hugo - -#endif //BFS_ITERATOR_HH diff -r be43902fadb7 -r 19f3943521ab src/work/edmonds_karp.hh --- a/src/work/edmonds_karp.hh Sat Apr 03 14:22:33 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,677 +0,0 @@ -#ifndef EDMONDS_KARP_HH -#define EDMONDS_KARP_HH - -#include -#include -#include - -#include -//#include - -namespace hugo { - - template - class ResGraph { - public: - typedef typename Graph::NodeIt NodeIt; - typedef typename Graph::EachNodeIt EachNodeIt; - private: - typedef typename Graph::SymEdgeIt OldSymEdgeIt; - const Graph& G; - FlowMap& flow; - const CapacityMap& capacity; - public: - ResGraph(const Graph& _G, FlowMap& _flow, - const CapacityMap& _capacity) : - G(_G), flow(_flow), capacity(_capacity) { } - - class EdgeIt; - class OutEdgeIt; - friend class EdgeIt; - friend class OutEdgeIt; - - class EdgeIt { - friend class ResGraph; - protected: - const ResGraph* resG; - OldSymEdgeIt sym; - public: - EdgeIt() { } - //EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { } - Number free() const { - if (resG->G.aNode(sym)==resG->G.tail(sym)) { - return (resG->capacity.get(sym)-resG->flow.get(sym)); - } else { - return (resG->flow.get(sym)); - } - } - bool valid() const { return sym.valid(); } - void augment(Number a) const { - if (resG->G.aNode(sym)==resG->G.tail(sym)) { - resG->flow.set(sym, resG->flow.get(sym)+a); - //resG->flow[sym]+=a; - } else { - resG->flow.set(sym, resG->flow.get(sym)-a); - //resG->flow[sym]-=a; - } - } - }; - - class OutEdgeIt : public EdgeIt { - friend class ResGraph; - public: - OutEdgeIt() { } - //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; } - private: - OutEdgeIt(const ResGraph& _resG, NodeIt v) { - resG=&_resG; - sym=resG->G.template first(v); - while( sym.valid() && !(free()>0) ) { ++sym; } - } - public: - OutEdgeIt& operator++() { - ++sym; - while( sym.valid() && !(free()>0) ) { ++sym; } - return *this; - } - }; - - void getFirst(OutEdgeIt& e, NodeIt v) const { - e=OutEdgeIt(*this, v); - } - void getFirst(EachNodeIt& v) const { G.getFirst(v); } - - template< typename It > - It first() const { - It e; - getFirst(e); - return e; - } - - template< typename It > - It first(NodeIt v) const { - It e; - getFirst(e, v); - return e; - } - - NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); } - NodeIt head(EdgeIt e) const { return G.bNode(e.sym); } - - NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); } - NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); } - - int id(NodeIt v) const { return G.id(v); } - - template - class NodeMap { - typename Graph::NodeMap node_map; - public: - NodeMap(const ResGraph& _G) : node_map(_G.G) { } - NodeMap(const ResGraph& _G, S a) : node_map(_G.G, a) { } - void set(NodeIt nit, S a) { node_map.set(nit, a); } - S get(NodeIt nit) const { return node_map.get(nit); } - S& operator[](NodeIt nit) { return node_map[nit]; } - const S& operator[](NodeIt nit) const { return node_map[nit]; } - }; - - }; - - - template - class ResGraph2 { - public: - typedef typename Graph::NodeIt NodeIt; - typedef typename Graph::EachNodeIt EachNodeIt; - private: - //typedef typename Graph::SymEdgeIt OldSymEdgeIt; - typedef typename Graph::OutEdgeIt OldOutEdgeIt; - typedef typename Graph::InEdgeIt OldInEdgeIt; - - const Graph& G; - FlowMap& flow; - const CapacityMap& capacity; - public: - ResGraph2(const Graph& _G, FlowMap& _flow, - const CapacityMap& _capacity) : - G(_G), flow(_flow), capacity(_capacity) { } - - class EdgeIt; - class OutEdgeIt; - friend class EdgeIt; - friend class OutEdgeIt; - - class EdgeIt { - friend class ResGraph2; - protected: - const ResGraph2* resG; - //OldSymEdgeIt sym; - OldOutEdgeIt out; - OldInEdgeIt in; - bool out_or_in; //true, iff out - public: - EdgeIt() : out_or_in(true) { } - Number free() const { - if (out_or_in) { - return (resG->capacity.get(out)-resG->flow.get(out)); - } else { - return (resG->flow.get(in)); - } - } - bool valid() const { - return out_or_in && out.valid() || in.valid(); } - void augment(Number a) const { - if (out_or_in) { - resG->flow.set(out, resG->flow.get(out)+a); - } else { - resG->flow.set(in, resG->flow.get(in)-a); - } - } - }; - - class OutEdgeIt : public EdgeIt { - friend class ResGraph2; - public: - OutEdgeIt() { } - private: - OutEdgeIt(const ResGraph2& _resG, NodeIt v) { - resG=&_resG; - out=resG->G.template first(v); - while( out.valid() && !(free()>0) ) { ++out; } - if (!out.valid()) { - out_or_in=0; - in=resG->G.template first(v); - while( in.valid() && !(free()>0) ) { ++in; } - } - } - public: - OutEdgeIt& operator++() { - if (out_or_in) { - NodeIt v=resG->G.aNode(out); - ++out; - while( out.valid() && !(free()>0) ) { ++out; } - if (!out.valid()) { - out_or_in=0; - in=resG->G.template first(v); - while( in.valid() && !(free()>0) ) { ++in; } - } - } else { - ++in; - while( in.valid() && !(free()>0) ) { ++in; } - } - return *this; - } - }; - - void getFirst(OutEdgeIt& e, NodeIt v) const { - e=OutEdgeIt(*this, v); - } - void getFirst(EachNodeIt& v) const { G.getFirst(v); } - - template< typename It > - It first() const { - It e; - getFirst(e); - return e; - } - - template< typename It > - It first(NodeIt v) const { - It e; - getFirst(e, v); - return e; - } - - NodeIt tail(EdgeIt e) const { - return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); } - NodeIt head(EdgeIt e) const { - return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); } - - NodeIt aNode(OutEdgeIt e) const { - return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); } - NodeIt bNode(OutEdgeIt e) const { - return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); } - - int id(NodeIt v) const { return G.id(v); } - - template - class NodeMap { - typename Graph::NodeMap node_map; - public: - NodeMap(const ResGraph2& _G) : node_map(_G.G) { } - NodeMap(const ResGraph2& _G, S a) : node_map(_G.G, a) { } - void set(NodeIt nit, S a) { node_map.set(nit, a); } - S get(NodeIt nit) const { return node_map.get(nit); } - }; - }; - - - template - class MaxFlow { - public: - typedef typename Graph::NodeIt NodeIt; - typedef typename Graph::EdgeIt EdgeIt; - typedef typename Graph::EachEdgeIt EachEdgeIt; - typedef typename Graph::OutEdgeIt OutEdgeIt; - typedef typename Graph::InEdgeIt InEdgeIt; - - private: - const Graph* G; - NodeIt s; - NodeIt t; - FlowMap* flow; - const CapacityMap* capacity; - typedef ResGraphWrapper AugGraph; - typedef typename AugGraph::OutEdgeIt AugOutEdgeIt; - typedef typename AugGraph::EdgeIt AugEdgeIt; - - //AugGraph res_graph; - //typedef typename AugGraph::NodeMap ReachedMap; - //typename AugGraph::NodeMap pred; - //typename AugGraph::NodeMap free; - public: - MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : - G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) //, - //res_graph(G, flow, capacity), pred(res_graph), free(res_graph) - { } - bool augmentOnShortestPath() { - AugGraph res_graph(*G, *flow, *capacity); - bool _augment=false; - - typedef typename AugGraph::NodeMap ReachedMap; - BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > res_bfs(res_graph); - res_bfs.pushAndSetReached(s); - - typename AugGraph::NodeMap pred(res_graph); - //filled up with invalid iterators - //pred.set(s, AugEdgeIt()); - - typename AugGraph::NodeMap free(res_graph); - - //searching for augmenting path - while ( !res_bfs.finished() ) { - AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs); - if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) { - NodeIt v=res_graph.tail(e); - NodeIt w=res_graph.head(e); - pred.set(w, e); - if (res_graph.valid(pred.get(v))) { - free.set(w, std::min(free.get(v), res_graph.free(e))); - } else { - free.set(w, res_graph.free(e)); - } - if (res_graph.head(e)==t) { _augment=true; break; } - } - - ++res_bfs; - } //end of searching augmenting path - - if (_augment) { - NodeIt n=t; - Number augment_value=free.get(t); - while (res_graph.valid(pred.get(n))) { - AugEdgeIt e=pred.get(n); - res_graph.augment(e, augment_value); - //e.augment(augment_value); - n=res_graph.tail(e); - } - } - - return _augment; - } - - template bool augmentOnBlockingFlow() { - bool _augment=false; - - AugGraph res_graph(*G, *flow, *capacity); - - typedef typename AugGraph::NodeMap ReachedMap; - BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph); - - bfs.pushAndSetReached(s); - typename AugGraph::NodeMap dist(res_graph); //filled up with 0's - while ( !bfs.finished() ) { - AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs); - if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { - dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1); - } - - ++bfs; - } //computing distances from s in the residual graph - - MutableGraph F; - typename AugGraph::NodeMap res_graph_to_F(res_graph); - for(typename AugGraph::EachNodeIt n=res_graph.template first(); res_graph.valid(n); res_graph.next(n)) { - res_graph_to_F.set(n, F.addNode()); - } - - typename MutableGraph::NodeIt sF=res_graph_to_F.get(s); - typename MutableGraph::NodeIt tF=res_graph_to_F.get(t); - - typename MutableGraph::EdgeMap original_edge(F); - typename MutableGraph::EdgeMap residual_capacity(F); - - //Making F to the graph containing the edges of the residual graph - //which are in some shortest paths - for(typename AugGraph::EachEdgeIt e=res_graph.template first(); res_graph.valid(e); res_graph.next(e)) { - if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) { - typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e))); - original_edge.update(); - original_edge.set(f, e); - residual_capacity.update(); - residual_capacity.set(f, res_graph.free(e)); - } - } - - bool __augment=true; - - while (__augment) { - __augment=false; - //computing blocking flow with dfs - typedef typename MutableGraph::NodeMap BlockingReachedMap; - DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F); - typename MutableGraph::NodeMap pred(F); //invalid iterators - typename MutableGraph::NodeMap free(F); - - dfs.pushAndSetReached(sF); - while (!dfs.finished()) { - ++dfs; - if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) { - if (dfs.isBNodeNewlyReached()) { -// std::cout << "OutEdgeIt: " << dfs; -// std::cout << " aNode: " << F.aNode(dfs); -// std::cout << " bNode: " << F.bNode(dfs) << " "; - - typename MutableGraph::NodeIt v=F.aNode(dfs); - typename MutableGraph::NodeIt w=F.bNode(dfs); - pred.set(w, dfs); - if (F.valid(pred.get(v))) { - free.set(w, std::min(free.get(v), residual_capacity.get(dfs))); - } else { - free.set(w, residual_capacity.get(dfs)); - } - if (w==tF) { - //std::cout << "AUGMENTATION"< EAugGraph; - typedef FilterGraphWrapper< ErasingResGraphWrapper > EAugGraph; - typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt; - typedef typename EAugGraph::EdgeIt EAugEdgeIt; - - EAugGraph res_graph(*G, *flow, *capacity); - - //std::cout << "meg jo1" << std::endl; - - //typedef typename EAugGraph::NodeMap ReachedMap; - BfsIterator4< - ErasingResGraphWrapper, - ErasingResGraphWrapper::OutEdgeIt, - ErasingResGraphWrapper::NodeMap > bfs(res_graph); - - //std::cout << "meg jo2" << std::endl; - - bfs.pushAndSetReached(s); - //std::cout << "meg jo2.5" << std::endl; - - //typename EAugGraph::NodeMap dist(res_graph); //filled up with 0's - typename ErasingResGraphWrapper:: - NodeMap& dist=res_graph.dist; - //std::cout << "meg jo2.6" << std::endl; - - while ( !bfs.finished() ) { - ErasingResGraphWrapper::OutEdgeIt e=bfs; -// EAugOutEdgeIt e=/*AugOutEdgeIt*/(bfs); - //if (res_graph.valid(e)) { - // std::cout<<"a:"<(); res_graph.valid(n); res_graph.next(n)) { -// std::cout << "dist: " << dist.get(n) << std::endl; -// } - - bool __augment=true; - - while (__augment) { -// std::cout << "new iteration"<< std::endl; - - __augment=false; - //computing blocking flow with dfs - typedef typename EAugGraph::NodeMap BlockingReachedMap; - DfsIterator4< EAugGraph, EAugOutEdgeIt, BlockingReachedMap > - dfs(res_graph); - typename EAugGraph::NodeMap pred(res_graph); //invalid iterators - typename EAugGraph::NodeMap free(res_graph); - - dfs.pushAndSetReached(s); - while (!dfs.finished()) { - ++dfs; - if (res_graph.valid(EAugOutEdgeIt(dfs))) { - if (dfs.isBNodeNewlyReached()) { -// std::cout << "OutEdgeIt: " << dfs; -// std::cout << " aNode: " << res_graph.aNode(dfs); -// std::cout << " res cap: " << EAugOutEdgeIt(dfs).free(); -// std::cout << " bNode: " << res_graph.bNode(dfs) << " "; - - typename EAugGraph::NodeIt v=res_graph.aNode(dfs); - typename EAugGraph::NodeIt w=res_graph.bNode(dfs); - - pred.set(w, EAugOutEdgeIt(dfs)); - - //std::cout << EAugOutEdgeIt(dfs).free() << std::endl; - if (res_graph.valid(pred.get(v))) { - free.set(w, std::min(free.get(v), res_graph.free(/*EAugOutEdgeIt*/(dfs)))); - } else { - free.set(w, res_graph.free(/*EAugOutEdgeIt*/(dfs))); - } - - if (w==t) { -// std::cout << "t is reached, AUGMENTATION"<> "; - - res_graph.erase(dfs); - } - } - - } - - if (__augment) { - typename EAugGraph::NodeIt n=t; - Number augment_value=free.get(t); -// std::cout << "av:" << augment_value << std::endl; - while (res_graph.valid(pred.get(n))) { - EAugEdgeIt e=pred.get(n); - res_graph.augment(e, augment_value); - //e.augment(augment_value); - n=res_graph.tail(e); - if (res_graph.free(e)==0) - res_graph.erase(e); - } - } - - } - - return _augment; - } - void run() { - //int num_of_augmentations=0; - while (augmentOnShortestPath()) { - //while (augmentOnBlockingFlow()) { - //std::cout << ++num_of_augmentations << " "; - //std::cout< void run() { - //int num_of_augmentations=0; - //while (augmentOnShortestPath()) { - while (augmentOnBlockingFlow()) { - //std::cout << ++num_of_augmentations << " "; - //std::cout<getFirst(e, s); G->valid(e); G->next(e)) { - a+=flow->get(e); - } - return a; - } - }; - - -// template -// class MaxFlow2 { -// public: -// typedef typename Graph::NodeIt NodeIt; -// typedef typename Graph::EdgeIt EdgeIt; -// typedef typename Graph::EachEdgeIt EachEdgeIt; -// typedef typename Graph::OutEdgeIt OutEdgeIt; -// typedef typename Graph::InEdgeIt InEdgeIt; -// private: -// const Graph& G; -// std::list& S; -// std::list& T; -// FlowMap& flow; -// const CapacityMap& capacity; -// typedef ResGraphWrapper AugGraph; -// typedef typename AugGraph::OutEdgeIt AugOutEdgeIt; -// typedef typename AugGraph::EdgeIt AugEdgeIt; -// typename Graph::NodeMap SMap; -// typename Graph::NodeMap TMap; -// public: -// MaxFlow2(const Graph& _G, std::list& _S, std::list& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) { -// for(typename std::list::const_iterator i=S.begin(); -// i!=S.end(); ++i) { -// SMap.set(*i, true); -// } -// for (typename std::list::const_iterator i=T.begin(); -// i!=T.end(); ++i) { -// TMap.set(*i, true); -// } -// } -// bool augment() { -// AugGraph res_graph(G, flow, capacity); -// bool _augment=false; -// NodeIt reached_t_node; - -// typedef typename AugGraph::NodeMap ReachedMap; -// BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph); -// for(typename std::list::const_iterator i=S.begin(); -// i!=S.end(); ++i) { -// res_bfs.pushAndSetReached(*i); -// } -// //res_bfs.pushAndSetReached(s); - -// typename AugGraph::NodeMap pred(res_graph); -// //filled up with invalid iterators - -// typename AugGraph::NodeMap free(res_graph); - -// //searching for augmenting path -// while ( !res_bfs.finished() ) { -// AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs); -// if (e.valid() && res_bfs.isBNodeNewlyReached()) { -// NodeIt v=res_graph.tail(e); -// NodeIt w=res_graph.head(e); -// pred.set(w, e); -// if (pred.get(v).valid()) { -// free.set(w, std::min(free.get(v), e.free())); -// } else { -// free.set(w, e.free()); -// } -// if (TMap.get(res_graph.head(e))) { -// _augment=true; -// reached_t_node=res_graph.head(e); -// break; -// } -// } - -// ++res_bfs; -// } //end of searching augmenting path - -// if (_augment) { -// NodeIt n=reached_t_node; -// Number augment_value=free.get(reached_t_node); -// while (pred.get(n).valid()) { -// AugEdgeIt e=pred.get(n); -// e.augment(augment_value); -// n=res_graph.tail(e); -// } -// } - -// return _augment; -// } -// void run() { -// while (augment()) { } -// } -// Number flowValue() { -// Number a=0; -// for(typename std::list::const_iterator i=S.begin(); -// i!=S.end(); ++i) { -// for(OutEdgeIt e=G.template first(*i); e.valid(); ++e) { -// a+=flow.get(e); -// } -// for(InEdgeIt e=G.template first(*i); e.valid(); ++e) { -// a-=flow.get(e); -// } -// } -// return a; -// } -// }; - - - -} // namespace hugo - -#endif //EDMONDS_KARP_HH diff -r be43902fadb7 -r 19f3943521ab src/work/list_graph.hh --- a/src/work/list_graph.hh Sat Apr 03 14:22:33 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,552 +0,0 @@ -#ifndef LIST_GRAPH_HH -#define LIST_GRAPH_HH - -#include -#include - -namespace hugo { - - template - int count(It it) { - int i=0; - for( ; it.valid(); ++it) { ++i; } - return i; - } - - class ListGraph { - - class node_item; - class edge_item; - - public: - - class NodeIt; - class EachNodeIt; - class EdgeIt; - class EachEdgeIt; - class OutEdgeIt; - class InEdgeIt; - class SymEdgeIt; - template class NodeMap; - template class EdgeMap; - - private: - - template friend class NodeMap; - template friend class EdgeMap; - - template - class NodeMap { - const ListGraph& G; - std::vector container; - public: - typedef T ValueType; - typedef NodeIt KeyType; - NodeMap(const ListGraph& _G) : G(_G), container(G.node_id) { } - NodeMap(const ListGraph& _G, T a) : - G(_G), container(G.node_id, a) { } - void set(NodeIt n, T a) { container[/*G.id(n)*/n.node->id]=a; } - T get(NodeIt n) const { return container[/*G.id(n)*/n.node->id]; } - T& operator[](NodeIt n) { return container[/*G.id(n)*/n.node->id]; } - const T& operator[](NodeIt n) const { return container[/*G.id(n)*/n.node->id]; } - void update() { container.resize(G.node_id); } - void update(T a) { container.resize(G.node_id, a); } - }; - - template - class EdgeMap { - const ListGraph& G; - std::vector container; - public: - typedef T ValueType; - typedef EdgeIt KeyType; - EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { } - EdgeMap(const ListGraph& _G, T a) : - G(_G), container(G.edge_id, a) { } - void set(EdgeIt e, T a) { container[/*G.id(e)*/e.edge->id]=a; } - T get(EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; } - T& operator[](EdgeIt e) { return container[/*G.id(e)*/e.edge->id]; } - const T& operator[](EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; } - void update() { container.resize(G.edge_id); } - void update(T a) { container.resize(G.edge_id, a); } - }; - - int node_id; - int edge_id; - int _node_num; - int _edge_num; - - node_item* _first_node; - node_item* _last_node; - - class node_item { - friend class ListGraph; - template friend class NodeMap; - - friend class NodeIt; - friend class EachNodeIt; - friend class EdgeIt; - friend class EachEdgeIt; - friend class OutEdgeIt; - friend class InEdgeIt; - friend class SymEdgeIt; - friend std::ostream& operator<<(std::ostream& os, const NodeIt& i); - friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); - //ListGraph* G; - int id; - edge_item* _first_out_edge; - edge_item* _last_out_edge; - edge_item* _first_in_edge; - edge_item* _last_in_edge; - node_item* _next_node; - node_item* _prev_node; - public: - node_item() { } - }; - - class edge_item { - friend class ListGraph; - template friend class EdgeMap; - - friend class NodeIt; - friend class EachNodeIt; - friend class EdgeIt; - friend class EachEdgeIt; - friend class OutEdgeIt; - friend class InEdgeIt; - friend class SymEdgeIt; - friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); - //ListGraph* G; - int id; - node_item* _tail; - node_item* _head; - edge_item* _next_out; - edge_item* _prev_out; - edge_item* _next_in; - edge_item* _prev_in; - public: - edge_item() { } - }; - - node_item* _add_node() { - node_item* p=new node_item; - p->id=node_id++; - p->_first_out_edge=0; - p->_last_out_edge=0; - p->_first_in_edge=0; - p->_last_in_edge=0; - p->_prev_node=_last_node; - p->_next_node=0; - if (_last_node) _last_node->_next_node=p; - _last_node=p; - if (!_first_node) _first_node=p; - - ++_node_num; - return p; - } - - edge_item* _add_edge(node_item* _tail, node_item* _head) { - edge_item* e=new edge_item; - e->id=edge_id++; - e->_tail=_tail; - e->_head=_head; - - e->_prev_out=_tail->_last_out_edge; - if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; - _tail->_last_out_edge=e; - if (!_tail->_first_out_edge) _tail->_first_out_edge=e; - e->_next_out=0; - - e->_prev_in=_head->_last_in_edge; - if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; - _head->_last_in_edge=e; - if (!_head->_first_in_edge) { _head->_first_in_edge=e; } - e->_next_in=0; - - ++_edge_num; - return e; - } - - //deletes a node which has no out edge and no in edge - void _delete_node(node_item* v) { - if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else - _last_node=v->_prev_node; - if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else - _first_node=v->_next_node; - - delete v; - --_node_num; - } - - void _delete_edge(edge_item* e) { - if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else - (e->_tail)->_last_out_edge=e->_prev_out; - if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else - (e->_tail)->_first_out_edge=e->_next_out; - if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else - (e->_head)->_last_in_edge=e->_prev_in; - if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else - (e->_head)->_first_in_edge=e->_next_in; - - delete e; - --_edge_num; - } - - void _set_tail(edge_item* e, node_item* _tail) { - if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else - (e->_tail)->_last_out_edge=e->_prev_out; - if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else - (e->_tail)->_first_out_edge=e->_next_out; - - e->_tail=_tail; - - e->_prev_out=_tail->_last_out_edge; - if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; - _tail->_last_out_edge=e; - if (!_tail->_first_out_edge) _tail->_first_out_edge=e; - e->_next_out=0; - } - - void _set_head(edge_item* e, node_item* _head) { - if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else - (e->_head)->_last_in_edge=e->_prev_in; - if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else - (e->_head)->_first_in_edge=e->_next_in; - - e->_head=_head; - - e->_prev_in=_head->_last_in_edge; - if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; - _head->_last_in_edge=e; - if (!_head->_first_in_edge) { _head->_first_in_edge=e; } - e->_next_in=0; - } - - public: - - /* default constructor */ - - ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { } - - ~ListGraph() { - while (first().valid()) erase(first()); - } - - int nodeNum() const { return _node_num; } - int edgeNum() const { return _edge_num; } - - /* functions to construct iterators from the graph, or from each other */ - - //EachNodeIt firstNode() const { return EachNodeIt(*this); } - //EachEdgeIt firstEdge() const { return EachEdgeIt(*this); } - - //OutEdgeIt firstOutEdge(const NodeIt v) const { return OutEdgeIt(v); } - //InEdgeIt firstInEdge(const NodeIt v) const { return InEdgeIt(v); } - //SymEdgeIt firstSymEdge(const NodeIt v) const { return SymEdgeIt(v); } - NodeIt tail(EdgeIt e) const { return e.tailNode(); } - NodeIt head(EdgeIt e) const { return e.headNode(); } - - NodeIt aNode(const OutEdgeIt& e) const { return e.aNode(); } - NodeIt aNode(const InEdgeIt& e) const { return e.aNode(); } - NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); } - - NodeIt bNode(const OutEdgeIt& e) const { return e.bNode(); } - NodeIt bNode(const InEdgeIt& e) const { return e.bNode(); } - NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); } - - //NodeIt invalid_node() { return NodeIt(); } - //EdgeIt invalid_edge() { return EdgeIt(); } - //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); } - //InEdgeIt invalid_in_edge() { return InEdgeIt(); } - //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); } - - /* same methods in other style */ - /* for experimental purpose */ - - EachNodeIt& getFirst(EachNodeIt& v) const { - v=EachNodeIt(*this); return v; } - EachEdgeIt& getFirst(EachEdgeIt& e) const { - e=EachEdgeIt(*this); return e; } - OutEdgeIt& getFirst(OutEdgeIt& e, NodeIt v) const { - e=OutEdgeIt(v); return e; } - InEdgeIt& getFirst(InEdgeIt& e, NodeIt v) const { - e=InEdgeIt(v); return e; } - SymEdgeIt& getFirst(SymEdgeIt& e, NodeIt v) const { - e=SymEdgeIt(v); return e; } - //void getTail(NodeIt& n, const EdgeIt& e) const { n=tail(e); } - //void getHead(NodeIt& n, const EdgeIt& e) const { n=head(e); } - - //void getANode(NodeIt& n, const OutEdgeIt& e) const { n=e.aNode(); } - //void getANode(NodeIt& n, const InEdgeIt& e) const { n=e.aNode(); } - //void getANode(NodeIt& n, const SymEdgeIt& e) const { n=e.aNode(); } - //void getBNode(NodeIt& n, const OutEdgeIt& e) const { n=e.bNode(); } - //void getBNode(NodeIt& n, const InEdgeIt& e) const { n=e.bNode(); } - //void getBNode(NodeIt& n, const SymEdgeIt& e) const { n=e.bNode(); } - //void get_invalid(NodeIt& n) { n=NodeIt(); } - //void get_invalid(EdgeIt& e) { e=EdgeIt(); } - //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); } - //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); } - //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); } - - template< typename It > - It first() const { - It e; - getFirst(e); - return e; - } - - template< typename It > - It first(NodeIt v) const { - It e; - getFirst(e, v); - return e; - } - - bool valid(NodeIt n) const { return n.valid(); } - bool valid(EdgeIt e) const { return e.valid(); } - - template It getNext(It it) const { - It tmp(it); return next(tmp); } - template It& next(It& it) const { return ++it; } - - - /* for getting id's of graph objects */ - /* these are important for the implementation of property vectors */ - - int id(NodeIt v) const { return v.node->id; } - int id(EdgeIt e) const { return e.edge->id; } - - /* adding nodes and edges */ - - NodeIt addNode() { return NodeIt(_add_node()); } - EdgeIt addEdge(NodeIt u, NodeIt v) { - return EdgeIt(_add_edge(u.node, v.node)); - } - - void erase(NodeIt i) { - while (first(i).valid()) erase(first(i)); - while (first(i).valid()) erase(first(i)); - _delete_node(i.node); - } - - void erase(EdgeIt e) { _delete_edge(e.edge); } - - void clear() { - while (first().valid()) erase(first()); - } - - void setTail(EdgeIt e, NodeIt tail) { - _set_tail(e.edge, tail.node); - } - - void setHead(EdgeIt e, NodeIt head) { - _set_head(e.edge, head.node); - } - - /* stream operations, for testing purpose */ - - friend std::ostream& operator<<(std::ostream& os, const NodeIt& i) { - os << i.node->id; return os; - } - friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i) { - os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; - return os; - } - - class NodeIt { - friend class ListGraph; - template friend class NodeMap; - - friend class EdgeIt; - friend class OutEdgeIt; - friend class InEdgeIt; - friend class SymEdgeIt; - //public: //FIXME: It is required by op= of EachNodeIt - protected: - node_item* node; - protected: - friend int ListGraph::id(NodeIt v) const; - public: - NodeIt() : node(0) { } - NodeIt(node_item* _node) : node(_node) { } - bool valid() const { return (node!=0); } - //void makeInvalid() { node=0; } - friend bool operator==(const NodeIt& u, const NodeIt& v) { - return v.node==u.node; - } - friend bool operator!=(const NodeIt& u, const NodeIt& v) { - return v.node!=u.node; - } - friend std::ostream& operator<<(std::ostream& os, const NodeIt& i); - }; - - class EachNodeIt : public NodeIt { - friend class ListGraph; - //protected: - public: //for everybody but marci - EachNodeIt(const ListGraph& G) : NodeIt(G._first_node) { } - public: - EachNodeIt() : NodeIt() { } - EachNodeIt(node_item* v) : NodeIt(v) { } - EachNodeIt& operator++() { node=node->_next_node; return *this; } - //FIXME:: - // EachNodeIt& operator=(const NodeIt& e) - // { node=e.node; return *this; } - }; - - class EdgeIt { - friend class ListGraph; - template friend class EdgeMap; - - friend class NodeIt; - friend class EachNodeIt; - protected: - edge_item* edge; - friend int ListGraph::id(EdgeIt e) const; - public: - EdgeIt() : edge(0) { } - //EdgeIt() { } - EdgeIt(edge_item* _edge) : edge(_edge) { } - bool valid() const { return (edge!=0); } - //void makeInvalid() { edge=0; } - friend bool operator==(const EdgeIt& u, const EdgeIt& v) { - return v.edge==u.edge; - } - friend bool operator!=(const EdgeIt& u, const EdgeIt& v) { - return v.edge!=u.edge; - } - protected: - NodeIt tailNode() const { return NodeIt(edge->_tail); } - NodeIt headNode() const { return NodeIt(edge->_head); } - public: - friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); - }; - - class EachEdgeIt : public EdgeIt { - friend class ListGraph; - //protected: - public: //for alpar - EachEdgeIt(const ListGraph& G) { - node_item* v=G._first_node; - if (v) edge=v->_first_out_edge; else edge=0; - while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } - } - public: - EachEdgeIt() : EdgeIt() { } - EachEdgeIt(edge_item* _e) : EdgeIt(_e) { } - EachEdgeIt& operator++() { - node_item* v=edge->_tail; - edge=edge->_next_out; - while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } - return *this; - } - }; - - class OutEdgeIt : public EdgeIt { - friend class ListGraph; - //node_item* v; - //protected: - public: //for alpar - OutEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; } - public: - OutEdgeIt() : EdgeIt()/*, v(0)*/ { } - OutEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; } - OutEdgeIt& operator++() { edge=edge->_next_out; return *this; } - protected: - NodeIt aNode() const { return NodeIt(edge->_tail); } - NodeIt bNode() const { return NodeIt(edge->_head); } - }; - - class InEdgeIt : public EdgeIt { - friend class ListGraph; - //node_item* v; - //protected: - public: //for alpar - InEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; } - public: - InEdgeIt() : EdgeIt()/*, v(0)*/ { } - InEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; } - InEdgeIt& operator++() { edge=edge->_next_in; return *this; } - protected: - NodeIt aNode() const { return NodeIt(edge->_head); } - NodeIt bNode() const { return NodeIt(edge->_tail); } - }; - - class SymEdgeIt : public EdgeIt { - friend class ListGraph; - bool out_or_in; //1 iff out, 0 iff in - //node_item* v; - //protected: - public: //for alpar - SymEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { - out_or_in=1; - edge=_v.node->_first_out_edge; - if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; } - } - public: - SymEdgeIt() : EdgeIt() /*, v(0)*/ { } - SymEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { - out_or_in=1; - edge=_v.node->_first_out_edge; - if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; } - } - SymEdgeIt& operator++() { - if (out_or_in) { - node_item* v=edge->_tail; - edge=edge->_next_out; - if (!edge) { out_or_in=0; edge=v->_first_in_edge; } - } else { - edge=edge->_next_in; - } - return *this; - } - protected: - NodeIt aNode() const { - return (out_or_in) ? NodeIt(edge->_tail) : NodeIt(edge->_head); } - NodeIt bNode() const { - return (out_or_in) ? NodeIt(edge->_head) : NodeIt(edge->_tail); } - }; - - }; - -// template< typename T > -// T ListGraph::first() const { -// std::cerr << "Invalid use of template T ListGraph::first();" << std::endl; -// return T(); -// } - -// template<> -// ListGraph::EachNodeIt ListGraph::first() const { -// return firstNode(); -// } - -// template<> -// ListGraph::EachEdgeIt ListGraph::first() const { -// return firstEdge(); -// } - -// template< typename T > -// T ListGraph::first(ListGraph::NodeIt v) const { -// std::cerr << "Invalid use of template T ListGraph::first(ListGRaph::NodeIt);" << std::endl; -// return T(); -// } - -// template<> -// ListGraph::OutEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { -// return firstOutEdge(v); -// } - -// template<> -// ListGraph::InEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { -// return firstInEdge(v); -// } - -// template<> -// ListGraph::SymEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { -// return firstSymEdge(v); -// } - - -} //namespace hugo - -#endif //LIST_GRAPH_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci/oldies/bfs_iterator.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/oldies/bfs_iterator.hh Sat Apr 03 14:41:31 2004 +0000 @@ -0,0 +1,836 @@ +#ifndef BFS_ITERATOR_HH +#define BFS_ITERATOR_HH + +#include +#include +#include +#include + +namespace hugo { + + template + struct bfs { + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::EdgeIt EdgeIt; + typedef typename Graph::EachNodeIt EachNodeIt; + typedef typename Graph::OutEdgeIt OutEdgeIt; + Graph& G; + NodeIt s; + typename Graph::NodeMap reached; + typename Graph::NodeMap pred; + typename Graph::NodeMap dist; + std::queue bfs_queue; + bfs(Graph& _G, NodeIt _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) { + bfs_queue.push(s); + for(EachNodeIt i=G.template first(); i.valid(); ++i) + reached.set(i, false); + reached.set(s, true); + dist.set(s, 0); + } + + void run() { + while (!bfs_queue.empty()) { + NodeIt v=bfs_queue.front(); + OutEdgeIt e=G.template first(v); + bfs_queue.pop(); + for( ; e.valid(); ++e) { + NodeIt w=G.bNode(e); + std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl; + if (!reached.get(w)) { + std::cout << G.id(w) << " is newly reached :-)" << std::endl; + bfs_queue.push(w); + dist.set(w, dist.get(v)+1); + pred.set(w, e); + reached.set(w, true); + } else { + std::cout << G.id(w) << " is already reached" << std::endl; + } + } + } + } + }; + + template + struct bfs_visitor { + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::EdgeIt EdgeIt; + typedef typename Graph::OutEdgeIt OutEdgeIt; + Graph& G; + bfs_visitor(Graph& _G) : G(_G) { } + void at_previously_reached(OutEdgeIt& e) { + //NodeIt v=G.aNode(e); + NodeIt w=G.bNode(e); + std::cout << G.id(w) << " is already reached" << std::endl; + } + void at_newly_reached(OutEdgeIt& e) { + //NodeIt v=G.aNode(e); + NodeIt w=G.bNode(e); + std::cout << G.id(w) << " is newly reached :-)" << std::endl; + } + }; + + template + struct bfs_iterator { + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::EdgeIt EdgeIt; + typedef typename Graph::OutEdgeIt OutEdgeIt; + Graph& G; + std::queue& bfs_queue; + ReachedMap& reached; + visitor_type& visitor; + void process() { + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + if (bfs_queue.empty()) return; + OutEdgeIt e=bfs_queue.front(); + //NodeIt v=G.aNode(e); + NodeIt w=G.bNode(e); + if (!reached.get(w)) { + visitor.at_newly_reached(e); + bfs_queue.push(G.template first(w)); + reached.set(w, true); + } else { + visitor.at_previously_reached(e); + } + } + bfs_iterator(Graph& _G, std::queue& _bfs_queue, ReachedMap& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) { + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + valid(); + } + bfs_iterator& operator++() { + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + //if (bfs_queue.empty()) return *this; + if (!valid()) return *this; + ++(bfs_queue.front()); + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + valid(); + return *this; + } + //void next() { + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + // if (bfs_queue.empty()) return; + // ++(bfs_queue.front()); + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + //} + bool valid() { + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + if (bfs_queue.empty()) return false; else return true; + } + //bool finished() { + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + // if (bfs_queue.empty()) return true; else return false; + //} + operator EdgeIt () { return bfs_queue.front(); } + + }; + + template + struct bfs_iterator1 { + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::EdgeIt EdgeIt; + typedef typename Graph::OutEdgeIt OutEdgeIt; + Graph& G; + std::queue& bfs_queue; + ReachedMap& reached; + bool _newly_reached; + bfs_iterator1(Graph& _G, std::queue& _bfs_queue, ReachedMap& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) { + valid(); + if (!bfs_queue.empty() && bfs_queue.front().valid()) { + OutEdgeIt e=bfs_queue.front(); + NodeIt w=G.bNode(e); + if (!reached.get(w)) { + bfs_queue.push(G.template first(w)); + reached.set(w, true); + _newly_reached=true; + } else { + _newly_reached=false; + } + } + } + bfs_iterator1& operator++() { + if (!valid()) return *this; + ++(bfs_queue.front()); + valid(); + if (!bfs_queue.empty() && bfs_queue.front().valid()) { + OutEdgeIt e=bfs_queue.front(); + NodeIt w=G.bNode(e); + if (!reached.get(w)) { + bfs_queue.push(G.template first(w)); + reached.set(w, true); + _newly_reached=true; + } else { + _newly_reached=false; + } + } + return *this; + } + bool valid() { + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + if (bfs_queue.empty()) return false; else return true; + } + operator OutEdgeIt() { return bfs_queue.front(); } + //ize + bool newly_reached() { return _newly_reached; } + + }; + + template + struct BfsIterator { + typedef typename Graph::NodeIt NodeIt; + Graph& G; + std::queue& bfs_queue; + ReachedMap& reached; + bool b_node_newly_reached; + OutEdgeIt actual_edge; + BfsIterator(Graph& _G, + std::queue& _bfs_queue, + ReachedMap& _reached) : + G(_G), bfs_queue(_bfs_queue), reached(_reached) { + actual_edge=bfs_queue.front(); + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + if (!reached.get(w)) { + bfs_queue.push(G.firstOutEdge(w)); + reached.set(w, true); + b_node_newly_reached=true; + } else { + b_node_newly_reached=false; + } + } + } + BfsIterator& + operator++() { + if (bfs_queue.front().valid()) { + ++(bfs_queue.front()); + actual_edge=bfs_queue.front(); + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + if (!reached.get(w)) { + bfs_queue.push(G.firstOutEdge(w)); + reached.set(w, true); + b_node_newly_reached=true; + } else { + b_node_newly_reached=false; + } + } + } else { + bfs_queue.pop(); + actual_edge=bfs_queue.front(); + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + if (!reached.get(w)) { + bfs_queue.push(G.firstOutEdge(w)); + reached.set(w, true); + b_node_newly_reached=true; + } else { + b_node_newly_reached=false; + } + } + } + return *this; + } + bool finished() { return bfs_queue.empty(); } + operator OutEdgeIt () { return actual_edge; } + bool bNodeIsNewlyReached() { return b_node_newly_reached; } + bool aNodeIsExamined() { return !(actual_edge.valid()); } + }; + + + template + struct DfsIterator { + typedef typename Graph::NodeIt NodeIt; + Graph& G; + std::stack& bfs_queue; + ReachedMap& reached; + bool b_node_newly_reached; + OutEdgeIt actual_edge; + DfsIterator(Graph& _G, + std::stack& _bfs_queue, + ReachedMap& _reached) : + G(_G), 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(G.firstOutEdge(w)); + reached.set(w, true); + b_node_newly_reached=true; + } else { + ++(bfs_queue.top()); + b_node_newly_reached=false; + } + } else { + bfs_queue.pop(); + } + } + DfsIterator& + operator++() { + actual_edge=bfs_queue.top(); + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + if (!reached.get(w)) { + bfs_queue.push(G.firstOutEdge(w)); + reached.set(w, true); + b_node_newly_reached=true; + } else { + ++(bfs_queue.top()); + b_node_newly_reached=false; + } + } else { + bfs_queue.pop(); + } + return *this; + } + bool finished() { return bfs_queue.empty(); } + operator OutEdgeIt () { return actual_edge; } + bool bNodeIsNewlyReached() { return b_node_newly_reached; } + bool aNodeIsExamined() { return !(actual_edge.valid()); } + }; + + template + struct BfsIterator1 { + typedef typename Graph::NodeIt NodeIt; + Graph& G; + std::queue& bfs_queue; + ReachedMap& reached; + bool b_node_newly_reached; + OutEdgeIt actual_edge; + BfsIterator1(Graph& _G, + std::queue& _bfs_queue, + ReachedMap& _reached) : + G(_G), bfs_queue(_bfs_queue), reached(_reached) { + actual_edge=bfs_queue.front(); + 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 { + b_node_newly_reached=false; + } + } + } + void next() { + if (bfs_queue.front().valid()) { + ++(bfs_queue.front()); + actual_edge=bfs_queue.front(); + 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 { + b_node_newly_reached=false; + } + } + } else { + bfs_queue.pop(); + actual_edge=bfs_queue.front(); + 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 { + b_node_newly_reached=false; + } + } + } + //return *this; + } + bool finished() { return bfs_queue.empty(); } + operator OutEdgeIt () { return actual_edge; } + bool bNodeIsNewlyReached() { return b_node_newly_reached; } + bool aNodeIsExamined() { return !(actual_edge.valid()); } + }; + + + template + struct DfsIterator1 { + typedef typename Graph::NodeIt NodeIt; + Graph& G; + std::stack& bfs_queue; + ReachedMap& reached; + bool b_node_newly_reached; + OutEdgeIt actual_edge; + DfsIterator1(Graph& _G, + std::stack& _bfs_queue, + ReachedMap& _reached) : + G(_G), 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 next() { + 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(); + } + //return *this; + } + bool finished() { return bfs_queue.empty(); } + operator OutEdgeIt () { return actual_edge; } + bool bNodeIsNewlyReached() { return b_node_newly_reached; } + bool aNodeIsLeaved() { return !(actual_edge.valid()); } + }; + + template + class BfsIterator2 { + typedef typename Graph::NodeIt NodeIt; + const Graph& G; + std::queue bfs_queue; + ReachedMap reached; + bool b_node_newly_reached; + OutEdgeIt actual_edge; + public: + BfsIterator2(const Graph& _G) : G(_G), reached(G, false) { } + void pushAndSetReached(NodeIt s) { + reached.set(s, true); + if (bfs_queue.empty()) { + bfs_queue.push(G.template first(s)); + actual_edge=bfs_queue.front(); + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + if (!reached.get(w)) { + bfs_queue.push(G.template first(w)); + reached.set(w, true); + b_node_newly_reached=true; + } else { + b_node_newly_reached=false; + } + } //else { + //} + } else { + bfs_queue.push(G.template first(s)); + } + } + BfsIterator2& + operator++() { + if (bfs_queue.front().valid()) { + ++(bfs_queue.front()); + actual_edge=bfs_queue.front(); + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + if (!reached.get(w)) { + bfs_queue.push(G.template first(w)); + reached.set(w, true); + b_node_newly_reached=true; + } else { + b_node_newly_reached=false; + } + } + } else { + bfs_queue.pop(); + if (!bfs_queue.empty()) { + actual_edge=bfs_queue.front(); + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + if (!reached.get(w)) { + bfs_queue.push(G.template first(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 !(actual_edge.valid()); } + const ReachedMap& getReachedMap() const { return reached; } + const std::queue& getBfsQueue() const { return bfs_queue; } + }; + + + template + class BfsIterator3 { + typedef typename Graph::NodeIt NodeIt; + const Graph& G; + std::queue< std::pair > bfs_queue; + ReachedMap reached; + bool b_node_newly_reached; + OutEdgeIt actual_edge; + public: + BfsIterator3(const Graph& _G) : G(_G), reached(G, false) { } + void pushAndSetReached(NodeIt s) { + reached.set(s, true); + if (bfs_queue.empty()) { + bfs_queue.push(std::pair(s, G.template first(s))); + actual_edge=bfs_queue.front().second; + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + if (!reached.get(w)) { + bfs_queue.push(std::pair(w, G.template first(w))); + reached.set(w, true); + b_node_newly_reached=true; + } else { + b_node_newly_reached=false; + } + } //else { + //} + } else { + bfs_queue.push(std::pair(s, G.template first(s))); + } + } + BfsIterator3& + operator++() { + if (bfs_queue.front().second.valid()) { + ++(bfs_queue.front().second); + actual_edge=bfs_queue.front().second; + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + if (!reached.get(w)) { + bfs_queue.push(std::pair(w, G.template first(w))); + reached.set(w, true); + b_node_newly_reached=true; + } else { + b_node_newly_reached=false; + } + } + } else { + bfs_queue.pop(); + if (!bfs_queue.empty()) { + actual_edge=bfs_queue.front().second; + if (actual_edge.valid()) { + NodeIt w=G.bNode(actual_edge); + if (!reached.get(w)) { + bfs_queue.push(std::pair(w, G.template first(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 !(actual_edge.valid()); } + NodeIt aNode() const { return bfs_queue.front().first; } + NodeIt bNode() const { return G.bNode(actual_edge); } + const ReachedMap& getReachedMap() const { return reached; } + //const std::queue< std::pair >& getBfsQueue() const { return bfs_queue; } + }; + + + template */ > + class BfsIterator4 { + typedef typename Graph::NodeIt NodeIt; + const Graph& G; + std::queue bfs_queue; + ReachedMap& reached; + bool b_node_newly_reached; + OutEdgeIt actual_edge; + bool own_reached_map; + public: + BfsIterator4(const Graph& _G, ReachedMap& _reached) : + G(_G), reached(_reached), + own_reached_map(false) { } + BfsIterator4(const Graph& _G) : + G(_G), reached(*(new ReachedMap(G /*, false*/))), + own_reached_map(true) { } + ~BfsIterator4() { if (own_reached_map) delete &reached; } + void pushAndSetReached(NodeIt s) { + //std::cout << "mimi" << &reached << std::endl; + reached.set(s, true); + //std::cout << "mumus" << std::endl; + if (bfs_queue.empty()) { + //std::cout << "bibi1" << std::endl; + bfs_queue.push(s); + //std::cout << "zizi" << std::endl; + G.getFirst(actual_edge, s); + //std::cout << "kiki" << std::endl; + 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 { + //std::cout << "bibi2" << std::endl; + bfs_queue.push(s); + } + } + BfsIterator4& + 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& getBfsQueue() const { return bfs_queue; } + }; + + + template */ > + class BfsIterator5 { + typedef typename GraphWrapper::NodeIt NodeIt; + typedef typename GraphWrapper::OutEdgeIt OutEdgeIt; + GraphWrapper G; + std::queue 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(const typename GraphWrapper::BaseGraph& _G, +// ReachedMap& _reached) : +// G(_G), reached(_reached), +// own_reached_map(false) { } +// BfsIterator5(const typename GraphWrapper::BaseGraph& _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& + 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& getBfsQueue() const { return bfs_queue; } + }; + + template */ > + class DfsIterator4 { + typedef typename Graph::NodeIt NodeIt; + const Graph& G; + std::stack dfs_stack; + bool b_node_newly_reached; + OutEdgeIt actual_edge; + NodeIt actual_node; + ReachedMap& reached; + bool own_reached_map; + public: + DfsIterator4(const Graph& _G, ReachedMap& _reached) : + G(_G), reached(_reached), + own_reached_map(false) { } + DfsIterator4(const Graph& _G) : + G(_G), reached(*(new ReachedMap(G /*, false*/))), + own_reached_map(true) { } + ~DfsIterator4() { if (own_reached_map) delete &reached; } + void pushAndSetReached(NodeIt s) { + actual_node=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 (G.valid(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 { + 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 !(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::stack& getDfsStack() const { return dfs_stack; } + }; + + template */ > + class DfsIterator5 { + typedef typename GraphWrapper::NodeIt NodeIt; + typedef typename GraphWrapper::OutEdgeIt OutEdgeIt; + GraphWrapper G; + std::stack dfs_stack; + bool b_node_newly_reached; + OutEdgeIt actual_edge; + NodeIt actual_node; + ReachedMap& reached; + bool own_reached_map; + public: + 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); + dfs_stack.push(G.template first(s)); + } + DfsIterator5& + operator++() { + 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(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 !(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::stack& getDfsStack() const { return dfs_stack; } + }; + + + +} // namespace hugo + +#endif //BFS_ITERATOR_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci/oldies/edmonds_karp.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/oldies/edmonds_karp.hh Sat Apr 03 14:41:31 2004 +0000 @@ -0,0 +1,677 @@ +#ifndef EDMONDS_KARP_HH +#define EDMONDS_KARP_HH + +#include +#include +#include + +#include +//#include + +namespace hugo { + + template + class ResGraph { + public: + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::EachNodeIt EachNodeIt; + private: + typedef typename Graph::SymEdgeIt OldSymEdgeIt; + const Graph& G; + FlowMap& flow; + const CapacityMap& capacity; + public: + ResGraph(const Graph& _G, FlowMap& _flow, + const CapacityMap& _capacity) : + G(_G), flow(_flow), capacity(_capacity) { } + + class EdgeIt; + class OutEdgeIt; + friend class EdgeIt; + friend class OutEdgeIt; + + class EdgeIt { + friend class ResGraph; + protected: + const ResGraph* resG; + OldSymEdgeIt sym; + public: + EdgeIt() { } + //EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { } + Number free() const { + if (resG->G.aNode(sym)==resG->G.tail(sym)) { + return (resG->capacity.get(sym)-resG->flow.get(sym)); + } else { + return (resG->flow.get(sym)); + } + } + bool valid() const { return sym.valid(); } + void augment(Number a) const { + if (resG->G.aNode(sym)==resG->G.tail(sym)) { + resG->flow.set(sym, resG->flow.get(sym)+a); + //resG->flow[sym]+=a; + } else { + resG->flow.set(sym, resG->flow.get(sym)-a); + //resG->flow[sym]-=a; + } + } + }; + + class OutEdgeIt : public EdgeIt { + friend class ResGraph; + public: + OutEdgeIt() { } + //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; } + private: + OutEdgeIt(const ResGraph& _resG, NodeIt v) { + resG=&_resG; + sym=resG->G.template first(v); + while( sym.valid() && !(free()>0) ) { ++sym; } + } + public: + OutEdgeIt& operator++() { + ++sym; + while( sym.valid() && !(free()>0) ) { ++sym; } + return *this; + } + }; + + void getFirst(OutEdgeIt& e, NodeIt v) const { + e=OutEdgeIt(*this, v); + } + void getFirst(EachNodeIt& v) const { G.getFirst(v); } + + template< typename It > + It first() const { + It e; + getFirst(e); + return e; + } + + template< typename It > + It first(NodeIt v) const { + It e; + getFirst(e, v); + return e; + } + + NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); } + NodeIt head(EdgeIt e) const { return G.bNode(e.sym); } + + NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); } + NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); } + + int id(NodeIt v) const { return G.id(v); } + + template + class NodeMap { + typename Graph::NodeMap node_map; + public: + NodeMap(const ResGraph& _G) : node_map(_G.G) { } + NodeMap(const ResGraph& _G, S a) : node_map(_G.G, a) { } + void set(NodeIt nit, S a) { node_map.set(nit, a); } + S get(NodeIt nit) const { return node_map.get(nit); } + S& operator[](NodeIt nit) { return node_map[nit]; } + const S& operator[](NodeIt nit) const { return node_map[nit]; } + }; + + }; + + + template + class ResGraph2 { + public: + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::EachNodeIt EachNodeIt; + private: + //typedef typename Graph::SymEdgeIt OldSymEdgeIt; + typedef typename Graph::OutEdgeIt OldOutEdgeIt; + typedef typename Graph::InEdgeIt OldInEdgeIt; + + const Graph& G; + FlowMap& flow; + const CapacityMap& capacity; + public: + ResGraph2(const Graph& _G, FlowMap& _flow, + const CapacityMap& _capacity) : + G(_G), flow(_flow), capacity(_capacity) { } + + class EdgeIt; + class OutEdgeIt; + friend class EdgeIt; + friend class OutEdgeIt; + + class EdgeIt { + friend class ResGraph2; + protected: + const ResGraph2* resG; + //OldSymEdgeIt sym; + OldOutEdgeIt out; + OldInEdgeIt in; + bool out_or_in; //true, iff out + public: + EdgeIt() : out_or_in(true) { } + Number free() const { + if (out_or_in) { + return (resG->capacity.get(out)-resG->flow.get(out)); + } else { + return (resG->flow.get(in)); + } + } + bool valid() const { + return out_or_in && out.valid() || in.valid(); } + void augment(Number a) const { + if (out_or_in) { + resG->flow.set(out, resG->flow.get(out)+a); + } else { + resG->flow.set(in, resG->flow.get(in)-a); + } + } + }; + + class OutEdgeIt : public EdgeIt { + friend class ResGraph2; + public: + OutEdgeIt() { } + private: + OutEdgeIt(const ResGraph2& _resG, NodeIt v) { + resG=&_resG; + out=resG->G.template first(v); + while( out.valid() && !(free()>0) ) { ++out; } + if (!out.valid()) { + out_or_in=0; + in=resG->G.template first(v); + while( in.valid() && !(free()>0) ) { ++in; } + } + } + public: + OutEdgeIt& operator++() { + if (out_or_in) { + NodeIt v=resG->G.aNode(out); + ++out; + while( out.valid() && !(free()>0) ) { ++out; } + if (!out.valid()) { + out_or_in=0; + in=resG->G.template first(v); + while( in.valid() && !(free()>0) ) { ++in; } + } + } else { + ++in; + while( in.valid() && !(free()>0) ) { ++in; } + } + return *this; + } + }; + + void getFirst(OutEdgeIt& e, NodeIt v) const { + e=OutEdgeIt(*this, v); + } + void getFirst(EachNodeIt& v) const { G.getFirst(v); } + + template< typename It > + It first() const { + It e; + getFirst(e); + return e; + } + + template< typename It > + It first(NodeIt v) const { + It e; + getFirst(e, v); + return e; + } + + NodeIt tail(EdgeIt e) const { + return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); } + NodeIt head(EdgeIt e) const { + return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); } + + NodeIt aNode(OutEdgeIt e) const { + return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); } + NodeIt bNode(OutEdgeIt e) const { + return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); } + + int id(NodeIt v) const { return G.id(v); } + + template + class NodeMap { + typename Graph::NodeMap node_map; + public: + NodeMap(const ResGraph2& _G) : node_map(_G.G) { } + NodeMap(const ResGraph2& _G, S a) : node_map(_G.G, a) { } + void set(NodeIt nit, S a) { node_map.set(nit, a); } + S get(NodeIt nit) const { return node_map.get(nit); } + }; + }; + + + template + class MaxFlow { + public: + typedef typename Graph::NodeIt NodeIt; + typedef typename Graph::EdgeIt EdgeIt; + typedef typename Graph::EachEdgeIt EachEdgeIt; + typedef typename Graph::OutEdgeIt OutEdgeIt; + typedef typename Graph::InEdgeIt InEdgeIt; + + private: + const Graph* G; + NodeIt s; + NodeIt t; + FlowMap* flow; + const CapacityMap* capacity; + typedef ResGraphWrapper AugGraph; + typedef typename AugGraph::OutEdgeIt AugOutEdgeIt; + typedef typename AugGraph::EdgeIt AugEdgeIt; + + //AugGraph res_graph; + //typedef typename AugGraph::NodeMap ReachedMap; + //typename AugGraph::NodeMap pred; + //typename AugGraph::NodeMap free; + public: + MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) : + G(&_G), s(_s), t(_t), flow(&_flow), capacity(&_capacity) //, + //res_graph(G, flow, capacity), pred(res_graph), free(res_graph) + { } + bool augmentOnShortestPath() { + AugGraph res_graph(*G, *flow, *capacity); + bool _augment=false; + + typedef typename AugGraph::NodeMap ReachedMap; + BfsIterator5< AugGraph, /*AugOutEdgeIt,*/ ReachedMap > res_bfs(res_graph); + res_bfs.pushAndSetReached(s); + + typename AugGraph::NodeMap pred(res_graph); + //filled up with invalid iterators + //pred.set(s, AugEdgeIt()); + + typename AugGraph::NodeMap free(res_graph); + + //searching for augmenting path + while ( !res_bfs.finished() ) { + AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs); + if (res_graph.valid(e) && res_bfs.isBNodeNewlyReached()) { + NodeIt v=res_graph.tail(e); + NodeIt w=res_graph.head(e); + pred.set(w, e); + if (res_graph.valid(pred.get(v))) { + free.set(w, std::min(free.get(v), res_graph.free(e))); + } else { + free.set(w, res_graph.free(e)); + } + if (res_graph.head(e)==t) { _augment=true; break; } + } + + ++res_bfs; + } //end of searching augmenting path + + if (_augment) { + NodeIt n=t; + Number augment_value=free.get(t); + while (res_graph.valid(pred.get(n))) { + AugEdgeIt e=pred.get(n); + res_graph.augment(e, augment_value); + //e.augment(augment_value); + n=res_graph.tail(e); + } + } + + return _augment; + } + + template bool augmentOnBlockingFlow() { + bool _augment=false; + + AugGraph res_graph(*G, *flow, *capacity); + + typedef typename AugGraph::NodeMap ReachedMap; + BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > bfs(res_graph); + + bfs.pushAndSetReached(s); + typename AugGraph::NodeMap dist(res_graph); //filled up with 0's + while ( !bfs.finished() ) { + AugOutEdgeIt e=/*AugOutEdgeIt*/(bfs); + if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) { + dist.set(res_graph.head(e), dist.get(res_graph.tail(e))+1); + } + + ++bfs; + } //computing distances from s in the residual graph + + MutableGraph F; + typename AugGraph::NodeMap res_graph_to_F(res_graph); + for(typename AugGraph::EachNodeIt n=res_graph.template first(); res_graph.valid(n); res_graph.next(n)) { + res_graph_to_F.set(n, F.addNode()); + } + + typename MutableGraph::NodeIt sF=res_graph_to_F.get(s); + typename MutableGraph::NodeIt tF=res_graph_to_F.get(t); + + typename MutableGraph::EdgeMap original_edge(F); + typename MutableGraph::EdgeMap residual_capacity(F); + + //Making F to the graph containing the edges of the residual graph + //which are in some shortest paths + for(typename AugGraph::EachEdgeIt e=res_graph.template first(); res_graph.valid(e); res_graph.next(e)) { + if (dist.get(res_graph.head(e))==dist.get(res_graph.tail(e))+1) { + typename MutableGraph::EdgeIt f=F.addEdge(res_graph_to_F.get(res_graph.tail(e)), res_graph_to_F.get(res_graph.head(e))); + original_edge.update(); + original_edge.set(f, e); + residual_capacity.update(); + residual_capacity.set(f, res_graph.free(e)); + } + } + + bool __augment=true; + + while (__augment) { + __augment=false; + //computing blocking flow with dfs + typedef typename MutableGraph::NodeMap BlockingReachedMap; + DfsIterator4< MutableGraph, typename MutableGraph::OutEdgeIt, BlockingReachedMap > dfs(F); + typename MutableGraph::NodeMap pred(F); //invalid iterators + typename MutableGraph::NodeMap free(F); + + dfs.pushAndSetReached(sF); + while (!dfs.finished()) { + ++dfs; + if (F.valid(typename MutableGraph::OutEdgeIt(dfs))) { + if (dfs.isBNodeNewlyReached()) { +// std::cout << "OutEdgeIt: " << dfs; +// std::cout << " aNode: " << F.aNode(dfs); +// std::cout << " bNode: " << F.bNode(dfs) << " "; + + typename MutableGraph::NodeIt v=F.aNode(dfs); + typename MutableGraph::NodeIt w=F.bNode(dfs); + pred.set(w, dfs); + if (F.valid(pred.get(v))) { + free.set(w, std::min(free.get(v), residual_capacity.get(dfs))); + } else { + free.set(w, residual_capacity.get(dfs)); + } + if (w==tF) { + //std::cout << "AUGMENTATION"< EAugGraph; + typedef FilterGraphWrapper< ErasingResGraphWrapper > EAugGraph; + typedef typename EAugGraph::OutEdgeIt EAugOutEdgeIt; + typedef typename EAugGraph::EdgeIt EAugEdgeIt; + + EAugGraph res_graph(*G, *flow, *capacity); + + //std::cout << "meg jo1" << std::endl; + + //typedef typename EAugGraph::NodeMap ReachedMap; + BfsIterator4< + ErasingResGraphWrapper, + ErasingResGraphWrapper::OutEdgeIt, + ErasingResGraphWrapper::NodeMap > bfs(res_graph); + + //std::cout << "meg jo2" << std::endl; + + bfs.pushAndSetReached(s); + //std::cout << "meg jo2.5" << std::endl; + + //typename EAugGraph::NodeMap dist(res_graph); //filled up with 0's + typename ErasingResGraphWrapper:: + NodeMap& dist=res_graph.dist; + //std::cout << "meg jo2.6" << std::endl; + + while ( !bfs.finished() ) { + ErasingResGraphWrapper::OutEdgeIt e=bfs; +// EAugOutEdgeIt e=/*AugOutEdgeIt*/(bfs); + //if (res_graph.valid(e)) { + // std::cout<<"a:"<(); res_graph.valid(n); res_graph.next(n)) { +// std::cout << "dist: " << dist.get(n) << std::endl; +// } + + bool __augment=true; + + while (__augment) { +// std::cout << "new iteration"<< std::endl; + + __augment=false; + //computing blocking flow with dfs + typedef typename EAugGraph::NodeMap BlockingReachedMap; + DfsIterator4< EAugGraph, EAugOutEdgeIt, BlockingReachedMap > + dfs(res_graph); + typename EAugGraph::NodeMap pred(res_graph); //invalid iterators + typename EAugGraph::NodeMap free(res_graph); + + dfs.pushAndSetReached(s); + while (!dfs.finished()) { + ++dfs; + if (res_graph.valid(EAugOutEdgeIt(dfs))) { + if (dfs.isBNodeNewlyReached()) { +// std::cout << "OutEdgeIt: " << dfs; +// std::cout << " aNode: " << res_graph.aNode(dfs); +// std::cout << " res cap: " << EAugOutEdgeIt(dfs).free(); +// std::cout << " bNode: " << res_graph.bNode(dfs) << " "; + + typename EAugGraph::NodeIt v=res_graph.aNode(dfs); + typename EAugGraph::NodeIt w=res_graph.bNode(dfs); + + pred.set(w, EAugOutEdgeIt(dfs)); + + //std::cout << EAugOutEdgeIt(dfs).free() << std::endl; + if (res_graph.valid(pred.get(v))) { + free.set(w, std::min(free.get(v), res_graph.free(/*EAugOutEdgeIt*/(dfs)))); + } else { + free.set(w, res_graph.free(/*EAugOutEdgeIt*/(dfs))); + } + + if (w==t) { +// std::cout << "t is reached, AUGMENTATION"<> "; + + res_graph.erase(dfs); + } + } + + } + + if (__augment) { + typename EAugGraph::NodeIt n=t; + Number augment_value=free.get(t); +// std::cout << "av:" << augment_value << std::endl; + while (res_graph.valid(pred.get(n))) { + EAugEdgeIt e=pred.get(n); + res_graph.augment(e, augment_value); + //e.augment(augment_value); + n=res_graph.tail(e); + if (res_graph.free(e)==0) + res_graph.erase(e); + } + } + + } + + return _augment; + } + void run() { + //int num_of_augmentations=0; + while (augmentOnShortestPath()) { + //while (augmentOnBlockingFlow()) { + //std::cout << ++num_of_augmentations << " "; + //std::cout< void run() { + //int num_of_augmentations=0; + //while (augmentOnShortestPath()) { + while (augmentOnBlockingFlow()) { + //std::cout << ++num_of_augmentations << " "; + //std::cout<getFirst(e, s); G->valid(e); G->next(e)) { + a+=flow->get(e); + } + return a; + } + }; + + +// template +// class MaxFlow2 { +// public: +// typedef typename Graph::NodeIt NodeIt; +// typedef typename Graph::EdgeIt EdgeIt; +// typedef typename Graph::EachEdgeIt EachEdgeIt; +// typedef typename Graph::OutEdgeIt OutEdgeIt; +// typedef typename Graph::InEdgeIt InEdgeIt; +// private: +// const Graph& G; +// std::list& S; +// std::list& T; +// FlowMap& flow; +// const CapacityMap& capacity; +// typedef ResGraphWrapper AugGraph; +// typedef typename AugGraph::OutEdgeIt AugOutEdgeIt; +// typedef typename AugGraph::EdgeIt AugEdgeIt; +// typename Graph::NodeMap SMap; +// typename Graph::NodeMap TMap; +// public: +// MaxFlow2(const Graph& _G, std::list& _S, std::list& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) { +// for(typename std::list::const_iterator i=S.begin(); +// i!=S.end(); ++i) { +// SMap.set(*i, true); +// } +// for (typename std::list::const_iterator i=T.begin(); +// i!=T.end(); ++i) { +// TMap.set(*i, true); +// } +// } +// bool augment() { +// AugGraph res_graph(G, flow, capacity); +// bool _augment=false; +// NodeIt reached_t_node; + +// typedef typename AugGraph::NodeMap ReachedMap; +// BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph); +// for(typename std::list::const_iterator i=S.begin(); +// i!=S.end(); ++i) { +// res_bfs.pushAndSetReached(*i); +// } +// //res_bfs.pushAndSetReached(s); + +// typename AugGraph::NodeMap pred(res_graph); +// //filled up with invalid iterators + +// typename AugGraph::NodeMap free(res_graph); + +// //searching for augmenting path +// while ( !res_bfs.finished() ) { +// AugOutEdgeIt e=/*AugOutEdgeIt*/(res_bfs); +// if (e.valid() && res_bfs.isBNodeNewlyReached()) { +// NodeIt v=res_graph.tail(e); +// NodeIt w=res_graph.head(e); +// pred.set(w, e); +// if (pred.get(v).valid()) { +// free.set(w, std::min(free.get(v), e.free())); +// } else { +// free.set(w, e.free()); +// } +// if (TMap.get(res_graph.head(e))) { +// _augment=true; +// reached_t_node=res_graph.head(e); +// break; +// } +// } + +// ++res_bfs; +// } //end of searching augmenting path + +// if (_augment) { +// NodeIt n=reached_t_node; +// Number augment_value=free.get(reached_t_node); +// while (pred.get(n).valid()) { +// AugEdgeIt e=pred.get(n); +// e.augment(augment_value); +// n=res_graph.tail(e); +// } +// } + +// return _augment; +// } +// void run() { +// while (augment()) { } +// } +// Number flowValue() { +// Number a=0; +// for(typename std::list::const_iterator i=S.begin(); +// i!=S.end(); ++i) { +// for(OutEdgeIt e=G.template first(*i); e.valid(); ++e) { +// a+=flow.get(e); +// } +// for(InEdgeIt e=G.template first(*i); e.valid(); ++e) { +// a-=flow.get(e); +// } +// } +// return a; +// } +// }; + + + +} // namespace hugo + +#endif //EDMONDS_KARP_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci/oldies/list_graph.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/oldies/list_graph.hh Sat Apr 03 14:41:31 2004 +0000 @@ -0,0 +1,552 @@ +#ifndef LIST_GRAPH_HH +#define LIST_GRAPH_HH + +#include +#include + +namespace hugo { + + template + int count(It it) { + int i=0; + for( ; it.valid(); ++it) { ++i; } + return i; + } + + class ListGraph { + + class node_item; + class edge_item; + + public: + + class NodeIt; + class EachNodeIt; + class EdgeIt; + class EachEdgeIt; + class OutEdgeIt; + class InEdgeIt; + class SymEdgeIt; + template class NodeMap; + template class EdgeMap; + + private: + + template friend class NodeMap; + template friend class EdgeMap; + + template + class NodeMap { + const ListGraph& G; + std::vector container; + public: + typedef T ValueType; + typedef NodeIt KeyType; + NodeMap(const ListGraph& _G) : G(_G), container(G.node_id) { } + NodeMap(const ListGraph& _G, T a) : + G(_G), container(G.node_id, a) { } + void set(NodeIt n, T a) { container[/*G.id(n)*/n.node->id]=a; } + T get(NodeIt n) const { return container[/*G.id(n)*/n.node->id]; } + T& operator[](NodeIt n) { return container[/*G.id(n)*/n.node->id]; } + const T& operator[](NodeIt n) const { return container[/*G.id(n)*/n.node->id]; } + void update() { container.resize(G.node_id); } + void update(T a) { container.resize(G.node_id, a); } + }; + + template + class EdgeMap { + const ListGraph& G; + std::vector container; + public: + typedef T ValueType; + typedef EdgeIt KeyType; + EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { } + EdgeMap(const ListGraph& _G, T a) : + G(_G), container(G.edge_id, a) { } + void set(EdgeIt e, T a) { container[/*G.id(e)*/e.edge->id]=a; } + T get(EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; } + T& operator[](EdgeIt e) { return container[/*G.id(e)*/e.edge->id]; } + const T& operator[](EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; } + void update() { container.resize(G.edge_id); } + void update(T a) { container.resize(G.edge_id, a); } + }; + + int node_id; + int edge_id; + int _node_num; + int _edge_num; + + node_item* _first_node; + node_item* _last_node; + + class node_item { + friend class ListGraph; + template friend class NodeMap; + + friend class NodeIt; + friend class EachNodeIt; + friend class EdgeIt; + friend class EachEdgeIt; + friend class OutEdgeIt; + friend class InEdgeIt; + friend class SymEdgeIt; + friend std::ostream& operator<<(std::ostream& os, const NodeIt& i); + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); + //ListGraph* G; + int id; + edge_item* _first_out_edge; + edge_item* _last_out_edge; + edge_item* _first_in_edge; + edge_item* _last_in_edge; + node_item* _next_node; + node_item* _prev_node; + public: + node_item() { } + }; + + class edge_item { + friend class ListGraph; + template friend class EdgeMap; + + friend class NodeIt; + friend class EachNodeIt; + friend class EdgeIt; + friend class EachEdgeIt; + friend class OutEdgeIt; + friend class InEdgeIt; + friend class SymEdgeIt; + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); + //ListGraph* G; + int id; + node_item* _tail; + node_item* _head; + edge_item* _next_out; + edge_item* _prev_out; + edge_item* _next_in; + edge_item* _prev_in; + public: + edge_item() { } + }; + + node_item* _add_node() { + node_item* p=new node_item; + p->id=node_id++; + p->_first_out_edge=0; + p->_last_out_edge=0; + p->_first_in_edge=0; + p->_last_in_edge=0; + p->_prev_node=_last_node; + p->_next_node=0; + if (_last_node) _last_node->_next_node=p; + _last_node=p; + if (!_first_node) _first_node=p; + + ++_node_num; + return p; + } + + edge_item* _add_edge(node_item* _tail, node_item* _head) { + edge_item* e=new edge_item; + e->id=edge_id++; + e->_tail=_tail; + e->_head=_head; + + e->_prev_out=_tail->_last_out_edge; + if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; + _tail->_last_out_edge=e; + if (!_tail->_first_out_edge) _tail->_first_out_edge=e; + e->_next_out=0; + + e->_prev_in=_head->_last_in_edge; + if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; + _head->_last_in_edge=e; + if (!_head->_first_in_edge) { _head->_first_in_edge=e; } + e->_next_in=0; + + ++_edge_num; + return e; + } + + //deletes a node which has no out edge and no in edge + void _delete_node(node_item* v) { + if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else + _last_node=v->_prev_node; + if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else + _first_node=v->_next_node; + + delete v; + --_node_num; + } + + void _delete_edge(edge_item* e) { + if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else + (e->_tail)->_last_out_edge=e->_prev_out; + if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else + (e->_tail)->_first_out_edge=e->_next_out; + if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else + (e->_head)->_last_in_edge=e->_prev_in; + if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else + (e->_head)->_first_in_edge=e->_next_in; + + delete e; + --_edge_num; + } + + void _set_tail(edge_item* e, node_item* _tail) { + if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else + (e->_tail)->_last_out_edge=e->_prev_out; + if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else + (e->_tail)->_first_out_edge=e->_next_out; + + e->_tail=_tail; + + e->_prev_out=_tail->_last_out_edge; + if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; + _tail->_last_out_edge=e; + if (!_tail->_first_out_edge) _tail->_first_out_edge=e; + e->_next_out=0; + } + + void _set_head(edge_item* e, node_item* _head) { + if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else + (e->_head)->_last_in_edge=e->_prev_in; + if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else + (e->_head)->_first_in_edge=e->_next_in; + + e->_head=_head; + + e->_prev_in=_head->_last_in_edge; + if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; + _head->_last_in_edge=e; + if (!_head->_first_in_edge) { _head->_first_in_edge=e; } + e->_next_in=0; + } + + public: + + /* default constructor */ + + ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { } + + ~ListGraph() { + while (first().valid()) erase(first()); + } + + int nodeNum() const { return _node_num; } + int edgeNum() const { return _edge_num; } + + /* functions to construct iterators from the graph, or from each other */ + + //EachNodeIt firstNode() const { return EachNodeIt(*this); } + //EachEdgeIt firstEdge() const { return EachEdgeIt(*this); } + + //OutEdgeIt firstOutEdge(const NodeIt v) const { return OutEdgeIt(v); } + //InEdgeIt firstInEdge(const NodeIt v) const { return InEdgeIt(v); } + //SymEdgeIt firstSymEdge(const NodeIt v) const { return SymEdgeIt(v); } + NodeIt tail(EdgeIt e) const { return e.tailNode(); } + NodeIt head(EdgeIt e) const { return e.headNode(); } + + NodeIt aNode(const OutEdgeIt& e) const { return e.aNode(); } + NodeIt aNode(const InEdgeIt& e) const { return e.aNode(); } + NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); } + + NodeIt bNode(const OutEdgeIt& e) const { return e.bNode(); } + NodeIt bNode(const InEdgeIt& e) const { return e.bNode(); } + NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); } + + //NodeIt invalid_node() { return NodeIt(); } + //EdgeIt invalid_edge() { return EdgeIt(); } + //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); } + //InEdgeIt invalid_in_edge() { return InEdgeIt(); } + //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); } + + /* same methods in other style */ + /* for experimental purpose */ + + EachNodeIt& getFirst(EachNodeIt& v) const { + v=EachNodeIt(*this); return v; } + EachEdgeIt& getFirst(EachEdgeIt& e) const { + e=EachEdgeIt(*this); return e; } + OutEdgeIt& getFirst(OutEdgeIt& e, NodeIt v) const { + e=OutEdgeIt(v); return e; } + InEdgeIt& getFirst(InEdgeIt& e, NodeIt v) const { + e=InEdgeIt(v); return e; } + SymEdgeIt& getFirst(SymEdgeIt& e, NodeIt v) const { + e=SymEdgeIt(v); return e; } + //void getTail(NodeIt& n, const EdgeIt& e) const { n=tail(e); } + //void getHead(NodeIt& n, const EdgeIt& e) const { n=head(e); } + + //void getANode(NodeIt& n, const OutEdgeIt& e) const { n=e.aNode(); } + //void getANode(NodeIt& n, const InEdgeIt& e) const { n=e.aNode(); } + //void getANode(NodeIt& n, const SymEdgeIt& e) const { n=e.aNode(); } + //void getBNode(NodeIt& n, const OutEdgeIt& e) const { n=e.bNode(); } + //void getBNode(NodeIt& n, const InEdgeIt& e) const { n=e.bNode(); } + //void getBNode(NodeIt& n, const SymEdgeIt& e) const { n=e.bNode(); } + //void get_invalid(NodeIt& n) { n=NodeIt(); } + //void get_invalid(EdgeIt& e) { e=EdgeIt(); } + //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); } + //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); } + //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); } + + template< typename It > + It first() const { + It e; + getFirst(e); + return e; + } + + template< typename It > + It first(NodeIt v) const { + It e; + getFirst(e, v); + return e; + } + + bool valid(NodeIt n) const { return n.valid(); } + bool valid(EdgeIt e) const { return e.valid(); } + + template It getNext(It it) const { + It tmp(it); return next(tmp); } + template It& next(It& it) const { return ++it; } + + + /* for getting id's of graph objects */ + /* these are important for the implementation of property vectors */ + + int id(NodeIt v) const { return v.node->id; } + int id(EdgeIt e) const { return e.edge->id; } + + /* adding nodes and edges */ + + NodeIt addNode() { return NodeIt(_add_node()); } + EdgeIt addEdge(NodeIt u, NodeIt v) { + return EdgeIt(_add_edge(u.node, v.node)); + } + + void erase(NodeIt i) { + while (first(i).valid()) erase(first(i)); + while (first(i).valid()) erase(first(i)); + _delete_node(i.node); + } + + void erase(EdgeIt e) { _delete_edge(e.edge); } + + void clear() { + while (first().valid()) erase(first()); + } + + void setTail(EdgeIt e, NodeIt tail) { + _set_tail(e.edge, tail.node); + } + + void setHead(EdgeIt e, NodeIt head) { + _set_head(e.edge, head.node); + } + + /* stream operations, for testing purpose */ + + friend std::ostream& operator<<(std::ostream& os, const NodeIt& i) { + os << i.node->id; return os; + } + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i) { + os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; + return os; + } + + class NodeIt { + friend class ListGraph; + template friend class NodeMap; + + friend class EdgeIt; + friend class OutEdgeIt; + friend class InEdgeIt; + friend class SymEdgeIt; + //public: //FIXME: It is required by op= of EachNodeIt + protected: + node_item* node; + protected: + friend int ListGraph::id(NodeIt v) const; + public: + NodeIt() : node(0) { } + NodeIt(node_item* _node) : node(_node) { } + bool valid() const { return (node!=0); } + //void makeInvalid() { node=0; } + friend bool operator==(const NodeIt& u, const NodeIt& v) { + return v.node==u.node; + } + friend bool operator!=(const NodeIt& u, const NodeIt& v) { + return v.node!=u.node; + } + friend std::ostream& operator<<(std::ostream& os, const NodeIt& i); + }; + + class EachNodeIt : public NodeIt { + friend class ListGraph; + //protected: + public: //for everybody but marci + EachNodeIt(const ListGraph& G) : NodeIt(G._first_node) { } + public: + EachNodeIt() : NodeIt() { } + EachNodeIt(node_item* v) : NodeIt(v) { } + EachNodeIt& operator++() { node=node->_next_node; return *this; } + //FIXME:: + // EachNodeIt& operator=(const NodeIt& e) + // { node=e.node; return *this; } + }; + + class EdgeIt { + friend class ListGraph; + template friend class EdgeMap; + + friend class NodeIt; + friend class EachNodeIt; + protected: + edge_item* edge; + friend int ListGraph::id(EdgeIt e) const; + public: + EdgeIt() : edge(0) { } + //EdgeIt() { } + EdgeIt(edge_item* _edge) : edge(_edge) { } + bool valid() const { return (edge!=0); } + //void makeInvalid() { edge=0; } + friend bool operator==(const EdgeIt& u, const EdgeIt& v) { + return v.edge==u.edge; + } + friend bool operator!=(const EdgeIt& u, const EdgeIt& v) { + return v.edge!=u.edge; + } + protected: + NodeIt tailNode() const { return NodeIt(edge->_tail); } + NodeIt headNode() const { return NodeIt(edge->_head); } + public: + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); + }; + + class EachEdgeIt : public EdgeIt { + friend class ListGraph; + //protected: + public: //for alpar + EachEdgeIt(const ListGraph& G) { + node_item* v=G._first_node; + if (v) edge=v->_first_out_edge; else edge=0; + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } + } + public: + EachEdgeIt() : EdgeIt() { } + EachEdgeIt(edge_item* _e) : EdgeIt(_e) { } + EachEdgeIt& operator++() { + node_item* v=edge->_tail; + edge=edge->_next_out; + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } + return *this; + } + }; + + class OutEdgeIt : public EdgeIt { + friend class ListGraph; + //node_item* v; + //protected: + public: //for alpar + OutEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; } + public: + OutEdgeIt() : EdgeIt()/*, v(0)*/ { } + OutEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; } + OutEdgeIt& operator++() { edge=edge->_next_out; return *this; } + protected: + NodeIt aNode() const { return NodeIt(edge->_tail); } + NodeIt bNode() const { return NodeIt(edge->_head); } + }; + + class InEdgeIt : public EdgeIt { + friend class ListGraph; + //node_item* v; + //protected: + public: //for alpar + InEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; } + public: + InEdgeIt() : EdgeIt()/*, v(0)*/ { } + InEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; } + InEdgeIt& operator++() { edge=edge->_next_in; return *this; } + protected: + NodeIt aNode() const { return NodeIt(edge->_head); } + NodeIt bNode() const { return NodeIt(edge->_tail); } + }; + + class SymEdgeIt : public EdgeIt { + friend class ListGraph; + bool out_or_in; //1 iff out, 0 iff in + //node_item* v; + //protected: + public: //for alpar + SymEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { + out_or_in=1; + edge=_v.node->_first_out_edge; + if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; } + } + public: + SymEdgeIt() : EdgeIt() /*, v(0)*/ { } + SymEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { + out_or_in=1; + edge=_v.node->_first_out_edge; + if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; } + } + SymEdgeIt& operator++() { + if (out_or_in) { + node_item* v=edge->_tail; + edge=edge->_next_out; + if (!edge) { out_or_in=0; edge=v->_first_in_edge; } + } else { + edge=edge->_next_in; + } + return *this; + } + protected: + NodeIt aNode() const { + return (out_or_in) ? NodeIt(edge->_tail) : NodeIt(edge->_head); } + NodeIt bNode() const { + return (out_or_in) ? NodeIt(edge->_head) : NodeIt(edge->_tail); } + }; + + }; + +// template< typename T > +// T ListGraph::first() const { +// std::cerr << "Invalid use of template T ListGraph::first();" << std::endl; +// return T(); +// } + +// template<> +// ListGraph::EachNodeIt ListGraph::first() const { +// return firstNode(); +// } + +// template<> +// ListGraph::EachEdgeIt ListGraph::first() const { +// return firstEdge(); +// } + +// template< typename T > +// T ListGraph::first(ListGraph::NodeIt v) const { +// std::cerr << "Invalid use of template T ListGraph::first(ListGRaph::NodeIt);" << std::endl; +// return T(); +// } + +// template<> +// ListGraph::OutEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { +// return firstOutEdge(v); +// } + +// template<> +// ListGraph::InEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { +// return firstInEdge(v); +// } + +// template<> +// ListGraph::SymEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { +// return firstSymEdge(v); +// } + + +} //namespace hugo + +#endif //LIST_GRAPH_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci/oldies/marci_bfs.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/oldies/marci_bfs.hh Sat Apr 03 14:41:31 2004 +0000 @@ -0,0 +1,176 @@ +#ifndef MARCI_BFS_HH +#define MARCI_BFS_HH + +#include + +#include + +namespace hugo { + + template + struct bfs { + typedef typename graph_type::node_iterator node_iterator; + typedef typename graph_type::edge_iterator edge_iterator; + typedef typename graph_type::each_node_iterator each_node_iterator; + typedef typename graph_type::out_edge_iterator out_edge_iterator; + graph_type& G; + node_iterator s; + node_property_vector reached; + node_property_vector pred; + node_property_vector dist; + std::queue bfs_queue; + bfs(graph_type& _G, node_iterator _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) { + bfs_queue.push(s); + for(each_node_iterator i=G.first_node(); i.valid(); ++i) + reached.put(i, false); + reached.put(s, true); + dist.put(s, 0); + } + + void run() { + while (!bfs_queue.empty()) { + node_iterator v=bfs_queue.front(); + out_edge_iterator e=G.first_out_edge(v); + bfs_queue.pop(); + for( ; e.valid(); ++e) { + node_iterator w=G.head(e); + std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl; + if (!reached.get(w)) { + std::cout << G.id(w) << " is newly reached :-)" << std::endl; + bfs_queue.push(w); + dist.put(w, dist.get(v)+1); + pred.put(w, e); + reached.put(w, true); + } else { + std::cout << G.id(w) << " is already reached" << std::endl; + } + } + } + } + }; + + template + struct bfs_visitor { + typedef typename graph_type::node_iterator node_iterator; + typedef typename graph_type::edge_iterator edge_iterator; + typedef typename graph_type::out_edge_iterator out_edge_iterator; + graph_type& G; + bfs_visitor(graph_type& _G) : G(_G) { } + void at_previously_reached(out_edge_iterator& e) { + //node_iterator v=G.tail(e); + node_iterator w=G.head(e); + std::cout << G.id(w) << " is already reached" << std::endl; + } + void at_newly_reached(out_edge_iterator& e) { + //node_iterator v=G.tail(e); + node_iterator w=G.head(e); + std::cout << G.id(w) << " is newly reached :-)" << std::endl; + } + }; + + template + struct bfs_iterator { + typedef typename graph_type::node_iterator node_iterator; + typedef typename graph_type::edge_iterator edge_iterator; + typedef typename graph_type::out_edge_iterator out_edge_iterator; + graph_type& G; + std::queue& bfs_queue; + reached_type& reached; + visitor_type& visitor; + void process() { + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + if (bfs_queue.empty()) return; + out_edge_iterator e=bfs_queue.front(); + //node_iterator v=G.tail(e); + node_iterator w=G.head(e); + if (!reached.get(w)) { + visitor.at_newly_reached(e); + bfs_queue.push(G.first_out_edge(w)); + reached.put(w, true); + } else { + visitor.at_previously_reached(e); + } + } + bfs_iterator(graph_type& _G, std::queue& _bfs_queue, reached_type& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) { + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + valid(); + } + bfs_iterator& operator++() { + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + //if (bfs_queue.empty()) return *this; + if (!valid()) return *this; + ++(bfs_queue.front()); + //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + valid(); + return *this; + } + //void next() { + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + // if (bfs_queue.empty()) return; + // ++(bfs_queue.front()); + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + //} + bool valid() { + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + if (bfs_queue.empty()) return false; else return true; + } + //bool finished() { + // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + // if (bfs_queue.empty()) return true; else return false; + //} + operator edge_iterator () { return bfs_queue.front(); } + + }; + + template + struct bfs_iterator1 { + typedef typename graph_type::node_iterator node_iterator; + typedef typename graph_type::edge_iterator edge_iterator; + typedef typename graph_type::out_edge_iterator out_edge_iterator; + graph_type& G; + std::queue& bfs_queue; + reached_type& reached; + bool _newly_reached; + bfs_iterator1(graph_type& _G, std::queue& _bfs_queue, reached_type& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) { + valid(); + if (!bfs_queue.empty() && bfs_queue.front().valid()) { + out_edge_iterator e=bfs_queue.front(); + node_iterator w=G.head(e); + if (!reached.get(w)) { + bfs_queue.push(G.first_out_edge(w)); + reached.put(w, true); + _newly_reached=true; + } else { + _newly_reached=false; + } + } + } + bfs_iterator1& operator++() { + if (!valid()) return *this; + ++(bfs_queue.front()); + valid(); + if (!bfs_queue.empty() && bfs_queue.front().valid()) { + out_edge_iterator e=bfs_queue.front(); + node_iterator w=G.head(e); + if (!reached.get(w)) { + bfs_queue.push(G.first_out_edge(w)); + reached.put(w, true); + _newly_reached=true; + } else { + _newly_reached=false; + } + } + return *this; + } + bool valid() { + while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } + if (bfs_queue.empty()) return false; else return true; + } + operator edge_iterator () { return bfs_queue.front(); } + bool newly_reached() { return _newly_reached; } + + }; + +} // namespace hugo + +#endif //MARCI_BFS_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci/oldies/marci_graph_concept.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/oldies/marci_graph_concept.txt Sat Apr 03 14:41:31 2004 +0000 @@ -0,0 +1,217 @@ +ETIK-OL-NOLIB-NEGRES graph concept-ek. + + Ebben a dokumentacioban graph concept tervek es azok megvalositasarol irok. +A felsorolt rutinok, osztalyok egyaltalan nem kikristalyosodottak, 1-1 elemi +operacio elvegzesere gyakran tobb mod is rendelkezesre all. A tervezesi fazisban pont annak kell kiderulnie, hogy milyen metodusok tavolithatok el, s milyen +ujakra van szukseg. + + Megvalositottunk egy graph osztalyt mely listaban tarolja a pontokat, +az 1 pontbol kiindulo eleket, s az 1 pontba bemeno eleket. Konstrualni lehet +ures grafot, hozzaadni pontokat, eleket. Az incidenciat node_iteratorok-kal +ill. edge_iteratorokkal lehet megfigyelni. Adott tovabba 1 template osztaly, +a graf pontjaihoz vagy eleihez tetszoleges tipusu property hozzarendelesere, +a jelen megvalositas ezeket vektorben tarolja. Fontos azonban, hogy ezen +property_vector csak azokra a graf-objektumokra ervenyes, melyek +letrehozasanak pillanataban a grafhoz tartoznak. + +marci_bfs.hh //bfs, tejesen kiserleti +marci_graph_demo.cc //peldaprogi a lisas graf hasznalatahoz +marci_list_graph.hh //list_graph megvalositas +marci_max_flow.hh //folyam, kiserleti +marci_property_vector.hh //property vector megvalosites indexelt grafokhoz +graf es iterator tipusok: + +class list_graph; + +class node_iterator; +trivialis node iterator, csak cimezni lehet vele, pl property vectort + +class each_node_iterator; +node iterator a graf pontjainak bejarasara, node_iterator-ra konvertalhato + +class edge_iterator; +trivialis edge iterator, csak cimezni lehet vele, pl property vectort + +class each_edge_iterator; +edge iterator a graf osszes elenek bejarasara + +class out_edge_iterator; +edge iterator 1 pont ki eleinek bejarasara, edge_iterator-ra konvertalhato + +class in_edge_iterator; +edge iterator 1 pont be eleinek bejarasara, edge_iterator-ra konvertalhato + +class sym_edge_iterator; +edge iterator 1 pont be es ki eleinek bejarasara, edge_iterator-ra +konvertalhato + +default constructor: + +list_graph(); + +A graf osztaly fobb publikus metodusai, az alapveto hasznalathoz: +Hasonlo funkciok megvalosithatok 1 kesobb leirt modon, illetve +ezek kozul nehany az iteratorok metodusaival, megis azt javasolnam, hogy az +iteratorok metodusait ne hasznaljuk. Miert? Azt szeretnenk, ha 1 ponthalmazon +van 2 graf, es csak az elhalmazhoz keszitunk uj iteratorokat, akkor pl 1 pont +out-edge-iteratora megkaphato legyen a grafbol es a node_iteratorbol. Ezert +out_edge_iterator(const node_iterator&) hasznalata nem javasolt, +esetleg majd szamuzzuk a concept-bol, s akkor nem nesz baj. + +each_node_iterator first_node(); +each_edge_iterator first_edge(); +out_edge_iterator first_out_edge(const node_iterator&); +in_edge_iterator first_in_edge(const node_iterator&); +sym_edge_iterator first_sym_edge(const node_iterator&); + +node_iterator tail(const edge_iterator&); +node_iterator head(const edge_iterator&); + +node_iterator a_node(const out_edge_iterator&); +node_iterator a_node(const in_edge_iterator&); +node_iterator a_node(const sym_edge_iterator&); +//az out, in or sym edge iterator rogzitett pontjara ad 1 node_iterator-t + +node_iterator b_node(const out_edge_iterator&); +node_iterator b_node(const in_edge_iterator&); +node_iterator b_node(const sym_edge_iterator&); +//az out, in or sym edge iterator nem rogzitett pontjara ad 1 node_iterator-t + +//node_iterator invalid_node(); +//edge_iterator invalid_edge(); +//out_edge_iterator invalid_out_edge(); +//in_edge_iterator invalid_in_edge(); +//sym_edge_iterator invalid_sym_edge(); + +//az iteratorok ures konstruktorai meghatarozatlan +tartalmu konstruktort adnak vissza, ezekkel a matodusokkal +lehet ervenytelent csinalni. +Lehet hogy ezt az ures konstruktorral kellene, tessek vitatkozni. + +Kiserleti cellal ugyanezen fv-ek mas stilusu megvalositasai: + +void get_first(each_node_iterator&); +void get_first(each_edge_iterator&); +void get_first(out_edge_iterator&, const node_iterator&); +void get_first(in_edge_iterator&, const node_iterator&); +void get_first(sym_edge_iterator&, const node_iterator&); + +void get_tail(node_iterator&, const edge_iterator&); +void get_head(node_iterator&, const edge_iterator&); + +void get_a_node(node_iterator&, const out_edge_iterator&); +void get_a_node(node_iterator&, const in_edge_iterator&); +void get_a_node(node_iterator&, const sym_edge_iterator&); + +void get_b_node(node_iterator&, const out_edge_iterator&); +void get_b_node(node_iterator&, const in_edge_iterator&); +void get_b_node(node_iterator&, const sym_edge_iterator&); + +//void get_invalid(node_iterator&); +//void get_invalid(edge_iterator&); +//void get_invalid(out_edge_iterator&); +//void get_invalid(in_edge_iterator&); +//void get_invalid(sym_edge_iterator&); + +Pontok azonositasara de meginkabb property vectorokhoz: + +int id(const node_iterator&); +int id(const edge_iterator&); + +Pontok es elek hozzaadasanak metodusai: + +node_iterator add_node(); +edge_iterator add_edge(const node_iterator&, const node_iterator&); + +Hogy konnyebb legyen a progikat tesztelni, nehany stream utasitas: +ezek nem a list_graph metodusai + +friend std::ostream& operator<<(std::ostream&, const node_iterator&); +friend std::ostream& operator<<(std::ostream&, const edge_iterator&); + +node_iterator metodusai: +node_iterator(); +bool valid(); +void make_invalid(); +ezek nem tagfuggvenyek: +friend bool operator==(const node_iterator&, const node_iterator&); +friend bool operator!=(const node_iterator& u, const node_iterator& v); + +each_node_iterator metodusai: +ez publikusan szarmazik a node_iterator-bol, tehat a fentiek is. +each_node_iterator(); +each_node_iterator& operator++(); + +edge_iterator metodusai: +edge_iterator(); +bool valid(); +void make_invalid(); +ezek nem tagfvek: +friend bool operator==(const edge_iterator&, const edge_iterator&); +friend bool operator!=(const edge_iterator&, const edge_iterator&); +ujra tagfv-ek. +//node_iterator tail_node() const; nem javasolt +//node_iterator head_node() const; nem javasolt + +each_edge_iterator metodusai: +edge_iterator-bol szarmazik +each_edge_iterator(); +each_edge_iterator& operator++(); + +out_edge_iterator metodusai: +edge_iterator-bol szarmazik +out_edge_iterator(); +//out_edge_iterator(const node_iterator&); nem javasolt +out_edge_iterator& operator++(); +//node_iterator a_node() const; nem javasolt +//node_iterator b_node() const; + + +in_edge_iterator metodusai: +edge_iterator-bol szarmazik +in_edge_iterator(); +//in_edge_iterator(const node_iterator&); nem javasolt +in_edge_iterator& operator++(); +//node_iterator a_node() const; nem javasolt +//node_iterator b_node() const; + + +sym_edge_iterator metodusai: +edge_iterator-bol szarmazik +sym_edge_iterator(); +//sym_edge_iterator(const node_iterator&); nem javasolt +sym_edge_iterator& operator++(); +//node_iterator a_node() const; nem javasolt +//node_iterator b_node() const; + +Node propery array-okrol: + +template +class node_property_vector; + +metodusok: + +node_property_vector(graph_type&); +void put(graph_type::node_iterator, const T&); +T get(graph_type::node_iterator); + +Ugyanez edge_property_array-okkal + +template +class edge_property_vector; + +edge_property_vector(graph_type&); +void put(graph_type::edge_iterator, const T&); +get(graph_type::edge_iterator); + + Ennyi nem javasolas utan, meg nehany szo. + Alparral ugy gondoltuk, hogy az iterator 1 olyan egyszeru objetum legyen +csak, mellyel, ha ervenyes, akkor lehet tovabblepni 1 pont vagy ellistaban. +Az hogy valamilyen pont-iteratorbeol el-iteratort csinalunk, igenis legyen a +graf objektum feladata, hiszen igy lehet csinelni ugyanazon a ponthalmazon +tobb grafot ugyanazon pont-iteratorokkal. + Sokkal komolyabb kerdesek merultek fel azzal kapcsolatban, hogy hogyan adjuk +at a propertyket az algoritmusoknak, algoritmus-objektumoknak. +Errol majd kesobb. + +marci@cs.elte.hu diff -r be43902fadb7 -r 19f3943521ab src/work/marci/oldies/marci_graph_demo.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/oldies/marci_graph_demo.cc Sat Apr 03 14:41:31 2004 +0000 @@ -0,0 +1,270 @@ +#include +#include +#include + +#include +#include +#include + +using namespace hugo; + +int main (int, char*[]) +{ + typedef ListGraph::Node Node; + typedef ListGraph::Edge Edge; + typedef ListGraph::NodeIt NodeIt; + typedef ListGraph::EdgeIt EdgeIt; + typedef ListGraph::OutEdgeIt OutEdgeIt; + typedef ListGraph::InEdgeIt InEdgeIt; + typedef ListGraph::SymEdgeIt SymEdgeIt; + ListGraph G; + std::vector vector_of_Nodes; + for(int i=0; i!=8; ++i) vector_of_Nodes.push_back(G.addNode()); + for(int i=0; i!=8; ++i) + for(int j=0; j!=8; ++j) + if ((ij is arc iff i()) << std::endl; + + for(NodeIt i=G.first(); G.valid(i); G.next(i)) { + std::cout << "node " << G.id(i) << std::endl; + std::cout << " outdegree (OutEdgeIt): " << count(G.first(i)) << " "; + for(OutEdgeIt j=G.first(i); G.valid(j); G.next(j)) { + std::cout << "(" << G.id(G.tail(j)) << "--" << G.id(j) << "->" << G.id(G.head(j)) << ") "; + } + std::cout << std::endl; + + std::cout<< " "; + for(OutEdgeIt j=G.first(i); G.valid(j); G.next(j)) { + std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; } + std::cout<(i)) << " "; + for(InEdgeIt j=G.first(i); G.valid(j); G.next(j)) { + std::cout << j << " "; } + std::cout << std::endl; + + std::cout<< " "; + for(InEdgeIt j=G.first(i); G.valid(j); G.next(j)) { + std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; } + std::cout<(i)) << " "; + for(SymEdgeIt j=G.first(i); G.valid(j); G.next(j)) { + std::cout << j << " "; } + std::cout<(i); G.valid(j); G.next(j)) { + std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; } + std::cout<(); G.valid(i); G.next(i)) { + std::cout << i << " "; + } + std::cout << std::endl; + + std::cout << "node property array test" << std::endl; + ListGraph::NodeMap my_property_vector(G); + NodeIt v; + G.first(v); + my_property_vector.set(v, 42); + my_property_vector.set(G.next(G.first()), 314); + my_property_vector.set(G.next(G.next(G.first())), 1956); + my_property_vector.set(vector_of_Nodes[3], 1989); + my_property_vector.set(vector_of_Nodes[4], 2003); + my_property_vector.set(vector_of_Nodes[7], 1978); + std::cout << "some node property values..." << std::endl; + for(NodeIt i=G.first(); G.valid(i); G.next(i)) { + std::cout << my_property_vector.get(i) << std::endl; + } + int _i=1; + int _ii=1; + ListGraph::EdgeMap my_edge_property(G); + for(EdgeIt i=G.first(); G.valid(i); G.next(i)) { + my_edge_property.set(i, _i); + _i*=_ii; ++_ii; + } + + std::cout << "node and edge property values on the tails and heads of edges..." << std::endl; + for(EdgeIt j=G.first(); G.valid(j); G.next(j)) { + std::cout << my_property_vector.get(G.tail(j)) << "--" << my_edge_property.get(j) << "-->" << my_property_vector.get(G.head(j)) << " "; + } + std::cout << std::endl; +/* + std::cout << "bfs from the first node" << std::endl; + bfs bfs_test(G, G.first()); + bfs_test.run(); + std::cout << "reached: "; + for(NodeIt i=G.first(); G.valid(i); G.next(i)) { + std::cout << bfs_test.reached.get(i) << " "; + } + std::cout<(); G.valid(i); G.next(i)) { + std::cout << bfs_test.dist.get(i) << " "; + } + std::cout< node_name(flowG); + node_name.set(s, "s"); + node_name.set(v1, "v1"); + node_name.set(v2, "v2"); + node_name.set(v3, "v3"); + node_name.set(v4, "v4"); + node_name.set(t, "t"); + + Edge s_v1=flowG.addEdge(s, v1); + Edge s_v2=flowG.addEdge(s, v2); + Edge v1_v2=flowG.addEdge(v1, v2); + Edge v2_v1=flowG.addEdge(v2, v1); + Edge v1_v3=flowG.addEdge(v1, v3); + Edge v3_v2=flowG.addEdge(v3, v2); + Edge v2_v4=flowG.addEdge(v2, v4); + Edge v4_v3=flowG.addEdge(v4, v3); + Edge v3_t=flowG.addEdge(v3, t); + Edge v4_t=flowG.addEdge(v4, t); + + ListGraph::EdgeMap cap(flowG); + + cap.set(s_v1, 16); + cap.set(s_v2, 13); + cap.set(v1_v2, 10); + cap.set(v2_v1, 4); + cap.set(v1_v3, 12); + cap.set(v3_v2, 9); + cap.set(v2_v4, 14); + cap.set(v4_v3, 7); + cap.set(v3_t, 20); + cap.set(v4_t, 4); + + std::cout << "on directed graph graph" << std::endl; //<< flowG; + std::cout << "names and capacity values" << std::endl; + for(NodeIt i=flowG.first(); flowG.valid(i); flowG.next(i)) { + std::cout << node_name.get(i) << ": "; + std::cout << "out edges: "; + for(OutEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; + std::cout << "in edges: "; + for(InEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; + std::cout << std::endl; + } + + //flowG.deleteEdge(s_v1); + //flowG.deleteEdge(s_v2); + //flowG.deleteEdge(v1_v2); + //flowG.deleteEdge(v1_v3); + + + //flowG.setTail(v3_t, v2); + //flowG.setHead(v3_t, s); +/* + for(NodeIt i=flowG.first(); flowG.valid(i); flowG.next(i)) { + std::cout << node_name.get(i) << ": "; + std::cout << "out edges: "; + for(OutEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; + std::cout << "in edges: "; + for(InEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; + std::cout << std::endl; + } + + for(EdgeIt e=flowG.first(); flowG.valid(e); flowG.next(e)) { + std::cout << node_name.get(flowG.tail(e)) << "-"<< cap.get(e) << "->" << node_name.get(flowG.head(e)) << " "; + } +*/ + /* + while (flowG.valid(flowG.first())) { + flowG.deleteEdge(flowG.first()); + for(NodeIt i=flowG.first(); flowG.valid(i); flowG.next(i)) { + std::cout << node_name.get(i) << ": "; + std::cout << "out edges: "; + for(OutEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; + std::cout << "in edges: "; + for(InEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; + std::cout << std::endl; + } + } + + while (flowG.valid(flowG.first())) { + flowG.deleteNode(flowG.first()); + for(NodeIt i=flowG.first(); flowG.valid(i); flowG.next(i)) { + std::cout << node_name.get(i) << ": "; + std::cout << "out edges: "; + for(OutEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; + std::cout << "in edges: "; + for(InEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) + std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; + std::cout << std::endl; + } + } + */ + + //std::cout << std::endl; + + + { + ListGraph::EdgeMap flow(flowG, 0); + MaxFlow, ListGraph::EdgeMap > max_flow_test(flowG, s, t, flow, cap); + /* + max_flow_test.augmentOnBlockingFlow(); + for(EdgeIt e=flowG.template first(); flowG.valid(e); flowG.next(e)) { + std::cout<<"("<"<(); + for(EdgeIt e=flowG.template first(); flowG.valid(e); flowG.next(e)) { + std::cout<<"("<"<(); flowG.valid(e); flowG.next(e)) { + std::cout<<"("<"< S; + S.push_back(s); S.push_back(v3); + std::list T; + T.push_back(t); + + ListGraph::EdgeMap flow(flowG, 0); + MaxFlow2, ListGraph::EdgeMap > max_flow_test(flowG, S, T, flow, cap); + max_flow_test.run(); + + std::cout << "maximum flow: "<< std::endl; + for(EdgeIt e=flowG.template first(); flowG.valid(e); flowG.next(e)) { + std::cout<<"("<"< + struct graph_traits { + typedef typename graph_type::node_iterator node_iterator; + typedef typename graph_type::edge_iterator edge_iterator; + typedef typename graph_type::each_node_iterator each_node_iterator; + typedef typename graph_type::each_edge_iterator each_edge_iterator; + typedef typename graph_type::out_edge_iterator out_edge_iterator; + typedef typename graph_type::in_edge_iterator in_edge_iterator; + typedef typename graph_type::sym_edge_iterator sym_edge_iterator; + }; + +} // namespace hugo + +#endif //MARCI_GRAPH_TRAITS_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci/oldies/marci_list_graph.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/oldies/marci_list_graph.hh Sat Apr 03 14:41:31 2004 +0000 @@ -0,0 +1,343 @@ +#ifndef MARCI_LIST_GRAPH_HH +#define MARCI_LIST_GRAPH_HH + +#include + +namespace hugo { + + class list_graph { + class node_item; + class edge_item; + public: + class node_iterator; + class each_node_iterator; + class edge_iterator; + class each_edge_iterator; + class out_edge_iterator; + class in_edge_iterator; + class sym_edge_iterator; + private: + int node_id; + int edge_id; + int _node_num; + int _edge_num; + + node_item* _first_node; + node_item* _last_node; + + class node_item { + friend class list_graph; + friend class node_iterator; + friend class each_node_iterator; + friend class edge_iterator; + friend class each_edge_iterator; + friend class out_edge_iterator; + friend class in_edge_iterator; + friend class sym_edge_iterator; + friend std::ostream& operator<<(std::ostream& os, const node_iterator& i); + friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i); + list_graph* G; + int id; + edge_item* _first_out_edge; + edge_item* _last_out_edge; + edge_item* _first_in_edge; + edge_item* _last_in_edge; + node_item* _next_node; + node_item* _prev_node; + public: + node_item() { } + }; + + class edge_item { + friend class list_graph; + friend class node_iterator; + friend class each_node_iterator; + friend class edge_iterator; + friend class each_edge_iterator; + friend class out_edge_iterator; + friend class in_edge_iterator; + friend class sym_edge_iterator; + friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i); + list_graph* G; + int id; + node_item* _tail; + node_item* _head; + edge_item* _next_out; + edge_item* _prev_out; + edge_item* _next_in; + edge_item* _prev_in; + public: + edge_item() { } + }; + + node_item* _add_node() { + node_item* p=new node_item; + p->id=node_id++; + p->_first_out_edge=0; + p->_last_out_edge=0; + p->_first_in_edge=0; + p->_last_in_edge=0; + p->_prev_node=_last_node; + p->_next_node=0; + if (_last_node) _last_node->_next_node=p; + _last_node=p; + if (!_first_node) _first_node=p; + ++_node_num; + return p; + } + + edge_item* _add_edge(node_item* _tail, node_item* _head) { + edge_item* e=new edge_item; + e->id=edge_id++; + e->_tail=_tail; + e->_head=_head; + + e->_prev_out=_tail->_last_out_edge; + if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; + _tail->_last_out_edge=e; + if (!_tail->_first_out_edge) _tail->_first_out_edge=e; + + e->_prev_in=_head->_last_in_edge; + if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; + _head->_last_in_edge=e; + if (!_head->_first_in_edge) { _head->_first_in_edge=e; } + ++_edge_num; + return e; + } + + public: + + /* default constructor */ + + list_graph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { } + + int node_num() { return _node_num; } + int edge_num() { return _edge_num; } + + /* functions to construct iterators from the graph, or from each other */ + + each_node_iterator first_node() { return each_node_iterator(_first_node); } + each_edge_iterator first_edge() { + node_item* v=_first_node; + edge_item* edge=v->_first_out_edge; + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } + return each_edge_iterator(v, edge); + } + + out_edge_iterator first_out_edge(const node_iterator& v) { + return out_edge_iterator(v); + } + in_edge_iterator first_in_edge(const node_iterator& v) { + return in_edge_iterator(v); + } + sym_edge_iterator first_sym_edge(const node_iterator& v) { + return sym_edge_iterator(v); + } + node_iterator tail(const edge_iterator& e) { return e.tail_node(); } + node_iterator head(const edge_iterator& e) { return e.head_node(); } + + node_iterator a_node(const out_edge_iterator& e) { return e.a_node(); } + node_iterator a_node(const in_edge_iterator& e) { return e.a_node(); } + node_iterator a_node(const sym_edge_iterator& e) { return e.a_node(); } + + node_iterator b_node(const out_edge_iterator& e) { return e.b_node(); } + node_iterator b_node(const in_edge_iterator& e) { return e.b_node(); } + node_iterator b_node(const sym_edge_iterator& e) { return e.b_node(); } + + //node_iterator invalid_node() { return node_iterator(); } + //edge_iterator invalid_edge() { return edge_iterator(); } + //out_edge_iterator invalid_out_edge() { return out_edge_iterator(); } + //in_edge_iterator invalid_in_edge() { return in_edge_iterator(); } + //sym_edge_iterator invalid_sym_edge() { return sym_edge_iterator(); } + + /* same methods in other style */ + /* for experimental purpose */ + + void get_first(each_node_iterator& v) { v=each_node_iterator(_first_node); } + void get_first(each_edge_iterator& e) { e=first_edge(); } + void get_first(out_edge_iterator& e, const node_iterator& v) { + e=out_edge_iterator(v); + } + void get_first(in_edge_iterator& e, const node_iterator& v) { + e=in_edge_iterator(v); + } + void get_first(sym_edge_iterator& e, const node_iterator& v) { + e=sym_edge_iterator(v); + } + void get_tail(node_iterator& n, const edge_iterator& e) { n=tail(e); } + void get_head(node_iterator& n, const edge_iterator& e) { n=head(e); } + + void get_a_node(node_iterator& n, const out_edge_iterator& e) { n=e.a_node(); } + void get_a_node(node_iterator& n, const in_edge_iterator& e) { n=e.a_node(); } + void get_a_node(node_iterator& n, const sym_edge_iterator& e) { n=e.a_node(); } + void get_b_node(node_iterator& n, const out_edge_iterator& e) { n=e.b_node(); } + void get_b_node(node_iterator& n, const in_edge_iterator& e) { n=e.b_node(); } + void get_b_node(node_iterator& n, const sym_edge_iterator& e) { n=e.b_node(); } + //void get_invalid(node_iterator& n) { n=node_iterator(); } + //void get_invalid(edge_iterator& e) { e=edge_iterator(); } + //void get_invalid(out_edge_iterator& e) { e=out_edge_iterator(); } + //void get_invalid(in_edge_iterator& e) { e=in_edge_iterator(); } + //void get_invalid(sym_edge_iterator& e) { e=sym_edge_iterator(); } + + + /* for getting id's of graph objects */ + /* these are important for the implementation of property vectors */ + + int id(const node_iterator& v) { return v.node->id; } + int id(const edge_iterator& e) { return e.edge->id; } + + /* adding nodes and edges */ + + node_iterator add_node() { return node_iterator(_add_node()); } + edge_iterator add_edge(const node_iterator& u, const node_iterator& v) { + return edge_iterator(_add_edge(u.node, v.node)); + } + + /* stream operations, for testing purpose */ + + friend std::ostream& operator<<(std::ostream& os, const node_iterator& i) { + os << i.node->id; return os; + } + friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i) { + os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; + return os; + } + + class node_iterator { + friend class list_graph; + + friend class edge_iterator; + friend class out_edge_iterator; + friend class in_edge_iterator; + friend class sym_edge_iterator; + protected: + node_item* node; + friend int list_graph::id(const node_iterator& v); + public: + node_iterator() : node(0) { } + node_iterator(node_item* _node) : node(_node) { } + bool valid() { return (node!=0); } + void make_invalid() { node=0; } + friend bool operator==(const node_iterator& u, const node_iterator& v) { + return v.node==u.node; + } + friend bool operator!=(const node_iterator& u, const node_iterator& v) { + return v.node!=u.node; + } + friend std::ostream& operator<<(std::ostream& os, const node_iterator& i); + }; + + class each_node_iterator : public node_iterator { + friend class list_graph; + public: + each_node_iterator() : node_iterator() { } + each_node_iterator(node_item* v) : node_iterator(v) { } + each_node_iterator& operator++() { node=node->_next_node; return *this; } + }; + + class edge_iterator { + friend class list_graph; + + friend class node_iterator; + friend class each_node_iterator; + protected: + edge_item* edge; + friend int list_graph::id(const edge_iterator& e); + public: + edge_iterator() : edge(0) { } + edge_iterator(edge_item* _edge) : edge(_edge) { } + bool valid() { return (edge!=0); } + void make_invalid() { edge=0; } + friend bool operator==(const edge_iterator& u, const edge_iterator& v) { + return v.edge==u.edge; + } + friend bool operator!=(const edge_iterator& u, const edge_iterator& v) { + return v.edge!=u.edge; + } + protected: + node_iterator tail_node() const { return node_iterator(edge->_tail); } + node_iterator head_node() const { return node_iterator(edge->_head); } + public: + friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i); + }; + + class each_edge_iterator : public edge_iterator { + friend class list_graph; + node_item* v; + public: + each_edge_iterator() : edge_iterator(), v(0) { } + each_edge_iterator(node_item* _v, edge_item* _e) : edge_iterator(_e), v(_v) { } + each_edge_iterator& operator++() { + edge=edge->_next_out; + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } + return *this; + } + }; + + class out_edge_iterator : public edge_iterator { + friend class list_graph; + node_item* v; + public: + out_edge_iterator() : edge_iterator(), v(0) { } + protected: + out_edge_iterator(const node_iterator& _v) : v(_v.node) { edge=v->_first_out_edge; } + public: + out_edge_iterator& operator++() { edge=edge->_next_out; return *this; } + protected: + node_iterator a_node() const { return node_iterator(v); } + node_iterator b_node() const { + return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); } + }; + + class in_edge_iterator : public edge_iterator { + friend class list_graph; + node_item* v; + public: + in_edge_iterator() : edge_iterator(), v(0) { } + protected: + in_edge_iterator(const node_iterator& _v) : v(_v.node) { + edge=v->_first_in_edge; + } + public: + in_edge_iterator& operator++() { edge=edge->_next_in; return *this; } + protected: + node_iterator a_node() const { return node_iterator(v); } + node_iterator b_node() const { + return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); } + }; + + class sym_edge_iterator : public edge_iterator { + friend class list_graph; + bool out_or_in; //1 iff out, 0 iff in + node_item* v; + public: + sym_edge_iterator() : edge_iterator(), v(0) { } + protected: + sym_edge_iterator(const node_iterator& _v) : v(_v.node) { + out_or_in=1; + edge=v->_first_out_edge; + if (!edge) { edge=v->_first_in_edge; out_or_in=0; } + } + public: + sym_edge_iterator& operator++() { + if (out_or_in) { + edge=edge->_next_out; + if (!edge) { out_or_in=0; edge=v->_first_in_edge; } + } else { + edge=edge->_next_in; + } + return *this; + } + protected: + node_iterator a_node() const { return node_iterator(v); } + node_iterator b_node() const { + return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); } + }; + + }; + + + +} //namespace hugo + +#endif //MARCI_LIST_GRAPH_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci/oldies/marci_makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/oldies/marci_makefile Sat Apr 03 14:41:31 2004 +0000 @@ -0,0 +1,5 @@ +CXXFLAGS = -Wall -ansi -I. +CXX = g++-3.0 + +marci_graph_demo: marci_graph_demo.cc marci_list_graph.hh marci_property_vector.hh marci_bfs.hh marci_max_flow.hh + $(CXX) $(CXXFLAGS) marci_graph_demo.cc -o marci_graph_demo \ No newline at end of file diff -r be43902fadb7 -r 19f3943521ab src/work/marci/oldies/marci_max_flow.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci/oldies/marci_max_flow.hh Sat Apr 03 14:41:31 2004 +0000 @@ -0,0 +1,183 @@ +#ifndef MARCI_MAX_FLOW_HH +#define MARCI_MAX_FLOW_HH + +#include + +#include +#include + +namespace hugo { + + template + class res_graph_type { + typedef typename graph_type::node_iterator node_iterator; + typedef typename graph_type::each_node_iterator each_node_iterator; + typedef typename graph_type::sym_edge_iterator old_sym_edge_iterator; + graph_type& G; + edge_property_vector& flow; + edge_property_vector& capacity; + public: + res_graph_type(graph_type& _G, edge_property_vector& _flow, edge_property_vector& _capacity) : G(_G), flow(_flow), capacity(_capacity) { } + + class edge_iterator { + friend class res_graph_type; + protected: + res_graph_type* resG; + old_sym_edge_iterator sym; + public: + edge_iterator() { } + //bool is_free() { + //if (resG->G.a_node(sym)==resG->G.tail(sym)) { + // return (resG->flow.get(sym)capacity.get(sym)); + //} else { + // return (resG->flow.get(sym)>0); + //} + //} + T free() { + if (resG->G.a_node(sym)==resG->G.tail(sym)) { + return (resG->capacity.get(sym)-resG->flow.get(sym)); + } else { + return (resG->flow.get(sym)); + } + } + bool valid() { return sym.valid(); } + void make_invalid() { sym.make_invalid(); } + void augment(T a) { + if (resG->G.a_node(sym)==resG->G.tail(sym)) { + resG->flow.put(sym, resG->flow.get(sym)+a); + } else { + resG->flow.put(sym, resG->flow.get(sym)-a); + } + } + }; + + class out_edge_iterator : public edge_iterator { + public: + out_edge_iterator() { } + out_edge_iterator(res_graph_type& _resG, const node_iterator& v) { + resG=&_resG; + sym=resG->G.first_sym_edge(v); + while( sym.valid() && !(free()>0) ) { ++sym; } + } + out_edge_iterator& operator++() { + ++sym; + while( sym.valid() && !(free()>0) ) { ++sym; } + return *this; + } + }; + + out_edge_iterator first_out_edge(const node_iterator& v) { + return out_edge_iterator(*this, v); + } + + each_node_iterator first_node() { + return G.first_node(); + } + + node_iterator tail(const edge_iterator& e) { return G.a_node(e.sym); } + node_iterator head(const edge_iterator& e) { return G.b_node(e.sym); } + + int id(const node_iterator& v) { return G.id(v); } + + //node_iterator invalid_node() { return G.invalid_node(); } + //res_edge_it invalid_edge() { res_edge_it n; n.sym=G.invalid_sym_edge(); return n; } + }; + + template + struct max_flow_type { + typedef typename graph_type::node_iterator node_iterator; + typedef typename graph_type::edge_iterator edge_iterator; + typedef typename graph_type::each_node_iterator each_node_iterator; + typedef typename graph_type::out_edge_iterator out_edge_iterator; + typedef typename graph_type::in_edge_iterator in_edge_iterator; + graph_type& G; + node_iterator s; + node_iterator t; + edge_property_vector flow; + edge_property_vector& capacity; + + max_flow_type(graph_type& _G, node_iterator _s, node_iterator _t, edge_property_vector& _capacity) : G(_G), s(_s), t(_t), flow(_G), capacity(_capacity) { + for(each_node_iterator i=G.first_node(); i.valid(); ++i) + for(out_edge_iterator j=G.first_out_edge(i); j.valid(); ++j) + flow.put(j, 0); + } + void run() { + typedef res_graph_type aug_graph_type; + aug_graph_type res_graph(G, flow, capacity); + + bool augment; + do { + augment=false; + + typedef std::queue bfs_queue_type; + bfs_queue_type bfs_queue; + bfs_queue.push(res_graph.first_out_edge(s)); + + typedef node_property_vector reached_type; + reached_type reached(res_graph, false); + reached.put(s, true); + + bfs_iterator1< aug_graph_type, reached_type > + res_bfs(res_graph, bfs_queue, reached); + + typedef node_property_vector pred_type; + pred_type pred(res_graph); + aug_graph_type::edge_iterator a; + a.make_invalid(); + pred.put(s, a); + + typedef node_property_vector free_type; + free_type free(res_graph); + + //searching for augmenting path + while ( res_bfs.valid() ) { + //std::cout<<"KULSO ciklus itt jar: "<"<"<"<"< + +namespace hugo { + + template + int number_of(iterator _it) { + int i=0; + for( ; _it.valid(); ++_it) { ++i; } + return i; + } + + template + class node_property_vector { + typedef typename graph_type::node_iterator node_iterator; + typedef typename graph_type::each_node_iterator each_node_iterator; + graph_type& G; + std::vector container; + public: + node_property_vector(graph_type& _G) : G(_G) { + int i=0; + for(each_node_iterator it=G.first_node(); it.valid(); ++it) ++i; + container.resize(i); + } + node_property_vector(graph_type& _G, T a) : G(_G) { + for(each_node_iterator it=G.first_node(); it.valid(); ++it) { container.push_back(a); } + } + void put(node_iterator nit, const T& a) { container[G.id(nit)]=a; } + T get(node_iterator nit) { return container[G.id(nit)]; } + }; + + template + class edge_property_vector { + typedef typename graph_type::edge_iterator edge_iterator; + typedef typename graph_type::each_edge_iterator each_edge_iterator; + graph_type& G; + std::vector container; + public: + edge_property_vector(graph_type& _G) : G(_G) { + int i=0; + for(each_edge_iterator it=G.first_edge(); it.valid(); ++it) ++i; + container.resize(i); + } + edge_property_vector(graph_type& _G, T a) : G(_G) { + for(each_edge_iterator it=G.first_edge(); it.valid(); ++it) { + container.push_back(a); + } + } + void put(edge_iterator eit, const T& a) { container[G.id(eit)]=a; } + T get(edge_iterator eit) { return container[G.id(eit)]; } + }; + +} // namespace hugo + +#endif //MARCI_PROPERTY_VECTOR_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci_bfs.hh --- a/src/work/marci_bfs.hh Sat Apr 03 14:22:33 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,176 +0,0 @@ -#ifndef MARCI_BFS_HH -#define MARCI_BFS_HH - -#include - -#include - -namespace hugo { - - template - struct bfs { - typedef typename graph_type::node_iterator node_iterator; - typedef typename graph_type::edge_iterator edge_iterator; - typedef typename graph_type::each_node_iterator each_node_iterator; - typedef typename graph_type::out_edge_iterator out_edge_iterator; - graph_type& G; - node_iterator s; - node_property_vector reached; - node_property_vector pred; - node_property_vector dist; - std::queue bfs_queue; - bfs(graph_type& _G, node_iterator _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) { - bfs_queue.push(s); - for(each_node_iterator i=G.first_node(); i.valid(); ++i) - reached.put(i, false); - reached.put(s, true); - dist.put(s, 0); - } - - void run() { - while (!bfs_queue.empty()) { - node_iterator v=bfs_queue.front(); - out_edge_iterator e=G.first_out_edge(v); - bfs_queue.pop(); - for( ; e.valid(); ++e) { - node_iterator w=G.head(e); - std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl; - if (!reached.get(w)) { - std::cout << G.id(w) << " is newly reached :-)" << std::endl; - bfs_queue.push(w); - dist.put(w, dist.get(v)+1); - pred.put(w, e); - reached.put(w, true); - } else { - std::cout << G.id(w) << " is already reached" << std::endl; - } - } - } - } - }; - - template - struct bfs_visitor { - typedef typename graph_type::node_iterator node_iterator; - typedef typename graph_type::edge_iterator edge_iterator; - typedef typename graph_type::out_edge_iterator out_edge_iterator; - graph_type& G; - bfs_visitor(graph_type& _G) : G(_G) { } - void at_previously_reached(out_edge_iterator& e) { - //node_iterator v=G.tail(e); - node_iterator w=G.head(e); - std::cout << G.id(w) << " is already reached" << std::endl; - } - void at_newly_reached(out_edge_iterator& e) { - //node_iterator v=G.tail(e); - node_iterator w=G.head(e); - std::cout << G.id(w) << " is newly reached :-)" << std::endl; - } - }; - - template - struct bfs_iterator { - typedef typename graph_type::node_iterator node_iterator; - typedef typename graph_type::edge_iterator edge_iterator; - typedef typename graph_type::out_edge_iterator out_edge_iterator; - graph_type& G; - std::queue& bfs_queue; - reached_type& reached; - visitor_type& visitor; - void process() { - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - if (bfs_queue.empty()) return; - out_edge_iterator e=bfs_queue.front(); - //node_iterator v=G.tail(e); - node_iterator w=G.head(e); - if (!reached.get(w)) { - visitor.at_newly_reached(e); - bfs_queue.push(G.first_out_edge(w)); - reached.put(w, true); - } else { - visitor.at_previously_reached(e); - } - } - bfs_iterator(graph_type& _G, std::queue& _bfs_queue, reached_type& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) { - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - valid(); - } - bfs_iterator& operator++() { - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - //if (bfs_queue.empty()) return *this; - if (!valid()) return *this; - ++(bfs_queue.front()); - //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - valid(); - return *this; - } - //void next() { - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - // if (bfs_queue.empty()) return; - // ++(bfs_queue.front()); - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - //} - bool valid() { - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - if (bfs_queue.empty()) return false; else return true; - } - //bool finished() { - // while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - // if (bfs_queue.empty()) return true; else return false; - //} - operator edge_iterator () { return bfs_queue.front(); } - - }; - - template - struct bfs_iterator1 { - typedef typename graph_type::node_iterator node_iterator; - typedef typename graph_type::edge_iterator edge_iterator; - typedef typename graph_type::out_edge_iterator out_edge_iterator; - graph_type& G; - std::queue& bfs_queue; - reached_type& reached; - bool _newly_reached; - bfs_iterator1(graph_type& _G, std::queue& _bfs_queue, reached_type& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) { - valid(); - if (!bfs_queue.empty() && bfs_queue.front().valid()) { - out_edge_iterator e=bfs_queue.front(); - node_iterator w=G.head(e); - if (!reached.get(w)) { - bfs_queue.push(G.first_out_edge(w)); - reached.put(w, true); - _newly_reached=true; - } else { - _newly_reached=false; - } - } - } - bfs_iterator1& operator++() { - if (!valid()) return *this; - ++(bfs_queue.front()); - valid(); - if (!bfs_queue.empty() && bfs_queue.front().valid()) { - out_edge_iterator e=bfs_queue.front(); - node_iterator w=G.head(e); - if (!reached.get(w)) { - bfs_queue.push(G.first_out_edge(w)); - reached.put(w, true); - _newly_reached=true; - } else { - _newly_reached=false; - } - } - return *this; - } - bool valid() { - while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } - if (bfs_queue.empty()) return false; else return true; - } - operator edge_iterator () { return bfs_queue.front(); } - bool newly_reached() { return _newly_reached; } - - }; - -} // namespace hugo - -#endif //MARCI_BFS_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci_graph_concept.txt --- a/src/work/marci_graph_concept.txt Sat Apr 03 14:22:33 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,217 +0,0 @@ -ETIK-OL-NOLIB-NEGRES graph concept-ek. - - Ebben a dokumentacioban graph concept tervek es azok megvalositasarol irok. -A felsorolt rutinok, osztalyok egyaltalan nem kikristalyosodottak, 1-1 elemi -operacio elvegzesere gyakran tobb mod is rendelkezesre all. A tervezesi fazisban pont annak kell kiderulnie, hogy milyen metodusok tavolithatok el, s milyen -ujakra van szukseg. - - Megvalositottunk egy graph osztalyt mely listaban tarolja a pontokat, -az 1 pontbol kiindulo eleket, s az 1 pontba bemeno eleket. Konstrualni lehet -ures grafot, hozzaadni pontokat, eleket. Az incidenciat node_iteratorok-kal -ill. edge_iteratorokkal lehet megfigyelni. Adott tovabba 1 template osztaly, -a graf pontjaihoz vagy eleihez tetszoleges tipusu property hozzarendelesere, -a jelen megvalositas ezeket vektorben tarolja. Fontos azonban, hogy ezen -property_vector csak azokra a graf-objektumokra ervenyes, melyek -letrehozasanak pillanataban a grafhoz tartoznak. - -marci_bfs.hh //bfs, tejesen kiserleti -marci_graph_demo.cc //peldaprogi a lisas graf hasznalatahoz -marci_list_graph.hh //list_graph megvalositas -marci_max_flow.hh //folyam, kiserleti -marci_property_vector.hh //property vector megvalosites indexelt grafokhoz -graf es iterator tipusok: - -class list_graph; - -class node_iterator; -trivialis node iterator, csak cimezni lehet vele, pl property vectort - -class each_node_iterator; -node iterator a graf pontjainak bejarasara, node_iterator-ra konvertalhato - -class edge_iterator; -trivialis edge iterator, csak cimezni lehet vele, pl property vectort - -class each_edge_iterator; -edge iterator a graf osszes elenek bejarasara - -class out_edge_iterator; -edge iterator 1 pont ki eleinek bejarasara, edge_iterator-ra konvertalhato - -class in_edge_iterator; -edge iterator 1 pont be eleinek bejarasara, edge_iterator-ra konvertalhato - -class sym_edge_iterator; -edge iterator 1 pont be es ki eleinek bejarasara, edge_iterator-ra -konvertalhato - -default constructor: - -list_graph(); - -A graf osztaly fobb publikus metodusai, az alapveto hasznalathoz: -Hasonlo funkciok megvalosithatok 1 kesobb leirt modon, illetve -ezek kozul nehany az iteratorok metodusaival, megis azt javasolnam, hogy az -iteratorok metodusait ne hasznaljuk. Miert? Azt szeretnenk, ha 1 ponthalmazon -van 2 graf, es csak az elhalmazhoz keszitunk uj iteratorokat, akkor pl 1 pont -out-edge-iteratora megkaphato legyen a grafbol es a node_iteratorbol. Ezert -out_edge_iterator(const node_iterator&) hasznalata nem javasolt, -esetleg majd szamuzzuk a concept-bol, s akkor nem nesz baj. - -each_node_iterator first_node(); -each_edge_iterator first_edge(); -out_edge_iterator first_out_edge(const node_iterator&); -in_edge_iterator first_in_edge(const node_iterator&); -sym_edge_iterator first_sym_edge(const node_iterator&); - -node_iterator tail(const edge_iterator&); -node_iterator head(const edge_iterator&); - -node_iterator a_node(const out_edge_iterator&); -node_iterator a_node(const in_edge_iterator&); -node_iterator a_node(const sym_edge_iterator&); -//az out, in or sym edge iterator rogzitett pontjara ad 1 node_iterator-t - -node_iterator b_node(const out_edge_iterator&); -node_iterator b_node(const in_edge_iterator&); -node_iterator b_node(const sym_edge_iterator&); -//az out, in or sym edge iterator nem rogzitett pontjara ad 1 node_iterator-t - -//node_iterator invalid_node(); -//edge_iterator invalid_edge(); -//out_edge_iterator invalid_out_edge(); -//in_edge_iterator invalid_in_edge(); -//sym_edge_iterator invalid_sym_edge(); - -//az iteratorok ures konstruktorai meghatarozatlan -tartalmu konstruktort adnak vissza, ezekkel a matodusokkal -lehet ervenytelent csinalni. -Lehet hogy ezt az ures konstruktorral kellene, tessek vitatkozni. - -Kiserleti cellal ugyanezen fv-ek mas stilusu megvalositasai: - -void get_first(each_node_iterator&); -void get_first(each_edge_iterator&); -void get_first(out_edge_iterator&, const node_iterator&); -void get_first(in_edge_iterator&, const node_iterator&); -void get_first(sym_edge_iterator&, const node_iterator&); - -void get_tail(node_iterator&, const edge_iterator&); -void get_head(node_iterator&, const edge_iterator&); - -void get_a_node(node_iterator&, const out_edge_iterator&); -void get_a_node(node_iterator&, const in_edge_iterator&); -void get_a_node(node_iterator&, const sym_edge_iterator&); - -void get_b_node(node_iterator&, const out_edge_iterator&); -void get_b_node(node_iterator&, const in_edge_iterator&); -void get_b_node(node_iterator&, const sym_edge_iterator&); - -//void get_invalid(node_iterator&); -//void get_invalid(edge_iterator&); -//void get_invalid(out_edge_iterator&); -//void get_invalid(in_edge_iterator&); -//void get_invalid(sym_edge_iterator&); - -Pontok azonositasara de meginkabb property vectorokhoz: - -int id(const node_iterator&); -int id(const edge_iterator&); - -Pontok es elek hozzaadasanak metodusai: - -node_iterator add_node(); -edge_iterator add_edge(const node_iterator&, const node_iterator&); - -Hogy konnyebb legyen a progikat tesztelni, nehany stream utasitas: -ezek nem a list_graph metodusai - -friend std::ostream& operator<<(std::ostream&, const node_iterator&); -friend std::ostream& operator<<(std::ostream&, const edge_iterator&); - -node_iterator metodusai: -node_iterator(); -bool valid(); -void make_invalid(); -ezek nem tagfuggvenyek: -friend bool operator==(const node_iterator&, const node_iterator&); -friend bool operator!=(const node_iterator& u, const node_iterator& v); - -each_node_iterator metodusai: -ez publikusan szarmazik a node_iterator-bol, tehat a fentiek is. -each_node_iterator(); -each_node_iterator& operator++(); - -edge_iterator metodusai: -edge_iterator(); -bool valid(); -void make_invalid(); -ezek nem tagfvek: -friend bool operator==(const edge_iterator&, const edge_iterator&); -friend bool operator!=(const edge_iterator&, const edge_iterator&); -ujra tagfv-ek. -//node_iterator tail_node() const; nem javasolt -//node_iterator head_node() const; nem javasolt - -each_edge_iterator metodusai: -edge_iterator-bol szarmazik -each_edge_iterator(); -each_edge_iterator& operator++(); - -out_edge_iterator metodusai: -edge_iterator-bol szarmazik -out_edge_iterator(); -//out_edge_iterator(const node_iterator&); nem javasolt -out_edge_iterator& operator++(); -//node_iterator a_node() const; nem javasolt -//node_iterator b_node() const; - - -in_edge_iterator metodusai: -edge_iterator-bol szarmazik -in_edge_iterator(); -//in_edge_iterator(const node_iterator&); nem javasolt -in_edge_iterator& operator++(); -//node_iterator a_node() const; nem javasolt -//node_iterator b_node() const; - - -sym_edge_iterator metodusai: -edge_iterator-bol szarmazik -sym_edge_iterator(); -//sym_edge_iterator(const node_iterator&); nem javasolt -sym_edge_iterator& operator++(); -//node_iterator a_node() const; nem javasolt -//node_iterator b_node() const; - -Node propery array-okrol: - -template -class node_property_vector; - -metodusok: - -node_property_vector(graph_type&); -void put(graph_type::node_iterator, const T&); -T get(graph_type::node_iterator); - -Ugyanez edge_property_array-okkal - -template -class edge_property_vector; - -edge_property_vector(graph_type&); -void put(graph_type::edge_iterator, const T&); -get(graph_type::edge_iterator); - - Ennyi nem javasolas utan, meg nehany szo. - Alparral ugy gondoltuk, hogy az iterator 1 olyan egyszeru objetum legyen -csak, mellyel, ha ervenyes, akkor lehet tovabblepni 1 pont vagy ellistaban. -Az hogy valamilyen pont-iteratorbeol el-iteratort csinalunk, igenis legyen a -graf objektum feladata, hiszen igy lehet csinelni ugyanazon a ponthalmazon -tobb grafot ugyanazon pont-iteratorokkal. - Sokkal komolyabb kerdesek merultek fel azzal kapcsolatban, hogy hogyan adjuk -at a propertyket az algoritmusoknak, algoritmus-objektumoknak. -Errol majd kesobb. - -marci@cs.elte.hu diff -r be43902fadb7 -r 19f3943521ab src/work/marci_graph_demo.cc --- a/src/work/marci_graph_demo.cc Sat Apr 03 14:22:33 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,270 +0,0 @@ -#include -#include -#include - -#include -#include -#include - -using namespace hugo; - -int main (int, char*[]) -{ - typedef ListGraph::Node Node; - typedef ListGraph::Edge Edge; - typedef ListGraph::NodeIt NodeIt; - typedef ListGraph::EdgeIt EdgeIt; - typedef ListGraph::OutEdgeIt OutEdgeIt; - typedef ListGraph::InEdgeIt InEdgeIt; - typedef ListGraph::SymEdgeIt SymEdgeIt; - ListGraph G; - std::vector vector_of_Nodes; - for(int i=0; i!=8; ++i) vector_of_Nodes.push_back(G.addNode()); - for(int i=0; i!=8; ++i) - for(int j=0; j!=8; ++j) - if ((ij is arc iff i()) << std::endl; - - for(NodeIt i=G.first(); G.valid(i); G.next(i)) { - std::cout << "node " << G.id(i) << std::endl; - std::cout << " outdegree (OutEdgeIt): " << count(G.first(i)) << " "; - for(OutEdgeIt j=G.first(i); G.valid(j); G.next(j)) { - std::cout << "(" << G.id(G.tail(j)) << "--" << G.id(j) << "->" << G.id(G.head(j)) << ") "; - } - std::cout << std::endl; - - std::cout<< " "; - for(OutEdgeIt j=G.first(i); G.valid(j); G.next(j)) { - std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; } - std::cout<(i)) << " "; - for(InEdgeIt j=G.first(i); G.valid(j); G.next(j)) { - std::cout << j << " "; } - std::cout << std::endl; - - std::cout<< " "; - for(InEdgeIt j=G.first(i); G.valid(j); G.next(j)) { - std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; } - std::cout<(i)) << " "; - for(SymEdgeIt j=G.first(i); G.valid(j); G.next(j)) { - std::cout << j << " "; } - std::cout<(i); G.valid(j); G.next(j)) { - std::cout << G.aNode(j) << "->" << G.bNode(j) << " "; } - std::cout<(); G.valid(i); G.next(i)) { - std::cout << i << " "; - } - std::cout << std::endl; - - std::cout << "node property array test" << std::endl; - ListGraph::NodeMap my_property_vector(G); - NodeIt v; - G.first(v); - my_property_vector.set(v, 42); - my_property_vector.set(G.next(G.first()), 314); - my_property_vector.set(G.next(G.next(G.first())), 1956); - my_property_vector.set(vector_of_Nodes[3], 1989); - my_property_vector.set(vector_of_Nodes[4], 2003); - my_property_vector.set(vector_of_Nodes[7], 1978); - std::cout << "some node property values..." << std::endl; - for(NodeIt i=G.first(); G.valid(i); G.next(i)) { - std::cout << my_property_vector.get(i) << std::endl; - } - int _i=1; - int _ii=1; - ListGraph::EdgeMap my_edge_property(G); - for(EdgeIt i=G.first(); G.valid(i); G.next(i)) { - my_edge_property.set(i, _i); - _i*=_ii; ++_ii; - } - - std::cout << "node and edge property values on the tails and heads of edges..." << std::endl; - for(EdgeIt j=G.first(); G.valid(j); G.next(j)) { - std::cout << my_property_vector.get(G.tail(j)) << "--" << my_edge_property.get(j) << "-->" << my_property_vector.get(G.head(j)) << " "; - } - std::cout << std::endl; -/* - std::cout << "bfs from the first node" << std::endl; - bfs bfs_test(G, G.first()); - bfs_test.run(); - std::cout << "reached: "; - for(NodeIt i=G.first(); G.valid(i); G.next(i)) { - std::cout << bfs_test.reached.get(i) << " "; - } - std::cout<(); G.valid(i); G.next(i)) { - std::cout << bfs_test.dist.get(i) << " "; - } - std::cout< node_name(flowG); - node_name.set(s, "s"); - node_name.set(v1, "v1"); - node_name.set(v2, "v2"); - node_name.set(v3, "v3"); - node_name.set(v4, "v4"); - node_name.set(t, "t"); - - Edge s_v1=flowG.addEdge(s, v1); - Edge s_v2=flowG.addEdge(s, v2); - Edge v1_v2=flowG.addEdge(v1, v2); - Edge v2_v1=flowG.addEdge(v2, v1); - Edge v1_v3=flowG.addEdge(v1, v3); - Edge v3_v2=flowG.addEdge(v3, v2); - Edge v2_v4=flowG.addEdge(v2, v4); - Edge v4_v3=flowG.addEdge(v4, v3); - Edge v3_t=flowG.addEdge(v3, t); - Edge v4_t=flowG.addEdge(v4, t); - - ListGraph::EdgeMap cap(flowG); - - cap.set(s_v1, 16); - cap.set(s_v2, 13); - cap.set(v1_v2, 10); - cap.set(v2_v1, 4); - cap.set(v1_v3, 12); - cap.set(v3_v2, 9); - cap.set(v2_v4, 14); - cap.set(v4_v3, 7); - cap.set(v3_t, 20); - cap.set(v4_t, 4); - - std::cout << "on directed graph graph" << std::endl; //<< flowG; - std::cout << "names and capacity values" << std::endl; - for(NodeIt i=flowG.first(); flowG.valid(i); flowG.next(i)) { - std::cout << node_name.get(i) << ": "; - std::cout << "out edges: "; - for(OutEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; - std::cout << "in edges: "; - for(InEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; - std::cout << std::endl; - } - - //flowG.deleteEdge(s_v1); - //flowG.deleteEdge(s_v2); - //flowG.deleteEdge(v1_v2); - //flowG.deleteEdge(v1_v3); - - - //flowG.setTail(v3_t, v2); - //flowG.setHead(v3_t, s); -/* - for(NodeIt i=flowG.first(); flowG.valid(i); flowG.next(i)) { - std::cout << node_name.get(i) << ": "; - std::cout << "out edges: "; - for(OutEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; - std::cout << "in edges: "; - for(InEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; - std::cout << std::endl; - } - - for(EdgeIt e=flowG.first(); flowG.valid(e); flowG.next(e)) { - std::cout << node_name.get(flowG.tail(e)) << "-"<< cap.get(e) << "->" << node_name.get(flowG.head(e)) << " "; - } -*/ - /* - while (flowG.valid(flowG.first())) { - flowG.deleteEdge(flowG.first()); - for(NodeIt i=flowG.first(); flowG.valid(i); flowG.next(i)) { - std::cout << node_name.get(i) << ": "; - std::cout << "out edges: "; - for(OutEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; - std::cout << "in edges: "; - for(InEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; - std::cout << std::endl; - } - } - - while (flowG.valid(flowG.first())) { - flowG.deleteNode(flowG.first()); - for(NodeIt i=flowG.first(); flowG.valid(i); flowG.next(i)) { - std::cout << node_name.get(i) << ": "; - std::cout << "out edges: "; - for(OutEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; - std::cout << "in edges: "; - for(InEdgeIt j=flowG.first(i); flowG.valid(j); flowG.next(j)) - std::cout << node_name.get(flowG.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flowG.head(j)) << " "; - std::cout << std::endl; - } - } - */ - - //std::cout << std::endl; - - - { - ListGraph::EdgeMap flow(flowG, 0); - MaxFlow, ListGraph::EdgeMap > max_flow_test(flowG, s, t, flow, cap); - /* - max_flow_test.augmentOnBlockingFlow(); - for(EdgeIt e=flowG.template first(); flowG.valid(e); flowG.next(e)) { - std::cout<<"("<"<(); - for(EdgeIt e=flowG.template first(); flowG.valid(e); flowG.next(e)) { - std::cout<<"("<"<(); flowG.valid(e); flowG.next(e)) { - std::cout<<"("<"< S; - S.push_back(s); S.push_back(v3); - std::list T; - T.push_back(t); - - ListGraph::EdgeMap flow(flowG, 0); - MaxFlow2, ListGraph::EdgeMap > max_flow_test(flowG, S, T, flow, cap); - max_flow_test.run(); - - std::cout << "maximum flow: "<< std::endl; - for(EdgeIt e=flowG.template first(); flowG.valid(e); flowG.next(e)) { - std::cout<<"("<"< - struct graph_traits { - typedef typename graph_type::node_iterator node_iterator; - typedef typename graph_type::edge_iterator edge_iterator; - typedef typename graph_type::each_node_iterator each_node_iterator; - typedef typename graph_type::each_edge_iterator each_edge_iterator; - typedef typename graph_type::out_edge_iterator out_edge_iterator; - typedef typename graph_type::in_edge_iterator in_edge_iterator; - typedef typename graph_type::sym_edge_iterator sym_edge_iterator; - }; - -} // namespace hugo - -#endif //MARCI_GRAPH_TRAITS_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci_list_graph.hh --- a/src/work/marci_list_graph.hh Sat Apr 03 14:22:33 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,343 +0,0 @@ -#ifndef MARCI_LIST_GRAPH_HH -#define MARCI_LIST_GRAPH_HH - -#include - -namespace hugo { - - class list_graph { - class node_item; - class edge_item; - public: - class node_iterator; - class each_node_iterator; - class edge_iterator; - class each_edge_iterator; - class out_edge_iterator; - class in_edge_iterator; - class sym_edge_iterator; - private: - int node_id; - int edge_id; - int _node_num; - int _edge_num; - - node_item* _first_node; - node_item* _last_node; - - class node_item { - friend class list_graph; - friend class node_iterator; - friend class each_node_iterator; - friend class edge_iterator; - friend class each_edge_iterator; - friend class out_edge_iterator; - friend class in_edge_iterator; - friend class sym_edge_iterator; - friend std::ostream& operator<<(std::ostream& os, const node_iterator& i); - friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i); - list_graph* G; - int id; - edge_item* _first_out_edge; - edge_item* _last_out_edge; - edge_item* _first_in_edge; - edge_item* _last_in_edge; - node_item* _next_node; - node_item* _prev_node; - public: - node_item() { } - }; - - class edge_item { - friend class list_graph; - friend class node_iterator; - friend class each_node_iterator; - friend class edge_iterator; - friend class each_edge_iterator; - friend class out_edge_iterator; - friend class in_edge_iterator; - friend class sym_edge_iterator; - friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i); - list_graph* G; - int id; - node_item* _tail; - node_item* _head; - edge_item* _next_out; - edge_item* _prev_out; - edge_item* _next_in; - edge_item* _prev_in; - public: - edge_item() { } - }; - - node_item* _add_node() { - node_item* p=new node_item; - p->id=node_id++; - p->_first_out_edge=0; - p->_last_out_edge=0; - p->_first_in_edge=0; - p->_last_in_edge=0; - p->_prev_node=_last_node; - p->_next_node=0; - if (_last_node) _last_node->_next_node=p; - _last_node=p; - if (!_first_node) _first_node=p; - ++_node_num; - return p; - } - - edge_item* _add_edge(node_item* _tail, node_item* _head) { - edge_item* e=new edge_item; - e->id=edge_id++; - e->_tail=_tail; - e->_head=_head; - - e->_prev_out=_tail->_last_out_edge; - if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; - _tail->_last_out_edge=e; - if (!_tail->_first_out_edge) _tail->_first_out_edge=e; - - e->_prev_in=_head->_last_in_edge; - if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; - _head->_last_in_edge=e; - if (!_head->_first_in_edge) { _head->_first_in_edge=e; } - ++_edge_num; - return e; - } - - public: - - /* default constructor */ - - list_graph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { } - - int node_num() { return _node_num; } - int edge_num() { return _edge_num; } - - /* functions to construct iterators from the graph, or from each other */ - - each_node_iterator first_node() { return each_node_iterator(_first_node); } - each_edge_iterator first_edge() { - node_item* v=_first_node; - edge_item* edge=v->_first_out_edge; - while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } - return each_edge_iterator(v, edge); - } - - out_edge_iterator first_out_edge(const node_iterator& v) { - return out_edge_iterator(v); - } - in_edge_iterator first_in_edge(const node_iterator& v) { - return in_edge_iterator(v); - } - sym_edge_iterator first_sym_edge(const node_iterator& v) { - return sym_edge_iterator(v); - } - node_iterator tail(const edge_iterator& e) { return e.tail_node(); } - node_iterator head(const edge_iterator& e) { return e.head_node(); } - - node_iterator a_node(const out_edge_iterator& e) { return e.a_node(); } - node_iterator a_node(const in_edge_iterator& e) { return e.a_node(); } - node_iterator a_node(const sym_edge_iterator& e) { return e.a_node(); } - - node_iterator b_node(const out_edge_iterator& e) { return e.b_node(); } - node_iterator b_node(const in_edge_iterator& e) { return e.b_node(); } - node_iterator b_node(const sym_edge_iterator& e) { return e.b_node(); } - - //node_iterator invalid_node() { return node_iterator(); } - //edge_iterator invalid_edge() { return edge_iterator(); } - //out_edge_iterator invalid_out_edge() { return out_edge_iterator(); } - //in_edge_iterator invalid_in_edge() { return in_edge_iterator(); } - //sym_edge_iterator invalid_sym_edge() { return sym_edge_iterator(); } - - /* same methods in other style */ - /* for experimental purpose */ - - void get_first(each_node_iterator& v) { v=each_node_iterator(_first_node); } - void get_first(each_edge_iterator& e) { e=first_edge(); } - void get_first(out_edge_iterator& e, const node_iterator& v) { - e=out_edge_iterator(v); - } - void get_first(in_edge_iterator& e, const node_iterator& v) { - e=in_edge_iterator(v); - } - void get_first(sym_edge_iterator& e, const node_iterator& v) { - e=sym_edge_iterator(v); - } - void get_tail(node_iterator& n, const edge_iterator& e) { n=tail(e); } - void get_head(node_iterator& n, const edge_iterator& e) { n=head(e); } - - void get_a_node(node_iterator& n, const out_edge_iterator& e) { n=e.a_node(); } - void get_a_node(node_iterator& n, const in_edge_iterator& e) { n=e.a_node(); } - void get_a_node(node_iterator& n, const sym_edge_iterator& e) { n=e.a_node(); } - void get_b_node(node_iterator& n, const out_edge_iterator& e) { n=e.b_node(); } - void get_b_node(node_iterator& n, const in_edge_iterator& e) { n=e.b_node(); } - void get_b_node(node_iterator& n, const sym_edge_iterator& e) { n=e.b_node(); } - //void get_invalid(node_iterator& n) { n=node_iterator(); } - //void get_invalid(edge_iterator& e) { e=edge_iterator(); } - //void get_invalid(out_edge_iterator& e) { e=out_edge_iterator(); } - //void get_invalid(in_edge_iterator& e) { e=in_edge_iterator(); } - //void get_invalid(sym_edge_iterator& e) { e=sym_edge_iterator(); } - - - /* for getting id's of graph objects */ - /* these are important for the implementation of property vectors */ - - int id(const node_iterator& v) { return v.node->id; } - int id(const edge_iterator& e) { return e.edge->id; } - - /* adding nodes and edges */ - - node_iterator add_node() { return node_iterator(_add_node()); } - edge_iterator add_edge(const node_iterator& u, const node_iterator& v) { - return edge_iterator(_add_edge(u.node, v.node)); - } - - /* stream operations, for testing purpose */ - - friend std::ostream& operator<<(std::ostream& os, const node_iterator& i) { - os << i.node->id; return os; - } - friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i) { - os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; - return os; - } - - class node_iterator { - friend class list_graph; - - friend class edge_iterator; - friend class out_edge_iterator; - friend class in_edge_iterator; - friend class sym_edge_iterator; - protected: - node_item* node; - friend int list_graph::id(const node_iterator& v); - public: - node_iterator() : node(0) { } - node_iterator(node_item* _node) : node(_node) { } - bool valid() { return (node!=0); } - void make_invalid() { node=0; } - friend bool operator==(const node_iterator& u, const node_iterator& v) { - return v.node==u.node; - } - friend bool operator!=(const node_iterator& u, const node_iterator& v) { - return v.node!=u.node; - } - friend std::ostream& operator<<(std::ostream& os, const node_iterator& i); - }; - - class each_node_iterator : public node_iterator { - friend class list_graph; - public: - each_node_iterator() : node_iterator() { } - each_node_iterator(node_item* v) : node_iterator(v) { } - each_node_iterator& operator++() { node=node->_next_node; return *this; } - }; - - class edge_iterator { - friend class list_graph; - - friend class node_iterator; - friend class each_node_iterator; - protected: - edge_item* edge; - friend int list_graph::id(const edge_iterator& e); - public: - edge_iterator() : edge(0) { } - edge_iterator(edge_item* _edge) : edge(_edge) { } - bool valid() { return (edge!=0); } - void make_invalid() { edge=0; } - friend bool operator==(const edge_iterator& u, const edge_iterator& v) { - return v.edge==u.edge; - } - friend bool operator!=(const edge_iterator& u, const edge_iterator& v) { - return v.edge!=u.edge; - } - protected: - node_iterator tail_node() const { return node_iterator(edge->_tail); } - node_iterator head_node() const { return node_iterator(edge->_head); } - public: - friend std::ostream& operator<<(std::ostream& os, const edge_iterator& i); - }; - - class each_edge_iterator : public edge_iterator { - friend class list_graph; - node_item* v; - public: - each_edge_iterator() : edge_iterator(), v(0) { } - each_edge_iterator(node_item* _v, edge_item* _e) : edge_iterator(_e), v(_v) { } - each_edge_iterator& operator++() { - edge=edge->_next_out; - while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } - return *this; - } - }; - - class out_edge_iterator : public edge_iterator { - friend class list_graph; - node_item* v; - public: - out_edge_iterator() : edge_iterator(), v(0) { } - protected: - out_edge_iterator(const node_iterator& _v) : v(_v.node) { edge=v->_first_out_edge; } - public: - out_edge_iterator& operator++() { edge=edge->_next_out; return *this; } - protected: - node_iterator a_node() const { return node_iterator(v); } - node_iterator b_node() const { - return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); } - }; - - class in_edge_iterator : public edge_iterator { - friend class list_graph; - node_item* v; - public: - in_edge_iterator() : edge_iterator(), v(0) { } - protected: - in_edge_iterator(const node_iterator& _v) : v(_v.node) { - edge=v->_first_in_edge; - } - public: - in_edge_iterator& operator++() { edge=edge->_next_in; return *this; } - protected: - node_iterator a_node() const { return node_iterator(v); } - node_iterator b_node() const { - return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); } - }; - - class sym_edge_iterator : public edge_iterator { - friend class list_graph; - bool out_or_in; //1 iff out, 0 iff in - node_item* v; - public: - sym_edge_iterator() : edge_iterator(), v(0) { } - protected: - sym_edge_iterator(const node_iterator& _v) : v(_v.node) { - out_or_in=1; - edge=v->_first_out_edge; - if (!edge) { edge=v->_first_in_edge; out_or_in=0; } - } - public: - sym_edge_iterator& operator++() { - if (out_or_in) { - edge=edge->_next_out; - if (!edge) { out_or_in=0; edge=v->_first_in_edge; } - } else { - edge=edge->_next_in; - } - return *this; - } - protected: - node_iterator a_node() const { return node_iterator(v); } - node_iterator b_node() const { - return (edge->_tail==v) ? node_iterator(edge->_head) : node_iterator(edge->_tail); } - }; - - }; - - - -} //namespace hugo - -#endif //MARCI_LIST_GRAPH_HH diff -r be43902fadb7 -r 19f3943521ab src/work/marci_makefile --- a/src/work/marci_makefile Sat Apr 03 14:22:33 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -CXXFLAGS = -Wall -ansi -I. -CXX = g++-3.0 - -marci_graph_demo: marci_graph_demo.cc marci_list_graph.hh marci_property_vector.hh marci_bfs.hh marci_max_flow.hh - $(CXX) $(CXXFLAGS) marci_graph_demo.cc -o marci_graph_demo \ No newline at end of file diff -r be43902fadb7 -r 19f3943521ab src/work/marci_max_flow.hh --- a/src/work/marci_max_flow.hh Sat Apr 03 14:22:33 2004 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,183 +0,0 @@ -#ifndef MARCI_MAX_FLOW_HH -#define MARCI_MAX_FLOW_HH - -#include - -#include -#include - -namespace hugo { - - template - class res_graph_type { - typedef typename graph_type::node_iterator node_iterator; - typedef typename graph_type::each_node_iterator each_node_iterator; - typedef typename graph_type::sym_edge_iterator old_sym_edge_iterator; - graph_type& G; - edge_property_vector& flow; - edge_property_vector& capacity; - public: - res_graph_type(graph_type& _G, edge_property_vector& _flow, edge_property_vector& _capacity) : G(_G), flow(_flow), capacity(_capacity) { } - - class edge_iterator { - friend class res_graph_type; - protected: - res_graph_type* resG; - old_sym_edge_iterator sym; - public: - edge_iterator() { } - //bool is_free() { - //if (resG->G.a_node(sym)==resG->G.tail(sym)) { - // return (resG->flow.get(sym)capacity.get(sym)); - //} else { - // return (resG->flow.get(sym)>0); - //} - //} - T free() { - if (resG->G.a_node(sym)==resG->G.tail(sym)) { - return (resG->capacity.get(sym)-resG->flow.get(sym)); - } else { - return (resG->flow.get(sym)); - } - } - bool valid() { return sym.valid(); } - void make_invalid() { sym.make_invalid(); } - void augment(T a) { - if (resG->G.a_node(sym)==resG->G.tail(sym)) { - resG->flow.put(sym, resG->flow.get(sym)+a); - } else { - resG->flow.put(sym, resG->flow.get(sym)-a); - } - } - }; - - class out_edge_iterator : public edge_iterator { - public: - out_edge_iterator() { } - out_edge_iterator(res_graph_type& _resG, const node_iterator& v) { - resG=&_resG; - sym=resG->G.first_sym_edge(v); - while( sym.valid() && !(free()>0) ) { ++sym; } - } - out_edge_iterator& operator++() { - ++sym; - while( sym.valid() && !(free()>0) ) { ++sym; } - return *this; - } - }; - - out_edge_iterator first_out_edge(const node_iterator& v) { - return out_edge_iterator(*this, v); - } - - each_node_iterator first_node() { - return G.first_node(); - } - - node_iterator tail(const edge_iterator& e) { return G.a_node(e.sym); } - node_iterator head(const edge_iterator& e) { return G.b_node(e.sym); } - - int id(const node_iterator& v) { return G.id(v); } - - //node_iterator invalid_node() { return G.invalid_node(); } - //res_edge_it invalid_edge() { res_edge_it n; n.sym=G.invalid_sym_edge(); return n; } - }; - - template - struct max_flow_type { - typedef typename graph_type::node_iterator node_iterator; - typedef typename graph_type::edge_iterator edge_iterator; - typedef typename graph_type::each_node_iterator each_node_iterator; - typedef typename graph_type::out_edge_iterator out_edge_iterator; - typedef typename graph_type::in_edge_iterator in_edge_iterator; - graph_type& G; - node_iterator s; - node_iterator t; - edge_property_vector flow; - edge_property_vector& capacity; - - max_flow_type(graph_type& _G, node_iterator _s, node_iterator _t, edge_property_vector& _capacity) : G(_G), s(_s), t(_t), flow(_G), capacity(_capacity) { - for(each_node_iterator i=G.first_node(); i.valid(); ++i) - for(out_edge_iterator j=G.first_out_edge(i); j.valid(); ++j) - flow.put(j, 0); - } - void run() { - typedef res_graph_type aug_graph_type; - aug_graph_type res_graph(G, flow, capacity); - - bool augment; - do { - augment=false; - - typedef std::queue bfs_queue_type; - bfs_queue_type bfs_queue; - bfs_queue.push(res_graph.first_out_edge(s)); - - typedef node_property_vector reached_type; - reached_type reached(res_graph, false); - reached.put(s, true); - - bfs_iterator1< aug_graph_type, reached_type > - res_bfs(res_graph, bfs_queue, reached); - - typedef node_property_vector pred_type; - pred_type pred(res_graph); - aug_graph_type::edge_iterator a; - a.make_invalid(); - pred.put(s, a); - - typedef node_property_vector free_type; - free_type free(res_graph); - - //searching for augmenting path - while ( res_bfs.valid() ) { - //std::cout<<"KULSO ciklus itt jar: "<"<"<"<"< - -namespace hugo { - - template - int number_of(iterator _it) { - int i=0; - for( ; _it.valid(); ++_it) { ++i; } - return i; - } - - template - class node_property_vector { - typedef typename graph_type::node_iterator node_iterator; - typedef typename graph_type::each_node_iterator each_node_iterator; - graph_type& G; - std::vector container; - public: - node_property_vector(graph_type& _G) : G(_G) { - int i=0; - for(each_node_iterator it=G.first_node(); it.valid(); ++it) ++i; - container.resize(i); - } - node_property_vector(graph_type& _G, T a) : G(_G) { - for(each_node_iterator it=G.first_node(); it.valid(); ++it) { container.push_back(a); } - } - void put(node_iterator nit, const T& a) { container[G.id(nit)]=a; } - T get(node_iterator nit) { return container[G.id(nit)]; } - }; - - template - class edge_property_vector { - typedef typename graph_type::edge_iterator edge_iterator; - typedef typename graph_type::each_edge_iterator each_edge_iterator; - graph_type& G; - std::vector container; - public: - edge_property_vector(graph_type& _G) : G(_G) { - int i=0; - for(each_edge_iterator it=G.first_edge(); it.valid(); ++it) ++i; - container.resize(i); - } - edge_property_vector(graph_type& _G, T a) : G(_G) { - for(each_edge_iterator it=G.first_edge(); it.valid(); ++it) { - container.push_back(a); - } - } - void put(edge_iterator eit, const T& a) { container[G.id(eit)]=a; } - T get(edge_iterator eit) { return container[G.id(eit)]; } - }; - -} // namespace hugo - -#endif //MARCI_PROPERTY_VECTOR_HH