2 * lemon/concept/graph_component.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 ///\ingroup graph_concepts
19 ///\brief The graph components.
22 #ifndef LEMON_CONCEPT_GRAPH_COMPONENT_H
23 #define LEMON_CONCEPT_GRAPH_COMPONENT_H
25 #include <lemon/invalid.h>
26 #include <lemon/concept/maps.h>
28 #include <lemon/bits/alteration_notifier.h>
33 /// \addtogroup graph_concepts
36 /**************** Graph iterator concepts ****************/
38 /// Skeleton class for graph Node and Edge types
40 /// This class describes the interface of Node and Edge (and UndirEdge
41 /// in undirected graphs) subtypes of graph types.
43 /// \note This class is a template class so that we can use it to
44 /// create graph skeleton classes. The reason for this is than Node
45 /// and Edge types should \em not derive from the same base class.
46 /// For Node you should instantiate it with character 'n' and for Edge
50 template <char _selector = '0'>
54 /// Default constructor.
56 /// \warning The default constructor is not required to set
57 /// the item to some well-defined value. So you should consider it
64 GraphItem(GraphItem const&) {}
65 /// Invalid constructor \& conversion.
67 /// This constructor initializes the item to be invalid.
68 /// \sa Invalid for more details.
70 /// Assign operator for nodes.
72 /// The nodes are assignable.
74 GraphItem& operator=(GraphItem const&) { return *this; }
75 /// Equality operator.
77 /// Two iterators are equal if and only if they represents the
78 /// same node in the graph or both are invalid.
79 bool operator==(GraphItem) const { return false; }
80 /// Inequality operator.
82 /// \sa operator==(const Node& n)
84 bool operator!=(GraphItem) const { return false; }
86 /// Artificial ordering operator.
88 /// To allow the use of graph descriptors as key type in std::map or
89 /// similar associative container we require this.
91 /// \note This operator only have to define some strict ordering of
92 /// the items; this order has nothing to do with the iteration
93 /// ordering of the items.
95 /// \bug This is a technical requirement. Do we really need this?
96 bool operator<(GraphItem) const { return false; }
98 template<typename _GraphItem>
103 _GraphItem i3 = INVALID;
108 // b = (ia == ib) && (ia != ib) && (ia < ib);
109 b = (ia == ib) && (ia != ib);
110 b = (ia == INVALID) && (ib != INVALID);
114 const _GraphItem &ia;
115 const _GraphItem &ib;
119 /// A type describing the concept of graph node
121 /// This is an instantiation of \ref GraphItem which can be used as a
122 /// Node subtype in graph skeleton definitions
123 typedef GraphItem<'n'> GraphNode;
125 /// A type describing the concept of graph edge
127 /// This is an instantiation of \ref GraphItem which can be used as a
128 /// Edge subtype in graph skeleton definitions
129 typedef GraphItem<'e'> GraphEdge;
132 /**************** Basic features of graphs ****************/
134 /// An empty base graph class.
136 /// This class provides the minimal set of features needed for a graph
137 /// structure. All graph concepts have to be conform to this base
140 /// \bug This is not true. The minimal graph concept is the
141 /// BaseIterableGraphComponent.
143 class BaseGraphComponent {
146 typedef BaseGraphComponent Graph;
148 /// Node class of the graph.
150 /// This class represents the Nodes of the graph.
152 typedef GraphItem<'n'> Node;
154 /// Edge class of the graph.
156 /// This class represents the Edges of the graph.
158 typedef GraphItem<'e'> Edge;
160 ///Gives back the target node of an edge.
162 ///Gives back the target node of an edge.
164 Node target(const Edge&) const { return INVALID;}
166 ///Gives back the source node of an edge.
168 ///Gives back the source node of an edge.
170 Node source(const Edge&) const { return INVALID;}
173 template <typename _Graph>
175 typedef typename _Graph::Node Node;
176 typedef typename _Graph::Edge Edge;
179 checkConcept<GraphItem<'n'>, Node>();
180 checkConcept<GraphItem<'e'>, Edge>();
193 /// An empty iterable base graph class.
195 /// This class provides beside the core graph features
196 /// core iterable interface for the graph structure.
197 /// Most of the base graphs should be conform to this concept.
199 class BaseIterableGraphComponent : virtual public BaseGraphComponent {
202 typedef BaseGraphComponent::Node Node;
203 typedef BaseGraphComponent::Edge Edge;
205 /// Gives back the first Node in the iterating order.
207 /// Gives back the first Node in the iterating order.
209 void first(Node&) const {}
211 /// Gives back the next Node in the iterating order.
213 /// Gives back the next Node in the iterating order.
215 void next(Node&) const {}
217 /// Gives back the first Edge in the iterating order.
219 /// Gives back the first Edge in the iterating order.
221 void first(Edge&) const {}
222 /// Gives back the next Edge in the iterating order.
224 /// Gives back the next Edge in the iterating order.
226 void next(Edge&) const {}
229 /// Gives back the first of the Edges point to the given Node.
231 /// Gives back the first of the Edges point to the given Node.
233 void firstIn(Edge&, const Node&) const {}
235 /// Gives back the next of the Edges points to the given Node.
238 /// Gives back the next of the Edges points to the given Node.
240 void nextIn(Edge&) const {}
242 /// Gives back the first of the Edges start from the given Node.
244 /// Gives back the first of the Edges start from the given Node.
246 void firstOut(Edge&, const Node&) const {}
248 /// Gives back the next of the Edges start from the given Node.
250 /// Gives back the next of the Edges start from the given Node.
252 void nextOut(Edge&) const {}
255 template <typename _Graph>
259 checkConcept< BaseGraphComponent, _Graph >();
260 typename _Graph::Node node;
261 typename _Graph::Edge edge;
271 graph.firstIn(edge, node);
275 graph.firstOut(edge, node);
284 /// An empty idable base graph class.
286 /// This class provides beside the core graph features
287 /// core id functions for the graph structure.
288 /// The most of the base graphs should be conform to this concept.
289 /// The id's are unique and immutable.
290 class IDableGraphComponent : virtual public BaseGraphComponent {
293 typedef BaseGraphComponent::Node Node;
294 typedef BaseGraphComponent::Edge Edge;
296 /// Gives back an unique integer id for the Node.
298 /// Gives back an unique integer id for the Node.
300 int id(const Node&) const { return -1;}
302 /// \brief Gives back the node by the unique id.
304 /// Gives back the node by the unique id.
305 /// If the graph does not contain node with the given id
306 /// then the result of the function is undetermined.
307 Node fromId(int , Node) const { return INVALID;}
309 /// \brief Gives back an unique integer id for the Edge.
311 /// Gives back an unique integer id for the Edge.
313 int id(const Edge&) const { return -1;}
315 /// \brief Gives back the edge by the unique id.
317 /// Gives back the edge by the unique id.
318 /// If the graph does not contain edge with the given id
319 /// then the result of the function is undetermined.
320 Edge fromId(int, Edge) const { return INVALID;}
322 template <typename _Graph>
326 checkConcept< BaseGraphComponent, _Graph >();
327 typename _Graph::Node node;
328 int nid = graph.id(node);
329 nid = graph.id(node);
330 node = graph.fromId(nid, Node());
331 typename _Graph::Edge edge;
332 int eid = graph.id(edge);
333 eid = graph.id(edge);
334 edge = graph.fromId(eid, Edge());
342 /// An empty max-idable base graph class.
344 /// This class provides beside the core graph features
345 /// core max id functions for the graph structure.
346 /// The most of the base graphs should be conform to this concept.
347 /// The id's are unique and immutable.
348 class MaxIDableGraphComponent : virtual public BaseGraphComponent {
351 /// Gives back an integer greater or equal to the maximum Node id.
353 /// Gives back an integer greater or equal to the maximum Node id.
355 int maxId(Node = INVALID) const { return -1;}
357 /// Gives back an integer greater or equal to the maximum Edge id.
359 /// Gives back an integer greater or equal to the maximum Edge id.
361 int maxId(Edge = INVALID) const { return -1;}
363 template <typename _Graph>
367 checkConcept<BaseGraphComponent, _Graph>();
368 int nid = graph.maxId(typename _Graph::Node());
369 ignore_unused_variable_warning(nid);
370 int eid = graph.maxId(typename _Graph::Edge());
371 ignore_unused_variable_warning(eid);
378 /// An empty extendable base graph class.
380 /// This class provides beside the core graph features
381 /// core graph extend interface for the graph structure.
382 /// The most of the base graphs should be conform to this concept.
383 class BaseExtendableGraphComponent : virtual public BaseGraphComponent {
386 typedef BaseGraphComponent::Node Node;
387 typedef BaseGraphComponent::Edge Edge;
389 /// Adds a new Node to the graph.
391 /// Adds a new Node to the graph.
397 /// Adds a new Edge connects the two Nodes to the graph.
399 /// Adds a new Edge connects the two Nodes to the graph.
401 Edge addEdge(const Node&, const Node&) {
405 template <typename _Graph>
408 checkConcept<BaseGraphComponent, _Graph >();
409 typename _Graph::Node node_a, node_b;
410 node_a = graph.addNode();
411 node_b = graph.addNode();
412 typename _Graph::Edge edge;
413 edge = graph.addEdge(node_a, node_b);
420 /// An empty erasable base graph class.
422 /// This class provides beside the core graph features
423 /// core erase functions for the graph structure.
424 /// The most of the base graphs should be conform to this concept.
425 class BaseErasableGraphComponent : virtual public BaseGraphComponent {
428 typedef BaseGraphComponent::Node Node;
429 typedef BaseGraphComponent::Edge Edge;
431 /// Erase a Node from the graph.
433 /// Erase a Node from the graph. This function should not
434 /// erase edges connecting to the Node.
435 void erase(const Node&) {}
437 /// Erase an Edge from the graph.
439 /// Erase an Edge from the graph.
441 void erase(const Edge&) {}
443 template <typename _Graph>
446 checkConcept<BaseGraphComponent, _Graph>();
447 typename _Graph::Node node;
449 typename _Graph::Edge edge;
457 /// An empty clearable base graph class.
459 /// This class provides beside the core graph features
460 /// core clear functions for the graph structure.
461 /// The most of the base graphs should be conform to this concept.
462 class ClearableGraphComponent : virtual public BaseGraphComponent {
465 /// Erase all the Nodes and Edges from the graph.
467 /// Erase all the Nodes and Edges from the graph.
471 template <typename _Graph>
474 checkConcept<BaseGraphComponent, _Graph>();
483 /// Skeleton class for graph NodeIt and EdgeIt
485 /// Skeleton class for graph NodeIt and EdgeIt.
487 template <typename _Graph, typename _Item>
488 class GraphIterator : public _Item {
490 /// \todo Don't we need the Item type as typedef?
492 /// Default constructor.
494 /// @warning The default constructor sets the iterator
495 /// to an undefined value.
497 /// Copy constructor.
499 /// Copy constructor.
501 GraphIterator(GraphIterator const&) {}
502 /// Sets the iterator to the first item.
504 /// Sets the iterator to the first item of \c the graph.
506 explicit GraphIterator(const _Graph&) {}
507 /// Invalid constructor \& conversion.
509 /// This constructor initializes the item to be invalid.
510 /// \sa Invalid for more details.
511 GraphIterator(Invalid) {}
512 /// Assign operator for items.
514 /// The items are assignable.
516 GraphIterator& operator=(GraphIterator const&) { return *this; }
519 /// Assign the iterator to the next item.
521 GraphIterator& operator++() { return *this; }
522 // Node operator*() const { return INVALID; }
523 /// Equality operator
525 /// Two iterators are equal if and only if they point to the
526 /// same object or both are invalid.
527 bool operator==(const GraphIterator&) const { return true;}
528 /// Inequality operator
530 /// \sa operator==(Node n)
532 bool operator!=(const GraphIterator&) const { return true;}
534 template<typename _GraphIterator>
537 // checkConcept< Item, _GraphIterator >();
538 _GraphIterator it1(g);
540 /// \todo Do we need NodeIt(Node) kind of constructor?
541 // _GraphIterator it2(bj);
547 /// \bug This should be: is_base_and_derived<BaseItem, _GraphIterator>
555 /// Skeleton class for graph InEdgeIt and OutEdgeIt
557 /// \note Because InEdgeIt and OutEdgeIt may not inherit from the same
558 /// base class, the _selector is a additional template parameter. For
559 /// InEdgeIt you should instantiate it with character 'i' and for
560 /// OutEdgeIt with 'o'.
561 /// \todo Is this a good name for this concept?
562 template <typename Graph,
563 typename Edge = typename Graph::Edge,
564 char _selector = '0'>
565 class GraphIncIterator : public Edge {
567 /// Default constructor.
569 /// @warning The default constructor sets the iterator
570 /// to an undefined value.
571 GraphIncIterator() {}
572 /// Copy constructor.
574 /// Copy constructor.
576 GraphIncIterator(GraphIncIterator const& gi) :Edge(gi) {}
577 /// Sets the iterator to the first edge incoming into or outgoing
580 /// Sets the iterator to the first edge incoming into or outgoing
583 explicit GraphIncIterator(const Graph&, const typename Graph::Node&) {}
584 /// Invalid constructor \& conversion.
586 /// This constructor initializes the item to be invalid.
587 /// \sa Invalid for more details.
588 GraphIncIterator(Invalid) {}
589 /// Assign operator for nodes.
591 /// The nodes are assignable.
593 GraphIncIterator& operator=(GraphIncIterator const&) { return *this; }
596 /// Assign the iterator to the next node.
598 GraphIncIterator& operator++() { return *this; }
600 // Node operator*() const { return INVALID; }
602 /// Equality operator
604 /// Two iterators are equal if and only if they point to the
605 /// same object or both are invalid.
606 bool operator==(const GraphIncIterator&) const { return true;}
608 /// Inequality operator
610 /// \sa operator==(Node n)
612 bool operator!=(const GraphIncIterator&) const { return true;}
614 template <typename _GraphIncIterator>
616 typedef typename Graph::Node Node;
618 checkConcept<GraphItem<'e'>, _GraphIncIterator>();
619 _GraphIncIterator it1(graph, node);
620 /// \todo Do we need OutEdgeIt(Edge) kind of constructor?
621 // _GraphIncIterator it2(edge);
622 _GraphIncIterator it2;
633 void const_constraits() {
634 Node n = graph.baseNode(it);
635 n = graph.runningNode(it);
641 _GraphIncIterator it;
646 /// An empty iterable base graph class.
648 /// This class provides beside the core graph features
649 /// iterator based iterable interface for the graph structure.
650 /// This concept is part of the StaticGraphConcept.
651 class IterableGraphComponent : virtual public BaseGraphComponent {
655 typedef IterableGraphComponent Graph;
657 typedef BaseGraphComponent::Node Node;
658 typedef BaseGraphComponent::Edge Edge;
660 /// This iterator goes through each node.
662 /// This iterator goes through each node.
664 typedef GraphIterator<Graph, Node> NodeIt;
665 /// This iterator goes through each node.
667 /// This iterator goes through each node.
669 typedef GraphIterator<Graph, Edge> EdgeIt;
670 /// This iterator goes trough the incoming edges of a node.
672 /// This iterator goes trough the \e inccoming edges of a certain node
674 typedef GraphIncIterator<Graph, Edge, 'i'> InEdgeIt;
675 /// This iterator goes trough the outgoing edges of a node.
677 /// This iterator goes trough the \e outgoing edges of a certain node
679 typedef GraphIncIterator<Graph, Edge, 'o'> OutEdgeIt;
681 /// \brief The base node of the iterator.
683 /// Gives back the base node of the iterator.
684 Node baseNode(const InEdgeIt&) const { return INVALID; }
686 /// \brief The running node of the iterator.
688 /// Gives back the running node of the iterator.
689 Node runningNode(const InEdgeIt&) const { return INVALID; }
691 /// \brief The base node of the iterator.
693 /// Gives back the base node of the iterator.
694 Node baseNode(const OutEdgeIt&) const { return INVALID; }
696 /// \brief The running node of the iterator.
698 /// Gives back the running node of the iterator.
699 Node runningNode(const OutEdgeIt&) const { return INVALID; }
702 template <typename _Graph>
705 checkConcept< BaseGraphComponent, _Graph>();
707 checkConcept<GraphIterator<_Graph, typename _Graph::Edge>,
708 typename _Graph::EdgeIt >();
709 checkConcept<GraphIterator<_Graph, typename _Graph::Node>,
710 typename _Graph::NodeIt >();
711 checkConcept<GraphIncIterator<_Graph>, typename _Graph::InEdgeIt>();
712 checkConcept<GraphIncIterator<_Graph>, typename _Graph::OutEdgeIt>();
717 /// An empty alteration notifier base graph class.
719 /// This class provides beside the core graph features
720 /// alteration notifier interface for the graph structure.
721 /// This is an observer-notifier pattern. More Obsevers can
722 /// be registered into the notifier and whenever an alteration
723 /// occured in the graph all the observers will notified about it.
724 class AlterableGraphComponent : virtual public BaseGraphComponent {
727 /// The edge observer registry.
728 typedef AlterationNotifier<Edge> EdgeNotifier;
729 /// The node observer registry.
730 typedef AlterationNotifier<Node> NodeNotifier;
732 /// \brief Gives back the edge alteration notifier.
734 /// Gives back the edge alteration notifier.
735 EdgeNotifier getNotifier(Edge) const {
736 return EdgeNotifier();
739 /// \brief Gives back the node alteration notifier.
741 /// Gives back the node alteration notifier.
742 NodeNotifier getNotifier(Node) const {
743 return NodeNotifier();
749 /// Class describing the concept of graph maps
751 /// This class describes the common interface of the graph maps
752 /// (NodeMap, EdgeMap), that is \ref maps-pages "maps" which can be used to
753 /// associate data to graph descriptors (nodes or edges).
754 template <typename Graph, typename Item, typename _Value>
755 class GraphMap : public ReadWriteMap<Item, _Value> {
759 /// \brief Construct a new map.
761 /// Construct a new map for the graph.
762 explicit GraphMap(const Graph&) {}
763 /// \brief Construct a new map with default value.
765 /// Construct a new map for the graph and initalise the values.
766 GraphMap(const Graph&, const _Value&) {}
767 /// \brief Copy constructor.
769 /// Copy Constructor.
770 GraphMap(const GraphMap& gm) :ReadWriteMap<Item, _Value>(gm) {}
772 /// \brief Assign operator.
775 GraphMap& operator=(const GraphMap&) { return *this;}
777 template<typename _Map>
780 checkConcept<ReadWriteMap<Item, _Value>, _Map >();
781 // Construction with a graph parameter
783 // Constructor with a graph and a default value parameter
785 // Copy constructor. Do we need it?
787 // Copy operator. Do we need it?
790 ignore_unused_variable_warning(a2);
795 const typename GraphMap::Value &t;
800 /// An empty mappable base graph class.
802 /// This class provides beside the core graph features
803 /// map interface for the graph structure.
804 /// This concept is part of the StaticGraphConcept.
805 class MappableGraphComponent : virtual public BaseGraphComponent {
808 typedef MappableGraphComponent Graph;
810 typedef BaseGraphComponent::Node Node;
811 typedef BaseGraphComponent::Edge Edge;
813 /// ReadWrite map of the nodes.
815 /// ReadWrite map of the nodes.
817 template <typename _Value>
818 class NodeMap : public GraphMap<Graph, Node, _Value> {
822 /// \brief Construct a new map.
824 /// Construct a new map for the graph.
825 /// \todo call the right parent class constructor
826 explicit NodeMap(const Graph&) {}
827 /// \brief Construct a new map with default value.
829 /// Construct a new map for the graph and initalise the values.
830 NodeMap(const Graph&, const _Value&) {}
831 /// \brief Copy constructor.
833 /// Copy Constructor.
834 NodeMap(const NodeMap& nm) : GraphMap<Graph, Node, _Value>(nm) {}
836 /// \brief Assign operator.
839 NodeMap& operator=(const NodeMap&) { return *this;}
843 /// ReadWrite map of the edges.
845 /// ReadWrite map of the edges.
847 template <typename _Value>
848 class EdgeMap : public GraphMap<Graph, Edge, _Value> {
852 /// \brief Construct a new map.
854 /// Construct a new map for the graph.
855 /// \todo call the right parent class constructor
856 explicit EdgeMap(const Graph&) {}
857 /// \brief Construct a new map with default value.
859 /// Construct a new map for the graph and initalise the values.
860 EdgeMap(const Graph&, const _Value&) {}
861 /// \brief Copy constructor.
863 /// Copy Constructor.
864 EdgeMap(const EdgeMap& em) :GraphMap<Graph, Edge, _Value>(em) {}
866 /// \brief Assign operator.
869 EdgeMap& operator=(const EdgeMap&) { return *this;}
873 template <typename _Graph>
879 Type(int _v) : value(_v) {}
883 checkConcept<BaseGraphComponent, _Graph>();
885 typedef typename _Graph::template NodeMap<int> IntNodeMap;
886 checkConcept<GraphMap<_Graph, typename _Graph::Node, int>,
889 typedef typename _Graph::template NodeMap<bool> BoolNodeMap;
890 checkConcept<GraphMap<_Graph, typename _Graph::Node, bool>,
893 typedef typename _Graph::template NodeMap<Type> TypeNodeMap;
894 checkConcept<GraphMap<_Graph, typename _Graph::Node, Type>,
899 typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
900 checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
903 typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
904 checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
907 typedef typename _Graph::template EdgeMap<Type> TypeEdgeMap;
908 checkConcept<GraphMap<_Graph, typename _Graph::Edge, Type>,
917 /// \brief An empty extendable extended graph class.
919 /// This class provides beside the core graph features
920 /// item addition interface for the graph structure.
921 /// The difference between this class and the
922 /// \c BaseExtendableGraphComponent is that it should
923 /// notify the item alteration observers.
924 class ExtendableGraphComponent : virtual public BaseGraphComponent {
927 typedef ExtendableGraphComponent Graph;
929 typedef BaseGraphComponent::Node Node;
930 typedef BaseGraphComponent::Edge Edge;
932 /// \brief Add a node to the graph.
934 /// Add a node to the graph and notify the observers.
939 /// \brief Add an edge to the graph.
941 /// Add an edge to the graph and notify the observers.
942 Edge addEdge(const Node&, const Node&) {
946 template <typename _Graph>
949 checkConcept<BaseGraphComponent, _Graph >();
950 typename _Graph::Node node_a, node_b;
951 node_a = graph.addNode();
952 node_b = graph.addNode();
953 typename _Graph::Edge edge;
954 edge = graph.addEdge(node_a, node_b);
960 /// \brief An empty erasable extended graph class.
962 /// This class provides beside the core graph features
963 /// item erase interface for the graph structure.
964 /// The difference between this class and the
965 /// \c BaseErasableGraphComponent is that it should
966 /// notify the item alteration observers.
967 class ErasableGraphComponent : virtual public BaseGraphComponent {
970 typedef ErasableGraphComponent Graph;
972 typedef BaseGraphComponent::Node Node;
973 typedef BaseGraphComponent::Edge Edge;
975 /// \brief Erase the Node and notify the node alteration observers.
977 /// Erase the Node and notify the node alteration observers.
978 void erase(const Node&) {}
980 /// \brief Erase the Edge and notify the edge alteration observers.
982 /// Erase the Edge and notify the edge alteration observers.
983 void erase(const Edge&) {}
985 template <typename _Graph>
988 checkConcept<BaseGraphComponent, _Graph >();
989 typename _Graph::Node node;
991 typename _Graph::Edge edge;