2 * src/lemon/concept/graph_component.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, 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>
31 /// \addtogroup graph_concepts
34 /**************** Graph iterator concepts ****************/
36 /// Skeleton class for graph Node and Edge types
38 /// This class describes the interface of Node and Edge (and UndirEdge
39 /// in undirected graphs) subtypes of graph types.
41 /// \note This class is a template class so that we can use it to
42 /// create graph skeleton classes. The reason for this is than Node
43 /// and Edge types should \em not derive from the same base class.
44 /// For Node you should instantiate it with character 'n' and for Edge
48 template <char _selector = '0'>
52 /// Default constructor.
54 /// \warning The default constructor is not required to set
55 /// the item to some well-defined value. So you should consider it
62 GraphItem(GraphItem const&) {}
63 /// Invalid constructor \& conversion.
65 /// This constructor initializes the item to be invalid.
66 /// \sa Invalid for more details.
68 /// Assign operator for nodes.
70 /// The nodes are assignable.
72 GraphItem& operator=(GraphItem const&) { return *this; }
73 /// Equality operator.
75 /// Two iterators are equal if and only if they represents the
76 /// same node in the graph or both are invalid.
77 bool operator==(GraphItem) const { return false; }
78 /// Inequality operator.
80 /// \sa operator==(const Node& n)
82 bool operator!=(GraphItem) const { return false; }
84 /// Artificial ordering operator.
86 /// To allow the use of graph descriptors as key type in std::map or
87 /// similar associative container we require this.
89 /// \note This operator only have to define some strict ordering of
90 /// the items; this order has nothing to do with the iteration
91 /// ordering of the items.
93 /// \bug This is a technical requirement. Do we really need this?
94 bool operator<(GraphItem) const { return false; }
96 template<typename _GraphItem>
101 _GraphItem i3 = INVALID;
106 // b = (ia == ib) && (ia != ib) && (ia < ib);
107 b = (ia == ib) && (ia != ib);
108 b = (ia == INVALID) && (ib != INVALID);
112 const _GraphItem &ia;
113 const _GraphItem &ib;
117 /// A type describing the concept of graph node
119 /// This is an instantiation of \ref GraphItem which can be used as a
120 /// Node subtype in graph skeleton definitions
121 typedef GraphItem<'n'> GraphNode;
123 /// A type describing the concept of graph edge
125 /// This is an instantiation of \ref GraphItem which can be used as a
126 /// Edge subtype in graph skeleton definitions
127 typedef GraphItem<'e'> GraphEdge;
130 /**************** Basic features of graphs ****************/
132 /// An empty base graph class.
134 /// This class provides the minimal set of features needed for a graph
135 /// structure. All graph concepts have to be conform to this base
138 /// \bug This is not true. The minimal graph concept is the
139 /// BaseIterableGraphComponent.
141 class BaseGraphComponent {
144 typedef BaseGraphComponent Graph;
146 /// Node class of the graph.
148 /// This class represents the Nodes of the graph.
150 typedef GraphItem<'n'> Node;
152 /// Edge class of the graph.
154 /// This class represents the Edges of the graph.
156 typedef GraphItem<'e'> Edge;
158 ///Gives back the target node of an edge.
160 ///Gives back the target node of an edge.
162 Node target(const Edge&) const { return INVALID;}
164 ///Gives back the source node of an edge.
166 ///Gives back the source node of an edge.
168 Node source(const Edge&) const { return INVALID;}
171 template <typename _Graph>
173 typedef typename _Graph::Node Node;
174 typedef typename _Graph::Edge Edge;
177 checkConcept<GraphItem<'n'>, Node>();
178 checkConcept<GraphItem<'e'>, Edge>();
191 /// An empty iterable base graph class.
193 /// This class provides beside the core graph features
194 /// core iterable interface for the graph structure.
195 /// Most of the base graphs should be conform to this concept.
197 class BaseIterableGraphComponent : virtual public BaseGraphComponent {
200 typedef BaseGraphComponent::Node Node;
201 typedef BaseGraphComponent::Edge Edge;
203 /// Gives back the first Node in the iterating order.
205 /// Gives back the first Node in the iterating order.
207 void first(Node&) const {}
209 /// Gives back the next Node in the iterating order.
211 /// Gives back the next Node in the iterating order.
213 void next(Node&) const {}
215 /// Gives back the first Edge in the iterating order.
217 /// Gives back the first Edge in the iterating order.
219 void first(Edge&) const {}
220 /// Gives back the next Edge in the iterating order.
222 /// Gives back the next Edge in the iterating order.
224 void next(Edge&) const {}
227 /// Gives back the first of the Edges point to the given Node.
229 /// Gives back the first of the Edges point to the given Node.
231 void firstIn(Edge&, const Node&) const {}
233 /// Gives back the next of the Edges points to the given Node.
236 /// Gives back the next of the Edges points to the given Node.
238 void nextIn(Edge&) const {}
240 /// Gives back the first of the Edges start from the given Node.
242 /// Gives back the first of the Edges start from the given Node.
244 void firstOut(Edge&, const Node&) const {}
246 /// Gives back the next of the Edges start from the given Node.
248 /// Gives back the next of the Edges start from the given Node.
250 void nextOut(Edge&) const {}
253 template <typename _Graph>
257 checkConcept< BaseGraphComponent, _Graph >();
258 typename _Graph::Node node;
259 typename _Graph::Edge edge;
269 graph.firstIn(edge, node);
273 graph.firstOut(edge, node);
282 /// An empty idable base graph class.
284 /// This class provides beside the core graph features
285 /// core id functions for the graph structure.
286 /// The most of the base graphs should be conform to this concept.
287 /// The id's are unique and immutable.
288 class IDableGraphComponent : virtual public BaseGraphComponent {
291 typedef BaseGraphComponent::Node Node;
292 typedef BaseGraphComponent::Edge Edge;
294 /// Gives back an unique integer id for the Node.
296 /// Gives back an unique integer id for the Node.
298 int id(const Node&) const { return -1;}
300 /// \brief Gives back the node by the unique id.
302 /// Gives back the node by the unique id.
303 /// If the graph does not contain node with the given id
304 /// then the result of the function is undetermined.
305 Node fromId(int id, Node) const { return INVALID;}
307 /// \brief Gives back an unique integer id for the Edge.
309 /// Gives back an unique integer id for the Edge.
311 int id(const Edge&) const { return -1;}
313 /// \brief Gives back the edge by the unique id.
315 /// Gives back the edge by the unique id.
316 /// If the graph does not contain edge with the given id
317 /// then the result of the function is undetermined.
318 Edge fromId(int id, Edge) const { return INVALID;}
320 template <typename _Graph>
324 checkConcept< BaseGraphComponent, _Graph >();
325 typename _Graph::Node node;
326 int nid = graph.id(node);
327 nid = graph.id(node);
328 node = graph.fromId(nid, Node());
329 typename _Graph::Edge edge;
330 int eid = graph.id(edge);
331 eid = graph.id(edge);
332 edge = graph.fromId(eid, Edge());
340 /// An empty max-idable base graph class.
342 /// This class provides beside the core graph features
343 /// core max id functions for the graph structure.
344 /// The most of the base graphs should be conform to this concept.
345 /// The id's are unique and immutable.
346 class MaxIDableGraphComponent : virtual public BaseGraphComponent {
349 /// Gives back an integer greater or equal to the maximum Node id.
351 /// Gives back an integer greater or equal to the maximum Node id.
353 int maxId(Node = INVALID) const { return -1;}
355 /// Gives back an integer greater or equal to the maximum Edge id.
357 /// Gives back an integer greater or equal to the maximum Edge id.
359 int maxId(Edge = INVALID) const { return -1;}
361 template <typename _Graph>
365 checkConcept<BaseGraphComponent, _Graph>();
366 int nid = graph.maxId(typename _Graph::Node());
367 ignore_unused_variable_warning(nid);
368 int eid = graph.maxId(typename _Graph::Edge());
369 ignore_unused_variable_warning(eid);
376 /// An empty extendable base graph class.
378 /// This class provides beside the core graph features
379 /// core graph extend interface for the graph structure.
380 /// The most of the base graphs should be conform to this concept.
381 class BaseExtendableGraphComponent : virtual public BaseGraphComponent {
384 typedef BaseGraphComponent::Node Node;
385 typedef BaseGraphComponent::Edge Edge;
387 /// Adds a new Node to the graph.
389 /// Adds a new Node to the graph.
395 /// Adds a new Edge connects the two Nodes to the graph.
397 /// Adds a new Edge connects the two Nodes to the graph.
399 Edge addEdge(const Node& from, const Node& to) {
403 template <typename _Graph>
406 checkConcept<BaseGraphComponent, _Graph >();
407 typename _Graph::Node node_a, node_b;
408 node_a = graph.addNode();
409 typename _Graph::Edge edge;
410 edge = graph.addEdge(node_a, node_b);
417 /// An empty erasable base graph class.
419 /// This class provides beside the core graph features
420 /// core erase functions for the graph structure.
421 /// The most of the base graphs should be conform to this concept.
422 class BaseErasableGraphComponent : virtual public BaseGraphComponent {
425 typedef BaseGraphComponent::Node Node;
426 typedef BaseGraphComponent::Edge Edge;
428 /// Erase a Node from the graph.
430 /// Erase a Node from the graph. This function should not
431 /// erase edges connecting to the Node.
432 void erase(const Node&) {}
434 /// Erase an Edge from the graph.
436 /// Erase an Edge from the graph.
438 void erase(const Edge&) {}
440 template <typename _Graph>
443 checkConcept<BaseGraphComponent, _Graph>();
444 typename _Graph::Node node;
446 typename _Graph::Edge edge;
454 /// An empty clearable base graph class.
456 /// This class provides beside the core graph features
457 /// core clear functions for the graph structure.
458 /// The most of the base graphs should be conform to this concept.
459 class ClearableGraphComponent : virtual public BaseGraphComponent {
462 /// Erase all the Nodes and Edges from the graph.
464 /// Erase all the Nodes and Edges from the graph.
468 template <typename _Graph>
471 checkConcept<BaseGraphComponent, _Graph>();
480 /// Skeleton class for graph NodeIt and EdgeIt
482 /// Skeleton class for graph NodeIt and EdgeIt.
484 template <typename _Graph, typename _Item>
485 class GraphIterator : public _Item {
487 /// \todo Don't we need the Item type as typedef?
489 /// Default constructor.
491 /// @warning The default constructor sets the iterator
492 /// to an undefined value.
494 /// Copy constructor.
496 /// Copy constructor.
498 GraphIterator(GraphIterator const&) {}
499 /// Sets the iterator to the first item.
501 /// Sets the iterator to the first item of \c the graph.
503 explicit GraphIterator(const _Graph&) {}
504 /// Invalid constructor \& conversion.
506 /// This constructor initializes the item to be invalid.
507 /// \sa Invalid for more details.
508 GraphIterator(Invalid) {}
509 /// Assign operator for items.
511 /// The items are assignable.
513 GraphIterator& operator=(GraphIterator const&) { return *this; }
516 /// Assign the iterator to the next item.
518 GraphIterator& operator++() { return *this; }
519 // Node operator*() const { return INVALID; }
520 /// Equality operator
522 /// Two iterators are equal if and only if they point to the
523 /// same object or both are invalid.
524 bool operator==(const GraphIterator&) const { return true;}
525 /// Inequality operator
527 /// \sa operator==(Node n)
529 bool operator!=(const GraphIterator&) const { return true;}
531 template<typename _GraphIterator>
534 // checkConcept< Item, _GraphIterator >();
535 _GraphIterator it1(g);
537 /// \todo Do we need NodeIt(Node) kind of constructor?
538 // _GraphIterator it2(bj);
544 /// \bug This should be: is_base_and_derived<BaseItem, _GraphIterator>
552 /// Skeleton class for graph InEdgeIt and OutEdgeIt
554 /// \note Because InEdgeIt and OutEdgeIt may not inherit from the same
555 /// base class, the _selector is a additional template parameter. For
556 /// InEdgeIt you should instantiate it with character 'i' and for
557 /// OutEdgeIt with 'o'.
558 /// \todo Is this a good name for this concept?
559 template <typename Graph,
560 typename Edge = typename Graph::Edge,
561 char _selector = '0'>
562 class GraphIncIterator : public Edge {
564 /// Default constructor.
566 /// @warning The default constructor sets the iterator
567 /// to an undefined value.
568 GraphIncIterator() {}
569 /// Copy constructor.
571 /// Copy constructor.
573 GraphIncIterator(GraphIncIterator const&) {}
574 /// Sets the iterator to the first edge incoming into or outgoing from the node.
576 /// Sets the iterator to the first edge incoming into or outgoing from the node.
578 explicit GraphIncIterator(const Graph&, const typename Graph::Node&) {}
579 /// Invalid constructor \& conversion.
581 /// This constructor initializes the item to be invalid.
582 /// \sa Invalid for more details.
583 GraphIncIterator(Invalid) {}
584 /// Assign operator for nodes.
586 /// The nodes are assignable.
588 GraphIncIterator& operator=(GraphIncIterator const&) { return *this; }
591 /// Assign the iterator to the next node.
593 GraphIncIterator& operator++() { return *this; }
595 // Node operator*() const { return INVALID; }
597 /// Equality operator
599 /// Two iterators are equal if and only if they point to the
600 /// same object or both are invalid.
601 bool operator==(const GraphIncIterator&) const { return true;}
603 /// Inequality operator
605 /// \sa operator==(Node n)
607 bool operator!=(const GraphIncIterator&) const { return true;}
609 template <typename _GraphIncIterator>
611 typedef typename Graph::Node Node;
613 checkConcept<GraphItem<'e'>, _GraphIncIterator>();
614 _GraphIncIterator it1(graph, node);
615 /// \todo Do we need OutEdgeIt(Edge) kind of constructor?
616 // _GraphIncIterator it2(edge);
617 _GraphIncIterator it2;
633 /// An empty iterable base graph class.
635 /// This class provides beside the core graph features
636 /// iterator based iterable interface for the graph structure.
637 /// This concept is part of the StaticGraphConcept.
638 class IterableGraphComponent : virtual public BaseGraphComponent {
642 typedef IterableGraphComponent Graph;
644 typedef BaseGraphComponent::Node Node;
645 typedef BaseGraphComponent::Edge Edge;
647 /// This iterator goes through each node.
649 /// This iterator goes through each node.
651 typedef GraphIterator<Graph, Node> NodeIt;
652 /// This iterator goes through each node.
654 /// This iterator goes through each node.
656 typedef GraphIterator<Graph, Edge> EdgeIt;
657 /// This iterator goes trough the incoming edges of a node.
659 /// This iterator goes trough the \e inccoming edges of a certain node
661 typedef GraphIncIterator<Graph, Edge, 'i'> InEdgeIt;
662 /// This iterator goes trough the outgoing edges of a node.
664 /// This iterator goes trough the \e outgoing edges of a certain node
666 typedef GraphIncIterator<Graph, Edge, 'o'> OutEdgeIt;
669 template <typename _Graph>
672 checkConcept< BaseGraphComponent, _Graph>();
674 checkConcept<GraphIterator<_Graph, typename _Graph::Edge>, typename _Graph::EdgeIt >();
675 checkConcept<GraphIterator<_Graph, typename _Graph::Node>, typename _Graph::NodeIt >();
676 checkConcept<GraphIncIterator<_Graph>, typename _Graph::InEdgeIt >();
677 checkConcept<GraphIncIterator<_Graph>, typename _Graph::OutEdgeIt >();
682 /// Class describing the concept of graph maps
684 /// This class describes the common interface of the graph maps
685 /// (NodeMap, EdgeMap), that is \ref maps-pages "maps" which can be used to
686 /// associate data to graph descriptors (nodes or edges).
687 template <typename Graph, typename Item, typename _Value>
688 class GraphMap : public ReadWriteMap<Item, _Value> {
692 explicit GraphMap(const Graph&) {}
693 GraphMap(const Graph&, const _Value&) {}
694 GraphMap(const GraphMap&) {}
696 GraphMap& operator=(const GraphMap&) { return *this;}
698 template<typename _Map>
701 checkConcept<ReadWriteMap<Item, _Value>, _Map >();
702 // Construction with a graph parameter
704 // Constructor with a graph and a default value parameter
706 // Copy constructor. Do we need it?
708 // Copy operator. Do we need it?
711 ignore_unused_variable_warning(a2);
716 const typename GraphMap::Value &t;
721 /// An empty mappable base graph class.
723 /// This class provides beside the core graph features
724 /// map interface for the graph structure.
725 /// This concept is part of the StaticGraphConcept.
726 class MappableGraphComponent : virtual public BaseGraphComponent {
729 typedef MappableGraphComponent Graph;
731 typedef BaseGraphComponent::Node Node;
732 typedef BaseGraphComponent::Edge Edge;
734 /// ReadWrite map of the nodes.
736 /// ReadWrite map of the nodes.
738 template <typename _Value>
739 class NodeMap : public GraphMap<Graph, Node, _Value> {
743 // \todo call the right parent class constructor
744 explicit NodeMap(const Graph&) {}
745 NodeMap(const Graph&, const _Value&) {}
746 NodeMap(const NodeMap&) {}
748 NodeMap& operator=(const NodeMap&) { return *this;}
752 /// ReadWrite map of the edges.
754 /// ReadWrite map of the edges.
756 template <typename _Value>
757 class EdgeMap : public GraphMap<Graph, Edge, _Value> {
761 // \todo call the right parent class constructor
762 explicit EdgeMap(const Graph&) {}
763 EdgeMap(const Graph&, const _Value&) {}
764 EdgeMap(const EdgeMap&) {}
766 EdgeMap& operator=(const EdgeMap&) { return *this;}
770 template <typename _Graph>
776 Type(int _v) : value(_v) {}
780 checkConcept<BaseGraphComponent, _Graph>();
782 typedef typename _Graph::template NodeMap<int> IntNodeMap;
783 checkConcept<GraphMap<_Graph, typename _Graph::Node, int>, IntNodeMap >();
785 typedef typename _Graph::template NodeMap<bool> BoolNodeMap;
786 checkConcept<GraphMap<_Graph, typename _Graph::Node, bool>, BoolNodeMap >();
788 typedef typename _Graph::template NodeMap<Type> TypeNodeMap;
789 checkConcept<GraphMap<_Graph, typename _Graph::Node, Type>, TypeNodeMap >();
793 typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
794 checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>, IntEdgeMap >();
796 typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
797 checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>, BoolEdgeMap >();
799 typedef typename _Graph::template EdgeMap<Type> TypeEdgeMap;
800 checkConcept<GraphMap<_Graph, typename _Graph::Edge, Type>, TypeEdgeMap >();
809 class ExtendableGraphComponent : virtual public BaseGraphComponent {
812 typedef ExtendableGraphComponent Graph;
814 typedef BaseGraphComponent::Node Node;
815 typedef BaseGraphComponent::Edge Edge;
821 Edge addEdge(const Node& from, const Node& to) {
825 template <typename _Graph>
828 checkConcept<BaseGraphComponent, _Graph >();
829 typename _Graph::Node node_a, node_b;
830 node_a = graph.addNode();
831 typename _Graph::Edge edge;
832 edge = graph.addEdge(node_a, node_b);
837 class ErasableGraphComponent : virtual public BaseGraphComponent {
840 typedef ErasableGraphComponent Graph;
842 typedef BaseGraphComponent::Node Node;
843 typedef BaseGraphComponent::Edge Edge;
845 void erase(const Node&) {}
846 void erase(const Edge&) {}
848 template <typename _Graph>
851 checkConcept<BaseGraphComponent, _Graph >();
852 typename _Graph::Node node;
854 typename _Graph::Edge edge;