lp_solver_wrapper stuff.
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>
16 #include <for_each_macros.h>
20 /// \brief A wrapper for composing a bipartite graph from a graph
21 /// and from a node-map showing for any node which color class it belongs to.
23 /// A wrapper for composing a bipartite graph.
24 /// \c _graph have to be a reference to a graph of type \c Graph
25 /// and \c _s_false_t_true_map is an \c IterableBoolMap
26 /// reference containing the elements for the
27 /// color classes S and T. \c _graph is to be referred to an undirected
28 /// graph or a directed graph with edges oriented from S to T.
30 /// \author Marton Makai
31 template<typename Graph>
32 class BipartiteGraphWrapper : public GraphWrapper<Graph> {
34 typedef IterableBoolMap< typename Graph::template NodeMap<int> >
36 SFalseTTrueMap* s_false_t_true_map;
38 BipartiteGraphWrapper() : GraphWrapper<Graph>()/*,
39 S_CLASS(false), T_CLASS(true)*/ { }
40 void setSFalseTTrueMap(SFalseTTrueMap& _s_false_t_true_map) {
41 s_false_t_true_map=&_s_false_t_true_map;
46 //FIXME vhogy igy kellene, csak az en forditom nem eszi meg
47 static const bool S_CLASS;
48 static const bool T_CLASS;
53 BipartiteGraphWrapper(Graph& _graph, SFalseTTrueMap& _s_false_t_true_map)
54 : GraphWrapper<Graph>(_graph),
55 s_false_t_true_map(&_s_false_t_true_map)/*,
56 S_CLASS(false), T_CLASS(true)*/ { }
57 typedef typename GraphWrapper<Graph>::Node Node;
58 //using GraphWrapper<Graph>::NodeIt;
59 typedef typename GraphWrapper<Graph>::Edge Edge;
60 //using GraphWrapper<Graph>::EdgeIt;
62 friend class ClassNodeIt;
64 friend class OutEdgeIt;
66 friend class InEdgeIt;
68 friend class BipartiteGraphWrapper<Graph>;
73 ClassNodeIt(const Invalid& i) : n(i) { }
74 ClassNodeIt(const BipartiteGraphWrapper<Graph>& _G, bool _class) {
75 _G.s_false_t_true_map->first(n, _class);
77 //FIXME needed in new concept, important here
78 ClassNodeIt(const Node& _n) : n(_n) { }
79 operator Node() const { return n; }
85 // SNodeIt(const Invalid& i) : n(i) { }
86 // SNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
87 // _G.s_false_t_true_map->first(n, false);
89 // operator Node() const { return n; }
95 // TNodeIt(const Invalid& i) : n(i) { }
96 // TNodeIt(const BipartiteGraphWrapper<Graph>& _G) {
97 // _G.s_false_t_true_map->first(n, true);
99 // operator Node() const { return n; }
102 friend class BipartiteGraphWrapper<Graph>;
104 typename Graph::OutEdgeIt e;
107 OutEdgeIt(const Invalid& i) : e(i) { }
108 OutEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
109 if (!(*(_G.s_false_t_true_map))[_n])
110 e=typename Graph::OutEdgeIt(*(_G.graph), typename Graph::Node(_n));
114 operator Edge() const { return Edge(typename Graph::Edge(e)); }
117 friend class BipartiteGraphWrapper<Graph>;
119 typename Graph::InEdgeIt e;
122 InEdgeIt(const Invalid& i) : e(i) { }
123 InEdgeIt(const BipartiteGraphWrapper<Graph>& _G, const Node& _n) {
124 if ((*(_G.s_false_t_true_map))[_n])
125 e=typename Graph::InEdgeIt(*(_G.graph), typename Graph::Node(_n));
129 operator Edge() const { return Edge(typename Graph::Edge(e)); }
132 using GraphWrapper<Graph>::first;
133 ClassNodeIt& first(ClassNodeIt& n, bool _class) const {
134 n=ClassNodeIt(*this, _class) ; return n; }
135 // SNodeIt& first(SNodeIt& n) const { n=SNodeIt(*this); return n; }
136 // TNodeIt& first(TNodeIt& n) const { n=TNodeIt(*this); return n; }
137 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
138 i=OutEdgeIt(*this, p); return i;
140 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
141 i=InEdgeIt(*this, p); return i;
144 using GraphWrapper<Graph>::next;
145 ClassNodeIt& next(ClassNodeIt& n) const {
146 this->s_false_t_true_map->next(n.n); return n;
148 // SNodeIt& next(SNodeIt& n) const {
149 // this->s_false_t_true_map->next(n); return n;
151 // TNodeIt& next(TNodeIt& n) const {
152 // this->s_false_t_true_map->next(n); return n;
154 OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
155 InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
157 Node tail(const Edge& e) {
158 if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
159 return Node(this->graph->tail(e));
161 return Node(this->graph->head(e));
163 Node head(const Edge& e) {
164 if (!(*(this->s_false_t_true_map))[this->graph->tail(e)])
165 return Node(this->graph->head(e));
167 return Node(this->graph->tail(e));
170 Node aNode(const OutEdgeIt& e) const {
171 return Node(this->graph->aNode(e.e));
173 Node aNode(const InEdgeIt& e) const {
174 return Node(this->graph->aNode(e.e));
176 Node bNode(const OutEdgeIt& e) const {
177 return Node(this->graph->bNode(e.e));
179 Node bNode(const InEdgeIt& e) const {
180 return Node(this->graph->bNode(e.e));
183 /// Returns true iff \c n is in S.
184 bool inSClass(const Node& n) const {
185 return !(*(this->s_false_t_true_map))[n];
188 /// Returns true iff \c n is in T.
189 bool inTClass(const Node& n) const {
190 return (*(this->s_false_t_true_map))[n];
196 const bool BipartiteGraphWrapper<G>::S_CLASS=false;
198 const bool BipartiteGraphWrapper<G>::T_CLASS=true;
200 /// \brief A bipartite graph template class
202 /// This class composes a bipartite graph over a directed or undirected
203 /// graph structure of type \c Graph.
204 /// \c _graph have to be a reference to a graph of type \c Graph
205 /// and \c _s_false_t_true_map is an \c IterableBoolMap
206 /// reference containing the elements for the
207 /// color classes S and T. \c _graph is to be referred to an undirected
208 /// graph or a directed graph with edges oriented from S to T.
210 ///\bug experimental. Do not use this while the bipartitemap augmentation
211 /// does not work well.
212 template<typename Graph>
213 class BipartiteGraph : public BipartiteGraphWrapper<Graph> {
214 typedef IterableBoolMap< typename Graph::template NodeMap<int> >
216 typedef BipartiteGraphWrapper<Graph> Parent;
219 typename Graph::template NodeMap<int> bipartite_map;
220 SFalseTTrueMap s_false_t_true_map;
222 typedef typename Parent::Node Node;
223 typedef typename Parent::Edge Edge;
224 BipartiteGraph() : BipartiteGraphWrapper<Graph>(),
225 gr(), bipartite_map(gr, -1),
226 s_false_t_true_map(bipartite_map) {
227 Parent::setGraph(gr);
228 Parent::setSFalseTTrueMap(s_false_t_true_map);
231 /// the \c bool parameter which can be \c S_Class or \c T_Class shows
232 /// the color class where the new node is to be inserted.
233 Node addNode(bool b) {
234 Node n=Parent::graph->addNode();
235 bipartite_map.update();
236 //bipartite_map.set(n, -1);
237 s_false_t_true_map.insert(n, b);
241 /// A new edge is inserted.
242 ///\pre \c tail have to be in \c S_Class and \c head in \c T_Class.
243 Edge addEdge(const Node& tail, const Node& head) {
244 return Parent::graph->addEdge(tail, head);
247 void erase(const Node& n) {
248 s_false_t_true_map.remove(n);
249 Parent::graph->erase(n);
251 void erase(const Edge& e) {
252 Parent::graph->erase(e);
256 FOR_EACH_LOC(typename Parent::EdgeIt, e, *this) erase(e);
257 FOR_EACH_LOC(typename Parent::NodeIt, n, *this) erase(n);
262 template<typename Graph>
263 class stGraphWrapper;
265 // template<typename Graph>
267 // operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Node& i) {
268 // os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
271 // template<typename Graph>
273 // operator<<(std::ostream& os, const typename stGraphWrapper<Graph>::Edge& i) {
274 // os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
275 // " node: " << i.n << ")";
279 /// \brief A wrapper for adding extra nodes s and t to a bipartite graph
280 /// and edges from s to each node of S and form each node of T to t.
282 /// A wrapper for adding extra nodes s and t to a bipartite graph
283 /// and edges from s to each node of S and form each node of T to t.
284 /// This class is very useful to reduce some matching or more
285 /// generally, capacitataed b-matching problem to a flow problem.
286 /// According to the bipartite graph concepts the bipartite
287 /// graph have to be oriented from S to T.
289 /// \author Marton Makai
290 template<typename Graph>
291 class stGraphWrapper : public GraphWrapper<Graph> {
296 //0 normalis, 1 s, 2, true, ez az iteralasi sorrend,
297 //es a vege a false azaz (invalid, 3)
304 //0 normalis, 1 s->vmi, 2 vmi->t, ez a sorrend,
305 //invalid: (invalid, 3, invalid)
307 friend class OutEdgeIt;
309 //normalis pontbol (first, 0, invalid), ..., (invalid, 2, vmi), ... (invalid, 3, invalid)
310 //s-bol (invalid, 1, first), ... (invalid, 3, invalid)
311 //t-bol (invalid, 3, invalid)
313 friend class InEdgeIt;
315 //normalis pontbol (first, 0, invalid), ..., (invalid, 1, vmi), ... (invalid, 3, invalid)
316 //s-be (invalid, 3, invalid)
317 //t-be (invalid, 2, first), ... (invalid, 3, invalid)
320 //(first, 0, invalid) ...
323 //invalid, 3, invalid)
324 template <typename T> class NodeMap;
325 template <typename T> class EdgeMap;
327 // template <typename T> friend class NodeMap;
328 // template <typename T> friend class EdgeMap;
330 ///\todo FIXME ezt majd static-ra kell javitani
334 static const bool S_CLASS=false;
335 static const bool T_CLASS=true;
337 stGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) ,
339 T_NODE(INVALID, 2) { }
343 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
344 // friend std::ostream&
345 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i);
346 // friend std::ostream&
347 // operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i);
349 class Node : public Graph::Node {
351 friend class GraphWrapper<Graph>;
352 friend class stGraphWrapper<Graph>;
353 template <typename T> friend class NodeMap;
355 friend class OutEdgeIt;
356 friend class InEdgeIt;
361 Node(const typename Graph::Node& _n, int _spec=0) :
362 Graph::Node(_n), spec(_spec) { }
363 Node(const Invalid& i) : Graph::Node(i), spec(3) { }
364 friend bool operator==(const Node& u, const Node& v) {
365 return (u.spec==v.spec &&
366 static_cast<typename Graph::Node>(u)==
367 static_cast<typename Graph::Node>(v));
369 friend bool operator!=(const Node& u, const Node& v) {
370 return (v.spec!=u.spec ||
371 static_cast<typename Graph::Node>(u)!=
372 static_cast<typename Graph::Node>(v));
374 // template<typename G>
375 // friend std::ostream&
376 // operator<<(std::ostream& os, const typename stGraphWrapper<G>::Node& i);
377 friend std::ostream& operator<< (std::ostream& os, const Node& i);
378 int getSpec() const { return spec; }
382 friend class GraphWrapper<Graph>;
383 friend class stGraphWrapper<Graph>;
384 typename Graph::NodeIt n;
388 NodeIt(const typename Graph::NodeIt& _n, int _spec) :
389 n(_n), spec(_spec) { }
390 NodeIt(const Invalid& i) : n(i), spec(3) { }
391 NodeIt(const stGraphWrapper<Graph>& _G) : n(*(_G.graph)), spec(0) {
392 if (!_G.graph->valid(n)) spec=1;
394 operator Node() const { return Node(n, spec); }
397 class Edge : public Graph::Edge {
398 friend class GraphWrapper<Graph>;
399 friend class stGraphWrapper<Graph>;
400 template <typename T> friend class EdgeMap;
402 typename Graph::Node n;
405 Edge(const typename Graph::Edge& _e, int _spec,
406 const typename Graph::Node& _n) :
407 Graph::Edge(_e), spec(_spec), n(_n) {
409 Edge(const Invalid& i) : Graph::Edge(i), spec(3), n(i) { }
410 friend bool operator==(const Edge& u, const Edge& v) {
411 return (u.spec==v.spec &&
412 static_cast<typename Graph::Edge>(u)==
413 static_cast<typename Graph::Edge>(v) &&
416 friend bool operator!=(const Edge& u, const Edge& v) {
417 return (v.spec!=u.spec ||
418 static_cast<typename Graph::Edge>(u)!=
419 static_cast<typename Graph::Edge>(v) ||
422 // template<typename G>
423 // friend std::ostream&
424 // operator<<(std::ostream& os, const typename stGraphWrapper<G>::Edge& i);
425 friend std::ostream& operator<< (std::ostream& os, const Edge& i);
426 int getSpec() const { return spec; }
427 typename Graph::Node getNode() const { return n; }
431 friend class GraphWrapper<Graph>;
432 friend class stGraphWrapper<Graph>;
433 typename Graph::OutEdgeIt e;
435 typename Graph::ClassNodeIt n;
438 OutEdgeIt(const typename Graph::OutEdgeIt& _e, int _spec,
439 const typename Graph::ClassNodeIt& _n) :
440 e(_e), spec(_spec), n(_n) {
442 OutEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
443 OutEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
446 if (_G.graph->inSClass(_n)) { //S, van normalis kiel
447 e=typename Graph::OutEdgeIt(*(_G.graph),
448 typename Graph::Node(_n));
451 if (!_G.graph->valid(e)) spec=3;
452 } else { //T, specko kiel van
461 _G.graph->first(n, S_CLASS); //s->vmi;
462 if (!_G.graph->valid(n)) spec=3; //Ha S ures
471 operator Edge() const { return Edge(e, spec, n); }
475 friend class GraphWrapper<Graph>;
476 friend class stGraphWrapper<Graph>;
477 typename Graph::InEdgeIt e;
479 typename Graph::ClassNodeIt n;
482 InEdgeIt(const typename Graph::InEdgeIt& _e, int _spec,
483 const typename Graph::ClassNodeIt& _n) :
484 e(_e), spec(_spec), n(_n) {
486 InEdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
487 InEdgeIt(const stGraphWrapper<Graph>& _G, const Node& _n) {
490 if (_G.graph->inTClass(_n)) { //T, van normalis beel
491 e=typename Graph::InEdgeIt(*(_G.graph),
492 typename Graph::Node(_n));
495 if (!_G.graph->valid(e)) spec=3;
496 } else { //S, specko beel van
510 _G.graph->first(n, T_CLASS); //vmi->t;
511 if (!_G.graph->valid(n)) spec=3; //Ha T ures
515 operator Edge() const { return Edge(e, spec, n); }
519 friend class GraphWrapper<Graph>;
520 friend class stGraphWrapper<Graph>;
521 typename Graph::EdgeIt e;
523 typename Graph::ClassNodeIt n;
526 EdgeIt(const typename Graph::EdgeIt& _e, int _spec,
527 const typename Graph::ClassNodeIt& _n) :
528 e(_e), spec(_spec), n(_n) { }
529 EdgeIt(const Invalid& i) : e(i), spec(3), n(i) { }
530 EdgeIt(const stGraphWrapper<Graph>& _G) :
531 e(*(_G.graph)), spec(0), n(INVALID) {
532 if (!_G.graph->valid(e)) {
534 _G.graph->first(n, S_CLASS);
535 if (!_G.graph->valid(n)) { //Ha S ures
537 _G.graph->first(n, T_CLASS);
538 if (!_G.graph->valid(n)) { //Ha T ures
544 operator Edge() const { return Edge(e, spec, n); }
547 NodeIt& first(NodeIt& i) const {
548 i=NodeIt(*this); return i;
550 OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
551 i=OutEdgeIt(*this, p); return i;
553 InEdgeIt& first(InEdgeIt& i, const Node& p) const {
554 i=InEdgeIt(*this, p); return i;
556 EdgeIt& first(EdgeIt& i) const {
557 i=EdgeIt(*this); return i;
560 NodeIt& next(NodeIt& i) const {
563 this->graph->next(i.n);
564 if (!this->graph->valid(i.n)) {
577 OutEdgeIt& next(OutEdgeIt& i) const {
578 typename Graph::Node v;
580 case 0: //normal edge
581 v=this->graph->aNode(i.e);
582 this->graph->next(i.e);
583 if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
584 if (this->graph->inSClass(v)) { //S, nincs kiel
587 } else { //T, van kiel
594 this->graph->next(i.n);
595 if (!this->graph->valid(i.n)) i.spec=3;
604 InEdgeIt& next(InEdgeIt& i) const {
605 typename Graph::Node v;
607 case 0: //normal edge
608 v=this->graph->aNode(i.e);
609 this->graph->next(i.e);
610 if (!this->graph->valid(i.e)) { //Az igazi elek vegere ertunk
611 if (this->graph->inTClass(v)) { //S, nincs beel
614 } else { //S, van beel
625 this->graph->next(i.n);
626 if (!this->graph->valid(i.n)) i.spec=3;
632 EdgeIt& next(EdgeIt& i) const {
635 this->graph->next(i.e);
636 if (!this->graph->valid(i.e)) {
638 this->graph->first(i.n, S_CLASS);
639 if (!this->graph->valid(i.n)) {
641 this->graph->first(i.n, T_CLASS);
642 if (!this->graph->valid(i.n)) i.spec=3;
647 this->graph->next(i.n);
648 if (!this->graph->valid(i.n)) {
650 this->graph->first(i.n, T_CLASS);
651 if (!this->graph->valid(i.n)) i.spec=3;
655 this->graph->next(i.n);
656 if (!this->graph->valid(i.n)) i.spec=3;
662 Node tail(const Edge& e) const {
665 return Node(this->graph->tail(e));
676 Node head(const Edge& e) const {
679 return Node(this->graph->head(e));
691 bool valid(const Node& n) const { return (n.spec<3); }
692 bool valid(const Edge& e) const { return (e.spec<3); }
694 int nodeNum() const { return this->graph->nodeNum()+2; }
695 int edgeNum() const {
696 return this->graph->edgeNum()+this->graph->nodeNum();
699 Node aNode(const OutEdgeIt& e) const { return tail(e); }
700 Node aNode(const InEdgeIt& e) const { return head(e); }
701 Node bNode(const OutEdgeIt& e) const { return head(e); }
702 Node bNode(const InEdgeIt& e) const { return tail(e); }
704 void addNode() const { }
705 void addEdge() const { }
707 // Node addNode() const { return Node(this->graph->addNode()); }
708 // Edge addEdge(const Node& tail, const Node& head) const {
709 // return Edge(this->graph->addEdge(tail, head)); }
711 // void erase(const Node& i) const { this->graph->erase(i); }
712 // void erase(const Edge& i) const { this->graph->erase(i); }
714 // void clear() const { this->graph->clear(); }
716 template<typename T> class NodeMap : public GraphWrapper<Graph>::template NodeMap<T> {
717 typedef typename GraphWrapper<Graph>::template NodeMap<T> Parent;
721 NodeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
724 NodeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
727 T operator[](const Node& n) const {
730 return Parent::operator[](n);
738 void set(const Node& n, T t) {
741 GraphWrapper<Graph>::template NodeMap<T>::set(n, t);
754 /// This class is to wrap a node-map of \c Graph and two variables
755 /// storing values for \c S_NODE and \c T_NODE to a node-map of
756 /// stGraphWrapper<Graph>.
757 template<typename NM> class NodeMapWrapper {
759 typedef Node KeyType;
760 typedef typename NM::ValueType ValueType;
763 ValueType* s_value, t_value;
765 NodeMapWrapper(NM& _nm, ValueType& _s_value, ValueType& _t_value) :
766 nm(&_nm), s_value(&_s_value), t_value(&_t_value) { }
767 ValueType operator[](const Node& n) const {
768 switch (n.getSpec()) {
778 void set(const Node& n, ValueType t) {
779 switch (n.getSpec()) {
795 class EdgeMap : public GraphWrapper<Graph>::template EdgeMap<T> {
796 typedef typename GraphWrapper<Graph>::template EdgeMap<T> Parent;
798 typename GraphWrapper<Graph>::template NodeMap<T> node_value;
800 EdgeMap(const stGraphWrapper<Graph>& _G) : Parent(_G),
802 EdgeMap(const stGraphWrapper<Graph>& _G, T a) : Parent(_G, a),
803 node_value(_G, a) { }
804 T operator[](const Edge& e) const {
807 return Parent::operator[](e);
809 return node_value[e.n];
812 return node_value[e.n];
815 void set(const Edge& e, T t) {
821 node_value.set(e.n, t);
825 node_value.set(e.n, t);
831 /// This class is to wrap an edge-map and a node-map of \c Graph
832 /// to an edge-map of stGraphWrapper<Graph>.
833 template<typename EM, typename NM>
834 class EdgeMapWrapper {
836 typedef Edge KeyType;
837 typedef typename EM::ValueType ValueType;
842 EdgeMapWrapper(EM& _em, NM& _nm) : em(&_em), nm(&_nm) { }
843 ValueType operator[](const Edge& e) const {
844 switch (e.getSpec()) {
848 return (*nm)[e.getNode()];
851 return (*nm)[e.getNode()];
854 void set(const Edge& e, ValueType t) {
855 switch (e.getSpec()) {
860 nm->set(e.getNode(), t);
864 nm->set(e.getNode(), t);
871 // template<typename G>
873 operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Node& i) {
874 os << "(node: " << typename Graph::Node(i) << " spec: " << i.spec <<")";
877 // template<typename G>
879 operator<<(std::ostream& os, const /*typename stGraphWrapper<Graph>::*/Edge& i) {
880 os << "(edge: " << typename Graph::Edge(i) << " spec: " << i.spec <<
881 " node: " << i.n << ")";
892 #endif //HUGO_BIPARTITE_GRAPH_WRAPPER_H