3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 ///\ingroup graph_concepts
21 ///\brief The graph components.
24 #ifndef LEMON_CONCEPT_GRAPH_COMPONENT_H
25 #define LEMON_CONCEPT_GRAPH_COMPONENT_H
27 #include <lemon/bits/invalid.h>
28 #include <lemon/concept/maps.h>
30 #include <lemon/bits/alteration_notifier.h>
35 /**************** Graph iterator concepts ****************/
37 /// Skeleton class for graph Node and Edge types
39 /// This class describes the interface of Node and Edge (and UEdge
40 /// in undirected graphs) subtypes of graph types.
42 /// \note This class is a template class so that we can use it to
43 /// create graph skeleton classes. The reason for this is than Node
44 /// and Edge types should \em not derive from the same base class.
45 /// For Node you should instantiate it with character 'n' and for Edge
49 template <char _selector = '0'>
53 /// Default constructor.
55 /// \warning The default constructor is not required to set
56 /// the item to some well-defined value. So you should consider it
63 GraphItem(GraphItem const&) {}
64 /// Invalid constructor \& conversion.
66 /// This constructor initializes the item to be invalid.
67 /// \sa Invalid for more details.
69 /// Assign operator for nodes.
71 /// The nodes are assignable.
73 GraphItem& operator=(GraphItem const&) { return *this; }
74 /// Equality operator.
76 /// Two iterators are equal if and only if they represents the
77 /// same node in the graph or both are invalid.
78 bool operator==(GraphItem) const { return false; }
79 /// Inequality operator.
81 /// \sa operator==(const Node& n)
83 bool operator!=(GraphItem) const { return false; }
85 /// Artificial ordering operator.
87 /// To allow the use of graph descriptors as key type in std::map or
88 /// similar associative container we require this.
90 /// \note This operator only have to define some strict ordering of
91 /// the items; this order has nothing to do with the iteration
92 /// ordering of the items.
94 /// \bug This is a technical requirement. Do we really need this?
95 bool operator<(GraphItem) const { return false; }
97 template<typename _GraphItem>
102 _GraphItem i3 = INVALID;
107 // b = (ia == ib) && (ia != ib) && (ia < ib);
108 b = (ia == ib) && (ia != ib);
109 b = (ia == INVALID) && (ib != INVALID);
113 const _GraphItem &ia;
114 const _GraphItem &ib;
118 /// A type describing the concept of graph node
120 /// This is an instantiation of \ref GraphItem which can be used as a
121 /// Node subtype in graph skeleton definitions
122 typedef GraphItem<'n'> GraphNode;
124 /// A type describing the concept of graph edge
126 /// This is an instantiation of \ref GraphItem which can be used as a
127 /// Edge subtype in graph skeleton definitions
128 typedef GraphItem<'e'> GraphEdge;
131 /**************** Basic features of graphs ****************/
133 /// An empty base graph class.
135 /// This class provides the minimal set of features needed for a graph
136 /// structure. All graph concepts have to be conform to this base
139 /// \bug This is not true. The minimal graph concept is the
140 /// BaseIterableGraphComponent.
142 class BaseGraphComponent {
145 typedef BaseGraphComponent Graph;
147 /// Node class of the graph.
149 /// This class represents the Nodes of the graph.
151 typedef GraphItem<'n'> Node;
153 /// Edge class of the graph.
155 /// This class represents the Edges of the graph.
157 typedef GraphItem<'e'> Edge;
159 ///Gives back the target node of an edge.
161 ///Gives back the target node of an edge.
163 Node target(const Edge&) const { return INVALID;}
165 ///Gives back the source node of an edge.
167 ///Gives back the source node of an edge.
169 Node source(const Edge&) const { return INVALID;}
172 template <typename _Graph>
174 typedef typename _Graph::Node Node;
175 typedef typename _Graph::Edge Edge;
178 checkConcept<GraphItem<'n'>, Node>();
179 checkConcept<GraphItem<'e'>, Edge>();
192 /// An empty iterable base graph class.
194 /// This class provides beside the core graph features
195 /// core iterable interface for the graph structure.
196 /// Most of the base graphs should be conform to this concept.
198 class BaseIterableGraphComponent : virtual public BaseGraphComponent {
201 typedef BaseGraphComponent::Node Node;
202 typedef BaseGraphComponent::Edge Edge;
204 /// Gives back the first Node in the iterating order.
206 /// Gives back the first Node in the iterating order.
208 void first(Node&) const {}
210 /// Gives back the next Node in the iterating order.
212 /// Gives back the next Node in the iterating order.
214 void next(Node&) const {}
216 /// Gives back the first Edge in the iterating order.
218 /// Gives back the first Edge in the iterating order.
220 void first(Edge&) const {}
221 /// Gives back the next Edge in the iterating order.
223 /// Gives back the next Edge in the iterating order.
225 void next(Edge&) const {}
228 /// Gives back the first of the Edges point to the given Node.
230 /// Gives back the first of the Edges point to the given Node.
232 void firstIn(Edge&, const Node&) const {}
234 /// Gives back the next of the Edges points to the given Node.
237 /// Gives back the next of the Edges points to the given Node.
239 void nextIn(Edge&) const {}
241 /// Gives back the first of the Edges start from the given Node.
243 /// Gives back the first of the Edges start from the given Node.
245 void firstOut(Edge&, const Node&) const {}
247 /// Gives back the next of the Edges start from the given Node.
249 /// Gives back the next of the Edges start from the given Node.
251 void nextOut(Edge&) const {}
254 template <typename _Graph>
258 checkConcept< BaseGraphComponent, _Graph >();
259 typename _Graph::Node node;
260 typename _Graph::Edge edge;
270 graph.firstIn(edge, node);
274 graph.firstOut(edge, node);
283 /// An empty idable base graph class.
285 /// This class provides beside the core graph features
286 /// core id functions for the graph structure.
287 /// The most of the base graphs should be conform to this concept.
288 /// The id's are unique and immutable.
289 class IDableGraphComponent : virtual public BaseGraphComponent {
292 typedef BaseGraphComponent::Node Node;
293 typedef BaseGraphComponent::Edge Edge;
295 /// Gives back an unique integer id for the Node.
297 /// Gives back an unique integer id for the Node.
299 int id(const Node&) const { return -1;}
301 /// \brief Gives back the node by the unique id.
303 /// Gives back the node by the unique id.
304 /// If the graph does not contain node with the given id
305 /// then the result of the function is undetermined.
306 Node fromId(int , Node) const { return INVALID;}
308 /// \brief Gives back an unique integer id for the Edge.
310 /// Gives back an unique integer id for the Edge.
312 int id(const Edge&) const { return -1;}
314 /// \brief Gives back the edge by the unique id.
316 /// Gives back the edge by the unique id.
317 /// If the graph does not contain edge with the given id
318 /// then the result of the function is undetermined.
319 Edge fromId(int, Edge) const { return INVALID;}
321 template <typename _Graph>
325 checkConcept< BaseGraphComponent, _Graph >();
326 typename _Graph::Node node;
327 int nid = graph.id(node);
328 nid = graph.id(node);
329 node = graph.fromId(nid, Node());
330 typename _Graph::Edge edge;
331 int eid = graph.id(edge);
332 eid = graph.id(edge);
333 edge = graph.fromId(eid, Edge());
341 /// An empty max-idable base graph class.
343 /// This class provides beside the core graph features
344 /// core max id functions for the graph structure.
345 /// The most of the base graphs should be conform to this concept.
346 /// The id's are unique and immutable.
347 class MaxIDableGraphComponent : virtual public BaseGraphComponent {
350 /// Gives back an integer greater or equal to the maximum Node id.
352 /// Gives back an integer greater or equal to the maximum Node id.
354 int maxId(Node = INVALID) const { return -1;}
356 /// Gives back an integer greater or equal to the maximum Edge id.
358 /// Gives back an integer greater or equal to the maximum Edge id.
360 int maxId(Edge = INVALID) const { return -1;}
362 template <typename _Graph>
366 checkConcept<BaseGraphComponent, _Graph>();
367 int nid = graph.maxId(typename _Graph::Node());
368 ignore_unused_variable_warning(nid);
369 int eid = graph.maxId(typename _Graph::Edge());
370 ignore_unused_variable_warning(eid);
377 /// An empty extendable base graph class.
379 /// This class provides beside the core graph features
380 /// core graph extend interface for the graph structure.
381 /// The most of the base graphs should be conform to this concept.
382 class BaseExtendableGraphComponent : virtual public BaseGraphComponent {
385 typedef BaseGraphComponent::Node Node;
386 typedef BaseGraphComponent::Edge Edge;
388 /// Adds a new Node to the graph.
390 /// Adds a new Node to the graph.
396 /// Adds a new Edge connects the two Nodes to the graph.
398 /// Adds a new Edge connects the two Nodes to the graph.
400 Edge addEdge(const Node&, const Node&) {
404 template <typename _Graph>
407 checkConcept<BaseGraphComponent, _Graph >();
408 typename _Graph::Node node_a, node_b;
409 node_a = graph.addNode();
410 node_b = 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;
680 /// \brief The base node of the iterator.
682 /// Gives back the base node of the iterator.
683 /// It is always the target of the pointed edge.
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 /// It is always the source of the pointed edge.
690 Node runningNode(const InEdgeIt&) const { return INVALID; }
692 /// \brief The base node of the iterator.
694 /// Gives back the base node of the iterator.
695 /// It is always the source of the pointed edge.
696 Node baseNode(const OutEdgeIt&) const { return INVALID; }
698 /// \brief The running node of the iterator.
700 /// Gives back the running node of the iterator.
701 /// It is always the target of the pointed edge.
702 Node runningNode(const OutEdgeIt&) const { return INVALID; }
704 /// \brief The opposite node on the given edge.
706 /// Gives back the opposite on the given edge.
707 /// \todo It should not be here.
708 Node oppositeNode(const Node&, const Edge&) const { return INVALID; }
711 template <typename _Graph>
714 checkConcept< BaseGraphComponent, _Graph>();
716 checkConcept<GraphIterator<_Graph, typename _Graph::Edge>,
717 typename _Graph::EdgeIt >();
718 checkConcept<GraphIterator<_Graph, typename _Graph::Node>,
719 typename _Graph::NodeIt >();
720 checkConcept<GraphIncIterator<_Graph>, typename _Graph::InEdgeIt>();
721 checkConcept<GraphIncIterator<_Graph>, typename _Graph::OutEdgeIt>();
723 typename _Graph::Node n(INVALID);
724 typename _Graph::Edge e(INVALID);
725 n = graph.oppositeNode(n, e);
733 /// An empty alteration notifier base graph class.
735 /// This class provides beside the core graph features
736 /// alteration notifier interface for the graph structure.
737 /// This is an observer-notifier pattern. More Obsevers can
738 /// be registered into the notifier and whenever an alteration
739 /// occured in the graph all the observers will notified about it.
740 class AlterableGraphComponent : virtual public BaseIterableGraphComponent {
743 /// The edge observer registry.
744 typedef AlterationNotifier<AlterableGraphComponent, Edge>
746 /// The node observer registry.
747 typedef AlterationNotifier<AlterableGraphComponent, Node>
750 /// \brief Gives back the edge alteration notifier.
752 /// Gives back the edge alteration notifier.
753 EdgeNotifier getNotifier(Edge) const {
754 return EdgeNotifier();
757 /// \brief Gives back the node alteration notifier.
759 /// Gives back the node alteration notifier.
760 NodeNotifier getNotifier(Node) const {
761 return NodeNotifier();
767 /// Class describing the concept of graph maps
769 /// This class describes the common interface of the graph maps
770 /// (NodeMap, EdgeMap), that is \ref maps-page "maps" which can be used to
771 /// associate data to graph descriptors (nodes or edges).
772 template <typename Graph, typename Item, typename _Value>
773 class GraphMap : public ReadWriteMap<Item, _Value> {
777 /// \brief Construct a new map.
779 /// Construct a new map for the graph.
780 explicit GraphMap(const Graph&) {}
781 /// \brief Construct a new map with default value.
783 /// Construct a new map for the graph and initalise the values.
784 GraphMap(const Graph&, const _Value&) {}
785 /// \brief Copy constructor.
787 /// Copy Constructor.
788 GraphMap(const GraphMap& gm) :ReadWriteMap<Item, _Value>(gm) {}
790 /// \brief Assign operator.
793 GraphMap& operator=(const GraphMap&) { return *this;}
795 template<typename _Map>
798 checkConcept<ReadWriteMap<Item, _Value>, _Map >();
799 // Construction with a graph parameter
801 // Constructor with a graph and a default value parameter
803 // Copy constructor. Do we need it?
806 ignore_unused_variable_warning(a2);
811 const typename GraphMap::Value &t;
816 /// An empty mappable base graph class.
818 /// This class provides beside the core graph features
819 /// map interface for the graph structure.
820 /// This concept is part of the StaticGraphConcept.
821 class MappableGraphComponent : virtual public BaseGraphComponent {
824 typedef MappableGraphComponent Graph;
826 typedef BaseGraphComponent::Node Node;
827 typedef BaseGraphComponent::Edge Edge;
829 /// ReadWrite map of the nodes.
831 /// ReadWrite map of the nodes.
833 template <typename _Value>
834 class NodeMap : public GraphMap<Graph, Node, _Value> {
838 /// \brief Construct a new map.
840 /// Construct a new map for the graph.
841 /// \todo call the right parent class constructor
842 explicit NodeMap(const Graph&) {}
843 /// \brief Construct a new map with default value.
845 /// Construct a new map for the graph and initalise the values.
846 NodeMap(const Graph&, const _Value&) {}
847 /// \brief Copy constructor.
849 /// Copy Constructor.
850 NodeMap(const NodeMap& nm) : GraphMap<Graph, Node, _Value>(nm) {}
852 /// \brief Assign operator.
855 NodeMap& operator=(const NodeMap&) { return *this;}
859 /// ReadWrite map of the edges.
861 /// ReadWrite map of the edges.
863 template <typename _Value>
864 class EdgeMap : public GraphMap<Graph, Edge, _Value> {
868 /// \brief Construct a new map.
870 /// Construct a new map for the graph.
871 /// \todo call the right parent class constructor
872 explicit EdgeMap(const Graph&) {}
873 /// \brief Construct a new map with default value.
875 /// Construct a new map for the graph and initalise the values.
876 EdgeMap(const Graph&, const _Value&) {}
877 /// \brief Copy constructor.
879 /// Copy Constructor.
880 EdgeMap(const EdgeMap& em) :GraphMap<Graph, Edge, _Value>(em) {}
882 /// \brief Assign operator.
885 EdgeMap& operator=(const EdgeMap&) { return *this;}
889 template <typename _Graph>
895 Type(int _v) : value(_v) {}
899 checkConcept<BaseGraphComponent, _Graph>();
901 typedef typename _Graph::template NodeMap<int> IntNodeMap;
902 checkConcept<GraphMap<_Graph, typename _Graph::Node, int>,
905 typedef typename _Graph::template NodeMap<bool> BoolNodeMap;
906 checkConcept<GraphMap<_Graph, typename _Graph::Node, bool>,
909 typedef typename _Graph::template NodeMap<Type> TypeNodeMap;
910 checkConcept<GraphMap<_Graph, typename _Graph::Node, Type>,
915 typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
916 checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
919 typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
920 checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
923 typedef typename _Graph::template EdgeMap<Type> TypeEdgeMap;
924 checkConcept<GraphMap<_Graph, typename _Graph::Edge, Type>,
933 /// \brief An empty extendable extended graph class.
935 /// This class provides beside the core graph features
936 /// item addition interface for the graph structure.
937 /// The difference between this class and the
938 /// \c BaseExtendableGraphComponent is that it should
939 /// notify the item alteration observers.
940 class ExtendableGraphComponent : virtual public BaseGraphComponent {
943 typedef ExtendableGraphComponent Graph;
945 typedef BaseGraphComponent::Node Node;
946 typedef BaseGraphComponent::Edge Edge;
948 /// \brief Add a node to the graph.
950 /// Add a node to the graph and notify the observers.
955 /// \brief Add an edge to the graph.
957 /// Add an edge to the graph and notify the observers.
958 Edge addEdge(const Node&, const Node&) {
962 template <typename _Graph>
965 checkConcept<BaseGraphComponent, _Graph >();
966 typename _Graph::Node node_a, node_b;
967 node_a = graph.addNode();
968 node_b = graph.addNode();
969 typename _Graph::Edge edge;
970 edge = graph.addEdge(node_a, node_b);
976 /// \brief An empty erasable extended graph class.
978 /// This class provides beside the core graph features
979 /// item erase interface for the graph structure.
980 /// The difference between this class and the
981 /// \c BaseErasableGraphComponent is that it should
982 /// notify the item alteration observers.
983 class ErasableGraphComponent : virtual public BaseGraphComponent {
986 typedef ErasableGraphComponent Graph;
988 typedef BaseGraphComponent::Node Node;
989 typedef BaseGraphComponent::Edge Edge;
991 /// \brief Erase the Node and notify the node alteration observers.
993 /// Erase the Node and notify the node alteration observers.
994 void erase(const Node&) {}
996 /// \brief Erase the Edge and notify the edge alteration observers.
998 /// Erase the Edge and notify the edge alteration observers.
999 void erase(const Edge&) {}
1001 template <typename _Graph>
1002 struct Constraints {
1003 void constraints() {
1004 checkConcept<BaseGraphComponent, _Graph >();
1005 typename _Graph::Node node;
1007 typename _Graph::Edge edge;