COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/concept/graph.h @ 1621:574f8a3f0971

Last change on this file since 1621:574f8a3f0971 was 1620:09feafe81053, checked in by Alpar Juttner, 14 years ago

Start working on UndirGraph? concept clarification and its harmonization with
the directed graph concept.
Not yet done!!!

File size: 19.8 KB
Line 
1/* -*- C++ -*-
2 * lemon/concept/graph.h - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
6 *
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.
10 *
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
13 * purpose.
14 *
15 */
16
17#ifndef LEMON_CONCEPT_GRAPH_H
18#define LEMON_CONCEPT_GRAPH_H
19
20///\ingroup graph_concepts
21///\file
22///\brief Declaration of Graph.
23
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>
29
30namespace lemon {
31  namespace concept {
32
33   
34    /**************** The full-featured graph concepts ****************/
35
36
37    /// \brief Modular static graph class.
38    ///     
39    /// It should be the same as the \c StaticGraph class.
40    class _StaticGraph
41      :  virtual public BaseGraphComponent,
42         public IterableGraphComponent, public MappableGraphComponent {
43    public:
44      ///\e
45
46      ///\todo undocumented
47      ///
48      typedef False UndirTag;
49     
50      typedef BaseGraphComponent::Node Node;
51      typedef BaseGraphComponent::Edge Edge;
52
53      template <typename _Graph>
54      struct Constraints {
55        void constraints() {
56          checkConcept<IterableGraphComponent, _Graph>();
57          checkConcept<MappableGraphComponent, _Graph>();
58        }
59      };
60    };
61
62    /// \brief Modular extendable graph class.
63    ///     
64    /// It should be the same as the \c ExtendableGraph class.
65    class _ExtendableGraph
66      :  virtual public BaseGraphComponent, public _StaticGraph,
67         public ExtendableGraphComponent, public ClearableGraphComponent {
68    public:
69      typedef BaseGraphComponent::Node Node;
70      typedef BaseGraphComponent::Edge Edge;
71
72      template <typename _Graph>
73      struct Constraints {
74        void constraints() {
75          checkConcept<_StaticGraph, _Graph >();
76          checkConcept<ExtendableGraphComponent, _Graph >();
77          checkConcept<ClearableGraphComponent, _Graph >();
78        }
79      };
80    };
81
82    /// \brief Modular erasable graph class.
83    ///     
84    /// It should be the same as the \c ErasableGraph class.
85    class _ErasableGraph
86      :  virtual public BaseGraphComponent, public _ExtendableGraph,
87         public ErasableGraphComponent {
88    public:
89      typedef BaseGraphComponent::Node Node;
90      typedef BaseGraphComponent::Edge Edge;
91
92      template <typename _Graph>
93      struct Constraints {
94        void constraints() {
95          checkConcept<_ExtendableGraph, _Graph >();
96          checkConcept<ErasableGraphComponent, _Graph >();
97        }
98      };
99    };
100
101    /// \addtogroup graph_concepts
102    /// @{
103
104    /// An empty static graph class.
105 
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.
111    ///
112    /// It can be used for checking the interface compatibility,
113    /// or it can serve as a skeleton of a new graph structure.
114    ///
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.
119    ///
120    /// \todo A pages describing the concept of concept description would
121    /// be nice.
122    class StaticGraph
123    {
124    public:
125      ///\e
126
127      ///\todo undocumented
128      ///
129      typedef False UndirTag;
130
131      /// Defalult constructor.
132
133      /// Defalult constructor.
134      ///
135      StaticGraph() { }
136      ///Copy consructor.
137
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) { }
141
142      /// The base type of node iterators,
143      /// or in other words, the trivial node iterator.
144
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.
149      class Node {
150      public:
151        /// Default constructor
152
153        /// @warning The default constructor sets the iterator
154        /// to an undefined value.
155        Node() { }
156        /// Copy constructor.
157
158        /// Copy constructor.
159        ///
160        Node(const Node&) { }
161
162        /// Invalid constructor \& conversion.
163
164        /// This constructor initializes the iterator to be invalid.
165        /// \sa Invalid for more details.
166        Node(Invalid) { }
167        /// Equality operator
168
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; }
172
173        /// Inequality operator
174       
175        /// \sa operator==(Node n)
176        ///
177        bool operator!=(Node) const { return true; }
178
179      };
180   
181      /// This iterator goes through each node.
182
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:
186      /// \code
187      /// int count=0;
188      /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
189      /// \endcode
190      class NodeIt : public Node {
191      public:
192        /// Default constructor
193
194        /// @warning The default constructor sets the iterator
195        /// to an undefined value.
196        NodeIt() { }
197        /// Copy constructor.
198       
199        /// Copy constructor.
200        ///
201        NodeIt(const NodeIt& n) : Node(n) { }
202        /// Invalid constructor \& conversion.
203
204        /// Initialize the iterator to be invalid.
205        /// \sa Invalid for more details.
206        NodeIt(Invalid) { }
207        /// Sets the iterator to the first node.
208
209        /// Sets the iterator to the first node of \c g.
210        ///
211        NodeIt(const StaticGraph&) { }
212        /// Node -> NodeIt conversion.
213
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&) { }
219        /// Next node.
220
221        /// Assign the iterator to the next node.
222        ///
223        NodeIt& operator++() { return *this; }
224      };
225   
226   
227      /// The base type of the edge iterators.
228
229      /// The base type of the edge iterators.
230      ///
231      class Edge {
232      public:
233        /// Default constructor
234
235        /// @warning The default constructor sets the iterator
236        /// to an undefined value.
237        Edge() { }
238        /// Copy constructor.
239
240        /// Copy constructor.
241        ///
242        Edge(const Edge&) { }
243        /// Initialize the iterator to be invalid.
244
245        /// Initialize the iterator to be invalid.
246        ///
247        Edge(Invalid) { }
248        /// Equality operator
249
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
254
255        /// \sa operator==(Edge n)
256        ///
257        bool operator!=(Edge) const { return true; }
258      };
259   
260      /// This iterator goes trough the outgoing edges of a node.
261
262      /// This iterator goes trough the \e outgoing edges of a certain node
263      /// of a graph.
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.
267      /// \code
268      /// int count=0;
269      /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
270      /// \endcode
271   
272      class OutEdgeIt : public Edge {
273      public:
274        /// Default constructor
275
276        /// @warning The default constructor sets the iterator
277        /// to an undefined value.
278        OutEdgeIt() { }
279        /// Copy constructor.
280
281        /// Copy constructor.
282        ///
283        OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
284        /// Initialize the iterator to be invalid.
285
286        /// Initialize the iterator to be invalid.
287        ///
288        OutEdgeIt(Invalid) { }
289        /// This constructor sets the iterator to the first outgoing edge.
290   
291        /// This constructor sets the iterator to the first outgoing edge of
292        /// the node.
293        ///@param n the node
294        ///@param g the graph
295        OutEdgeIt(const StaticGraph&, const Node&) { }
296        /// Edge -> OutEdgeIt conversion
297
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
303       
304        /// Assign the iterator to the next
305        /// outgoing edge of the corresponding node.
306        OutEdgeIt& operator++() { return *this; }
307      };
308
309      /// This iterator goes trough the incoming edges of a node.
310
311      /// This iterator goes trough the \e incoming edges of a certain node
312      /// of a graph.
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.
316      /// \code
317      /// int count=0;
318      /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
319      /// \endcode
320
321      class InEdgeIt : public Edge {
322      public:
323        /// Default constructor
324
325        /// @warning The default constructor sets the iterator
326        /// to an undefined value.
327        InEdgeIt() { }
328        /// Copy constructor.
329
330        /// Copy constructor.
331        ///
332        InEdgeIt(const InEdgeIt& e) : Edge(e) { }
333        /// Initialize the iterator to be invalid.
334
335        /// Initialize the iterator to be invalid.
336        ///
337        InEdgeIt(Invalid) { }
338        /// This constructor sets the iterator to first incoming edge.
339   
340        /// This constructor set the iterator to the first incoming edge of
341        /// the node.
342        ///@param n the node
343        ///@param g the graph
344        InEdgeIt(const StaticGraph&, const Node&) { }
345        /// Edge -> InEdgeIt conversion
346
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
352
353        /// Assign the iterator to the next inedge of the corresponding node.
354        ///
355        InEdgeIt& operator++() { return *this; }
356      };
357      /// This iterator goes through each edge.
358
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:
362      /// \code
363      /// int count=0;
364      /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
365      /// \endcode
366      class EdgeIt : public Edge {
367      public:
368        /// Default constructor
369
370        /// @warning The default constructor sets the iterator
371        /// to an undefined value.
372        EdgeIt() { }
373        /// Copy constructor.
374
375        /// Copy constructor.
376        ///
377        EdgeIt(const EdgeIt& e) : Edge(e) { }
378        /// Initialize the iterator to be invalid.
379
380        /// Initialize the iterator to be invalid.
381        ///
382        EdgeIt(Invalid) { }
383        /// This constructor sets the iterator to the first edge.
384   
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
389
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&) { }
394        ///Next edge
395       
396        /// Assign the iterator to the next edge.
397        EdgeIt& operator++() { return *this; }
398      };
399      ///Gives back the target node of an edge.
400
401      ///Gives back the target node of an edge.
402      ///
403      Node target(Edge) const { return INVALID; }
404      ///Gives back the source node of an edge.
405
406      ///Gives back the source node of an edge.
407      ///
408      Node source(Edge) const { return INVALID; }
409
410      /// Gives back the first Node in the iterating order.
411     
412      /// Gives back the first Node in the iterating order.
413      ///     
414      void first(Node&) const {}
415
416      /// Gives back the next Node in the iterating order.
417     
418      /// Gives back the next Node in the iterating order.
419      ///     
420      void next(Node&) const {}
421
422      /// Gives back the first Edge in the iterating order.
423     
424      /// Gives back the first Edge in the iterating order.
425      ///     
426      void first(Edge&) const {}
427      /// Gives back the next Edge in the iterating order.
428     
429      /// Gives back the next Edge in the iterating order.
430      ///     
431      void next(Edge&) const {}
432
433
434      /// Gives back the first of the Edges point to the given Node.
435     
436      /// Gives back the first of the Edges point to the given Node.
437      ///     
438      void firstIn(Edge&, const Node&) const {}
439
440      /// Gives back the next of the Edges points to the given Node.
441
442
443      /// Gives back the next of the Edges points to the given Node.
444      ///
445      void nextIn(Edge&) const {}
446
447      /// Gives back the first of the Edges start from the given Node.
448     
449      /// Gives back the first of the Edges start from the given Node.
450      ///     
451      void firstOut(Edge&, const Node&) const {}
452
453      /// Gives back the next of the Edges start from the given Node.
454     
455      /// Gives back the next of the Edges start from the given Node.
456      ///     
457      void nextOut(Edge&) const {}
458
459      /// \brief The base node of the iterator.
460      ///
461      /// Gives back the base node of the iterator.
462      Node baseNode(const InEdgeIt&) const { return INVALID; }
463
464      /// \brief The running node of the iterator.
465      ///
466      /// Gives back the running node of the iterator.
467      Node runningNode(const InEdgeIt&) const { return INVALID; }
468
469      /// \brief The base node of the iterator.
470      ///
471      /// Gives back the base node of the iterator.
472      Node baseNode(const OutEdgeIt&) const { return INVALID; }
473
474      /// \brief The running node of the iterator.
475      ///
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.
479
480      /// \ingroup concept
481      /// ReadWrite map of the nodes to type \c T.
482      /// \sa Reference
483      /// \warning Making maps that can handle bool type (NodeMap<bool>)
484      /// needs some extra attention!
485      template<class T>
486      class NodeMap : public ReadWriteMap< Node, T >
487      {
488      public:
489
490        ///\e
491        NodeMap(const StaticGraph&) { }
492        ///\e
493        NodeMap(const StaticGraph&, T) { }
494
495        ///Copy constructor
496        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
497        ///Assignment operator
498        NodeMap& operator=(const NodeMap&) { return *this; }
499        // \todo fix this concept
500      };
501
502      /// Read write map of the edges to type \c T.
503
504      /// \ingroup concept
505      ///Reference map of the edges to type \c T.
506      /// \sa Reference
507      /// \warning Making maps that can handle bool type (EdgeMap<bool>)
508      /// needs some extra attention!
509      template<class T>
510      class EdgeMap : public ReadWriteMap<Edge,T>
511      {
512      public:
513
514        ///\e
515        EdgeMap(const StaticGraph&) { }
516        ///\e
517        EdgeMap(const StaticGraph&, T) { }
518        ///Copy constructor
519        EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
520        ///Assignment operator
521        EdgeMap& operator=(const EdgeMap&) { return *this; }
522        // \todo fix this concept   
523      };
524
525      template <typename _Graph>
526      struct Constraints : public _StaticGraph::Constraints<_Graph> {};
527
528    };
529
530    /// An empty non-static graph class.
531   
532    /// This class provides everything that \ref StaticGraph does.
533    /// Additionally it enables building graphs from scratch.
534    class ExtendableGraph : public StaticGraph
535    {
536    public:
537      /// Defalult constructor.
538
539      /// Defalult constructor.
540      ///
541      ExtendableGraph() { }
542      ///Add a new node to the graph.
543
544      /// \return the new node.
545      ///
546      Node addNode() { return INVALID; }
547      ///Add a new edge to the graph.
548
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; }
553   
554      /// Resets the graph.
555
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.
559      void clear() { }
560
561      template <typename _Graph>
562      struct Constraints : public _ExtendableGraph::Constraints<_Graph> {};
563
564    };
565
566    /// An empty erasable graph class.
567 
568    /// This class is an extension of \ref ExtendableGraph. It makes it
569    /// possible to erase edges or nodes.
570    class ErasableGraph : public ExtendableGraph
571    {
572    public:
573      /// Defalult constructor.
574
575      /// Defalult constructor.
576      ///
577      ErasableGraph() { }
578      /// Deletes a node.
579
580      /// Deletes node \c n node.
581      ///
582      void erase(Node) { }
583      /// Deletes an edge.
584
585      /// Deletes edge \c e edge.
586      ///
587      void erase(Edge) { }
588
589      template <typename _Graph>
590      struct Constraints : public _ErasableGraph::Constraints<_Graph> {};
591
592    };
593
594   
595    /************* New GraphBase stuff **************/
596
597
598//     /// A minimal GraphBase concept
599
600//     /// This class describes a minimal concept which can be extended to a
601//     /// full-featured graph with \ref GraphFactory.
602//     class GraphBase {
603//     public:
604
605//       GraphBase() {}
606
607//       /// \bug Should we demand that Node and Edge be subclasses of the
608//       /// Graph class???
609
610//       typedef GraphItem<'n'> Node;
611//       typedef GraphItem<'e'> Edge;
612
613// //       class Node : public BaseGraphItem<'n'> {};
614// //       class Edge : public BaseGraphItem<'e'> {};
615
616//       // Graph operation
617//       void firstNode(Node &n) const { }
618//       void firstEdge(Edge &e) const { }
619
620//       void firstOutEdge(Edge &e, Node) const { }
621//       void firstInEdge(Edge &e, Node) const { }
622
623//       void nextNode(Node &n) const { }
624//       void nextEdge(Edge &e) const { }
625
626
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 { }
632
633//       Node target(Edge) const { return Node(); }
634//       Node source(Edge) const { return Node(); }
635     
636
637//       // Do we need id, nodeNum, edgeNum and co. in this basic graphbase
638//       // concept?
639
640
641//       // Maps.
642//       //
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
645//       // a required...)
646
647//       template<typename T>
648//       class NodeMap : public GraphMap<GraphBase, Node, T> {};
649
650//       template<typename T>
651//       class EdgeMap : public GraphMap<GraphBase, Node, T> {};
652//     };
653
654    // @}
655  } //namespace concept 
656} //namespace lemon
657
658
659
660#endif // LEMON_CONCEPT_GRAPH_H
Note: See TracBrowser for help on using the repository browser.