3 * lemon/concept/ugraph_component.h - Part of LEMON, a generic
4 * C++ optimization library
6 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi
7 * Kutatocsoport (Egervary Research Group on Combinatorial Optimization,
10 * Permission to use, modify and distribute this software is granted
11 * provided that this copyright notice appears in all copies. For
12 * precise terms see the accompanying LICENSE file.
14 * This software is provided "AS IS" with no warranty of any kind,
15 * express or implied, and with no claim as to its suitability for any
20 /// \ingroup graph_concepts
22 /// \brief Undirected bipartite graphs and components of.
25 #ifndef LEMON_CONCEPT_BPUGRAPH_H
26 #define LEMON_CONCEPT_BPUGRAPH_H
28 #include <lemon/concept/graph_component.h>
30 #include <lemon/concept/graph.h>
31 #include <lemon/concept/ugraph.h>
33 #include <lemon/utility.h>
38 /// \addtogroup graph_concepts
42 /// \brief Class describing the concept of Bipartite Undirected Graphs.
44 /// This class describes the common interface of all
45 /// Undirected Bipartite Graphs.
47 /// As all concept describing classes it provides only interface
48 /// without any sensible implementation. So any algorithm for
49 /// bipartite undirected graph should compile with this class, but it
50 /// will not run properly, of course.
52 /// In LEMON bipartite undirected graphs also fulfill the concept of
53 /// the undirected graphs (\ref lemon::concept::UGraph "UGraph Concept").
55 /// You can assume that all undirected bipartite graph can be handled
56 /// as an undirected graph and consequently as a static graph.
58 /// The bipartite graph stores two types of nodes which are named
59 /// ANode and BNode. Even so the graph type does not contain ANode
60 /// and BNode classes, becaue the nodes can be accessed just with the
61 /// common Node class.
63 /// The iteration on the partition can be done with the ANodeIt and
64 /// BNodeIt classes. The node map can be used to map values to the nodes
65 /// and similarly we can use to map values for just the ANodes and
66 /// BNodes the ANodeMap and BNodeMap template classes.
70 /// \todo undocumented
74 /// \brief The base type of node iterators,
75 /// or in other words, the trivial node iterator.
77 /// This is the base type of each node iterator,
78 /// thus each kind of node iterator converts to this.
79 /// More precisely each kind of node iterator should be inherited
80 /// from the trivial node iterator. The Node class represents
81 /// both of two types of nodes.
84 /// Default constructor
86 /// @warning The default constructor sets the iterator
87 /// to an undefined value.
95 /// Invalid constructor \& conversion.
97 /// This constructor initializes the iterator to be invalid.
98 /// \sa Invalid for more details.
100 /// Equality operator
102 /// Two iterators are equal if and only if they point to the
103 /// same object or both are invalid.
104 bool operator==(Node) const { return true; }
106 /// Inequality operator
108 /// \sa operator==(Node n)
110 bool operator!=(Node) const { return true; }
112 /// Artificial ordering operator.
114 /// To allow the use of graph descriptors as key type in std::map or
115 /// similar associative container we require this.
117 /// \note This operator only have to define some strict ordering of
118 /// the items; this order has nothing to do with the iteration
119 /// ordering of the items.
121 /// \bug This is a technical requirement. Do we really need this?
122 bool operator<(Node) const { return false; }
126 /// This iterator goes through each node.
128 /// This iterator goes through each node.
129 /// Its usage is quite simple, for example you can count the number
130 /// of nodes in graph \c g of type \c Graph like this:
133 /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
135 class NodeIt : public Node {
137 /// Default constructor
139 /// @warning The default constructor sets the iterator
140 /// to an undefined value.
142 /// Copy constructor.
144 /// Copy constructor.
146 NodeIt(const NodeIt& n) : Node(n) { }
147 /// Invalid constructor \& conversion.
149 /// Initialize the iterator to be invalid.
150 /// \sa Invalid for more details.
152 /// Sets the iterator to the first node.
154 /// Sets the iterator to the first node of \c g.
156 NodeIt(const BpUGraph&) { }
157 /// Node -> NodeIt conversion.
159 /// Sets the iterator to the node of \c the graph pointed by
160 /// the trivial iterator.
161 /// This feature necessitates that each time we
162 /// iterate the edge-set, the iteration order is the same.
163 NodeIt(const BpUGraph&, const Node&) { }
166 /// Assign the iterator to the next node.
168 NodeIt& operator++() { return *this; }
171 /// This iterator goes through each ANode.
173 /// This iterator goes through each ANode.
174 /// Its usage is quite simple, for example you can count the number
175 /// of nodes in graph \c g of type \c Graph like this:
178 /// for (Graph::ANodeIt n(g); n!=INVALID; ++n) ++count;
180 class ANodeIt : public Node {
182 /// Default constructor
184 /// @warning The default constructor sets the iterator
185 /// to an undefined value.
187 /// Copy constructor.
189 /// Copy constructor.
191 ANodeIt(const ANodeIt& n) : Node(n) { }
192 /// Invalid constructor \& conversion.
194 /// Initialize the iterator to be invalid.
195 /// \sa Invalid for more details.
197 /// Sets the iterator to the first node.
199 /// Sets the iterator to the first node of \c g.
201 ANodeIt(const BpUGraph&) { }
202 /// Node -> ANodeIt conversion.
204 /// Sets the iterator to the node of \c the graph pointed by
205 /// the trivial iterator.
206 /// This feature necessitates that each time we
207 /// iterate the edge-set, the iteration order is the same.
208 ANodeIt(const BpUGraph&, const Node&) { }
211 /// Assign the iterator to the next node.
213 ANodeIt& operator++() { return *this; }
216 /// This iterator goes through each BNode.
218 /// This iterator goes through each BNode.
219 /// Its usage is quite simple, for example you can count the number
220 /// of nodes in graph \c g of type \c Graph like this:
223 /// for (Graph::BNodeIt n(g); n!=INVALID; ++n) ++count;
225 class BNodeIt : public Node {
227 /// Default constructor
229 /// @warning The default constructor sets the iterator
230 /// to an undefined value.
232 /// Copy constructor.
234 /// Copy constructor.
236 BNodeIt(const BNodeIt& n) : Node(n) { }
237 /// Invalid constructor \& conversion.
239 /// Initialize the iterator to be invalid.
240 /// \sa Invalid for more details.
242 /// Sets the iterator to the first node.
244 /// Sets the iterator to the first node of \c g.
246 BNodeIt(const BpUGraph&) { }
247 /// Node -> BNodeIt conversion.
249 /// Sets the iterator to the node of \c the graph pointed by
250 /// the trivial iterator.
251 /// This feature necessitates that each time we
252 /// iterate the edge-set, the iteration order is the same.
253 BNodeIt(const BpUGraph&, const Node&) { }
256 /// Assign the iterator to the next node.
258 BNodeIt& operator++() { return *this; }
262 /// The base type of the undirected edge iterators.
264 /// The base type of the undirected edge iterators.
268 /// Default constructor
270 /// @warning The default constructor sets the iterator
271 /// to an undefined value.
273 /// Copy constructor.
275 /// Copy constructor.
277 UEdge(const UEdge&) { }
278 /// Initialize the iterator to be invalid.
280 /// Initialize the iterator to be invalid.
283 /// Equality operator
285 /// Two iterators are equal if and only if they point to the
286 /// same object or both are invalid.
287 bool operator==(UEdge) const { return true; }
288 /// Inequality operator
290 /// \sa operator==(UEdge n)
292 bool operator!=(UEdge) const { return true; }
294 /// Artificial ordering operator.
296 /// To allow the use of graph descriptors as key type in std::map or
297 /// similar associative container we require this.
299 /// \note This operator only have to define some strict ordering of
300 /// the items; this order has nothing to do with the iteration
301 /// ordering of the items.
303 /// \bug This is a technical requirement. Do we really need this?
304 bool operator<(UEdge) const { return false; }
307 /// This iterator goes through each undirected edge.
309 /// This iterator goes through each undirected edge of a graph.
310 /// Its usage is quite simple, for example you can count the number
311 /// of undirected edges in a graph \c g of type \c Graph as follows:
314 /// for(Graph::UEdgeIt e(g); e!=INVALID; ++e) ++count;
316 class UEdgeIt : public UEdge {
318 /// Default constructor
320 /// @warning The default constructor sets the iterator
321 /// to an undefined value.
323 /// Copy constructor.
325 /// Copy constructor.
327 UEdgeIt(const UEdgeIt& e) : UEdge(e) { }
328 /// Initialize the iterator to be invalid.
330 /// Initialize the iterator to be invalid.
333 /// This constructor sets the iterator to the first undirected edge.
335 /// This constructor sets the iterator to the first undirected edge.
336 UEdgeIt(const BpUGraph&) { }
337 /// UEdge -> UEdgeIt conversion
339 /// Sets the iterator to the value of the trivial iterator.
340 /// This feature necessitates that each time we
341 /// iterate the undirected edge-set, the iteration order is the
343 UEdgeIt(const BpUGraph&, const UEdge&) { }
344 /// Next undirected edge
346 /// Assign the iterator to the next undirected edge.
347 UEdgeIt& operator++() { return *this; }
350 /// \brief This iterator goes trough the incident undirected
353 /// This iterator goes trough the incident undirected edges
354 /// of a certain node
356 /// Its usage is quite simple, for example you can compute the
357 /// degree (i.e. count the number
358 /// of incident edges of a node \c n
359 /// in graph \c g of type \c Graph as follows.
362 /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
364 class IncEdgeIt : public UEdge {
366 /// Default constructor
368 /// @warning The default constructor sets the iterator
369 /// to an undefined value.
371 /// Copy constructor.
373 /// Copy constructor.
375 IncEdgeIt(const IncEdgeIt& e) : UEdge(e) { }
376 /// Initialize the iterator to be invalid.
378 /// Initialize the iterator to be invalid.
380 IncEdgeIt(Invalid) { }
381 /// This constructor sets the iterator to first incident edge.
383 /// This constructor set the iterator to the first incident edge of
385 IncEdgeIt(const BpUGraph&, const Node&) { }
386 /// UEdge -> IncEdgeIt conversion
388 /// Sets the iterator to the value of the trivial iterator \c e.
389 /// This feature necessitates that each time we
390 /// iterate the edge-set, the iteration order is the same.
391 IncEdgeIt(const BpUGraph&, const UEdge&) { }
392 /// Next incident edge
394 /// Assign the iterator to the next incident edge
395 /// of the corresponding node.
396 IncEdgeIt& operator++() { return *this; }
399 /// The directed edge type.
401 /// The directed edge type. It can be converted to the
403 class Edge : public UEdge {
405 /// Default constructor
407 /// @warning The default constructor sets the iterator
408 /// to an undefined value.
410 /// Copy constructor.
412 /// Copy constructor.
414 Edge(const Edge& e) : UEdge(e) { }
415 /// Initialize the iterator to be invalid.
417 /// Initialize the iterator to be invalid.
420 /// Equality operator
422 /// Two iterators are equal if and only if they point to the
423 /// same object or both are invalid.
424 bool operator==(Edge) const { return true; }
425 /// Inequality operator
427 /// \sa operator==(Edge n)
429 bool operator!=(Edge) const { return true; }
431 /// Artificial ordering operator.
433 /// To allow the use of graph descriptors as key type in std::map or
434 /// similar associative container we require this.
436 /// \note This operator only have to define some strict ordering of
437 /// the items; this order has nothing to do with the iteration
438 /// ordering of the items.
440 /// \bug This is a technical requirement. Do we really need this?
441 bool operator<(Edge) const { return false; }
444 /// This iterator goes through each directed edge.
446 /// This iterator goes through each edge of a graph.
447 /// Its usage is quite simple, for example you can count the number
448 /// of edges in a graph \c g of type \c Graph as follows:
451 /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
453 class EdgeIt : public Edge {
455 /// Default constructor
457 /// @warning The default constructor sets the iterator
458 /// to an undefined value.
460 /// Copy constructor.
462 /// Copy constructor.
464 EdgeIt(const EdgeIt& e) : Edge(e) { }
465 /// Initialize the iterator to be invalid.
467 /// Initialize the iterator to be invalid.
470 /// This constructor sets the iterator to the first edge.
472 /// This constructor sets the iterator to the first edge of \c g.
473 ///@param g the graph
474 EdgeIt(const BpUGraph &g) { ignore_unused_variable_warning(g); }
475 /// Edge -> EdgeIt conversion
477 /// Sets the iterator to the value of the trivial iterator \c e.
478 /// This feature necessitates that each time we
479 /// iterate the edge-set, the iteration order is the same.
480 EdgeIt(const BpUGraph&, const Edge&) { }
483 /// Assign the iterator to the next edge.
484 EdgeIt& operator++() { return *this; }
487 /// This iterator goes trough the outgoing directed edges of a node.
489 /// This iterator goes trough the \e outgoing edges of a certain node
491 /// Its usage is quite simple, for example you can count the number
492 /// of outgoing edges of a node \c n
493 /// in graph \c g of type \c Graph as follows.
496 /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
499 class OutEdgeIt : public Edge {
501 /// Default constructor
503 /// @warning The default constructor sets the iterator
504 /// to an undefined value.
506 /// Copy constructor.
508 /// Copy constructor.
510 OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
511 /// Initialize the iterator to be invalid.
513 /// Initialize the iterator to be invalid.
515 OutEdgeIt(Invalid) { }
516 /// This constructor sets the iterator to the first outgoing edge.
518 /// This constructor sets the iterator to the first outgoing edge of
521 ///@param g the graph
522 OutEdgeIt(const BpUGraph& n, const Node& g) {
523 ignore_unused_variable_warning(n);
524 ignore_unused_variable_warning(g);
526 /// Edge -> OutEdgeIt conversion
528 /// Sets the iterator to the value of the trivial iterator.
529 /// This feature necessitates that each time we
530 /// iterate the edge-set, the iteration order is the same.
531 OutEdgeIt(const BpUGraph&, const Edge&) { }
532 ///Next outgoing edge
534 /// Assign the iterator to the next
535 /// outgoing edge of the corresponding node.
536 OutEdgeIt& operator++() { return *this; }
539 /// This iterator goes trough the incoming directed edges of a node.
541 /// This iterator goes trough the \e incoming edges of a certain node
543 /// Its usage is quite simple, for example you can count the number
544 /// of outgoing edges of a node \c n
545 /// in graph \c g of type \c Graph as follows.
548 /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
551 class InEdgeIt : public Edge {
553 /// Default constructor
555 /// @warning The default constructor sets the iterator
556 /// to an undefined value.
558 /// Copy constructor.
560 /// Copy constructor.
562 InEdgeIt(const InEdgeIt& e) : Edge(e) { }
563 /// Initialize the iterator to be invalid.
565 /// Initialize the iterator to be invalid.
567 InEdgeIt(Invalid) { }
568 /// This constructor sets the iterator to first incoming edge.
570 /// This constructor set the iterator to the first incoming edge of
573 ///@param g the graph
574 InEdgeIt(const BpUGraph& g, const Node& n) {
575 ignore_unused_variable_warning(n);
576 ignore_unused_variable_warning(g);
578 /// Edge -> InEdgeIt conversion
580 /// Sets the iterator to the value of the trivial iterator \c e.
581 /// This feature necessitates that each time we
582 /// iterate the edge-set, the iteration order is the same.
583 InEdgeIt(const BpUGraph&, const Edge&) { }
584 /// Next incoming edge
586 /// Assign the iterator to the next inedge of the corresponding node.
588 InEdgeIt& operator++() { return *this; }
591 /// \brief Read write map of the nodes to type \c T.
593 /// ReadWrite map of the nodes to type \c T.
595 /// \warning Making maps that can handle bool type (NodeMap<bool>)
596 /// needs some extra attention!
597 /// \todo Wrong documentation
599 class NodeMap : public ReadWriteMap< Node, T >
604 NodeMap(const BpUGraph&) { }
606 NodeMap(const BpUGraph&, T) { }
609 NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
610 ///Assignment operator
611 NodeMap& operator=(const NodeMap&) { return *this; }
612 // \todo fix this concept
615 /// \brief Read write map of the ANodes to type \c T.
617 /// ReadWrite map of the ANodes to type \c T.
619 /// \warning Making maps that can handle bool type (NodeMap<bool>)
620 /// needs some extra attention!
621 /// \todo Wrong documentation
623 class ANodeMap : public ReadWriteMap< Node, T >
628 ANodeMap(const BpUGraph&) { }
630 ANodeMap(const BpUGraph&, T) { }
633 ANodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
634 ///Assignment operator
635 ANodeMap& operator=(const NodeMap&) { return *this; }
636 // \todo fix this concept
639 /// \brief Read write map of the BNodes to type \c T.
641 /// ReadWrite map of the BNodes to type \c T.
643 /// \warning Making maps that can handle bool type (NodeMap<bool>)
644 /// needs some extra attention!
645 /// \todo Wrong documentation
647 class BNodeMap : public ReadWriteMap< Node, T >
652 BNodeMap(const BpUGraph&) { }
654 BNodeMap(const BpUGraph&, T) { }
657 BNodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
658 ///Assignment operator
659 BNodeMap& operator=(const NodeMap&) { return *this; }
660 // \todo fix this concept
663 /// \brief Read write map of the directed edges to type \c T.
665 /// Reference map of the directed edges to type \c T.
667 /// \warning Making maps that can handle bool type (EdgeMap<bool>)
668 /// needs some extra attention!
669 /// \todo Wrong documentation
671 class EdgeMap : public ReadWriteMap<Edge,T>
676 EdgeMap(const BpUGraph&) { }
678 EdgeMap(const BpUGraph&, T) { }
680 EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
681 ///Assignment operator
682 EdgeMap& operator=(const EdgeMap&) { return *this; }
683 // \todo fix this concept
686 /// Read write map of the undirected edges to type \c T.
688 /// Reference map of the edges to type \c T.
690 /// \warning Making maps that can handle bool type (UEdgeMap<bool>)
691 /// needs some extra attention!
692 /// \todo Wrong documentation
694 class UEdgeMap : public ReadWriteMap<UEdge,T>
699 UEdgeMap(const BpUGraph&) { }
701 UEdgeMap(const BpUGraph&, T) { }
703 UEdgeMap(const UEdgeMap& em) : ReadWriteMap<UEdge,T>(em) {}
704 ///Assignment operator
705 UEdgeMap &operator=(const UEdgeMap&) { return *this; }
706 // \todo fix this concept
709 /// \brief Direct the given undirected edge.
711 /// Direct the given undirected edge. The returned edge source
712 /// will be the given edge.
713 Edge direct(const UEdge&, const Node&) const {
717 /// \brief Direct the given undirected edge.
719 /// Direct the given undirected edge. The returned edge source
720 /// will be the source of the undirected edge if the given bool
722 Edge direct(const UEdge&, bool) const {
726 /// \brief Returns true when the given node is an ANode.
728 /// Returns true when the given node is an ANode.
729 bool aNode(Node) const { return true;}
731 /// \brief Returns true when the given node is an BNode.
733 /// Returns true when the given node is an BNode.
734 bool bNode(Node) const { return true;}
736 /// \brief Returns the edge's end node which is in the ANode set.
738 /// Returns the edge's end node which is in the ANode set.
739 Node aNode(UEdge) const { return INVALID;}
741 /// \brief Returns the edge's end node which is in the BNode set.
743 /// Returns the edge's end node which is in the BNode set.
744 Node bNode(UEdge) const { return INVALID;}
746 /// \brief Returns true if the edge has default orientation.
748 /// Returns whether the given directed edge is same orientation as
749 /// the corresponding undirected edge.
750 bool direction(Edge) const { return true; }
752 /// \brief Returns the opposite directed edge.
754 /// Returns the opposite directed edge.
755 Edge oppositeEdge(Edge) const { return INVALID; }
757 /// \brief Opposite node on an edge
759 /// \return the opposite of the given Node on the given Edge
760 Node oppositeNode(Node, UEdge) const { return INVALID; }
762 /// \brief First node of the undirected edge.
764 /// \return the first node of the given UEdge.
766 /// Naturally uectected edges don't have direction and thus
767 /// don't have source and target node. But we use these two methods
768 /// to query the two endnodes of the edge. The direction of the edge
769 /// which arises this way is called the inherent direction of the
770 /// undirected edge, and is used to define the "default" direction
771 /// of the directed versions of the edges.
773 Node source(UEdge) const { return INVALID; }
775 /// \brief Second node of the undirected edge.
776 Node target(UEdge) const { return INVALID; }
778 /// \brief Source node of the directed edge.
779 Node source(Edge) const { return INVALID; }
781 /// \brief Target node of the directed edge.
782 Node target(Edge) const { return INVALID; }
784 /// \brief Base node of the iterator
786 /// Returns the base node (the source in this case) of the iterator
787 Node baseNode(OutEdgeIt e) const {
791 /// \brief Running node of the iterator
793 /// Returns the running node (the target in this case) of the
795 Node runningNode(OutEdgeIt e) const {
799 /// \brief Base node of the iterator
801 /// Returns the base node (the target in this case) of the iterator
802 Node baseNode(InEdgeIt e) const {
805 /// \brief Running node of the iterator
807 /// Returns the running node (the source in this case) of the
809 Node runningNode(InEdgeIt e) const {
813 /// \brief Base node of the iterator
815 /// Returns the base node of the iterator
816 Node baseNode(IncEdgeIt) const {
820 /// \brief Running node of the iterator
822 /// Returns the running node of the iterator
823 Node runningNode(IncEdgeIt) const {
827 template <typename Graph>
835 /// \brief An empty non-static undirected graph class.
837 /// This class provides everything that \ref BpUGraph does.
838 /// Additionally it enables building graphs from scratch.
839 class ExtendableBpUGraph : public BpUGraph {
842 /// \brief Add a new ANode to the graph.
844 /// Add a new ANode to the graph.
845 /// \return the new node.
848 /// \brief Add a new ANode to the graph.
850 /// Add a new ANode to the graph.
851 /// \return the new node.
854 /// \brief Add a new undirected edge to the graph.
856 /// Add a new undirected edge to the graph. One of the nodes
857 /// should be ANode and the other should be BNode.
858 /// \pre The nodes are not in the same nodeset.
859 /// \return the new edge.
860 UEdge addEdge(const Node& from, const Node& to);
862 /// \brief Resets the graph.
864 /// This function deletes all undirected edges and nodes of the graph.
865 /// It also frees the memory allocated to store them.
868 template <typename Graph>
870 void constraints() {}
875 /// \brief An empty erasable undirected graph class.
877 /// This class is an extension of \ref ExtendableBpUGraph. It makes it
878 /// possible to erase undirected edges or nodes.
879 class ErasableBpUGraph : public ExtendableBpUGraph {
882 /// \brief Deletes a node.
887 /// \brief Deletes an undirected edge.
889 /// Deletes an undirected edge.
891 void erase(UEdge) { }
893 template <typename Graph>
895 void constraints() {}