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 /**************** The full-featured graph concepts ****************/
37 // \brief Modular static graph class.
39 // It should be the same as the \c StaticGraph class.
41 : virtual public BaseGraphComponent,
42 public IterableGraphComponent, public MappableGraphComponent {
45 typedef False UndirTag;
47 typedef BaseGraphComponent::Node Node;
48 typedef BaseGraphComponent::Edge Edge;
50 template <typename _Graph>
53 checkConcept<IterableGraphComponent, _Graph>();
54 checkConcept<MappableGraphComponent, _Graph>();
59 // \brief Modular extendable graph class.
61 // It should be the same as the \c ExtendableGraph class.
62 class _ExtendableGraph
63 : virtual public BaseGraphComponent, public _StaticGraph,
64 public ExtendableGraphComponent, public ClearableGraphComponent {
66 typedef BaseGraphComponent::Node Node;
67 typedef BaseGraphComponent::Edge Edge;
69 template <typename _Graph>
72 checkConcept<_StaticGraph, _Graph >();
73 checkConcept<ExtendableGraphComponent, _Graph >();
74 checkConcept<ClearableGraphComponent, _Graph >();
79 // \brief Modular erasable graph class.
81 // It should be the same as the \c ErasableGraph class.
83 : virtual public BaseGraphComponent, public _ExtendableGraph,
84 public ErasableGraphComponent {
86 typedef BaseGraphComponent::Node Node;
87 typedef BaseGraphComponent::Edge Edge;
89 template <typename _Graph>
92 checkConcept<_ExtendableGraph, _Graph >();
93 checkConcept<ErasableGraphComponent, _Graph >();
98 /// \addtogroup graph_concepts
101 /// An empty static graph class.
103 /// This class provides all the common features of a graph structure,
104 /// however completely without implementations and real data structures
105 /// behind the interface.
106 /// All graph algorithms should compile with this class, but it will not
107 /// run properly, of course.
109 /// It can be used for checking the interface compatibility,
110 /// or it can serve as a skeleton of a new graph structure.
112 /// Also, you will find here the full documentation of a certain graph
113 /// feature, the documentation of a real graph imlementation
114 /// like @ref ListGraph or
115 /// @ref SmartGraph will just refer to this structure.
117 /// \todo A pages describing the concept of concept description would
124 ///\todo undocumented
126 typedef False UndirTag;
128 /// Defalult constructor.
130 /// Defalult constructor.
135 // ///\todo It is not clear, what we expect from a copy constructor.
136 // ///E.g. How to assign the nodes/edges to each other? What about maps?
137 // StaticGraph(const StaticGraph& g) { }
139 /// The base type of node iterators,
140 /// or in other words, the trivial node iterator.
142 /// This is the base type of each node iterator,
143 /// thus each kind of node iterator converts to this.
144 /// More precisely each kind of node iterator should be inherited
145 /// from the trivial node iterator.
148 /// Default constructor
150 /// @warning The default constructor sets the iterator
151 /// to an undefined value.
153 /// Copy constructor.
155 /// Copy constructor.
157 Node(const Node&) { }
159 /// Invalid constructor \& conversion.
161 /// This constructor initializes the iterator to be invalid.
162 /// \sa Invalid for more details.
164 /// Equality operator
166 /// Two iterators are equal if and only if they point to the
167 /// same object or both are invalid.
168 bool operator==(Node) const { return true; }
170 /// Inequality operator
172 /// \sa operator==(Node n)
174 bool operator!=(Node) const { return true; }
176 /// Artificial ordering operator.
178 /// To allow the use of graph descriptors as key type in std::map or
179 /// similar associative container we require this.
181 /// \note This operator only have to define some strict ordering of
182 /// the items; this order has nothing to do with the iteration
183 /// ordering of the items.
185 /// \bug This is a technical requirement. Do we really need this?
186 bool operator<(Node) const { return false; }
190 /// This iterator goes through each node.
192 /// This iterator goes through each node.
193 /// Its usage is quite simple, for example you can count the number
194 /// of nodes in graph \c g of type \c Graph like this:
197 /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
199 class NodeIt : public Node {
201 /// Default constructor
203 /// @warning The default constructor sets the iterator
204 /// to an undefined value.
206 /// Copy constructor.
208 /// Copy constructor.
210 NodeIt(const NodeIt& n) : Node(n) { }
211 /// Invalid constructor \& conversion.
213 /// Initialize the iterator to be invalid.
214 /// \sa Invalid for more details.
216 /// Sets the iterator to the first node.
218 /// Sets the iterator to the first node of \c g.
220 NodeIt(const StaticGraph&) { }
221 /// Node -> NodeIt conversion.
223 /// Sets the iterator to the node of \c the graph pointed by
224 /// the trivial iterator.
225 /// This feature necessitates that each time we
226 /// iterate the edge-set, the iteration order is the same.
227 NodeIt(const StaticGraph&, const Node&) { }
230 /// Assign the iterator to the next node.
232 NodeIt& operator++() { return *this; }
236 /// The base type of the edge iterators.
238 /// The base type of the edge iterators.
242 /// Default constructor
244 /// @warning The default constructor sets the iterator
245 /// to an undefined value.
247 /// Copy constructor.
249 /// Copy constructor.
251 Edge(const Edge&) { }
252 /// Initialize the iterator to be invalid.
254 /// Initialize the iterator to be invalid.
257 /// Equality operator
259 /// Two iterators are equal if and only if they point to the
260 /// same object or both are invalid.
261 bool operator==(Edge) const { return true; }
262 /// Inequality operator
264 /// \sa operator==(Edge n)
266 bool operator!=(Edge) const { return true; }
268 /// Artificial ordering operator.
270 /// To allow the use of graph descriptors as key type in std::map or
271 /// similar associative container we require this.
273 /// \note This operator only have to define some strict ordering of
274 /// the items; this order has nothing to do with the iteration
275 /// ordering of the items.
277 /// \bug This is a technical requirement. Do we really need this?
278 bool operator<(Edge) const { return false; }
281 /// This iterator goes trough the outgoing edges of a node.
283 /// This iterator goes trough the \e outgoing edges of a certain node
285 /// Its usage is quite simple, for example you can count the number
286 /// of outgoing edges of a node \c n
287 /// in graph \c g of type \c Graph as follows.
290 /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
293 class OutEdgeIt : public Edge {
295 /// Default constructor
297 /// @warning The default constructor sets the iterator
298 /// to an undefined value.
300 /// Copy constructor.
302 /// Copy constructor.
304 OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
305 /// Initialize the iterator to be invalid.
307 /// Initialize the iterator to be invalid.
309 OutEdgeIt(Invalid) { }
310 /// This constructor sets the iterator to the first outgoing edge.
312 /// This constructor sets the iterator to the first outgoing edge of
314 OutEdgeIt(const StaticGraph&, const Node&) { }
315 /// Edge -> OutEdgeIt conversion
317 /// Sets the iterator to the value of the trivial iterator.
318 /// This feature necessitates that each time we
319 /// iterate the edge-set, the iteration order is the same.
320 OutEdgeIt(const StaticGraph&, const Edge&) { }
321 ///Next outgoing edge
323 /// Assign the iterator to the next
324 /// outgoing edge of the corresponding node.
325 OutEdgeIt& operator++() { return *this; }
328 /// This iterator goes trough the incoming edges of a node.
330 /// This iterator goes trough the \e incoming edges of a certain node
332 /// Its usage is quite simple, for example you can count the number
333 /// of outgoing edges of a node \c n
334 /// in graph \c g of type \c Graph as follows.
337 /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
340 class InEdgeIt : public Edge {
342 /// Default constructor
344 /// @warning The default constructor sets the iterator
345 /// to an undefined value.
347 /// Copy constructor.
349 /// Copy constructor.
351 InEdgeIt(const InEdgeIt& e) : Edge(e) { }
352 /// Initialize the iterator to be invalid.
354 /// Initialize the iterator to be invalid.
356 InEdgeIt(Invalid) { }
357 /// This constructor sets the iterator to first incoming edge.
359 /// This constructor set the iterator to the first incoming edge of
361 InEdgeIt(const StaticGraph&, const Node&) { }
362 /// Edge -> InEdgeIt conversion
364 /// Sets the iterator to the value of the trivial iterator \c e.
365 /// This feature necessitates that each time we
366 /// iterate the edge-set, the iteration order is the same.
367 InEdgeIt(const StaticGraph&, const Edge&) { }
368 /// Next incoming edge
370 /// Assign the iterator to the next inedge of the corresponding node.
372 InEdgeIt& operator++() { return *this; }
374 /// This iterator goes through each edge.
376 /// This iterator goes through each edge of a graph.
377 /// Its usage is quite simple, for example you can count the number
378 /// of edges in a graph \c g of type \c Graph as follows:
381 /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
383 class EdgeIt : public Edge {
385 /// Default constructor
387 /// @warning The default constructor sets the iterator
388 /// to an undefined value.
390 /// Copy constructor.
392 /// Copy constructor.
394 EdgeIt(const EdgeIt& e) : Edge(e) { }
395 /// Initialize the iterator to be invalid.
397 /// Initialize the iterator to be invalid.
400 /// This constructor sets the iterator to the first edge.
402 /// This constructor sets the iterator to the first edge of \c g.
403 ///@param g the graph
404 EdgeIt(const StaticGraph& g) { ignore_unused_variable_warning(g); }
405 /// Edge -> EdgeIt conversion
407 /// Sets the iterator to the value of the trivial iterator \c e.
408 /// This feature necessitates that each time we
409 /// iterate the edge-set, the iteration order is the same.
410 EdgeIt(const StaticGraph&, const Edge&) { }
413 /// Assign the iterator to the next edge.
414 EdgeIt& operator++() { return *this; }
416 ///Gives back the target node of an edge.
418 ///Gives back the target node of an edge.
420 Node target(Edge) const { return INVALID; }
421 ///Gives back the source node of an edge.
423 ///Gives back the source node of an edge.
425 Node source(Edge) const { return INVALID; }
427 // /// Gives back the first Node in the iterating order.
429 // /// Gives back the first Node in the iterating order.
431 void first(Node&) const {}
433 // /// Gives back the next Node in the iterating order.
435 // /// Gives back the next Node in the iterating order.
437 void next(Node&) const {}
439 // /// Gives back the first Edge in the iterating order.
441 // /// Gives back the first Edge in the iterating order.
443 void first(Edge&) const {}
444 // /// Gives back the next Edge in the iterating order.
446 // /// Gives back the next Edge in the iterating order.
448 void next(Edge&) const {}
451 // /// Gives back the first of the Edges point to the given Node.
453 // /// Gives back the first of the Edges point to the given Node.
455 void firstIn(Edge&, const Node&) const {}
457 // /// Gives back the next of the Edges points to the given Node.
460 // /// Gives back the next of the Edges points to the given Node.
462 void nextIn(Edge&) const {}
464 // /// Gives back the first of the Edges start from the given Node.
466 // /// Gives back the first of the Edges start from the given Node.
468 void firstOut(Edge&, const Node&) const {}
470 // /// Gives back the next of the Edges start from the given Node.
472 // /// Gives back the next of the Edges start from the given Node.
474 void nextOut(Edge&) const {}
476 /// \brief The base node of the iterator.
478 /// Gives back the base node of the iterator.
479 /// It is always the target of the pointed edge.
480 Node baseNode(const InEdgeIt&) const { return INVALID; }
482 /// \brief The running node of the iterator.
484 /// Gives back the running node of the iterator.
485 /// It is always the source of the pointed edge.
486 Node runningNode(const InEdgeIt&) const { return INVALID; }
488 /// \brief The base node of the iterator.
490 /// Gives back the base node of the iterator.
491 /// It is always the source of the pointed edge.
492 Node baseNode(const OutEdgeIt&) const { return INVALID; }
494 /// \brief The running node of the iterator.
496 /// Gives back the running node of the iterator.
497 /// It is always the target of the pointed edge.
498 Node runningNode(const OutEdgeIt&) const { return INVALID; }
500 /// \brief The opposite node on the given edge.
502 /// Gives back the opposite node on the given edge.
503 Node oppositeNode(const Node&, const Edge&) const { return INVALID; }
505 /// \brief Read write map of the nodes to type \c T.
507 /// ReadWrite map of the nodes to type \c T.
509 /// \warning Making maps that can handle bool type (NodeMap<bool>)
510 /// needs some extra attention!
511 /// \todo Wrong documentation
513 class NodeMap : public ReadWriteMap< Node, T >
518 NodeMap(const StaticGraph&) { }
520 NodeMap(const StaticGraph&, T) { }
523 NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
524 ///Assignment operator
525 NodeMap& operator=(const NodeMap&) { return *this; }
526 // \todo fix this concept
529 /// \brief Read write map of the edges to type \c T.
531 /// Reference map of the edges to type \c T.
533 /// \warning Making maps that can handle bool type (EdgeMap<bool>)
534 /// needs some extra attention!
535 /// \todo Wrong documentation
537 class EdgeMap : public ReadWriteMap<Edge,T>
542 EdgeMap(const StaticGraph&) { }
544 EdgeMap(const StaticGraph&, T) { }
546 EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
547 ///Assignment operator
548 EdgeMap& operator=(const EdgeMap&) { return *this; }
549 // \todo fix this concept
552 template <typename _Graph>
553 struct Constraints : public _StaticGraph::Constraints<_Graph> {};
557 /// An empty non-static graph class.
559 /// This class provides everything that \ref StaticGraph does.
560 /// Additionally it enables building graphs from scratch.
561 class ExtendableGraph : public StaticGraph
564 /// Defalult constructor.
566 /// Defalult constructor.
568 ExtendableGraph() { }
569 ///Add a new node to the graph.
571 /// \return the new node.
573 Node addNode() { return INVALID; }
574 ///Add a new edge to the graph.
576 ///Add a new edge to the graph with source node \c s
577 ///and target node \c t.
578 ///\return the new edge.
579 Edge addEdge(Node, Node) { return INVALID; }
581 /// Resets the graph.
583 /// This function deletes all edges and nodes of the graph.
584 /// It also frees the memory allocated to store them.
585 /// \todo It might belong to \ref ErasableGraph.
588 template <typename _Graph>
589 struct Constraints : public _ExtendableGraph::Constraints<_Graph> {};
593 /// An empty erasable graph class.
595 /// This class is an extension of \ref ExtendableGraph. It makes it
596 /// possible to erase edges or nodes.
597 class ErasableGraph : public ExtendableGraph
600 /// Defalult constructor.
602 /// Defalult constructor.
607 /// Deletes node \c n node.
612 /// Deletes edge \c e edge.
616 template <typename _Graph>
617 struct Constraints : public _ErasableGraph::Constraints<_Graph> {};
622 } //namespace concept
627 #endif // LEMON_CONCEPT_GRAPH_H