Some documentation got revised.
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 typename _Graph::Edge edge;
412 edge = graph.addEdge(node_a, node_b);
419 /// An empty erasable base graph class.
421 /// This class provides beside the core graph features
422 /// core erase functions for the graph structure.
423 /// The most of the base graphs should be conform to this concept.
424 class BaseErasableGraphComponent : virtual public BaseGraphComponent {
427 typedef BaseGraphComponent::Node Node;
428 typedef BaseGraphComponent::Edge Edge;
430 /// Erase a Node from the graph.
432 /// Erase a Node from the graph. This function should not
433 /// erase edges connecting to the Node.
434 void erase(const Node&) {}
436 /// Erase an Edge from the graph.
438 /// Erase an Edge from the graph.
440 void erase(const Edge&) {}
442 template <typename _Graph>
445 checkConcept<BaseGraphComponent, _Graph>();
446 typename _Graph::Node node;
448 typename _Graph::Edge edge;
456 /// An empty clearable base graph class.
458 /// This class provides beside the core graph features
459 /// core clear functions for the graph structure.
460 /// The most of the base graphs should be conform to this concept.
461 class ClearableGraphComponent : virtual public BaseGraphComponent {
464 /// Erase all the Nodes and Edges from the graph.
466 /// Erase all the Nodes and Edges from the graph.
470 template <typename _Graph>
473 checkConcept<BaseGraphComponent, _Graph>();
482 /// Skeleton class for graph NodeIt and EdgeIt
484 /// Skeleton class for graph NodeIt and EdgeIt.
486 template <typename _Graph, typename _Item>
487 class GraphIterator : public _Item {
489 /// \todo Don't we need the Item type as typedef?
491 /// Default constructor.
493 /// @warning The default constructor sets the iterator
494 /// to an undefined value.
496 /// Copy constructor.
498 /// Copy constructor.
500 GraphIterator(GraphIterator const&) {}
501 /// Sets the iterator to the first item.
503 /// Sets the iterator to the first item of \c the graph.
505 explicit GraphIterator(const _Graph&) {}
506 /// Invalid constructor \& conversion.
508 /// This constructor initializes the item to be invalid.
509 /// \sa Invalid for more details.
510 GraphIterator(Invalid) {}
511 /// Assign operator for items.
513 /// The items are assignable.
515 GraphIterator& operator=(GraphIterator const&) { return *this; }
518 /// Assign the iterator to the next item.
520 GraphIterator& operator++() { return *this; }
521 // Node operator*() const { return INVALID; }
522 /// Equality operator
524 /// Two iterators are equal if and only if they point to the
525 /// same object or both are invalid.
526 bool operator==(const GraphIterator&) const { return true;}
527 /// Inequality operator
529 /// \sa operator==(Node n)
531 bool operator!=(const GraphIterator&) const { return true;}
533 template<typename _GraphIterator>
536 // checkConcept< Item, _GraphIterator >();
537 _GraphIterator it1(g);
539 /// \todo Do we need NodeIt(Node) kind of constructor?
540 // _GraphIterator it2(bj);
546 /// \bug This should be: is_base_and_derived<BaseItem, _GraphIterator>
554 /// Skeleton class for graph InEdgeIt and OutEdgeIt
556 /// \note Because InEdgeIt and OutEdgeIt may not inherit from the same
557 /// base class, the _selector is a additional template parameter. For
558 /// InEdgeIt you should instantiate it with character 'i' and for
559 /// OutEdgeIt with 'o'.
560 /// \todo Is this a good name for this concept?
561 template <typename Graph,
562 typename Edge = typename Graph::Edge,
563 char _selector = '0'>
564 class GraphIncIterator : public Edge {
566 /// Default constructor.
568 /// @warning The default constructor sets the iterator
569 /// to an undefined value.
570 GraphIncIterator() {}
571 /// Copy constructor.
573 /// Copy constructor.
575 GraphIncIterator(GraphIncIterator const& gi) :Edge(gi) {}
576 /// Sets the iterator to the first edge incoming into or outgoing
579 /// Sets the iterator to the first edge incoming into or outgoing
582 explicit GraphIncIterator(const Graph&, const typename Graph::Node&) {}
583 /// Invalid constructor \& conversion.
585 /// This constructor initializes the item to be invalid.
586 /// \sa Invalid for more details.
587 GraphIncIterator(Invalid) {}
588 /// Assign operator for nodes.
590 /// The nodes are assignable.
592 GraphIncIterator& operator=(GraphIncIterator const&) { return *this; }
595 /// Assign the iterator to the next node.
597 GraphIncIterator& operator++() { return *this; }
599 // Node operator*() const { return INVALID; }
601 /// Equality operator
603 /// Two iterators are equal if and only if they point to the
604 /// same object or both are invalid.
605 bool operator==(const GraphIncIterator&) const { return true;}
607 /// Inequality operator
609 /// \sa operator==(Node n)
611 bool operator!=(const GraphIncIterator&) const { return true;}
613 template <typename _GraphIncIterator>
615 typedef typename Graph::Node Node;
617 checkConcept<GraphItem<'e'>, _GraphIncIterator>();
618 _GraphIncIterator it1(graph, node);
619 /// \todo Do we need OutEdgeIt(Edge) kind of constructor?
620 // _GraphIncIterator it2(edge);
621 _GraphIncIterator it2;
632 void const_constraits() {
633 Node n = graph.baseNode(it);
634 n = graph.runningNode(it);
640 _GraphIncIterator it;
645 /// An empty iterable base graph class.
647 /// This class provides beside the core graph features
648 /// iterator based iterable interface for the graph structure.
649 /// This concept is part of the StaticGraphConcept.
650 class IterableGraphComponent : virtual public BaseGraphComponent {
654 typedef IterableGraphComponent Graph;
656 typedef BaseGraphComponent::Node Node;
657 typedef BaseGraphComponent::Edge Edge;
659 /// This iterator goes through each node.
661 /// This iterator goes through each node.
663 typedef GraphIterator<Graph, Node> NodeIt;
664 /// This iterator goes through each node.
666 /// This iterator goes through each node.
668 typedef GraphIterator<Graph, Edge> EdgeIt;
669 /// This iterator goes trough the incoming edges of a node.
671 /// This iterator goes trough the \e inccoming edges of a certain node
673 typedef GraphIncIterator<Graph, Edge, 'i'> InEdgeIt;
674 /// This iterator goes trough the outgoing edges of a node.
676 /// This iterator goes trough the \e outgoing edges of a certain node
678 typedef GraphIncIterator<Graph, Edge, 'o'> OutEdgeIt;
681 template <typename _Graph>
684 checkConcept< BaseGraphComponent, _Graph>();
686 checkConcept<GraphIterator<_Graph, typename _Graph::Edge>,
687 typename _Graph::EdgeIt >();
688 checkConcept<GraphIterator<_Graph, typename _Graph::Node>,
689 typename _Graph::NodeIt >();
690 checkConcept<GraphIncIterator<_Graph>, typename _Graph::InEdgeIt >();
691 checkConcept<GraphIncIterator<_Graph>, typename _Graph::OutEdgeIt >();
695 /// An empty alteration notifier base graph class.
697 /// This class provides beside the core graph features
698 /// alteration notifier interface for the graph structure.
699 /// This is an observer-notifier pattern. More Obsevers can
700 /// be registered into the notifier and whenever an alteration
701 /// occured in the graph all the observers will notified about it.
702 class AlterableGraphComponent : virtual public BaseGraphComponent {
705 /// The edge observer registry.
706 typedef AlterationNotifier<Edge> EdgeNotifier;
707 /// The node observer registry.
708 typedef AlterationNotifier<Node> NodeNotifier;
710 /// \brief Gives back the edge alteration notifier.
712 /// Gives back the edge alteration notifier.
713 EdgeNotifier getNotifier(Edge) const {
714 return EdgeNotifier();
717 /// \brief Gives back the node alteration notifier.
719 /// Gives back the node alteration notifier.
720 NodeNotifier getNotifier(Node) const {
721 return NodeNotifier();
727 /// Class describing the concept of graph maps
729 /// This class describes the common interface of the graph maps
730 /// (NodeMap, EdgeMap), that is \ref maps-pages "maps" which can be used to
731 /// associate data to graph descriptors (nodes or edges).
732 template <typename Graph, typename Item, typename _Value>
733 class GraphMap : public ReadWriteMap<Item, _Value> {
737 /// \brief Construct a new map.
739 /// Construct a new map for the graph.
740 explicit GraphMap(const Graph&) {}
741 /// \brief Construct a new map with default value.
743 /// Construct a new map for the graph and initalise the values.
744 GraphMap(const Graph&, const _Value&) {}
745 /// \brief Copy constructor.
747 /// Copy Constructor.
748 GraphMap(const GraphMap& gm) :ReadWriteMap<Item, _Value>(gm) {}
750 /// \brief Assign operator.
753 GraphMap& operator=(const GraphMap&) { return *this;}
755 template<typename _Map>
758 checkConcept<ReadWriteMap<Item, _Value>, _Map >();
759 // Construction with a graph parameter
761 // Constructor with a graph and a default value parameter
763 // Copy constructor. Do we need it?
765 // Copy operator. Do we need it?
768 ignore_unused_variable_warning(a2);
773 const typename GraphMap::Value &t;
778 /// An empty mappable base graph class.
780 /// This class provides beside the core graph features
781 /// map interface for the graph structure.
782 /// This concept is part of the StaticGraphConcept.
783 class MappableGraphComponent : virtual public BaseGraphComponent {
786 typedef MappableGraphComponent Graph;
788 typedef BaseGraphComponent::Node Node;
789 typedef BaseGraphComponent::Edge Edge;
791 /// ReadWrite map of the nodes.
793 /// ReadWrite map of the nodes.
795 template <typename _Value>
796 class NodeMap : public GraphMap<Graph, Node, _Value> {
800 /// \brief Construct a new map.
802 /// Construct a new map for the graph.
803 /// \todo call the right parent class constructor
804 explicit NodeMap(const Graph&) {}
805 /// \brief Construct a new map with default value.
807 /// Construct a new map for the graph and initalise the values.
808 NodeMap(const Graph&, const _Value&) {}
809 /// \brief Copy constructor.
811 /// Copy Constructor.
812 NodeMap(const NodeMap& nm) : GraphMap<Graph, Node, _Value>(nm) {}
814 /// \brief Assign operator.
817 NodeMap& operator=(const NodeMap&) { return *this;}
821 /// ReadWrite map of the edges.
823 /// ReadWrite map of the edges.
825 template <typename _Value>
826 class EdgeMap : public GraphMap<Graph, Edge, _Value> {
830 /// \brief Construct a new map.
832 /// Construct a new map for the graph.
833 /// \todo call the right parent class constructor
834 explicit EdgeMap(const Graph&) {}
835 /// \brief Construct a new map with default value.
837 /// Construct a new map for the graph and initalise the values.
838 EdgeMap(const Graph&, const _Value&) {}
839 /// \brief Copy constructor.
841 /// Copy Constructor.
842 EdgeMap(const EdgeMap& em) :GraphMap<Graph, Edge, _Value>(em) {}
844 /// \brief Assign operator.
847 EdgeMap& operator=(const EdgeMap&) { return *this;}
851 template <typename _Graph>
857 Type(int _v) : value(_v) {}
861 checkConcept<BaseGraphComponent, _Graph>();
863 typedef typename _Graph::template NodeMap<int> IntNodeMap;
864 checkConcept<GraphMap<_Graph, typename _Graph::Node, int>,
867 typedef typename _Graph::template NodeMap<bool> BoolNodeMap;
868 checkConcept<GraphMap<_Graph, typename _Graph::Node, bool>,
871 typedef typename _Graph::template NodeMap<Type> TypeNodeMap;
872 checkConcept<GraphMap<_Graph, typename _Graph::Node, Type>,
877 typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
878 checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
881 typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
882 checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
885 typedef typename _Graph::template EdgeMap<Type> TypeEdgeMap;
886 checkConcept<GraphMap<_Graph, typename _Graph::Edge, Type>,
895 /// \brief An empty extendable extended graph class.
897 /// This class provides beside the core graph features
898 /// item addition interface for the graph structure.
899 /// The difference between this class and the
900 /// \c BaseExtendableGraphComponent is that it should
901 /// notify the item alteration observers.
902 class ExtendableGraphComponent : virtual public BaseGraphComponent {
905 typedef ExtendableGraphComponent Graph;
907 typedef BaseGraphComponent::Node Node;
908 typedef BaseGraphComponent::Edge Edge;
910 /// \brief Add a node to the graph.
912 /// Add a node to the graph and notify the observers.
917 /// \brief Add an edge to the graph.
919 /// Add an edge to the graph and notify the observers.
920 Edge addEdge(const Node&, const Node&) {
924 template <typename _Graph>
927 checkConcept<BaseGraphComponent, _Graph >();
928 typename _Graph::Node node_a, node_b;
929 node_a = graph.addNode();
930 typename _Graph::Edge edge;
931 edge = graph.addEdge(node_a, node_b);
937 /// \brief An empty erasable extended graph class.
939 /// This class provides beside the core graph features
940 /// item erase interface for the graph structure.
941 /// The difference between this class and the
942 /// \c BaseErasableGraphComponent is that it should
943 /// notify the item alteration observers.
944 class ErasableGraphComponent : virtual public BaseGraphComponent {
947 typedef ErasableGraphComponent Graph;
949 typedef BaseGraphComponent::Node Node;
950 typedef BaseGraphComponent::Edge Edge;
952 /// \brief Erase the Node and notify the node alteration observers.
954 /// Erase the Node and notify the node alteration observers.
955 void erase(const Node&) {}
957 /// \brief Erase the Edge and notify the edge alteration observers.
959 /// Erase the Edge and notify the edge alteration observers.
960 void erase(const Edge&) {}
962 template <typename _Graph>
965 checkConcept<BaseGraphComponent, _Graph >();
966 typename _Graph::Node node;
968 typename _Graph::Edge edge;