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 /// Gives back an unique integer id for the Edge.
302 /// Gives back an unique integer id for the Edge.
304 int id(const Edge&) const { return -1;}
306 template <typename _Graph>
310 checkConcept< BaseGraphComponent, _Graph >();
311 typename _Graph::Node node;
312 int nid = graph.id(node);
313 nid = graph.id(node);
314 typename _Graph::Edge edge;
315 int eid = graph.id(edge);
316 eid = graph.id(edge);
324 /// An empty max-idable base graph class.
326 /// This class provides beside the core graph features
327 /// core max id functions for the graph structure.
328 /// The most of the base graphs should be conform to this concept.
329 /// The id's are unique and immutable.
330 class MaxIDableGraphComponent : virtual public BaseGraphComponent {
333 /// Gives back an integer greater or equal to the maximum Node id.
335 /// Gives back an integer greater or equal to the maximum Node id.
337 int maxId(Node = INVALID) const { return -1;}
339 /// Gives back an integer greater or equal to the maximum Edge id.
341 /// Gives back an integer greater or equal to the maximum Edge id.
343 int maxId(Edge = INVALID) const { return -1;}
345 template <typename _Graph>
349 checkConcept<BaseGraphComponent, _Graph>();
350 int nid = graph.maxId(typename _Graph::Node());
351 ignore_unused_variable_warning(nid);
352 int eid = graph.maxId(typename _Graph::Edge());
353 ignore_unused_variable_warning(eid);
360 /// An empty extendable base graph class.
362 /// This class provides beside the core graph features
363 /// core graph extend interface for the graph structure.
364 /// The most of the base graphs should be conform to this concept.
365 class BaseExtendableGraphComponent : virtual public BaseGraphComponent {
368 typedef BaseGraphComponent::Node Node;
369 typedef BaseGraphComponent::Edge Edge;
371 /// Adds a new Node to the graph.
373 /// Adds a new Node to the graph.
379 /// Adds a new Edge connects the two Nodes to the graph.
381 /// Adds a new Edge connects the two Nodes to the graph.
383 Edge addEdge(const Node& from, const Node& to) {
387 template <typename _Graph>
390 checkConcept<BaseGraphComponent, _Graph >();
391 typename _Graph::Node node_a, node_b;
392 node_a = graph.addNode();
393 typename _Graph::Edge edge;
394 edge = graph.addEdge(node_a, node_b);
401 /// An empty erasable base graph class.
403 /// This class provides beside the core graph features
404 /// core erase functions for the graph structure.
405 /// The most of the base graphs should be conform to this concept.
406 class BaseErasableGraphComponent : virtual public BaseGraphComponent {
409 typedef BaseGraphComponent::Node Node;
410 typedef BaseGraphComponent::Edge Edge;
412 /// Erase a Node from the graph.
414 /// Erase a Node from the graph. This function should not
415 /// erase edges connecting to the Node.
416 void erase(const Node&) {}
418 /// Erase an Edge from the graph.
420 /// Erase an Edge from the graph.
422 void erase(const Edge&) {}
424 template <typename _Graph>
427 checkConcept<BaseGraphComponent, _Graph>();
428 typename _Graph::Node node;
430 typename _Graph::Edge edge;
438 /// An empty clearable base graph class.
440 /// This class provides beside the core graph features
441 /// core clear functions for the graph structure.
442 /// The most of the base graphs should be conform to this concept.
443 class ClearableGraphComponent : virtual public BaseGraphComponent {
446 /// Erase all the Nodes and Edges from the graph.
448 /// Erase all the Nodes and Edges from the graph.
452 template <typename _Graph>
455 checkConcept<BaseGraphComponent, _Graph>();
464 /// Skeleton class for graph NodeIt and EdgeIt
466 /// Skeleton class for graph NodeIt and EdgeIt.
468 template <typename _Graph, typename _Item>
469 class GraphIterator : public _Item {
471 /// \todo Don't we need the Item type as typedef?
473 /// Default constructor.
475 /// @warning The default constructor sets the iterator
476 /// to an undefined value.
478 /// Copy constructor.
480 /// Copy constructor.
482 GraphIterator(GraphIterator const&) {}
483 /// Sets the iterator to the first item.
485 /// Sets the iterator to the first item of \c the graph.
487 explicit GraphIterator(const _Graph&) {}
488 /// Invalid constructor \& conversion.
490 /// This constructor initializes the item to be invalid.
491 /// \sa Invalid for more details.
492 GraphIterator(Invalid) {}
493 /// Assign operator for items.
495 /// The items are assignable.
497 GraphIterator& operator=(GraphIterator const&) { return *this; }
500 /// Assign the iterator to the next item.
502 GraphIterator& operator++() { return *this; }
503 // Node operator*() const { return INVALID; }
504 /// Equality operator
506 /// Two iterators are equal if and only if they point to the
507 /// same object or both are invalid.
508 bool operator==(const GraphIterator&) const { return true;}
509 /// Inequality operator
511 /// \sa operator==(Node n)
513 bool operator!=(const GraphIterator&) const { return true;}
515 template<typename _GraphIterator>
518 // checkConcept< Item, _GraphIterator >();
519 _GraphIterator it1(g);
521 /// \todo Do we need NodeIt(Node) kind of constructor?
522 // _GraphIterator it2(bj);
528 /// \bug This should be: is_base_and_derived<BaseItem, _GraphIterator>
536 /// Skeleton class for graph InEdgeIt and OutEdgeIt
538 /// \note Because InEdgeIt and OutEdgeIt may not inherit from the same
539 /// base class, the _selector is a additional template parameter. For
540 /// InEdgeIt you should instantiate it with character 'i' and for
541 /// OutEdgeIt with 'o'.
542 /// \todo Is this a good name for this concept?
543 template <typename Graph,
544 typename Edge = typename Graph::Edge,
545 char _selector = '0'>
546 class GraphIncIterator : public Edge {
548 /// Default constructor.
550 /// @warning The default constructor sets the iterator
551 /// to an undefined value.
552 GraphIncIterator() {}
553 /// Copy constructor.
555 /// Copy constructor.
557 GraphIncIterator(GraphIncIterator const&) {}
558 /// Sets the iterator to the first edge incoming into or outgoing from the node.
560 /// Sets the iterator to the first edge incoming into or outgoing from the node.
562 explicit GraphIncIterator(const Graph&, const typename Graph::Node&) {}
563 /// Invalid constructor \& conversion.
565 /// This constructor initializes the item to be invalid.
566 /// \sa Invalid for more details.
567 GraphIncIterator(Invalid) {}
568 /// Assign operator for nodes.
570 /// The nodes are assignable.
572 GraphIncIterator& operator=(GraphIncIterator const&) { return *this; }
575 /// Assign the iterator to the next node.
577 GraphIncIterator& operator++() { return *this; }
579 // Node operator*() const { return INVALID; }
581 /// Equality operator
583 /// Two iterators are equal if and only if they point to the
584 /// same object or both are invalid.
585 bool operator==(const GraphIncIterator&) const { return true;}
587 /// Inequality operator
589 /// \sa operator==(Node n)
591 bool operator!=(const GraphIncIterator&) const { return true;}
593 template <typename _GraphIncIterator>
595 typedef typename Graph::Node Node;
597 checkConcept<GraphItem<'e'>, _GraphIncIterator>();
598 _GraphIncIterator it1(graph, node);
599 /// \todo Do we need OutEdgeIt(Edge) kind of constructor?
600 // _GraphIncIterator it2(edge);
601 _GraphIncIterator it2;
617 /// An empty iterable base graph class.
619 /// This class provides beside the core graph features
620 /// iterator based iterable interface for the graph structure.
621 /// This concept is part of the StaticGraphConcept.
622 class IterableGraphComponent : virtual public BaseGraphComponent {
626 typedef IterableGraphComponent Graph;
628 typedef BaseGraphComponent::Node Node;
629 typedef BaseGraphComponent::Edge Edge;
631 /// This iterator goes through each node.
633 /// This iterator goes through each node.
635 typedef GraphIterator<Graph, Node> NodeIt;
636 /// This iterator goes through each node.
638 /// This iterator goes through each node.
640 typedef GraphIterator<Graph, Edge> EdgeIt;
641 /// This iterator goes trough the incoming edges of a node.
643 /// This iterator goes trough the \e inccoming edges of a certain node
645 typedef GraphIncIterator<Graph, Edge, 'i'> InEdgeIt;
646 /// This iterator goes trough the outgoing edges of a node.
648 /// This iterator goes trough the \e outgoing edges of a certain node
650 typedef GraphIncIterator<Graph, Edge, 'o'> OutEdgeIt;
653 template <typename _Graph>
656 checkConcept< BaseGraphComponent, _Graph>();
658 checkConcept<GraphIterator<_Graph, typename _Graph::Edge>, typename _Graph::EdgeIt >();
659 checkConcept<GraphIterator<_Graph, typename _Graph::Node>, typename _Graph::NodeIt >();
660 checkConcept<GraphIncIterator<_Graph>, typename _Graph::InEdgeIt >();
661 checkConcept<GraphIncIterator<_Graph>, typename _Graph::OutEdgeIt >();
666 /// Class describing the concept of graph maps
668 /// This class describes the common interface of the graph maps
669 /// (NodeMap, EdgeMap), that is \ref maps "maps" which can be used to
670 /// associate data to graph descriptors (nodes or edges).
671 template <typename Graph, typename Item, typename _Value>
672 class GraphMap : public ReadWriteMap<Item, _Value> {
676 explicit GraphMap(const Graph&) {}
677 GraphMap(const Graph&, const _Value&) {}
678 GraphMap(const GraphMap&) {}
680 GraphMap& operator=(const GraphMap&) { return *this;}
682 template<typename _Map>
685 checkConcept<ReadWriteMap<Item, _Value>, _Map >();
686 // Construction with a graph parameter
688 // Constructor with a graph and a default value parameter
690 // Copy constructor. Do we need it?
692 // Copy operator. Do we need it?
695 ignore_unused_variable_warning(a2);
700 const typename GraphMap::Value &t;
705 /// An empty mappable base graph class.
707 /// This class provides beside the core graph features
708 /// map interface for the graph structure.
709 /// This concept is part of the StaticGraphConcept.
710 class MappableGraphComponent : virtual public BaseGraphComponent {
713 typedef MappableGraphComponent Graph;
715 typedef BaseGraphComponent::Node Node;
716 typedef BaseGraphComponent::Edge Edge;
718 /// ReadWrite map of the nodes.
720 /// ReadWrite map of the nodes.
722 template <typename _Value>
723 class NodeMap : public GraphMap<Graph, Node, _Value> {
727 // \todo call the right parent class constructor
728 explicit NodeMap(const Graph&) {}
729 NodeMap(const Graph&, const _Value&) {}
730 NodeMap(const NodeMap&) {}
732 NodeMap& operator=(const NodeMap&) { return *this;}
736 /// ReadWrite map of the edges.
738 /// ReadWrite map of the edges.
740 template <typename _Value>
741 class EdgeMap : public GraphMap<Graph, Edge, _Value> {
745 // \todo call the right parent class constructor
746 explicit EdgeMap(const Graph&) {}
747 EdgeMap(const Graph&, const _Value&) {}
748 EdgeMap(const EdgeMap&) {}
750 EdgeMap& operator=(const EdgeMap&) { return *this;}
754 template <typename _Graph>
760 Type(int _v) : value(_v) {}
764 checkConcept<BaseGraphComponent, _Graph>();
766 typedef typename _Graph::template NodeMap<int> IntNodeMap;
767 checkConcept<GraphMap<_Graph, typename _Graph::Node, int>, IntNodeMap >();
769 typedef typename _Graph::template NodeMap<bool> BoolNodeMap;
770 checkConcept<GraphMap<_Graph, typename _Graph::Node, bool>, BoolNodeMap >();
772 typedef typename _Graph::template NodeMap<Type> TypeNodeMap;
773 checkConcept<GraphMap<_Graph, typename _Graph::Node, Type>, TypeNodeMap >();
777 typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
778 checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>, IntEdgeMap >();
780 typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
781 checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>, BoolEdgeMap >();
783 typedef typename _Graph::template EdgeMap<Type> TypeEdgeMap;
784 checkConcept<GraphMap<_Graph, typename _Graph::Edge, Type>, TypeEdgeMap >();
793 class ExtendableGraphComponent : virtual public BaseGraphComponent {
796 typedef ExtendableGraphComponent Graph;
798 typedef BaseGraphComponent::Node Node;
799 typedef BaseGraphComponent::Edge Edge;
805 Edge addEdge(const Node& from, const Node& to) {
809 template <typename _Graph>
812 checkConcept<BaseGraphComponent, _Graph >();
813 typename _Graph::Node node_a, node_b;
814 node_a = graph.addNode();
815 typename _Graph::Edge edge;
816 edge = graph.addEdge(node_a, node_b);
821 class ErasableGraphComponent : virtual public BaseGraphComponent {
824 typedef ErasableGraphComponent Graph;
826 typedef BaseGraphComponent::Node Node;
827 typedef BaseGraphComponent::Edge Edge;
829 void erase(const Node&) {}
830 void erase(const Edge&) {}
832 template <typename _Graph>
835 checkConcept<BaseGraphComponent, _Graph >();
836 typename _Graph::Node node;
838 typename _Graph::Edge edge;