Itt van.
1 #ifndef EDMONDS_KARP_HH
2 #define EDMONDS_KARP_HH
8 #include <bfs_iterator.hh>
9 #include <time_measure.h>
13 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
15 typedef typename Graph::NodeIt NodeIt;
16 typedef typename Graph::EachNodeIt EachNodeIt;
17 typedef typename Graph::SymEdgeIt OldSymEdgeIt;
20 const CapacityMap& capacity;
22 ResGraph(const Graph& _G, FlowMap& _flow,
23 const CapacityMap& _capacity) :
24 G(_G), flow(_flow), capacity(_capacity) { }
29 friend class OutEdgeIt;
32 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
34 const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
38 //EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { }
40 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
41 return (resG->capacity.get(sym)-resG->flow.get(sym));
43 return (resG->flow.get(sym));
46 bool valid() const { return sym.valid(); }
47 void augment(Number a) const {
48 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
49 resG->flow.set(sym, resG->flow.get(sym)+a);
52 resG->flow.set(sym, resG->flow.get(sym)-a);
58 class OutEdgeIt : public EdgeIt {
59 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
62 //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
64 OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
66 sym=resG->G.template first<OldSymEdgeIt>(v);
67 while( sym.valid() && !(free()>0) ) { ++sym; }
70 OutEdgeIt& operator++() {
72 while( sym.valid() && !(free()>0) ) { ++sym; }
77 void getFirst(OutEdgeIt& e, NodeIt v) const {
78 e=OutEdgeIt(*this, v);
80 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
82 template< typename It >
89 template< typename It >
90 It first(NodeIt v) const {
96 NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); }
97 NodeIt head(EdgeIt e) const { return G.bNode(e.sym); }
99 NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
100 NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
102 int id(NodeIt v) const { return G.id(v); }
104 template <typename S>
106 typename Graph::NodeMap<S> node_map;
108 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
109 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
110 void set(NodeIt nit, S a) { node_map.set(nit, a); }
111 S get(NodeIt nit) const { return node_map.get(nit); }
112 S& operator[](NodeIt nit) { return node_map[nit]; }
113 const S& operator[](NodeIt nit) const { return node_map[nit]; }
119 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
121 typedef typename Graph::NodeIt NodeIt;
122 typedef typename Graph::EachNodeIt EachNodeIt;
123 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
124 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
125 typedef typename Graph::InEdgeIt OldInEdgeIt;
129 const CapacityMap& capacity;
131 ResGraph2(const Graph& _G, FlowMap& _flow,
132 const CapacityMap& _capacity) :
133 G(_G), flow(_flow), capacity(_capacity) { }
138 friend class OutEdgeIt;
141 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
143 const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
147 bool out_or_in; //1, iff out
149 EdgeIt() : out_or_in(1) { }
150 Number free() const {
152 return (resG->capacity.get(out)-resG->flow.get(out));
154 return (resG->flow.get(in));
158 return out_or_in && out.valid() || in.valid(); }
159 void augment(Number a) const {
161 resG->flow.set(out, resG->flow.get(out)+a);
163 resG->flow.set(in, resG->flow.get(in)-a);
168 class OutEdgeIt : public EdgeIt {
169 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
173 OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
175 out=resG->G.template first<OldOutEdgeIt>(v);
176 while( out.valid() && !(free()>0) ) { ++out; }
179 in=resG->G.template first<OldInEdgeIt>(v);
180 while( in.valid() && !(free()>0) ) { ++in; }
184 OutEdgeIt& operator++() {
186 NodeIt v=resG->G.aNode(out);
188 while( out.valid() && !(free()>0) ) { ++out; }
191 in=resG->G.template first<OldInEdgeIt>(v);
192 while( in.valid() && !(free()>0) ) { ++in; }
196 while( in.valid() && !(free()>0) ) { ++in; }
202 void getFirst(OutEdgeIt& e, NodeIt v) const {
203 e=OutEdgeIt(*this, v);
205 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
207 template< typename It >
214 template< typename It >
215 It first(NodeIt v) const {
221 NodeIt tail(EdgeIt e) const {
222 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
223 NodeIt head(EdgeIt e) const {
224 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
226 NodeIt aNode(OutEdgeIt e) const {
227 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
228 NodeIt bNode(OutEdgeIt e) const {
229 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
231 int id(NodeIt v) const { return G.id(v); }
233 template <typename S>
235 typename Graph::NodeMap<S> node_map;
237 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
238 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
239 void set(NodeIt nit, S a) { node_map.set(nit, a); }
240 S get(NodeIt nit) const { return node_map.get(nit); }
244 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
247 typedef typename Graph::NodeIt NodeIt;
248 typedef typename Graph::EachNodeIt EachNodeIt;
249 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
250 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
251 typedef typename Graph::InEdgeIt OldInEdgeIt;
256 const CapacityMap& capacity;
258 ResGraph3(const Graph& _G, FlowMap& _flow,
259 const CapacityMap& _capacity) :
260 G(_G), flow(_flow), capacity(_capacity) { }
265 friend class OutEdgeIt;
268 friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
270 //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
273 const CapacityMap* capacity;
277 bool out_or_in; //1, iff out
279 EdgeIt() : out_or_in(1) { }
280 Number free() const {
282 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out));
284 return (/*resG->*/flow->get(in));
288 return out_or_in && out.valid() || in.valid(); }
289 void augment(Number a) const {
291 /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
293 /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
298 class OutEdgeIt : public EdgeIt {
299 friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
303 OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) {
307 out=/*resG->*/G->template first<OldOutEdgeIt>(v);
308 while( out.valid() && !(free()>0) ) { ++out; }
311 in=/*resG->*/G->template first<OldInEdgeIt>(v);
312 while( in.valid() && !(free()>0) ) { ++in; }
316 OutEdgeIt& operator++() {
318 NodeIt v=/*resG->*/G->aNode(out);
320 while( out.valid() && !(free()>0) ) { ++out; }
323 in=/*resG->*/G->template first<OldInEdgeIt>(v);
324 while( in.valid() && !(free()>0) ) { ++in; }
328 while( in.valid() && !(free()>0) ) { ++in; }
334 void getFirst(OutEdgeIt& e, NodeIt v) const {
335 e=OutEdgeIt(G, v, flow, capacity);
337 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
339 template< typename It >
346 template< typename It >
347 It first(NodeIt v) const {
353 NodeIt tail(EdgeIt e) const {
354 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
355 NodeIt head(EdgeIt e) const {
356 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
358 NodeIt aNode(OutEdgeIt e) const {
359 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
360 NodeIt bNode(OutEdgeIt e) const {
361 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
363 int id(NodeIt v) const { return G.id(v); }
365 template <typename S>
367 typename Graph::NodeMap<S> node_map;
369 NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
370 NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
371 void set(NodeIt nit, S a) { node_map.set(nit, a); }
372 S get(NodeIt nit) const { return node_map.get(nit); }
378 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
380 typedef typename Graph::NodeIt NodeIt;
381 typedef typename Graph::EdgeIt EdgeIt;
382 typedef typename Graph::EachEdgeIt EachEdgeIt;
383 typedef typename Graph::OutEdgeIt OutEdgeIt;
384 typedef typename Graph::InEdgeIt InEdgeIt;
389 const CapacityMap& capacity;
390 typedef ResGraph3<Graph, Number, FlowMap, CapacityMap > AugGraph;
391 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
392 typedef typename AugGraph::EdgeIt AugEdgeIt;
394 //AugGraph res_graph;
395 //typedef typename AugGraph::NodeMap<bool> ReachedMap;
396 //typename AugGraph::NodeMap<AugEdgeIt> pred;
397 //typename AugGraph::NodeMap<int> free;
399 MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) :
400 G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) //,
401 //res_graph(G, flow, capacity), pred(res_graph), free(res_graph)
404 AugGraph res_graph(G, flow, capacity);
407 typedef typename AugGraph::NodeMap<bool> ReachedMap;
408 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
409 res_bfs.pushAndSetReached(s);
411 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
412 //filled up with invalid iterators
413 //pred.set(s, AugEdgeIt());
415 typename AugGraph::NodeMap<int> free(res_graph);
417 //searching for augmenting path
418 while ( !res_bfs.finished() ) {
419 AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
420 if (e.valid() && res_bfs.isBNodeNewlyReached()) {
421 NodeIt v=res_graph.tail(e);
422 NodeIt w=res_graph.head(e);
424 if (pred.get(v).valid()) {
425 free.set(w, std::min(free.get(v), e.free()));
427 free.set(w, e.free());
429 if (res_graph.head(e)==t) { _augment=true; break; }
433 } //end of searching augmenting path
437 Number augment_value=free.get(t);
438 while (pred.get(n).valid()) {
439 AugEdgeIt e=pred.get(n);
440 e.augment(augment_value);
447 bool augmentWithBlockingFlow() {
448 BfsIterator4< Graph, OutEdgeIt, typename Graph::NodeMap<bool> > bfs(G);
449 bfs.pushAndSetReached(s);
450 typename Graph::NodeMap<int> dist(G); //filled up with 0's
451 while ( !bfs.finished() ) {
452 OutEdgeIt e=OutEdgeIt(bfs);
453 if (e.valid() && bfs.isBNodeNewlyReached()) {
454 dist.set(G.head(e), dist.get(G.tail(e))+1);
455 //NodeIt v=res_graph.tail(e);
456 //NodeIt w=res_graph.head(e);
458 //if (pred.get(v).valid()) {
459 // free.set(w, std::min(free.get(v), e.free()));
461 // free.set(w, e.free());
463 //if (res_graph.head(e)==t) { _augment=true; break; }
467 } //end of searching augmenting path
469 double pre_time_copy=currTime();
470 typedef Graph MutableGraph;
472 typename Graph::NodeMap<NodeIt> G_to_F(G);
473 for(typename Graph::EachNodeIt n=G.template first<typename Graph::EachNodeIt>(); n.valid(); ++n) {
474 G_to_F.set(n, F.addNode());
476 for(typename Graph::EachEdgeIt e=G.template first<typename Graph::EachEdgeIt>(); e.valid(); ++e) {
477 if (dist.get(G.head(e))==dist.get(G.tail(e))+1) {
478 F.addEdge(G_to_F.get(G.tail(e)), G_to_F.get(G.head(e)));
481 double post_time_copy=currTime();
482 std::cout << "copy time: " << post_time_copy-pre_time_copy << " sec"<< std::endl;
487 //int num_of_augmentations=0;
489 //std::cout << ++num_of_augmentations << " ";
490 //std::cout<<std::endl;
495 for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i) {
504 template <typename Graph>
505 class IteratorBoolNodeMap {
506 typedef typename Graph::NodeIt NodeIt;
507 typedef typename Graph::EachNodeIt EachNodeIt;
513 BoolItem() : value(false), prev(), next() {}
520 typename Graph::NodeMap<BoolItem> container;
522 typedef bool ValueType;
523 typedef NodeIt KeyType;
524 IteratorBoolNodeMap(const Graph& _G) : G(_G), container(G), first_true(NodeIt()) {
525 //for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
526 //BoolItem b=container.get(e);
530 G.getFirst(first_false);
532 for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
533 container[e].prev=e_prev;
534 if (e_prev.valid()) container[e_prev].next=e;
538 //NodeMap(const Graph& _G, T a) :
539 // G(_G), container(G.node_id, a) { }
541 void set(NodeIt nit, T a) { container[G.id(nit)]=a; }
542 T get(NodeIt nit) const { return container[G.id(nit)]; }
543 //void update() { container.resize(G.node_id); }
544 //void update(T a) { container.resize(G.node_id, a); }
549 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
551 typedef typename Graph::NodeIt NodeIt;
552 typedef typename Graph::EdgeIt EdgeIt;
553 typedef typename Graph::EachEdgeIt EachEdgeIt;
554 typedef typename Graph::OutEdgeIt OutEdgeIt;
555 typedef typename Graph::InEdgeIt InEdgeIt;
557 std::list<NodeIt>& S;
558 std::list<NodeIt>& T;
560 const CapacityMap& capacity;
561 typedef ResGraph<Graph, Number, FlowMap, CapacityMap > AugGraph;
562 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
563 typedef typename AugGraph::EdgeIt AugEdgeIt;
564 typename Graph::NodeMap<bool> SMap;
565 typename Graph::NodeMap<bool> TMap;
567 MaxFlow2(const Graph& _G, std::list<NodeIt>& _S, std::list<NodeIt>& _T, FlowMap& _flow, const CapacityMap& _capacity) : G(_G), S(_S), T(_T), flow(_flow), capacity(_capacity), SMap(_G), TMap(_G) {
568 for(typename std::list<NodeIt>::const_iterator i=S.begin();
572 for (typename std::list<NodeIt>::const_iterator i=T.begin();
578 AugGraph res_graph(G, flow, capacity);
580 NodeIt reached_t_node;
582 typedef typename AugGraph::NodeMap<bool> ReachedMap;
583 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
584 for(typename std::list<NodeIt>::const_iterator i=S.begin();
586 res_bfs.pushAndSetReached(*i);
588 //res_bfs.pushAndSetReached(s);
590 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
591 //filled up with invalid iterators
593 typename AugGraph::NodeMap<int> free(res_graph);
595 //searching for augmenting path
596 while ( !res_bfs.finished() ) {
597 AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
598 if (e.valid() && res_bfs.isBNodeNewlyReached()) {
599 NodeIt v=res_graph.tail(e);
600 NodeIt w=res_graph.head(e);
602 if (pred.get(v).valid()) {
603 free.set(w, std::min(free.get(v), e.free()));
605 free.set(w, e.free());
607 if (TMap.get(res_graph.head(e))) {
609 reached_t_node=res_graph.head(e);
615 } //end of searching augmenting path
618 NodeIt n=reached_t_node;
619 Number augment_value=free.get(reached_t_node);
620 while (pred.get(n).valid()) {
621 AugEdgeIt e=pred.get(n);
622 e.augment(augment_value);
630 while (augment()) { }
634 for(typename std::list<NodeIt>::const_iterator i=S.begin();
636 for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
639 for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
651 #endif //EDMONDS_KARP_HH