2 * lemon/concept/graph.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 #ifndef LEMON_CONCEPT_GRAPH_H
18 #define LEMON_CONCEPT_GRAPH_H
20 ///\ingroup graph_concepts
22 ///\brief Declaration of Graph.
24 #include <lemon/invalid.h>
25 #include <lemon/utility.h>
26 #include <lemon/concept/maps.h>
27 #include <lemon/concept_check.h>
28 #include <lemon/concept/graph_component.h>
34 /// \addtogroup graph_concepts
37 /**************** The full-featured graph concepts ****************/
40 /// \brief Modular static graph class.
42 /// It should be the same as the \c StaticGraph class.
44 : virtual public BaseGraphComponent,
45 public IterableGraphComponent, public MappableGraphComponent {
51 typedef False UndirTag;
53 typedef BaseGraphComponent::Node Node;
54 typedef BaseGraphComponent::Edge Edge;
56 template <typename _Graph>
59 checkConcept<IterableGraphComponent, _Graph>();
60 checkConcept<MappableGraphComponent, _Graph>();
65 /// \brief Modular extendable graph class.
67 /// It should be the same as the \c ExtendableGraph class.
68 class _ExtendableGraph
69 : virtual public BaseGraphComponent, public _StaticGraph,
70 public ExtendableGraphComponent, public ClearableGraphComponent {
72 typedef BaseGraphComponent::Node Node;
73 typedef BaseGraphComponent::Edge Edge;
75 template <typename _Graph>
78 checkConcept<_StaticGraph, _Graph >();
79 checkConcept<ExtendableGraphComponent, _Graph >();
80 checkConcept<ClearableGraphComponent, _Graph >();
85 /// \brief Modular erasable graph class.
87 /// It should be the same as the \c ErasableGraph class.
89 : virtual public BaseGraphComponent, public _ExtendableGraph,
90 public ErasableGraphComponent {
92 typedef BaseGraphComponent::Node Node;
93 typedef BaseGraphComponent::Edge Edge;
95 template <typename _Graph>
98 checkConcept<_ExtendableGraph, _Graph >();
99 checkConcept<ErasableGraphComponent, _Graph >();
104 /// An empty static graph class.
106 /// This class provides all the common features of a graph structure,
107 /// however completely without implementations and real data structures
108 /// behind the interface.
109 /// All graph algorithms should compile with this class, but it will not
110 /// run properly, of course.
112 /// It can be used for checking the interface compatibility,
113 /// or it can serve as a skeleton of a new graph structure.
115 /// Also, you will find here the full documentation of a certain graph
116 /// feature, the documentation of a real graph imlementation
117 /// like @ref ListGraph or
118 /// @ref SmartGraph will just refer to this structure.
120 /// \todo A pages describing the concept of concept description would
127 ///\todo undocumented
129 typedef False UndirTag;
131 /// Defalult constructor.
133 /// Defalult constructor.
138 // ///\todo It is not clear, what we expect from a copy constructor.
139 // ///E.g. How to assign the nodes/edges to each other? What about maps?
140 // StaticGraph(const StaticGraph& g) { }
142 /// The base type of node iterators,
143 /// or in other words, the trivial node iterator.
145 /// This is the base type of each node iterator,
146 /// thus each kind of node iterator converts to this.
147 /// More precisely each kind of node iterator should be inherited
148 /// from the trivial node iterator.
151 /// Default constructor
153 /// @warning The default constructor sets the iterator
154 /// to an undefined value.
156 /// Copy constructor.
158 /// Copy constructor.
160 Node(const Node&) { }
162 /// Invalid constructor \& conversion.
164 /// This constructor initializes the iterator to be invalid.
165 /// \sa Invalid for more details.
167 /// Equality operator
169 /// Two iterators are equal if and only if they point to the
170 /// same object or both are invalid.
171 bool operator==(Node) const { return true; }
173 /// Inequality operator
175 /// \sa operator==(Node n)
177 bool operator!=(Node) const { return true; }
181 /// This iterator goes through each node.
183 /// This iterator goes through each node.
184 /// Its usage is quite simple, for example you can count the number
185 /// of nodes in graph \c g of type \c Graph like this:
188 /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
190 class NodeIt : public Node {
192 /// Default constructor
194 /// @warning The default constructor sets the iterator
195 /// to an undefined value.
197 /// Copy constructor.
199 /// Copy constructor.
201 NodeIt(const NodeIt& n) : Node(n) { }
202 /// Invalid constructor \& conversion.
204 /// Initialize the iterator to be invalid.
205 /// \sa Invalid for more details.
207 /// Sets the iterator to the first node.
209 /// Sets the iterator to the first node of \c g.
211 NodeIt(const StaticGraph&) { }
212 /// Node -> NodeIt conversion.
214 /// Sets the iterator to the node of \c the graph pointed by
215 /// the trivial iterator.
216 /// This feature necessitates that each time we
217 /// iterate the edge-set, the iteration order is the same.
218 NodeIt(const StaticGraph&, const Node&) { }
221 /// Assign the iterator to the next node.
223 NodeIt& operator++() { return *this; }
227 /// The base type of the edge iterators.
229 /// The base type of the edge iterators.
233 /// Default constructor
235 /// @warning The default constructor sets the iterator
236 /// to an undefined value.
238 /// Copy constructor.
240 /// Copy constructor.
242 Edge(const Edge&) { }
243 /// Initialize the iterator to be invalid.
245 /// Initialize the iterator to be invalid.
248 /// Equality operator
250 /// Two iterators are equal if and only if they point to the
251 /// same object or both are invalid.
252 bool operator==(Edge) const { return true; }
253 /// Inequality operator
255 /// \sa operator==(Node n)
257 bool operator!=(Edge) const { return true; }
260 /// This iterator goes trough the outgoing edges of a node.
262 /// This iterator goes trough the \e outgoing edges of a certain node
264 /// Its usage is quite simple, for example you can count the number
265 /// of outgoing edges of a node \c n
266 /// in graph \c g of type \c Graph as follows.
269 /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
272 class OutEdgeIt : public Edge {
274 /// Default constructor
276 /// @warning The default constructor sets the iterator
277 /// to an undefined value.
279 /// Copy constructor.
281 /// Copy constructor.
283 OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
284 /// Initialize the iterator to be invalid.
286 /// Initialize the iterator to be invalid.
288 OutEdgeIt(Invalid) { }
289 /// This constructor sets the iterator to the first outgoing edge.
291 /// This constructor sets the iterator to the first outgoing edge of
294 ///@param g the graph
295 OutEdgeIt(const StaticGraph&, const Node&) { }
296 /// Edge -> OutEdgeIt conversion
298 /// Sets the iterator to the value of the trivial iterator.
299 /// This feature necessitates that each time we
300 /// iterate the edge-set, the iteration order is the same.
301 OutEdgeIt(const StaticGraph&, const Edge&) { }
302 ///Next outgoing edge
304 /// Assign the iterator to the next
305 /// outgoing edge of the corresponding node.
306 OutEdgeIt& operator++() { return *this; }
309 /// This iterator goes trough the incoming edges of a node.
311 /// This iterator goes trough the \e incoming edges of a certain node
313 /// Its usage is quite simple, for example you can count the number
314 /// of outgoing edges of a node \c n
315 /// in graph \c g of type \c Graph as follows.
318 /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
321 class InEdgeIt : public Edge {
323 /// Default constructor
325 /// @warning The default constructor sets the iterator
326 /// to an undefined value.
328 /// Copy constructor.
330 /// Copy constructor.
332 InEdgeIt(const InEdgeIt& e) : Edge(e) { }
333 /// Initialize the iterator to be invalid.
335 /// Initialize the iterator to be invalid.
337 InEdgeIt(Invalid) { }
338 /// This constructor sets the iterator to first incoming edge.
340 /// This constructor set the iterator to the first incoming edge of
343 ///@param g the graph
344 InEdgeIt(const StaticGraph&, const Node&) { }
345 /// Edge -> InEdgeIt conversion
347 /// Sets the iterator to the value of the trivial iterator \c e.
348 /// This feature necessitates that each time we
349 /// iterate the edge-set, the iteration order is the same.
350 InEdgeIt(const StaticGraph&, const Edge&) { }
351 /// Next incoming edge
353 /// Assign the iterator to the next inedge of the corresponding node.
355 InEdgeIt& operator++() { return *this; }
357 /// This iterator goes through each edge.
359 /// This iterator goes through each edge of a graph.
360 /// Its usage is quite simple, for example you can count the number
361 /// of edges in a graph \c g of type \c Graph as follows:
364 /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
366 class EdgeIt : public Edge {
368 /// Default constructor
370 /// @warning The default constructor sets the iterator
371 /// to an undefined value.
373 /// Copy constructor.
375 /// Copy constructor.
377 EdgeIt(const EdgeIt& e) : Edge(e) { }
378 /// Initialize the iterator to be invalid.
380 /// Initialize the iterator to be invalid.
383 /// This constructor sets the iterator to the first edge.
385 /// This constructor sets the iterator to the first edge of \c g.
386 ///@param g the graph
387 EdgeIt(const StaticGraph&) { }
388 /// Edge -> EdgeIt conversion
390 /// Sets the iterator to the value of the trivial iterator \c e.
391 /// This feature necessitates that each time we
392 /// iterate the edge-set, the iteration order is the same.
393 EdgeIt(const StaticGraph&, const Edge&) { }
396 /// Assign the iterator to the next edge.
397 EdgeIt& operator++() { return *this; }
399 ///Gives back the target node of an edge.
401 ///Gives back the target node of an edge.
403 Node target(Edge) const { return INVALID; }
404 ///Gives back the source node of an edge.
406 ///Gives back the source node of an edge.
408 Node source(Edge) const { return INVALID; }
410 /// Gives back the first Node in the iterating order.
412 /// Gives back the first Node in the iterating order.
414 void first(Node&) const {}
416 /// Gives back the next Node in the iterating order.
418 /// Gives back the next Node in the iterating order.
420 void next(Node&) const {}
422 /// Gives back the first Edge in the iterating order.
424 /// Gives back the first Edge in the iterating order.
426 void first(Edge&) const {}
427 /// Gives back the next Edge in the iterating order.
429 /// Gives back the next Edge in the iterating order.
431 void next(Edge&) const {}
434 /// Gives back the first of the Edges point to the given Node.
436 /// Gives back the first of the Edges point to the given Node.
438 void firstIn(Edge&, const Node&) const {}
440 /// Gives back the next of the Edges points to the given Node.
443 /// Gives back the next of the Edges points to the given Node.
445 void nextIn(Edge&) const {}
447 /// Gives back the first of the Edges start from the given Node.
449 /// Gives back the first of the Edges start from the given Node.
451 void firstOut(Edge&, const Node&) const {}
453 /// Gives back the next of the Edges start from the given Node.
455 /// Gives back the next of the Edges start from the given Node.
457 void nextOut(Edge&) const {}
459 /// \brief The base node of the iterator.
461 /// Gives back the base node of the iterator.
462 Node baseNode(const InEdgeIt&) const { return INVALID; }
464 /// \brief The running node of the iterator.
466 /// Gives back the running node of the iterator.
467 Node runningNode(const InEdgeIt&) const { return INVALID; }
469 /// \brief The base node of the iterator.
471 /// Gives back the base node of the iterator.
472 Node baseNode(const OutEdgeIt&) const { return INVALID; }
474 /// \brief The running node of the iterator.
476 /// Gives back the running node of the iterator.
477 Node runningNode(const OutEdgeIt&) const { return INVALID; }
478 /// Read write map of the nodes to type \c T.
481 /// ReadWrite map of the nodes to type \c T.
483 /// \warning Making maps that can handle bool type (NodeMap<bool>)
484 /// needs some extra attention!
486 class NodeMap : public ReadWriteMap< Node, T >
491 NodeMap(const StaticGraph&) { }
493 NodeMap(const StaticGraph&, T) { }
496 NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
497 ///Assignment operator
498 NodeMap& operator=(const NodeMap&) { return *this; }
499 // \todo fix this concept
502 /// Read write map of the edges to type \c T.
505 ///Reference map of the edges to type \c T.
507 /// \warning Making maps that can handle bool type (EdgeMap<bool>)
508 /// needs some extra attention!
510 class EdgeMap : public ReadWriteMap<Edge,T>
515 EdgeMap(const StaticGraph&) { }
517 EdgeMap(const StaticGraph&, T) { }
519 EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
520 ///Assignment operator
521 EdgeMap& operator=(const EdgeMap&) { return *this; }
522 // \todo fix this concept
525 template <typename _Graph>
526 struct Constraints : public _StaticGraph::Constraints<_Graph> {};
530 /// An empty non-static graph class.
532 /// This class provides everything that \ref StaticGraph does.
533 /// Additionally it enables building graphs from scratch.
534 class ExtendableGraph : public StaticGraph
537 /// Defalult constructor.
539 /// Defalult constructor.
541 ExtendableGraph() { }
542 ///Add a new node to the graph.
544 /// \return the new node.
546 Node addNode() { return INVALID; }
547 ///Add a new edge to the graph.
549 ///Add a new edge to the graph with source node \c s
550 ///and target node \c t.
551 ///\return the new edge.
552 Edge addEdge(Node, Node) { return INVALID; }
554 /// Resets the graph.
556 /// This function deletes all edges and nodes of the graph.
557 /// It also frees the memory allocated to store them.
558 /// \todo It might belong to \ref ErasableGraph.
561 template <typename _Graph>
562 struct Constraints : public _ExtendableGraph::Constraints<_Graph> {};
566 /// An empty erasable graph class.
568 /// This class is an extension of \ref ExtendableGraph. It makes it
569 /// possible to erase edges or nodes.
570 class ErasableGraph : public ExtendableGraph
573 /// Defalult constructor.
575 /// Defalult constructor.
580 /// Deletes node \c n node.
585 /// Deletes edge \c e edge.
589 template <typename _Graph>
590 struct Constraints : public _ErasableGraph::Constraints<_Graph> {};
595 /************* New GraphBase stuff **************/
598 // /// A minimal GraphBase concept
600 // /// This class describes a minimal concept which can be extended to a
601 // /// full-featured graph with \ref GraphFactory.
607 // /// \bug Should we demand that Node and Edge be subclasses of the
608 // /// Graph class???
610 // typedef GraphItem<'n'> Node;
611 // typedef GraphItem<'e'> Edge;
613 // // class Node : public BaseGraphItem<'n'> {};
614 // // class Edge : public BaseGraphItem<'e'> {};
616 // // Graph operation
617 // void firstNode(Node &n) const { }
618 // void firstEdge(Edge &e) const { }
620 // void firstOutEdge(Edge &e, Node) const { }
621 // void firstInEdge(Edge &e, Node) const { }
623 // void nextNode(Node &n) const { }
624 // void nextEdge(Edge &e) const { }
627 // // Question: isn't it reasonable if this methods have a Node
628 // // parameter? Like this:
629 // // Edge& nextOut(Edge &e, Node) const { return e; }
630 // void nextOutEdge(Edge &e) const { }
631 // void nextInEdge(Edge &e) const { }
633 // Node target(Edge) const { return Node(); }
634 // Node source(Edge) const { return Node(); }
637 // // Do we need id, nodeNum, edgeNum and co. in this basic graphbase
643 // // We need a special slimer concept which does not provide maps (it
644 // // wouldn't be strictly slimer, cause for map-factory id() & friends
647 // template<typename T>
648 // class NodeMap : public GraphMap<GraphBase, Node, T> {};
650 // template<typename T>
651 // class EdgeMap : public GraphMap<GraphBase, Node, T> {};
655 } //namespace concept
660 #endif // LEMON_CONCEPT_GRAPH_H