2 #ifndef HUGO_BIPARTITE_GRAPH_WRAPPER_H
3 #define HUGO_BIPARTITE_GRAPH_WRAPPER_H
7 ///\brief Several graph wrappers.
9 ///This file contains several useful graph wrapper functions.
11 ///\author Marton Makai
15 #include <graph_wrapper.h>
19 /// A wrapper for composing a bipartite graph.
20 /// \c _graph have to be a reference to a graph of type \c Graph
21 /// and \c _s_false_t_true_map is an \c IterableBoolMap
22 /// reference containing the elements for the
23 /// color classes S and T. \c _graph is to be referred to an undirected
24 /// graph or a directed graph with edges oriented from S to T.
26 ///\author Marton Makai
27 template<typename Graph>
28 class BipartiteGraphWrapper : public GraphWrapper<Graph> {
30 typedef IterableBoolMap< typename Graph::template NodeMap<int> >
32 SFalseTTrueMap* s_false_t_true_map;
34 BipartiteGraphWrapper() : GraphWrapper<Graph>(0) { }
35 void setSFalseTTrueMap(SFalseTTrueMap& _s_false_t_true_map) {
36 s_false_t_true_map=_s_false_t_true_map;
41 //FIXME vhogy igy kellene, csak az en forditom nem eszi meg
42 //static const bool S_CLASS=false;
43 //static const bool T_CLASS=true;
48 BipartiteGraphWrapper(Graph& _graph, SFalseTTrueMap& _s_false_t_true_map)
49 : GraphWrapper<Graph>(_graph), s_false_t_true_map(&_s_false_t_true_map),
50 S_CLASS(false), T_CLASS(true) { }
51 typedef typename GraphWrapper<Graph>::Node Node;
52 //using GraphWrapper<Graph>::NodeIt;
53 typedef typename GraphWrapper<Graph>::Edge Edge;
54 //using GraphWrapper<Graph>::EdgeIt;
56 friend class ClassNodeIt;
58 friend class OutEdgeIt;
60 friend class InEdgeIt;
62 friend class BipartiteGraphWrapper<Graph>;
67 ClassNodeIt(const Invalid& i) : n(i) { }
68 ClassNodeIt(const BipartiteGraphWrapper<Graph>& _G, bool _class) {
69 _G.s_false_t_true_map->first(n, _class);
71 //FIXME needed in new concept, important here
72 ClassNodeIt(const Node& _n) : n(_n) { }
73 operator Node() const { return n; }
79 // SNodeIt(const Invalid& i) : n(i) { }
80 // SNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
81 // _G.s_false_t_true_map->first(n, false);
83 // operator Node() const { return n; }
89 // TNodeIt(const Invalid& i) : n(i) { }
90 // TNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
91 // _G.s_false_t_true_map->first(n, true);
93 // operator Node() const { return n; }
96 friend class BipartiteGraphWrapper<Graph>;
98 typename Graph::OutEdgeIt e;
101 OutEdgeIt(const Invalid& i) : e(i) { }
102 OutEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
103 if (!(*(_G.s_false_t_true_map))[_n])
104 e=typename Graph::OutEdgeIt(*(_G.graph), typename Graph::Node(_n));
108 operator Edge() const { return Edge(typename Graph::Edge(e)); }
111 friend class BipartiteGraphWrapper<Graph>;
113 typename Graph::InEdgeIt e;
116 InEdgeIt(const Invalid& i) : e(i) { }
117 InEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
118 if ((*(_G.s_false_t_true_map))[_n])
119 e=typename Graph::InEdgeIt(*(_G.graph), typename Graph::Node(_n));
123 operator Edge() const { return Edge(typename Graph::Edge(e)); }
126 using GraphWrapper<Graph>::first;
127 ClassNodeIt& first(ClassNodeIt& n, bool _class) const {
128 n=ClassNodeIt(*this, _class) ; return n; }
129 // SNodeIt& first(SNodeIt& n) const { n=SNodeIt(*this); return n; }
130 // TNodeIt& first(TNodeIt& n) const { n=TNodeIt(*this); return n; }
131 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
132 i=OutEdgeIt(*this, p); return i;
134 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
135 i=InEdgeIt(*this, p); return i;
138 using GraphWrapper<Graph>::next;
139 ClassNodeIt& next(ClassNodeIt& n) const {
140 this->s_false_t_true_map->next(n.n); return n;
142 // SNodeIt& next(SNodeIt& n) const {
143 // this->s_false_t_true_map->next(n); return n;
145 // TNodeIt& next(TNodeIt& n) const {
146 // this->s_false_t_true_map->next(n); return n;
148 OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
149 InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
151 Node tail(const Edge& e) {
152 if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
153 return Node(this->graph->tail(e));
155 return Node(this->graph->head(e));
157 Node head(const Edge& e) {
158 if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
159 return Node(this->graph->head(e));
161 return Node(this->graph->tail(e));
164 Node aNode(const OutEdgeIt& e) const {
165 return Node(this->graph->aNode(e.e));
167 Node aNode(const InEdgeIt& e) const {
168 return Node(this->graph->aNode(e.e));
170 Node bNode(const OutEdgeIt& e) const {
171 return Node(this->graph->bNode(e.e));
173 Node bNode(const InEdgeIt& e) const {
174 return Node(this->graph->bNode(e.e));
177 bool inSClass(const Node& n) const {
178 return !(*(this->s_false_t_true_map))[n];
180 bool inTClass(const Node& n) const {
181 return (*(this->s_false_t_true_map))[n];
185 ///\bug Do not use this while the bipartitemap augmentation
186 /// does not work well.
187 template<typename Graph>
188 class BipartiteGraph : public BipartiteGraphWrapper<Graph> {
189 typedef IterableBoolMap< typename Graph::template NodeMap<int> >
191 typedef BipartiteGraphWrapper<Graph> Parent;
194 typename Graph::template NodeMap<int> bipartite_map;
195 SFalseTTrueMap s_false_t_true_map;
197 typedef typename Parent::Node Node;
198 typedef typename Parent::Edge Edge;
199 BipartiteGraph() : BipartiteGraphWrapper<Graph>(0),
200 gr(), bipartite_map(gr),
201 s_false_t_true_map(bipartite_map) {
202 Parent::setGraph(gr);
203 Parent::setSFalseTTrueMap(bipartite_map);
206 /// the \c bool parameter which can be \c S_Class or \c T_Class shows
207 /// the color class where the new node is to be inserted.
208 Node addNode(bool b) {
209 Node n=Parent::graph->addNode();
210 bipartite_map.update();
211 s_false_t_true_map.insert(n, b);
215 /// A new edge is inserted.
216 ///\pre \c tail have to be in \c S_Class and \c head in \c T_Class.
217 Edge addEdge(const Node& tail, const Node& head) {
218 return Parent::graph->addEdge(tail, head);
221 void erase(const Node& n) {
222 s_false_t_true_map.remove(n);
223 Parent::graph->erase(n);
225 void erase(const Edge& e) {
226 Parent::graph->erase(e);
230 FOR_EACH_LOC(typename Parent::EdgeIt, e, G) erase(e);
231 FOR_EACH_LOC(typename Parent::NodeIt, n, G) erase(n);
236 template<typename Graph>
237 class stGraphWrapper;
239 // template<typename Graph>
241 // operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Node& i) {
242 // os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
245 // template<typename Graph>
247 // operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Edge& i) {
248 // os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
249 // " node: " << i.n << ")";
253 /// experimentral, do not try it.
254 /// It eats a bipartite graph, oriented from S to T.
255 /// Such one can be made e.g. by the above wrapper.
257 ///\author Marton Makai
258 template<typename Graph>
259 class stGraphWrapper : public GraphWrapper<Graph> {
264 //0 normalis, 1 s, 2, true, ez az iteralasi sorrend,
265 //es a vege a false azaz (invalid, 3)
272 //0 normalis, 1 s->vmi, 2 vmi->t, ez a sorrend,
273 //invalid: (invalid, 3, invalid)
275 friend class OutEdgeIt;
277 //normalis pontbol (first, 0, invalid), ..., (invalid, 2, vmi), ... (invalid, 3, invalid)
278 //s-bol (invalid, 1, first), ... (invalid, 3, invalid)
279 //t-bol (invalid, 3, invalid)
281 friend class InEdgeIt;
283 //normalis pontbol (first, 0, invalid), ..., (invalid, 1, vmi), ... (invalid, 3, invalid)
284 //s-be (invalid, 3, invalid)
285 //t-be (invalid, 2, first), ... (invalid, 3, invalid)
288 //(first, 0, invalid) ...
291 //invalid, 3, invalid)
292 template <typename T> class NodeMap;
293 template <typename T, typename Parent> class EdgeMap;
295 // template <typename T> friend class NodeMap;
296 // template <typename T> friend class EdgeMap;
301 static const bool S_CLASS=false;
302 static const bool T_CLASS=true;
304 stGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) ,
306 T_NODE(INVALID, 2) { }
310 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
311 // friend std::ostream&
312 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
313 // friend std::ostream&
314 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i);
316 class Node : public Graph::Node {
318 friend class GraphWrapper<Graph>;
319 friend class stGraphWrapper<Graph>;
320 template <typename T> friend class NodeMap;
322 friend class OutEdgeIt;
323 friend class InEdgeIt;
328 Node(const typename Graph::Node& _n, int _spec=0) :
329 Graph::Node(_n), spec(_spec) { }
330 Node(const Invalid& i) : Graph::Node(i), spec(3) { }
331 friend bool operator==(const Node& u, const Node& v) {
332 return (u.spec==v.spec &&
333 static_cast<typename Graph::Node>(u)==
334 static_cast<typename Graph::Node>(v));
336 friend bool operator!=(const Node& u, const Node& v) {
337 return (v.spec!=u.spec ||
338 static_cast<typename Graph::Node>(u)!=
339 static_cast<typename Graph::Node>(v));
341 // template<typename G>
342 // friend std::ostream&
343 // operator<<(std::ostream& os, const typename stGraphWrapper<G>::Node& i);
344 friend std::ostream& operator<< (std::ostream& os, const Node& i);
345 int getSpec() const { return spec; }
349 friend class GraphWrapper<Graph>;
350 friend class stGraphWrapper<Graph>;
351 typename Graph::NodeIt n;
355 NodeIt(const typename Graph::NodeIt& _n, int _spec) :
356 n(_n), spec(_spec) { }
357 NodeIt(const Invalid& i) : n(i), spec(3) { }
358 NodeIt(const stGraphWrapper<Graph>& _G) : n(*(_G.graph)), spec(0) {
359 if (!_G.graph->valid(n)) spec=1;
361 operator Node() const { return Node(n, spec); }
364 class Edge : public Graph::Edge {
365 friend class GraphWrapper<Graph>;
366 friend class stGraphWrapper<Graph>;
367 template <typename T, typename Parent> friend class EdgeMap;
369 typename Graph::Node n;
372 Edge(const typename Graph::Edge& _e, int _spec,
373 const typename Graph::Node& _n) :
374 Graph::Edge(_e), spec(_spec), n(_n) {
376 Edge(const Invalid& i) : Graph::Edge(i), spec(3), n(i) { }
377 friend bool operator==(const Edge& u, const Edge& v) {
378 return (u.spec==v.spec &&
379 static_cast<typename Graph::Edge>(u)==
380 static_cast<typename Graph::Edge>(v) &&
383 friend bool operator!=(const Edge& u, const Edge& v) {
384 return (v.spec!=u.spec ||
385 static_cast<typename Graph::Edge>(u)!=
386 static_cast<typename Graph::Edge>(v) ||
389 // template<typename G>
390 // friend std::ostream&
391 // operator<<(std::ostream& os, const typename stGraphWrapper<G>::Edge& i);
392 friend std::ostream& operator<< (std::ostream& os, const Edge& i);
393 int getSpec() const { return spec; }
397 friend class GraphWrapper<Graph>;
398 friend class stGraphWrapper<Graph>;
399 typename Graph::OutEdgeIt e;
401 typename Graph::ClassNodeIt n;
404 OutEdgeIt(const typename Graph::OutEdgeIt& _e, int _spec,
405 const typename Graph::ClassNodeIt& _n) :
406 e(_e), spec(_spec), n(_n) {
408 OutEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
409 OutEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
412 if (_G.graph->inSClass(_n)) { //S, van normalis kiel
413 e=typename Graph::OutEdgeIt(*(_G.graph),
414 typename Graph::Node(_n));
417 if (!_G.graph->valid(e)) spec=3;
418 } else { //T, specko kiel van
427 _G.graph->first(n, S_CLASS); //s->vmi;
428 if (!_G.graph->valid(n)) spec=3; //Ha S ures
437 operator Edge() const { return Edge(e, spec, n); }
441 friend class GraphWrapper<Graph>;
442 friend class stGraphWrapper<Graph>;
443 typename Graph::InEdgeIt e;
445 typename Graph::ClassNodeIt n;
448 InEdgeIt(const typename Graph::InEdgeIt& _e, int _spec,
449 const typename Graph::ClassNodeIt& _n) :
450 e(_e), spec(_spec), n(_n) {
452 InEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
453 InEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
456 if (_G.graph->inTClass(_n)) { //T, van normalis beel
457 e=typename Graph::InEdgeIt(*(_G.graph),
458 typename Graph::Node(_n));
461 if (!_G.graph->valid(e)) spec=3;
462 } else { //S, specko beel van
476 _G.graph->first(n, T_CLASS); //vmi->t;
477 if (!_G.graph->valid(n)) spec=3; //Ha T ures
481 operator Edge() const { return Edge(e, spec, n); }
485 friend class GraphWrapper<Graph>;
486 friend class stGraphWrapper<Graph>;
487 typename Graph::EdgeIt e;
489 typename Graph::ClassNodeIt n;
492 EdgeIt(const typename Graph::EdgeIt& _e, int _spec,
493 const typename Graph::ClassNodeIt& _n) :
494 e(_e), spec(_spec), n(_n) { }
495 EdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
496 EdgeIt(const stGraphWrapper<Graph>& _G) :
497 e(*(_G.graph)), spec(0), n(INVALID) {
498 if (!_G.graph->valid(e)) {
500 _G.graph->first(n, S_CLASS);
501 if (!_G.graph->valid(n)) { //Ha S ures
503 _G.graph->first(n, T_CLASS);
504 if (!_G.graph->valid(n)) { //Ha T ures
510 operator Edge() const { return Edge(e, spec, n); }
513 NodeIt& first(NodeIt& i) const {
514 i=NodeIt(*this); return i;
516 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
517 i=OutEdgeIt(*this, p); return i;
519 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
520 i=InEdgeIt(*this, p); return i;
522 EdgeIt& first(EdgeIt& i) const {
523 i=EdgeIt(*this); return i;
526 NodeIt& next(NodeIt& i) const {
529 this->graph->next(i.n);
530 if (!this->graph->valid(i.n)) {
543 OutEdgeIt& next(OutEdgeIt& i) const {
544 typename Graph::Node v;
546 case 0: //normal edge
547 v=this->graph->aNode(i.e);
548 this->graph->next(i.e);
549 if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
550 if (this->graph->inSClass(v)) { //S, nincs kiel
553 } else { //T, van kiel
560 this->graph->next(i.n);
561 if (!this->graph->valid(i.n)) i.spec=3;
570 InEdgeIt& next(InEdgeIt& i) const {
571 typename Graph::Node v;
573 case 0: //normal edge
574 v=this->graph->aNode(i.e);
575 this->graph->next(i.e);
576 if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
577 if (this->graph->inTClass(v)) { //S, nincs beel
580 } else { //S, van beel
591 this->graph->next(i.n);
592 if (!this->graph->valid(i.n)) i.spec=3;
598 EdgeIt& next(EdgeIt& i) const {
601 this->graph->next(i.e);
602 if (!this->graph->valid(i.e)) {
604 this->graph->first(i.n, S_CLASS);
605 if (!this->graph->valid(i.n)) {
607 this->graph->first(i.n, T_CLASS);
608 if (!this->graph->valid(i.n)) i.spec=3;
613 this->graph->next(i.n);
614 if (!this->graph->valid(i.n)) {
616 this->graph->first(i.n, T_CLASS);
617 if (!this->graph->valid(i.n)) i.spec=3;
621 this->graph->next(i.n);
622 if (!this->graph->valid(i.n)) i.spec=3;
628 Node tail(const Edge& e) const {
631 return Node(this->graph->tail(e));
642 Node head(const Edge& e) const {
645 return Node(this->graph->head(e));
657 bool valid(const Node& n) const { return (n.spec<3); }
658 bool valid(const Edge& e) const { return (e.spec<3); }
660 int nodeNum() const { return this->graph->nodeNum()+2; }
661 int edgeNum() const {
662 return this->graph->edgeNum()+this->graph->nodeNum();
665 Node aNode(const OutEdgeIt& e) const { return tail(e); }
666 Node aNode(const InEdgeIt& e) const { return head(e); }
667 Node bNode(const OutEdgeIt& e) const { return head(e); }
668 Node bNode(const InEdgeIt& e) const { return tail(e); }
670 void addNode() const { }
671 void addEdge() const { }
673 // Node addNode() const { return Node(this->graph->addNode()); }
674 // Edge addEdge(const Node& tail, const Node& head) const {
675 // return Edge(this->graph->addEdge(tail, head)); }
677 // void erase(const Node& i) const { this->graph->erase(i); }
678 // void erase(const Edge& i) const { this->graph->erase(i); }
680 // void clear() const { this->graph->clear(); }
682 template<typename T> class NodeMap : public GraphWrapper<Graph>::template NodeMap<T> {
683 typedef typename GraphWrapper<Graph>::template NodeMap<T> Parent;
686 NodeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
689 NodeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
692 T operator[](const Node& n) const {
695 return Parent::operator[](n);
706 void set(const Node& n, T t) {
709 GraphWrapper<Graph>::template NodeMap<T>::set(n, t);
724 typename GraphWrapper<Graph>::template EdgeMap<T> >
725 class EdgeMap : public Parent {
726 //typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
727 typename GraphWrapper<Graph>::template NodeMap<T> node_value;
729 EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
731 EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
732 node_value(_G, a) { }
733 T operator[](const Edge& e) const {
736 return Parent::operator[](e);
739 return node_value[e.n];
743 return node_value[e.n];
747 void set(const Edge& e, T t) {
753 node_value.set(e.n, t);
757 node_value.set(e.n, t);
763 // template<typename T> class EdgeMap : public GraphWrapper<Graph>::template EdgeMap<T> {
764 // typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
765 // typename GraphWrapper<Graph>::template NodeMap<T> node_value;
767 // EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
768 // node_value(_G) { }
769 // EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
770 // node_value(_G, a) { }
771 // T operator[](const Edge& e) const {
774 // return Parent::operator[](e);
777 // return node_value[e.n];
781 // return node_value[e.n];
785 // void set(const Edge& e, T t) {
788 // GraphWrapper<Graph>::template EdgeMap<T>::set(e, t);
791 // node_value.set(e.n, t);
795 // node_value.set(e.n, t);
801 // template<typename G>
803 operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i) {
804 os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
807 // template<typename G>
809 operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i) {
810 os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
811 " node: " << i.n << ")";
822 #endif //HUGO_GRAPH_WRAPPER_H