setInvalid() functions added.
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>
16 typedef typename Graph::NodeIt NodeIt;
17 typedef typename Graph::EachNodeIt EachNodeIt;
19 typedef typename Graph::SymEdgeIt OldSymEdgeIt;
22 const CapacityMap& capacity;
24 ResGraph(const Graph& _G, FlowMap& _flow,
25 const CapacityMap& _capacity) :
26 G(_G), flow(_flow), capacity(_capacity) { }
31 friend class OutEdgeIt;
34 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
36 const ResGraph<Graph, Number, FlowMap, CapacityMap>* resG;
40 //EdgeIt(const EdgeIt& e) : resG(e.resG), sym(e.sym) { }
42 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
43 return (resG->capacity.get(sym)-resG->flow.get(sym));
45 return (resG->flow.get(sym));
48 bool valid() const { return sym.valid(); }
49 void augment(Number a) const {
50 if (resG->G.aNode(sym)==resG->G.tail(sym)) {
51 resG->flow.set(sym, resG->flow.get(sym)+a);
54 resG->flow.set(sym, resG->flow.get(sym)-a);
60 class OutEdgeIt : public EdgeIt {
61 friend class ResGraph<Graph, Number, FlowMap, CapacityMap>;
64 //OutEdgeIt(const OutEdgeIt& e) { resG=e.resG; sym=e.sym; }
66 OutEdgeIt(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
68 sym=resG->G.template first<OldSymEdgeIt>(v);
69 while( sym.valid() && !(free()>0) ) { ++sym; }
72 OutEdgeIt& operator++() {
74 while( sym.valid() && !(free()>0) ) { ++sym; }
79 void getFirst(OutEdgeIt& e, NodeIt v) const {
80 e=OutEdgeIt(*this, v);
82 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
84 template< typename It >
91 template< typename It >
92 It first(NodeIt v) const {
98 NodeIt tail(EdgeIt e) const { return G.aNode(e.sym); }
99 NodeIt head(EdgeIt e) const { return G.bNode(e.sym); }
101 NodeIt aNode(OutEdgeIt e) const { return G.aNode(e.sym); }
102 NodeIt bNode(OutEdgeIt e) const { return G.bNode(e.sym); }
104 int id(NodeIt v) const { return G.id(v); }
106 template <typename S>
108 typename Graph::NodeMap<S> node_map;
110 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
111 NodeMap(const ResGraph<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
112 void set(NodeIt nit, S a) { node_map.set(nit, a); }
113 S get(NodeIt nit) const { return node_map.get(nit); }
114 S& operator[](NodeIt nit) { return node_map[nit]; }
115 const S& operator[](NodeIt nit) const { return node_map[nit]; }
121 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
124 typedef typename Graph::NodeIt NodeIt;
125 typedef typename Graph::EachNodeIt EachNodeIt;
127 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
128 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
129 typedef typename Graph::InEdgeIt OldInEdgeIt;
133 const CapacityMap& capacity;
135 ResGraph2(const Graph& _G, FlowMap& _flow,
136 const CapacityMap& _capacity) :
137 G(_G), flow(_flow), capacity(_capacity) { }
142 friend class OutEdgeIt;
145 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
147 const ResGraph2<Graph, Number, FlowMap, CapacityMap>* resG;
151 bool out_or_in; //1, iff out
153 EdgeIt() : out_or_in(1) { }
154 Number free() const {
156 return (resG->capacity.get(out)-resG->flow.get(out));
158 return (resG->flow.get(in));
162 return out_or_in && out.valid() || in.valid(); }
163 void augment(Number a) const {
165 resG->flow.set(out, resG->flow.get(out)+a);
167 resG->flow.set(in, resG->flow.get(in)-a);
172 class OutEdgeIt : public EdgeIt {
173 friend class ResGraph2<Graph, Number, FlowMap, CapacityMap>;
177 OutEdgeIt(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _resG, NodeIt v) {
179 out=resG->G.template first<OldOutEdgeIt>(v);
180 while( out.valid() && !(free()>0) ) { ++out; }
183 in=resG->G.template first<OldInEdgeIt>(v);
184 while( in.valid() && !(free()>0) ) { ++in; }
188 OutEdgeIt& operator++() {
190 NodeIt v=resG->G.aNode(out);
192 while( out.valid() && !(free()>0) ) { ++out; }
195 in=resG->G.template first<OldInEdgeIt>(v);
196 while( in.valid() && !(free()>0) ) { ++in; }
200 while( in.valid() && !(free()>0) ) { ++in; }
206 void getFirst(OutEdgeIt& e, NodeIt v) const {
207 e=OutEdgeIt(*this, v);
209 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
211 template< typename It >
218 template< typename It >
219 It first(NodeIt v) const {
225 NodeIt tail(EdgeIt e) const {
226 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
227 NodeIt head(EdgeIt e) const {
228 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
230 NodeIt aNode(OutEdgeIt e) const {
231 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
232 NodeIt bNode(OutEdgeIt e) const {
233 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
235 int id(NodeIt v) const { return G.id(v); }
237 template <typename S>
239 typename Graph::NodeMap<S> node_map;
241 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
242 NodeMap(const ResGraph2<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
243 void set(NodeIt nit, S a) { node_map.set(nit, a); }
244 S get(NodeIt nit) const { return node_map.get(nit); }
248 template<typename Graph, typename Number, typename FlowMap, typename CapacityMap>
251 typedef typename Graph::NodeIt NodeIt;
252 typedef typename Graph::EachNodeIt EachNodeIt;
255 //typedef typename Graph::SymEdgeIt OldSymEdgeIt;
256 typedef typename Graph::OutEdgeIt OldOutEdgeIt;
257 typedef typename Graph::InEdgeIt OldInEdgeIt;
260 const CapacityMap& capacity;
262 ResGraph3(const Graph& _G, FlowMap& _flow,
263 const CapacityMap& _capacity) :
264 G(_G), flow(_flow), capacity(_capacity) { }
269 friend class OutEdgeIt;
272 friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
274 //const ResGraph3<Graph, Number, FlowMap, CapacityMap>* resG;
277 const CapacityMap* capacity;
281 bool out_or_in; //1, iff out
283 EdgeIt() : out_or_in(1) { }
284 Number free() const {
286 return (/*resG->*/capacity->get(out)-/*resG->*/flow->get(out));
288 return (/*resG->*/flow->get(in));
292 return out_or_in && out.valid() || in.valid(); }
293 void augment(Number a) const {
295 /*resG->*/flow->set(out, /*resG->*/flow->get(out)+a);
297 /*resG->*/flow->set(in, /*resG->*/flow->get(in)-a);
302 class OutEdgeIt : public EdgeIt {
303 friend class ResGraph3<Graph, Number, FlowMap, CapacityMap>;
307 OutEdgeIt(const Graph& _G, NodeIt v, FlowMap& _flow, const CapacityMap& _capacity) {
311 out=/*resG->*/G->template first<OldOutEdgeIt>(v);
312 while( out.valid() && !(free()>0) ) { ++out; }
315 in=/*resG->*/G->template first<OldInEdgeIt>(v);
316 while( in.valid() && !(free()>0) ) { ++in; }
320 OutEdgeIt& operator++() {
322 NodeIt v=/*resG->*/G->aNode(out);
324 while( out.valid() && !(free()>0) ) { ++out; }
327 in=/*resG->*/G->template first<OldInEdgeIt>(v);
328 while( in.valid() && !(free()>0) ) { ++in; }
332 while( in.valid() && !(free()>0) ) { ++in; }
338 void getFirst(OutEdgeIt& e, NodeIt v) const {
339 e=OutEdgeIt(G, v, flow, capacity);
341 void getFirst(EachNodeIt& v) const { G.getFirst(v); }
343 template< typename It >
350 template< typename It >
351 It first(NodeIt v) const {
357 NodeIt tail(EdgeIt e) const {
358 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
359 NodeIt head(EdgeIt e) const {
360 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
362 NodeIt aNode(OutEdgeIt e) const {
363 return ((e.out_or_in) ? G.aNode(e.out) : G.aNode(e.in)); }
364 NodeIt bNode(OutEdgeIt e) const {
365 return ((e.out_or_in) ? G.bNode(e.out) : G.bNode(e.in)); }
367 int id(NodeIt v) const { return G.id(v); }
369 template <typename S>
371 typename Graph::NodeMap<S> node_map;
373 NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G) : node_map(_G.G) { }
374 NodeMap(const ResGraph3<Graph, Number, FlowMap, CapacityMap>& _G, S a) : node_map(_G.G, a) { }
375 void set(NodeIt nit, S a) { node_map.set(nit, a); }
376 S get(NodeIt nit) const { return node_map.get(nit); }
382 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
385 typedef typename Graph::NodeIt NodeIt;
386 typedef typename Graph::EdgeIt EdgeIt;
387 typedef typename Graph::EachEdgeIt EachEdgeIt;
388 typedef typename Graph::OutEdgeIt OutEdgeIt;
389 typedef typename Graph::InEdgeIt InEdgeIt;
396 const CapacityMap& capacity;
397 typedef ResGraph3<Graph, Number, FlowMap, CapacityMap > AugGraph;
398 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
399 typedef typename AugGraph::EdgeIt AugEdgeIt;
401 //AugGraph res_graph;
402 //typedef typename AugGraph::NodeMap<bool> ReachedMap;
403 //typename AugGraph::NodeMap<AugEdgeIt> pred;
404 //typename AugGraph::NodeMap<int> free;
406 MaxFlow(const Graph& _G, NodeIt _s, NodeIt _t, FlowMap& _flow, const CapacityMap& _capacity) :
407 G(_G), s(_s), t(_t), flow(_flow), capacity(_capacity) //,
408 //res_graph(G, flow, capacity), pred(res_graph), free(res_graph)
411 AugGraph res_graph(G, flow, capacity);
414 typedef typename AugGraph::NodeMap<bool> ReachedMap;
415 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
416 res_bfs.pushAndSetReached(s);
418 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
419 //filled up with invalid iterators
420 //pred.set(s, AugEdgeIt());
422 typename AugGraph::NodeMap<int> free(res_graph);
424 //searching for augmenting path
425 while ( !res_bfs.finished() ) {
426 AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
427 if (e.valid() && res_bfs.isBNodeNewlyReached()) {
428 NodeIt v=res_graph.tail(e);
429 NodeIt w=res_graph.head(e);
431 if (pred.get(v).valid()) {
432 free.set(w, std::min(free.get(v), e.free()));
434 free.set(w, e.free());
436 if (res_graph.head(e)==t) { _augment=true; break; }
440 } //end of searching augmenting path
444 Number augment_value=free.get(t);
445 while (pred.get(n).valid()) {
446 AugEdgeIt e=pred.get(n);
447 e.augment(augment_value);
454 bool augmentWithBlockingFlow() {
455 BfsIterator4< Graph, OutEdgeIt, typename Graph::NodeMap<bool> > bfs(G);
456 bfs.pushAndSetReached(s);
457 typename Graph::NodeMap<int> dist(G); //filled up with 0's
458 while ( !bfs.finished() ) {
459 OutEdgeIt e=OutEdgeIt(bfs);
460 if (e.valid() && bfs.isBNodeNewlyReached()) {
461 dist.set(G.head(e), dist.get(G.tail(e))+1);
462 //NodeIt v=res_graph.tail(e);
463 //NodeIt w=res_graph.head(e);
465 //if (pred.get(v).valid()) {
466 // free.set(w, std::min(free.get(v), e.free()));
468 // free.set(w, e.free());
470 //if (res_graph.head(e)==t) { _augment=true; break; }
474 } //end of searching augmenting path
476 double pre_time_copy=currTime();
477 typedef Graph MutableGraph;
479 typename Graph::NodeMap<NodeIt> G_to_F(G);
480 for(typename Graph::EachNodeIt n=G.template first<typename Graph::EachNodeIt>(); n.valid(); ++n) {
481 G_to_F.set(n, F.addNode());
483 for(typename Graph::EachEdgeIt e=G.template first<typename Graph::EachEdgeIt>(); e.valid(); ++e) {
484 if (dist.get(G.head(e))==dist.get(G.tail(e))+1) {
485 F.addEdge(G_to_F.get(G.tail(e)), G_to_F.get(G.head(e)));
488 double post_time_copy=currTime();
489 std::cout << "copy time: " << post_time_copy-pre_time_copy << " sec"<< std::endl;
494 //int num_of_augmentations=0;
496 //std::cout << ++num_of_augmentations << " ";
497 //std::cout<<std::endl;
502 for(OutEdgeIt i=G.template first<OutEdgeIt>(s); i.valid(); ++i) {
511 template <typename Graph>
512 class IteratorBoolNodeMap {
513 typedef typename Graph::NodeIt NodeIt;
514 typedef typename Graph::EachNodeIt EachNodeIt;
520 BoolItem() : value(false), prev(), next() {}
527 typename Graph::NodeMap<BoolItem> container;
529 typedef bool ValueType;
530 typedef NodeIt KeyType;
531 IteratorBoolNodeMap(const Graph& _G) : G(_G), container(G), first_true(NodeIt()) {
532 //for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
533 //BoolItem b=container.get(e);
537 G.getFirst(first_false);
539 for (EachNodeIt e=G.template first<EacNodeIt>(); e.valid(); ++e) {
540 container[e].prev=e_prev;
541 if (e_prev.valid()) container[e_prev].next=e;
545 //NodeMap(const Graph& _G, T a) :
546 // G(_G), container(G.node_id, a) { }
548 void set(NodeIt nit, T a) { container[G.id(nit)]=a; }
549 T get(NodeIt nit) const { return container[G.id(nit)]; }
550 //void update() { container.resize(G.node_id); }
551 //void update(T a) { container.resize(G.node_id, a); }
556 template <typename Graph, typename Number, typename FlowMap, typename CapacityMap>
559 typedef typename Graph::NodeIt NodeIt;
560 typedef typename Graph::EdgeIt EdgeIt;
561 typedef typename Graph::EachEdgeIt EachEdgeIt;
562 typedef typename Graph::OutEdgeIt OutEdgeIt;
563 typedef typename Graph::InEdgeIt InEdgeIt;
566 std::list<NodeIt>& S;
567 std::list<NodeIt>& T;
569 const CapacityMap& capacity;
570 typedef ResGraph<Graph, Number, FlowMap, CapacityMap > AugGraph;
571 typedef typename AugGraph::OutEdgeIt AugOutEdgeIt;
572 typedef typename AugGraph::EdgeIt AugEdgeIt;
573 typename Graph::NodeMap<bool> SMap;
574 typename Graph::NodeMap<bool> TMap;
576 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) {
577 for(typename std::list<NodeIt>::const_iterator i=S.begin();
581 for (typename std::list<NodeIt>::const_iterator i=T.begin();
587 AugGraph res_graph(G, flow, capacity);
589 NodeIt reached_t_node;
591 typedef typename AugGraph::NodeMap<bool> ReachedMap;
592 BfsIterator4< AugGraph, AugOutEdgeIt, ReachedMap > res_bfs(res_graph);
593 for(typename std::list<NodeIt>::const_iterator i=S.begin();
595 res_bfs.pushAndSetReached(*i);
597 //res_bfs.pushAndSetReached(s);
599 typename AugGraph::NodeMap<AugEdgeIt> pred(res_graph);
600 //filled up with invalid iterators
602 typename AugGraph::NodeMap<int> free(res_graph);
604 //searching for augmenting path
605 while ( !res_bfs.finished() ) {
606 AugOutEdgeIt e=AugOutEdgeIt(res_bfs);
607 if (e.valid() && res_bfs.isBNodeNewlyReached()) {
608 NodeIt v=res_graph.tail(e);
609 NodeIt w=res_graph.head(e);
611 if (pred.get(v).valid()) {
612 free.set(w, std::min(free.get(v), e.free()));
614 free.set(w, e.free());
616 if (TMap.get(res_graph.head(e))) {
618 reached_t_node=res_graph.head(e);
624 } //end of searching augmenting path
627 NodeIt n=reached_t_node;
628 Number augment_value=free.get(reached_t_node);
629 while (pred.get(n).valid()) {
630 AugEdgeIt e=pred.get(n);
631 e.augment(augment_value);
639 while (augment()) { }
643 for(typename std::list<NodeIt>::const_iterator i=S.begin();
645 for(OutEdgeIt e=G.template first<OutEdgeIt>(*i); e.valid(); ++e) {
648 for(InEdgeIt e=G.template first<InEdgeIt>(*i); e.valid(); ++e) {
660 #endif //EDMONDS_KARP_HH