Some modifications and another testfile.
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
13 #include <hugo/invalid.h>
15 #include <hugo/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, *this) erase(e);
251 FOR_EACH_LOC(typename Parent::NodeIt, n, *this) 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> 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> 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; }
415 typename Graph::Node getNode() const { return n; }
419 friend class GraphWrapper<Graph>;
420 friend class stGraphWrapper<Graph>;
421 typename Graph::OutEdgeIt e;
423 typename Graph::ClassNodeIt n;
426 OutEdgeIt(const typename Graph::OutEdgeIt& _e, int _spec,
427 const typename Graph::ClassNodeIt& _n) :
428 e(_e), spec(_spec), n(_n) {
430 OutEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
431 OutEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
434 if (_G.graph->inSClass(_n)) { //S, van normalis kiel
435 e=typename Graph::OutEdgeIt(*(_G.graph),
436 typename Graph::Node(_n));
439 if (!_G.graph->valid(e)) spec=3;
440 } else { //T, specko kiel van
449 _G.graph->first(n, S_CLASS); //s->vmi;
450 if (!_G.graph->valid(n)) spec=3; //Ha S ures
459 operator Edge() const { return Edge(e, spec, n); }
463 friend class GraphWrapper<Graph>;
464 friend class stGraphWrapper<Graph>;
465 typename Graph::InEdgeIt e;
467 typename Graph::ClassNodeIt n;
470 InEdgeIt(const typename Graph::InEdgeIt& _e, int _spec,
471 const typename Graph::ClassNodeIt& _n) :
472 e(_e), spec(_spec), n(_n) {
474 InEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
475 InEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
478 if (_G.graph->inTClass(_n)) { //T, van normalis beel
479 e=typename Graph::InEdgeIt(*(_G.graph),
480 typename Graph::Node(_n));
483 if (!_G.graph->valid(e)) spec=3;
484 } else { //S, specko beel van
498 _G.graph->first(n, T_CLASS); //vmi->t;
499 if (!_G.graph->valid(n)) spec=3; //Ha T ures
503 operator Edge() const { return Edge(e, spec, n); }
507 friend class GraphWrapper<Graph>;
508 friend class stGraphWrapper<Graph>;
509 typename Graph::EdgeIt e;
511 typename Graph::ClassNodeIt n;
514 EdgeIt(const typename Graph::EdgeIt& _e, int _spec,
515 const typename Graph::ClassNodeIt& _n) :
516 e(_e), spec(_spec), n(_n) { }
517 EdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
518 EdgeIt(const stGraphWrapper<Graph>& _G) :
519 e(*(_G.graph)), spec(0), n(INVALID) {
520 if (!_G.graph->valid(e)) {
522 _G.graph->first(n, S_CLASS);
523 if (!_G.graph->valid(n)) { //Ha S ures
525 _G.graph->first(n, T_CLASS);
526 if (!_G.graph->valid(n)) { //Ha T ures
532 operator Edge() const { return Edge(e, spec, n); }
535 NodeIt& first(NodeIt& i) const {
536 i=NodeIt(*this); return i;
538 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
539 i=OutEdgeIt(*this, p); return i;
541 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
542 i=InEdgeIt(*this, p); return i;
544 EdgeIt& first(EdgeIt& i) const {
545 i=EdgeIt(*this); return i;
548 NodeIt& next(NodeIt& i) const {
551 this->graph->next(i.n);
552 if (!this->graph->valid(i.n)) {
565 OutEdgeIt& next(OutEdgeIt& i) const {
566 typename Graph::Node v;
568 case 0: //normal edge
569 v=this->graph->aNode(i.e);
570 this->graph->next(i.e);
571 if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
572 if (this->graph->inSClass(v)) { //S, nincs kiel
575 } else { //T, van kiel
582 this->graph->next(i.n);
583 if (!this->graph->valid(i.n)) i.spec=3;
592 InEdgeIt& next(InEdgeIt& i) const {
593 typename Graph::Node v;
595 case 0: //normal edge
596 v=this->graph->aNode(i.e);
597 this->graph->next(i.e);
598 if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
599 if (this->graph->inTClass(v)) { //S, nincs beel
602 } else { //S, van beel
613 this->graph->next(i.n);
614 if (!this->graph->valid(i.n)) i.spec=3;
620 EdgeIt& next(EdgeIt& i) const {
623 this->graph->next(i.e);
624 if (!this->graph->valid(i.e)) {
626 this->graph->first(i.n, S_CLASS);
627 if (!this->graph->valid(i.n)) {
629 this->graph->first(i.n, T_CLASS);
630 if (!this->graph->valid(i.n)) i.spec=3;
635 this->graph->next(i.n);
636 if (!this->graph->valid(i.n)) {
638 this->graph->first(i.n, T_CLASS);
639 if (!this->graph->valid(i.n)) i.spec=3;
643 this->graph->next(i.n);
644 if (!this->graph->valid(i.n)) i.spec=3;
650 Node tail(const Edge& e) const {
653 return Node(this->graph->tail(e));
664 Node head(const Edge& e) const {
667 return Node(this->graph->head(e));
679 bool valid(const Node& n) const { return (n.spec<3); }
680 bool valid(const Edge& e) const { return (e.spec<3); }
682 int nodeNum() const { return this->graph->nodeNum()+2; }
683 int edgeNum() const {
684 return this->graph->edgeNum()+this->graph->nodeNum();
687 Node aNode(const OutEdgeIt& e) const { return tail(e); }
688 Node aNode(const InEdgeIt& e) const { return head(e); }
689 Node bNode(const OutEdgeIt& e) const { return head(e); }
690 Node bNode(const InEdgeIt& e) const { return tail(e); }
692 void addNode() const { }
693 void addEdge() const { }
695 // Node addNode() const { return Node(this->graph->addNode()); }
696 // Edge addEdge(const Node& tail, const Node& head) const {
697 // return Edge(this->graph->addEdge(tail, head)); }
699 // void erase(const Node& i) const { this->graph->erase(i); }
700 // void erase(const Edge& i) const { this->graph->erase(i); }
702 // void clear() const { this->graph->clear(); }
704 template<typename T> class NodeMap : public GraphWrapper<Graph>::template NodeMap<T> {
705 typedef typename GraphWrapper<Graph>::template NodeMap<T> Parent;
709 NodeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
712 NodeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
715 T operator[](const Node& n) const {
718 return Parent::operator[](n);
726 void set(const Node& n, T t) {
729 GraphWrapper<Graph>::template NodeMap<T>::set(n, t);
742 /// This class is to wrap a node-map of \c Graph and two variables
743 /// storing values for \c S_NODE and \c T_NODE to a node-map of
744 /// stGraphWrapper<Graph>.
745 template<typename NM> class NodeMapWrapper {
747 typedef Node KeyType;
748 typedef typename NM::ValueType ValueType;
751 ValueType* s_value, t_value;
753 NodeMapWrapper(NM& _nm, ValueType& _s_value, ValueType& _t_value) :
754 nm(&_nm), s_value(&_s_value), t_value(&_t_value) { }
755 ValueType operator[](const Node& n) const {
756 switch (n.getSpec()) {
766 void set(const Node& n, ValueType t) {
767 switch (n.getSpec()) {
783 class EdgeMap : public GraphWrapper<Graph>::template EdgeMap<T> {
784 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),
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);
797 return node_value[e.n];
800 return node_value[e.n];
803 void set(const Edge& e, T t) {
809 node_value.set(e.n, t);
813 node_value.set(e.n, t);
819 /// This class is to wrap an edge-map and a node-map of \c Graph
820 /// to an edge-map of stGraphWrapper<Graph>.
821 template<typename EM, typename NM>
822 class EdgeMapWrapper {
824 typedef Edge KeyType;
825 typedef typename EM::ValueType ValueType;
830 EdgeMapWrapper(EM& _em, NM& _nm) : em(&_em), nm(&_nm) { }
831 ValueType operator[](const Edge& e) const {
832 switch (e.getSpec()) {
836 return (*nm)[e.getNode()];
839 return (*nm)[e.getNode()];
842 void set(const Edge& e, ValueType t) {
843 switch (e.getSpec()) {
848 nm->set(e.getNode(), t);
852 nm->set(e.getNode(), t);
859 // template<typename G>
861 operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i) {
862 os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
865 // template<typename G>
867 operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i) {
868 os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
869 " node: " << i.n << ")";
880 #endif //HUGO_BIPARTITE_GRAPH_WRAPPER_H