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> {
29 typedef IterableBoolMap< typename Graph::template NodeMap<int> >
31 SFalseTTrueMap* s_false_t_true_map;
35 //FIXME vhogy igy kellene, csak az en forditom nem eszi meg
36 //static const bool S_CLASS=false;
37 //static const bool T_CLASS=true;
42 BipartiteGraphWrapper(Graph& _graph, SFalseTTrueMap& _s_false_t_true_map)
43 : GraphWrapper<Graph>(_graph), s_false_t_true_map(&_s_false_t_true_map),
44 S_CLASS(false), T_CLASS(true) { }
45 typedef typename GraphWrapper<Graph>::Node Node;
46 //using GraphWrapper<Graph>::NodeIt;
47 typedef typename GraphWrapper<Graph>::Edge Edge;
48 //using GraphWrapper<Graph>::EdgeIt;
50 friend class ClassNodeIt;
52 friend class OutEdgeIt;
54 friend class InEdgeIt;
56 friend class BipartiteGraphWrapper<Graph>;
61 ClassNodeIt(const Invalid& i) : n(i) { }
62 ClassNodeIt(const BipartiteGraphWrapper<Graph>& _G, bool _class) {
63 _G.s_false_t_true_map->first(n, _class);
65 //FIXME needed in new concept, important here
66 ClassNodeIt(const Node& _n) : n(_n) { }
67 operator Node() const { return n; }
73 // SNodeIt(const Invalid& i) : n(i) { }
74 // SNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
75 // _G.s_false_t_true_map->first(n, false);
77 // operator Node() const { return n; }
83 // TNodeIt(const Invalid& i) : n(i) { }
84 // TNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
85 // _G.s_false_t_true_map->first(n, true);
87 // operator Node() const { return n; }
90 friend class BipartiteGraphWrapper<Graph>;
92 typename Graph::OutEdgeIt e;
95 OutEdgeIt(const Invalid& i) : e(i) { }
96 OutEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
97 if (!(*(_G.s_false_t_true_map))[_n])
98 e=typename Graph::OutEdgeIt(*(_G.graph), typename Graph::Node(_n));
102 operator Edge() const { return Edge(typename Graph::Edge(e)); }
105 friend class BipartiteGraphWrapper<Graph>;
107 typename Graph::InEdgeIt e;
110 InEdgeIt(const Invalid& i) : e(i) { }
111 InEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
112 if ((*(_G.s_false_t_true_map))[_n])
113 e=typename Graph::InEdgeIt(*(_G.graph), typename Graph::Node(_n));
117 operator Edge() const { return Edge(typename Graph::Edge(e)); }
120 using GraphWrapper<Graph>::first;
121 ClassNodeIt& first(ClassNodeIt& n, bool _class) const {
122 n=ClassNodeIt(*this, _class) ; return n; }
123 // SNodeIt& first(SNodeIt& n) const { n=SNodeIt(*this); return n; }
124 // TNodeIt& first(TNodeIt& n) const { n=TNodeIt(*this); return n; }
125 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
126 i=OutEdgeIt(*this, p); return i;
128 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
129 i=InEdgeIt(*this, p); return i;
132 using GraphWrapper<Graph>::next;
133 ClassNodeIt& next(ClassNodeIt& n) const {
134 this->s_false_t_true_map->next(n.n); return n;
136 // SNodeIt& next(SNodeIt& n) const {
137 // this->s_false_t_true_map->next(n); return n;
139 // TNodeIt& next(TNodeIt& n) const {
140 // this->s_false_t_true_map->next(n); return n;
142 OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
143 InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
145 Node tail(const Edge& e) {
146 if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
147 return Node(this->graph->tail(e));
149 return Node(this->graph->head(e));
151 Node head(const Edge& e) {
152 if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
153 return Node(this->graph->head(e));
155 return Node(this->graph->tail(e));
158 Node aNode(const OutEdgeIt& e) const {
159 return Node(this->graph->aNode(e.e));
161 Node aNode(const InEdgeIt& e) const {
162 return Node(this->graph->aNode(e.e));
164 Node bNode(const OutEdgeIt& e) const {
165 return Node(this->graph->bNode(e.e));
167 Node bNode(const InEdgeIt& e) const {
168 return Node(this->graph->bNode(e.e));
171 bool inSClass(const Node& n) const {
172 return !(*(this->s_false_t_true_map))[n];
174 bool inTClass(const Node& n) const {
175 return (*(this->s_false_t_true_map))[n];
179 template<typename Graph>
180 class stGraphWrapper;
182 // template<typename Graph>
184 // operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Node& i) {
185 // os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
188 // template<typename Graph>
190 // operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Edge& i) {
191 // os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
192 // " node: " << i.n << ")";
196 /// experimentral, do not try it.
197 /// It eats a bipartite graph, oriented from S to T.
198 /// Such one can be made e.g. by the above wrapper.
200 ///\author Marton Makai
201 template<typename Graph>
202 class stGraphWrapper : public GraphWrapper<Graph> {
207 //0 normalis, 1 s, 2, true, ez az iteralasi sorrend,
208 //es a vege a false azaz (invalid, 3)
215 //0 normalis, 1 s->vmi, 2 vmi->t, ez a sorrend,
216 //invalid: (invalid, 3, invalid)
218 friend class OutEdgeIt;
220 //normalis pontbol (first, 0, invalid), ..., (invalid, 2, vmi), ... (invalid, 3, invalid)
221 //s-bol (invalid, 1, first), ... (invalid, 3, invalid)
222 //t-bol (invalid, 3, invalid)
224 friend class InEdgeIt;
226 //normalis pontbol (first, 0, invalid), ..., (invalid, 1, vmi), ... (invalid, 3, invalid)
227 //s-be (invalid, 3, invalid)
228 //t-be (invalid, 2, first), ... (invalid, 3, invalid)
231 //(first, 0, invalid) ...
234 //invalid, 3, invalid)
235 template <typename T> class NodeMap;
236 template <typename T, typename Parent> class EdgeMap;
238 // template <typename T> friend class NodeMap;
239 // template <typename T> friend class EdgeMap;
244 static const bool S_CLASS=false;
245 static const bool T_CLASS=true;
247 stGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) ,
249 T_NODE(INVALID, 2) { }
253 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
254 // friend std::ostream&
255 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
256 // friend std::ostream&
257 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i);
259 class Node : public Graph::Node {
261 friend class GraphWrapper<Graph>;
262 friend class stGraphWrapper<Graph>;
263 template <typename T> friend class NodeMap;
265 friend class OutEdgeIt;
266 friend class InEdgeIt;
271 Node(const typename Graph::Node& _n, int _spec=0) :
272 Graph::Node(_n), spec(_spec) { }
273 Node(const Invalid& i) : Graph::Node(i), spec(3) { }
274 friend bool operator==(const Node& u, const Node& v) {
275 return (u.spec==v.spec &&
276 static_cast<typename Graph::Node>(u)==
277 static_cast<typename Graph::Node>(v));
279 friend bool operator!=(const Node& u, const Node& v) {
280 return (v.spec!=u.spec ||
281 static_cast<typename Graph::Node>(u)!=
282 static_cast<typename Graph::Node>(v));
284 // template<typename G>
285 // friend std::ostream&
286 // operator<<(std::ostream& os, const typename stGraphWrapper<G>::Node& i);
287 friend std::ostream& operator<< (std::ostream& os, const Node& i);
288 int getSpec() const { return spec; }
292 friend class GraphWrapper<Graph>;
293 friend class stGraphWrapper<Graph>;
294 typename Graph::NodeIt n;
298 NodeIt(const typename Graph::NodeIt& _n, int _spec) :
299 n(_n), spec(_spec) { }
300 NodeIt(const Invalid& i) : n(i), spec(3) { }
301 NodeIt(const stGraphWrapper<Graph>& _G) : n(*(_G.graph)), spec(0) {
302 if (!_G.graph->valid(n)) spec=1;
304 operator Node() const { return Node(n, spec); }
307 class Edge : public Graph::Edge {
308 friend class GraphWrapper<Graph>;
309 friend class stGraphWrapper<Graph>;
310 template <typename T, typename Parent> friend class EdgeMap;
312 typename Graph::Node n;
315 Edge(const typename Graph::Edge& _e, int _spec,
316 const typename Graph::Node& _n) :
317 Graph::Edge(_e), spec(_spec), n(_n) {
319 Edge(const Invalid& i) : Graph::Edge(i), spec(3), n(i) { }
320 friend bool operator==(const Edge& u, const Edge& v) {
321 return (u.spec==v.spec &&
322 static_cast<typename Graph::Edge>(u)==
323 static_cast<typename Graph::Edge>(v) &&
326 friend bool operator!=(const Edge& u, const Edge& v) {
327 return (v.spec!=u.spec ||
328 static_cast<typename Graph::Edge>(u)!=
329 static_cast<typename Graph::Edge>(v) ||
332 // template<typename G>
333 // friend std::ostream&
334 // operator<<(std::ostream& os, const typename stGraphWrapper<G>::Edge& i);
335 friend std::ostream& operator<< (std::ostream& os, const Edge& i);
336 int getSpec() const { return spec; }
340 friend class GraphWrapper<Graph>;
341 friend class stGraphWrapper<Graph>;
342 typename Graph::OutEdgeIt e;
344 typename Graph::ClassNodeIt n;
347 OutEdgeIt(const typename Graph::OutEdgeIt& _e, int _spec,
348 const typename Graph::ClassNodeIt& _n) :
349 e(_e), spec(_spec), n(_n) {
351 OutEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
352 OutEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
355 if (_G.graph->inSClass(_n)) { //S, van normalis kiel
356 e=typename Graph::OutEdgeIt(*(_G.graph),
357 typename Graph::Node(_n));
360 if (!_G.graph->valid(e)) spec=3;
361 } else { //T, specko kiel van
370 _G.graph->first(n, S_CLASS); //s->vmi;
371 if (!_G.graph->valid(n)) spec=3; //Ha S ures
380 operator Edge() const { return Edge(e, spec, n); }
384 friend class GraphWrapper<Graph>;
385 friend class stGraphWrapper<Graph>;
386 typename Graph::InEdgeIt e;
388 typename Graph::ClassNodeIt n;
391 InEdgeIt(const typename Graph::InEdgeIt& _e, int _spec,
392 const typename Graph::ClassNodeIt& _n) :
393 e(_e), spec(_spec), n(_n) {
395 InEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
396 InEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
399 if (_G.graph->inTClass(_n)) { //T, van normalis beel
400 e=typename Graph::InEdgeIt(*(_G.graph),
401 typename Graph::Node(_n));
404 if (!_G.graph->valid(e)) spec=3;
405 } else { //S, specko beel van
419 _G.graph->first(n, T_CLASS); //vmi->t;
420 if (!_G.graph->valid(n)) spec=3; //Ha T ures
424 operator Edge() const { return Edge(e, spec, n); }
428 friend class GraphWrapper<Graph>;
429 friend class stGraphWrapper<Graph>;
430 typename Graph::EdgeIt e;
432 typename Graph::ClassNodeIt n;
435 EdgeIt(const typename Graph::EdgeIt& _e, int _spec,
436 const typename Graph::ClassNodeIt& _n) :
437 e(_e), spec(_spec), n(_n) { }
438 EdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
439 EdgeIt(const stGraphWrapper<Graph>& _G) :
440 e(*(_G.graph)), spec(0), n(INVALID) {
441 if (!_G.graph->valid(e)) {
443 _G.graph->first(n, S_CLASS);
444 if (!_G.graph->valid(n)) { //Ha S ures
446 _G.graph->first(n, T_CLASS);
447 if (!_G.graph->valid(n)) { //Ha T ures
453 operator Edge() const { return Edge(e, spec, n); }
456 NodeIt& first(NodeIt& i) const {
457 i=NodeIt(*this); return i;
459 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
460 i=OutEdgeIt(*this, p); return i;
462 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
463 i=InEdgeIt(*this, p); return i;
465 EdgeIt& first(EdgeIt& i) const {
466 i=EdgeIt(*this); return i;
469 NodeIt& next(NodeIt& i) const {
472 this->graph->next(i.n);
473 if (!this->graph->valid(i.n)) {
486 OutEdgeIt& next(OutEdgeIt& i) const {
487 typename Graph::Node v;
489 case 0: //normal edge
490 v=this->graph->aNode(i.e);
491 this->graph->next(i.e);
492 if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
493 if (this->graph->inSClass(v)) { //S, nincs kiel
496 } else { //T, van kiel
503 this->graph->next(i.n);
504 if (!this->graph->valid(i.n)) i.spec=3;
513 InEdgeIt& next(InEdgeIt& i) const {
514 typename Graph::Node v;
516 case 0: //normal edge
517 v=this->graph->aNode(i.e);
518 this->graph->next(i.e);
519 if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
520 if (this->graph->inTClass(v)) { //S, nincs beel
523 } else { //S, van beel
534 this->graph->next(i.n);
535 if (!this->graph->valid(i.n)) i.spec=3;
541 EdgeIt& next(EdgeIt& i) const {
544 this->graph->next(i.e);
545 if (!this->graph->valid(i.e)) {
547 this->graph->first(i.n, S_CLASS);
548 if (!this->graph->valid(i.n)) {
550 this->graph->first(i.n, T_CLASS);
551 if (!this->graph->valid(i.n)) i.spec=3;
556 this->graph->next(i.n);
557 if (!this->graph->valid(i.n)) {
559 this->graph->first(i.n, T_CLASS);
560 if (!this->graph->valid(i.n)) i.spec=3;
564 this->graph->next(i.n);
565 if (!this->graph->valid(i.n)) i.spec=3;
571 Node tail(const Edge& e) const {
574 return Node(this->graph->tail(e));
585 Node head(const Edge& e) const {
588 return Node(this->graph->head(e));
600 bool valid(const Node& n) const { return (n.spec<3); }
601 bool valid(const Edge& e) const { return (e.spec<3); }
603 int nodeNum() const { return this->graph->nodeNum()+2; }
604 int edgeNum() const {
605 return this->graph->edgeNum()+this->graph->nodeNum();
608 Node aNode(const OutEdgeIt& e) const { return tail(e); }
609 Node aNode(const InEdgeIt& e) const { return head(e); }
610 Node bNode(const OutEdgeIt& e) const { return head(e); }
611 Node bNode(const InEdgeIt& e) const { return tail(e); }
613 void addNode() const { }
614 void addEdge() const { }
616 // Node addNode() const { return Node(this->graph->addNode()); }
617 // Edge addEdge(const Node& tail, const Node& head) const {
618 // return Edge(this->graph->addEdge(tail, head)); }
620 // void erase(const Node& i) const { this->graph->erase(i); }
621 // void erase(const Edge& i) const { this->graph->erase(i); }
623 // void clear() const { this->graph->clear(); }
625 template<typename T> class NodeMap : public GraphWrapper<Graph>::template NodeMap<T> {
626 typedef typename GraphWrapper<Graph>::template NodeMap<T> Parent;
629 NodeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
632 NodeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
635 T operator[](const Node& n) const {
638 return Parent::operator[](n);
649 void set(const Node& n, T t) {
652 GraphWrapper<Graph>::template NodeMap<T>::set(n, t);
667 typename GraphWrapper<Graph>::template EdgeMap<T> >
668 class EdgeMap : public Parent {
669 //typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
670 typename GraphWrapper<Graph>::template NodeMap<T> node_value;
672 EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
674 EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
675 node_value(_G, a) { }
676 T operator[](const Edge& e) const {
679 return Parent::operator[](e);
682 return node_value[e.n];
686 return node_value[e.n];
690 void set(const Edge& e, T t) {
696 node_value.set(e.n, t);
700 node_value.set(e.n, t);
706 // template<typename T> class EdgeMap : public GraphWrapper<Graph>::template EdgeMap<T> {
707 // typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
708 // typename GraphWrapper<Graph>::template NodeMap<T> node_value;
710 // EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
711 // node_value(_G) { }
712 // EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
713 // node_value(_G, a) { }
714 // T operator[](const Edge& e) const {
717 // return Parent::operator[](e);
720 // return node_value[e.n];
724 // return node_value[e.n];
728 // void set(const Edge& e, T t) {
731 // GraphWrapper<Graph>::template EdgeMap<T>::set(e, t);
734 // node_value.set(e.n, t);
738 // node_value.set(e.n, t);
744 // template<typename G>
746 operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i) {
747 os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
750 // template<typename G>
752 operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i) {
753 os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
754 " node: " << i.n << ")";
765 #endif //HUGO_GRAPH_WRAPPER_H