diff -r cd54905012bc -r a9ed3f1c2c63 src/work/marci_max_flow.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/marci_max_flow.hh Tue Dec 30 13:59:08 2003 +0000 @@ -0,0 +1,230 @@ +#ifndef MARCI_MAX_FLOW_HH +#define MARCI_MAX_FLOW_HH + +#include + +#include +#include +#include + +namespace marci { + + template + class res_graph_type { + typedef typename graph_traits::node_iterator node_iterator; + typedef typename graph_traits::edge_iterator edge_iterator; + typedef typename graph_traits::each_node_iterator each_node_iterator; + typedef typename graph_traits::sym_edge_iterator 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 res_edge_it { + friend class res_graph_type; + protected: + res_graph_type* resG; + sym_edge_iterator sym; + public: + res_edge_it() { } + //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 is_valid() { return sym.is_valid(); } + 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 res_out_edge_it : public res_edge_it { + public: + res_out_edge_it() { } + res_out_edge_it(res_graph_type& _resG, const node_iterator& v) { + resG=&_resG; + sym=resG->G.first_sym_edge(v); + while( sym.is_valid() && !(free()>0) ) { ++sym; } + } + res_out_edge_it& operator++() { + ++sym; + while( sym.is_valid() && !(free()>0) ) { ++sym; } + return *this; + } + }; + + res_out_edge_it first_out_edge(const node_iterator& v) { + return res_out_edge_it(*this, v); + } + + each_node_iterator first_node() { + return G.first_node(); + } + + node_iterator tail(const res_edge_it& e) { return G.a_node(e.sym); } + node_iterator head(const res_edge_it& 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 graph_traits< res_graph_type > { + typedef typename graph_traits::node_iterator node_iterator; + typedef typename res_graph_type::res_edge_it edge_iterator; + typedef typename graph_traits::each_node_iterator each_node_iterator; + typedef typename res_graph_type::res_out_edge_it out_edge_iterator; + }; + + template + struct flow_visitor { + typedef typename graph_traits::node_iterator node_iterator; + typedef typename graph_traits::edge_iterator edge_iterator; + typedef typename graph_traits::each_node_iterator each_node_iterator; + typedef typename graph_traits::out_edge_iterator out_edge_iterator; + graph_type& G; + pred_type& pred; + free_type& free; + flow_visitor(graph_type& _G, pred_type& _pred, free_type& _free) : G(_G), pred(_pred), free(_free) { } + void at_previously_reached(out_edge_iterator& e) { + //node_iterator v=G.tail(e); + //node_iterator w=G.head(e); + //std::cout<"<"< + struct max_flow_type { + + typedef typename graph_traits::node_iterator node_iterator; + typedef typename graph_traits::edge_iterator edge_iterator; + typedef typename graph_traits::each_node_iterator each_node_iterator; + typedef typename graph_traits::out_edge_iterator out_edge_iterator; + typedef typename graph_traits::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.is_valid(); ++i) + for(out_edge_iterator j=G.first_out_edge(i); j.is_valid(); ++j) + flow.put(j, 0); + } + void run() { + typedef res_graph_type aug_graph_type; + aug_graph_type res_graph(G, flow, capacity); + + typedef std::queue::out_edge_iterator> 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_type reached(res_graph); + //reached.put(s, true); + + typedef node_property_vector::edge_iterator> pred_type; + pred_type pred(res_graph); + pred.put(s, res_graph.invalid_edge()); + + typedef node_property_vector free_type; + free_type free(res_graph); + + typedef flow_visitor visitor_type; + visitor_type vis(res_graph, pred, free); + + bfs_iterator< aug_graph_type, reached_type, visitor_type > + res_bfs(res_graph, bfs_queue, reached, vis); + + //for(graph_traits::each_node_iterator i=res_graph.first_node(); i.is_valid(); ++i) { + //for(graph_traits::out_edge_iterator j=res_graph.first_out_edge(i); j.is_valid(); ++j) { + // std::cout<<"("<"<::each_node_iterator i=res_graph.first_node(); i.is_valid(); ++i) { reached.put(i, false); } + reached.put(s, true); + + //searching for augmenting path + while ( /*std::cin>>c &&*/ res_bfs.is_valid() ) { + res_bfs.process(); + //if (res_graph.head(graph_traits::out_edge_iterator(res_bfs))==t) break; + if (res_graph.head(res_bfs)==t) break; + //res_bfs.next(); + ++res_bfs; + } + //for (; std::cin>>c && !res_bfs.finished() && res_graph.head(res_bfs.current())!=t; res_bfs.next()) { res_bfs.process(); } + if (reached.get(t)) { + augment=true; + node_iterator n=t; + T augment_value=free.get(t); + std::cout<<"augmentation: "; + while (pred.get(n).is_valid()) { + graph_traits::edge_iterator e=pred.get(n); + e.augment(augment_value); + std::cout<<"("<"<::each_edge_iterator e=G.first_edge(); e.is_valid(); ++e) { + std::cout<<"("<"<