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>()/*,
35 S_CLASS(false), T_CLASS(true)*/ { }
36 void setSFalseTTrueMap(SFalseTTrueMap& _s_false_t_true_map) {
37 s_false_t_true_map=&_s_false_t_true_map;
42 //FIXME vhogy igy kellene, csak az en forditom nem eszi meg
43 static const bool S_CLASS;
44 static const bool T_CLASS;
49 BipartiteGraphWrapper(Graph& _graph, SFalseTTrueMap& _s_false_t_true_map)
50 : GraphWrapper<Graph>(_graph),
51 s_false_t_true_map(&_s_false_t_true_map)/*,
52 S_CLASS(false), T_CLASS(true)*/ { }
53 typedef typename GraphWrapper<Graph>::Node Node;
54 //using GraphWrapper<Graph>::NodeIt;
55 typedef typename GraphWrapper<Graph>::Edge Edge;
56 //using GraphWrapper<Graph>::EdgeIt;
58 friend class ClassNodeIt;
60 friend class OutEdgeIt;
62 friend class InEdgeIt;
64 friend class BipartiteGraphWrapper<Graph>;
69 ClassNodeIt(const Invalid& i) : n(i) { }
70 ClassNodeIt(const BipartiteGraphWrapper<Graph>& _G, bool _class) {
71 _G.s_false_t_true_map->first(n, _class);
73 //FIXME needed in new concept, important here
74 ClassNodeIt(const Node& _n) : n(_n) { }
75 operator Node() const { return n; }
81 // SNodeIt(const Invalid& i) : n(i) { }
82 // SNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
83 // _G.s_false_t_true_map->first(n, false);
85 // operator Node() const { return n; }
91 // TNodeIt(const Invalid& i) : n(i) { }
92 // TNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
93 // _G.s_false_t_true_map->first(n, true);
95 // operator Node() const { return n; }
98 friend class BipartiteGraphWrapper<Graph>;
100 typename Graph::OutEdgeIt e;
103 OutEdgeIt(const Invalid& i) : e(i) { }
104 OutEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
105 if (!(*(_G.s_false_t_true_map))[_n])
106 e=typename Graph::OutEdgeIt(*(_G.graph), typename Graph::Node(_n));
110 operator Edge() const { return Edge(typename Graph::Edge(e)); }
113 friend class BipartiteGraphWrapper<Graph>;
115 typename Graph::InEdgeIt e;
118 InEdgeIt(const Invalid& i) : e(i) { }
119 InEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
120 if ((*(_G.s_false_t_true_map))[_n])
121 e=typename Graph::InEdgeIt(*(_G.graph), typename Graph::Node(_n));
125 operator Edge() const { return Edge(typename Graph::Edge(e)); }
128 using GraphWrapper<Graph>::first;
129 ClassNodeIt& first(ClassNodeIt& n, bool _class) const {
130 n=ClassNodeIt(*this, _class) ; return n; }
131 // SNodeIt& first(SNodeIt& n) const { n=SNodeIt(*this); return n; }
132 // TNodeIt& first(TNodeIt& n) const { n=TNodeIt(*this); return n; }
133 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
134 i=OutEdgeIt(*this, p); return i;
136 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
137 i=InEdgeIt(*this, p); return i;
140 using GraphWrapper<Graph>::next;
141 ClassNodeIt& next(ClassNodeIt& n) const {
142 this->s_false_t_true_map->next(n.n); return n;
144 // SNodeIt& next(SNodeIt& n) const {
145 // this->s_false_t_true_map->next(n); return n;
147 // TNodeIt& next(TNodeIt& n) const {
148 // this->s_false_t_true_map->next(n); return n;
150 OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
151 InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
153 Node tail(const Edge& e) {
154 if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
155 return Node(this->graph->tail(e));
157 return Node(this->graph->head(e));
159 Node head(const Edge& e) {
160 if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
161 return Node(this->graph->head(e));
163 return Node(this->graph->tail(e));
166 Node aNode(const OutEdgeIt& e) const {
167 return Node(this->graph->aNode(e.e));
169 Node aNode(const InEdgeIt& e) const {
170 return Node(this->graph->aNode(e.e));
172 Node bNode(const OutEdgeIt& e) const {
173 return Node(this->graph->bNode(e.e));
175 Node bNode(const InEdgeIt& e) const {
176 return Node(this->graph->bNode(e.e));
179 bool inSClass(const Node& n) const {
180 return !(*(this->s_false_t_true_map))[n];
182 bool inTClass(const Node& n) const {
183 return (*(this->s_false_t_true_map))[n];
189 const bool BipartiteGraphWrapper<G>::S_CLASS=false;
191 const bool BipartiteGraphWrapper<G>::T_CLASS=true;
204 ///\bug Do not use this while the bipartitemap augmentation
205 /// does not work well.
206 template<typename Graph>
207 class BipartiteGraph : public BipartiteGraphWrapper<Graph> {
208 typedef IterableBoolMap< typename Graph::template NodeMap<int> >
210 typedef BipartiteGraphWrapper<Graph> Parent;
213 typename Graph::template NodeMap<int> bipartite_map;
214 SFalseTTrueMap s_false_t_true_map;
216 typedef typename Parent::Node Node;
217 typedef typename Parent::Edge Edge;
218 BipartiteGraph() : BipartiteGraphWrapper<Graph>(),
219 gr(), bipartite_map(gr, -1),
220 s_false_t_true_map(bipartite_map) {
221 Parent::setGraph(gr);
222 Parent::setSFalseTTrueMap(s_false_t_true_map);
225 /// the \c bool parameter which can be \c S_Class or \c T_Class shows
226 /// the color class where the new node is to be inserted.
227 Node addNode(bool b) {
228 Node n=Parent::graph->addNode();
229 bipartite_map.update();
230 //bipartite_map.set(n, -1);
231 s_false_t_true_map.insert(n, b);
235 /// A new edge is inserted.
236 ///\pre \c tail have to be in \c S_Class and \c head in \c T_Class.
237 Edge addEdge(const Node& tail, const Node& head) {
238 return Parent::graph->addEdge(tail, head);
241 void erase(const Node& n) {
242 s_false_t_true_map.remove(n);
243 Parent::graph->erase(n);
245 void erase(const Edge& e) {
246 Parent::graph->erase(e);
250 FOR_EACH_LOC(typename Parent::EdgeIt, e, G) erase(e);
251 FOR_EACH_LOC(typename Parent::NodeIt, n, G) erase(n);
256 template<typename Graph>
257 class stGraphWrapper;
259 // template<typename Graph>
261 // operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Node& i) {
262 // os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
265 // template<typename Graph>
267 // operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Edge& i) {
268 // os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
269 // " node: " << i.n << ")";
273 /// experimentral, do not try it.
274 /// It eats a bipartite graph, oriented from S to T.
275 /// Such one can be made e.g. by the above wrapper.
277 ///\author Marton Makai
278 template<typename Graph>
279 class stGraphWrapper : public GraphWrapper<Graph> {
284 //0 normalis, 1 s, 2, true, ez az iteralasi sorrend,
285 //es a vege a false azaz (invalid, 3)
292 //0 normalis, 1 s->vmi, 2 vmi->t, ez a sorrend,
293 //invalid: (invalid, 3, invalid)
295 friend class OutEdgeIt;
297 //normalis pontbol (first, 0, invalid), ..., (invalid, 2, vmi), ... (invalid, 3, invalid)
298 //s-bol (invalid, 1, first), ... (invalid, 3, invalid)
299 //t-bol (invalid, 3, invalid)
301 friend class InEdgeIt;
303 //normalis pontbol (first, 0, invalid), ..., (invalid, 1, vmi), ... (invalid, 3, invalid)
304 //s-be (invalid, 3, invalid)
305 //t-be (invalid, 2, first), ... (invalid, 3, invalid)
308 //(first, 0, invalid) ...
311 //invalid, 3, invalid)
312 template <typename T> class NodeMap;
313 template <typename T, typename Parent> class EdgeMap;
315 // template <typename T> friend class NodeMap;
316 // template <typename T> friend class EdgeMap;
318 ///\todo FIXME ezt majd static-ra kell javitani
322 static const bool S_CLASS=false;
323 static const bool T_CLASS=true;
325 stGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) ,
327 T_NODE(INVALID, 2) { }
331 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
332 // friend std::ostream&
333 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
334 // friend std::ostream&
335 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i);
337 class Node : public Graph::Node {
339 friend class GraphWrapper<Graph>;
340 friend class stGraphWrapper<Graph>;
341 template <typename T> friend class NodeMap;
343 friend class OutEdgeIt;
344 friend class InEdgeIt;
349 Node(const typename Graph::Node& _n, int _spec=0) :
350 Graph::Node(_n), spec(_spec) { }
351 Node(const Invalid& i) : Graph::Node(i), spec(3) { }
352 friend bool operator==(const Node& u, const Node& v) {
353 return (u.spec==v.spec &&
354 static_cast<typename Graph::Node>(u)==
355 static_cast<typename Graph::Node>(v));
357 friend bool operator!=(const Node& u, const Node& v) {
358 return (v.spec!=u.spec ||
359 static_cast<typename Graph::Node>(u)!=
360 static_cast<typename Graph::Node>(v));
362 // template<typename G>
363 // friend std::ostream&
364 // operator<<(std::ostream& os, const typename stGraphWrapper<G>::Node& i);
365 friend std::ostream& operator<< (std::ostream& os, const Node& i);
366 int getSpec() const { return spec; }
370 friend class GraphWrapper<Graph>;
371 friend class stGraphWrapper<Graph>;
372 typename Graph::NodeIt n;
376 NodeIt(const typename Graph::NodeIt& _n, int _spec) :
377 n(_n), spec(_spec) { }
378 NodeIt(const Invalid& i) : n(i), spec(3) { }
379 NodeIt(const stGraphWrapper<Graph>& _G) : n(*(_G.graph)), spec(0) {
380 if (!_G.graph->valid(n)) spec=1;
382 operator Node() const { return Node(n, spec); }
385 class Edge : public Graph::Edge {
386 friend class GraphWrapper<Graph>;
387 friend class stGraphWrapper<Graph>;
388 template <typename T, typename Parent> friend class EdgeMap;
390 typename Graph::Node n;
393 Edge(const typename Graph::Edge& _e, int _spec,
394 const typename Graph::Node& _n) :
395 Graph::Edge(_e), spec(_spec), n(_n) {
397 Edge(const Invalid& i) : Graph::Edge(i), spec(3), n(i) { }
398 friend bool operator==(const Edge& u, const Edge& v) {
399 return (u.spec==v.spec &&
400 static_cast<typename Graph::Edge>(u)==
401 static_cast<typename Graph::Edge>(v) &&
404 friend bool operator!=(const Edge& u, const Edge& v) {
405 return (v.spec!=u.spec ||
406 static_cast<typename Graph::Edge>(u)!=
407 static_cast<typename Graph::Edge>(v) ||
410 // template<typename G>
411 // friend std::ostream&
412 // operator<<(std::ostream& os, const typename stGraphWrapper<G>::Edge& i);
413 friend std::ostream& operator<< (std::ostream& os, const Edge& i);
414 int getSpec() const { return spec; }
418 friend class GraphWrapper<Graph>;
419 friend class stGraphWrapper<Graph>;
420 typename Graph::OutEdgeIt e;
422 typename Graph::ClassNodeIt n;
425 OutEdgeIt(const typename Graph::OutEdgeIt& _e, int _spec,
426 const typename Graph::ClassNodeIt& _n) :
427 e(_e), spec(_spec), n(_n) {
429 OutEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
430 OutEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
433 if (_G.graph->inSClass(_n)) { //S, van normalis kiel
434 e=typename Graph::OutEdgeIt(*(_G.graph),
435 typename Graph::Node(_n));
438 if (!_G.graph->valid(e)) spec=3;
439 } else { //T, specko kiel van
448 _G.graph->first(n, S_CLASS); //s->vmi;
449 if (!_G.graph->valid(n)) spec=3; //Ha S ures
458 operator Edge() const { return Edge(e, spec, n); }
462 friend class GraphWrapper<Graph>;
463 friend class stGraphWrapper<Graph>;
464 typename Graph::InEdgeIt e;
466 typename Graph::ClassNodeIt n;
469 InEdgeIt(const typename Graph::InEdgeIt& _e, int _spec,
470 const typename Graph::ClassNodeIt& _n) :
471 e(_e), spec(_spec), n(_n) {
473 InEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
474 InEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
477 if (_G.graph->inTClass(_n)) { //T, van normalis beel
478 e=typename Graph::InEdgeIt(*(_G.graph),
479 typename Graph::Node(_n));
482 if (!_G.graph->valid(e)) spec=3;
483 } else { //S, specko beel van
497 _G.graph->first(n, T_CLASS); //vmi->t;
498 if (!_G.graph->valid(n)) spec=3; //Ha T ures
502 operator Edge() const { return Edge(e, spec, n); }
506 friend class GraphWrapper<Graph>;
507 friend class stGraphWrapper<Graph>;
508 typename Graph::EdgeIt e;
510 typename Graph::ClassNodeIt n;
513 EdgeIt(const typename Graph::EdgeIt& _e, int _spec,
514 const typename Graph::ClassNodeIt& _n) :
515 e(_e), spec(_spec), n(_n) { }
516 EdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
517 EdgeIt(const stGraphWrapper<Graph>& _G) :
518 e(*(_G.graph)), spec(0), n(INVALID) {
519 if (!_G.graph->valid(e)) {
521 _G.graph->first(n, S_CLASS);
522 if (!_G.graph->valid(n)) { //Ha S ures
524 _G.graph->first(n, T_CLASS);
525 if (!_G.graph->valid(n)) { //Ha T ures
531 operator Edge() const { return Edge(e, spec, n); }
534 NodeIt& first(NodeIt& i) const {
535 i=NodeIt(*this); return i;
537 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
538 i=OutEdgeIt(*this, p); return i;
540 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
541 i=InEdgeIt(*this, p); return i;
543 EdgeIt& first(EdgeIt& i) const {
544 i=EdgeIt(*this); return i;
547 NodeIt& next(NodeIt& i) const {
550 this->graph->next(i.n);
551 if (!this->graph->valid(i.n)) {
564 OutEdgeIt& next(OutEdgeIt& i) const {
565 typename Graph::Node v;
567 case 0: //normal edge
568 v=this->graph->aNode(i.e);
569 this->graph->next(i.e);
570 if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
571 if (this->graph->inSClass(v)) { //S, nincs kiel
574 } else { //T, van kiel
581 this->graph->next(i.n);
582 if (!this->graph->valid(i.n)) i.spec=3;
591 InEdgeIt& next(InEdgeIt& i) const {
592 typename Graph::Node v;
594 case 0: //normal edge
595 v=this->graph->aNode(i.e);
596 this->graph->next(i.e);
597 if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
598 if (this->graph->inTClass(v)) { //S, nincs beel
601 } else { //S, van beel
612 this->graph->next(i.n);
613 if (!this->graph->valid(i.n)) i.spec=3;
619 EdgeIt& next(EdgeIt& i) const {
622 this->graph->next(i.e);
623 if (!this->graph->valid(i.e)) {
625 this->graph->first(i.n, S_CLASS);
626 if (!this->graph->valid(i.n)) {
628 this->graph->first(i.n, T_CLASS);
629 if (!this->graph->valid(i.n)) i.spec=3;
634 this->graph->next(i.n);
635 if (!this->graph->valid(i.n)) {
637 this->graph->first(i.n, T_CLASS);
638 if (!this->graph->valid(i.n)) i.spec=3;
642 this->graph->next(i.n);
643 if (!this->graph->valid(i.n)) i.spec=3;
649 Node tail(const Edge& e) const {
652 return Node(this->graph->tail(e));
663 Node head(const Edge& e) const {
666 return Node(this->graph->head(e));
678 bool valid(const Node& n) const { return (n.spec<3); }
679 bool valid(const Edge& e) const { return (e.spec<3); }
681 int nodeNum() const { return this->graph->nodeNum()+2; }
682 int edgeNum() const {
683 return this->graph->edgeNum()+this->graph->nodeNum();
686 Node aNode(const OutEdgeIt& e) const { return tail(e); }
687 Node aNode(const InEdgeIt& e) const { return head(e); }
688 Node bNode(const OutEdgeIt& e) const { return head(e); }
689 Node bNode(const InEdgeIt& e) const { return tail(e); }
691 void addNode() const { }
692 void addEdge() const { }
694 // Node addNode() const { return Node(this->graph->addNode()); }
695 // Edge addEdge(const Node& tail, const Node& head) const {
696 // return Edge(this->graph->addEdge(tail, head)); }
698 // void erase(const Node& i) const { this->graph->erase(i); }
699 // void erase(const Edge& i) const { this->graph->erase(i); }
701 // void clear() const { this->graph->clear(); }
703 template<typename T> class NodeMap : public GraphWrapper<Graph>::template NodeMap<T> {
704 typedef typename GraphWrapper<Graph>::template NodeMap<T> Parent;
707 NodeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
710 NodeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
713 T operator[](const Node& n) const {
716 return Parent::operator[](n);
727 void set(const Node& n, T t) {
730 GraphWrapper<Graph>::template NodeMap<T>::set(n, t);
745 typename GraphWrapper<Graph>::template EdgeMap<T> >
746 class EdgeMap : public Parent {
747 //typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
748 typename GraphWrapper<Graph>::template NodeMap<T> node_value;
750 EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
752 EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
753 node_value(_G, a) { }
754 T operator[](const Edge& e) const {
757 return Parent::operator[](e);
760 return node_value[e.n];
764 return node_value[e.n];
768 void set(const Edge& e, T t) {
774 node_value.set(e.n, t);
778 node_value.set(e.n, t);
784 // template<typename T> class EdgeMap : public GraphWrapper<Graph>::template EdgeMap<T> {
785 // typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
786 // typename GraphWrapper<Graph>::template NodeMap<T> node_value;
788 // EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
789 // node_value(_G) { }
790 // EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
791 // node_value(_G, a) { }
792 // T operator[](const Edge& e) const {
795 // return Parent::operator[](e);
798 // return node_value[e.n];
802 // return node_value[e.n];
806 // void set(const Edge& e, T t) {
809 // GraphWrapper<Graph>::template EdgeMap<T>::set(e, t);
812 // node_value.set(e.n, t);
816 // node_value.set(e.n, t);
822 // template<typename G>
824 operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i) {
825 os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
828 // template<typename G>
830 operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i) {
831 os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
832 " node: " << i.n << ")";
843 #endif //HUGO_GRAPH_WRAPPER_H