COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/concept/graph.h @ 1622:9c98841eda96

Last change on this file since 1622:9c98841eda96 was 1622:9c98841eda96, checked in by Balazs Dezso, 19 years ago

Ordering in the graph concept.

File size: 20.6 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        /// Artificial ordering operator.
180       
181        /// To allow the use of graph descriptors as key type in std::map or
182        /// similar associative container we require this.
183        ///
184        /// \note This operator only have to define some strict ordering of
185        /// the items; this order has nothing to do with the iteration
186        /// ordering of the items.
187        ///
188        /// \bug This is a technical requirement. Do we really need this?
189        bool operator<(Node) const { return false; }
190
191      };
192   
193      /// This iterator goes through each node.
194
195      /// This iterator goes through each node.
196      /// Its usage is quite simple, for example you can count the number
197      /// of nodes in graph \c g of type \c Graph like this:
198      /// \code
199      /// int count=0;
200      /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
201      /// \endcode
202      class NodeIt : public Node {
203      public:
204        /// Default constructor
205
206        /// @warning The default constructor sets the iterator
207        /// to an undefined value.
208        NodeIt() { }
209        /// Copy constructor.
210       
211        /// Copy constructor.
212        ///
213        NodeIt(const NodeIt& n) : Node(n) { }
214        /// Invalid constructor \& conversion.
215
216        /// Initialize the iterator to be invalid.
217        /// \sa Invalid for more details.
218        NodeIt(Invalid) { }
219        /// Sets the iterator to the first node.
220
221        /// Sets the iterator to the first node of \c g.
222        ///
223        NodeIt(const StaticGraph&) { }
224        /// Node -> NodeIt conversion.
225
226        /// Sets the iterator to the node of \c the graph pointed by
227        /// the trivial iterator.
228        /// This feature necessitates that each time we
229        /// iterate the edge-set, the iteration order is the same.
230        NodeIt(const StaticGraph&, const Node&) { }
231        /// Next node.
232
233        /// Assign the iterator to the next node.
234        ///
235        NodeIt& operator++() { return *this; }
236      };
237   
238   
239      /// The base type of the edge iterators.
240
241      /// The base type of the edge iterators.
242      ///
243      class Edge {
244      public:
245        /// Default constructor
246
247        /// @warning The default constructor sets the iterator
248        /// to an undefined value.
249        Edge() { }
250        /// Copy constructor.
251
252        /// Copy constructor.
253        ///
254        Edge(const Edge&) { }
255        /// Initialize the iterator to be invalid.
256
257        /// Initialize the iterator to be invalid.
258        ///
259        Edge(Invalid) { }
260        /// Equality operator
261
262        /// Two iterators are equal if and only if they point to the
263        /// same object or both are invalid.
264        bool operator==(Edge) const { return true; }
265        /// Inequality operator
266
267        /// \sa operator==(Edge n)
268        ///
269        bool operator!=(Edge) const { return true; }
270
271        /// Artificial ordering operator.
272       
273        /// To allow the use of graph descriptors as key type in std::map or
274        /// similar associative container we require this.
275        ///
276        /// \note This operator only have to define some strict ordering of
277        /// the items; this order has nothing to do with the iteration
278        /// ordering of the items.
279        ///
280        /// \bug This is a technical requirement. Do we really need this?
281        bool operator<(Edge) const { return false; }
282      };
283   
284      /// This iterator goes trough the outgoing edges of a node.
285
286      /// This iterator goes trough the \e outgoing edges of a certain node
287      /// of a graph.
288      /// Its usage is quite simple, for example you can count the number
289      /// of outgoing edges of a node \c n
290      /// in graph \c g of type \c Graph as follows.
291      /// \code
292      /// int count=0;
293      /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
294      /// \endcode
295   
296      class OutEdgeIt : public Edge {
297      public:
298        /// Default constructor
299
300        /// @warning The default constructor sets the iterator
301        /// to an undefined value.
302        OutEdgeIt() { }
303        /// Copy constructor.
304
305        /// Copy constructor.
306        ///
307        OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
308        /// Initialize the iterator to be invalid.
309
310        /// Initialize the iterator to be invalid.
311        ///
312        OutEdgeIt(Invalid) { }
313        /// This constructor sets the iterator to the first outgoing edge.
314   
315        /// This constructor sets the iterator to the first outgoing edge of
316        /// the node.
317        ///@param n the node
318        ///@param g the graph
319        OutEdgeIt(const StaticGraph&, const Node&) { }
320        /// Edge -> OutEdgeIt conversion
321
322        /// Sets the iterator to the value of the trivial iterator.
323        /// This feature necessitates that each time we
324        /// iterate the edge-set, the iteration order is the same.
325        OutEdgeIt(const StaticGraph&, const Edge&) { }
326        ///Next outgoing edge
327       
328        /// Assign the iterator to the next
329        /// outgoing edge of the corresponding node.
330        OutEdgeIt& operator++() { return *this; }
331      };
332
333      /// This iterator goes trough the incoming edges of a node.
334
335      /// This iterator goes trough the \e incoming edges of a certain node
336      /// of a graph.
337      /// Its usage is quite simple, for example you can count the number
338      /// of outgoing edges of a node \c n
339      /// in graph \c g of type \c Graph as follows.
340      /// \code
341      /// int count=0;
342      /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
343      /// \endcode
344
345      class InEdgeIt : public Edge {
346      public:
347        /// Default constructor
348
349        /// @warning The default constructor sets the iterator
350        /// to an undefined value.
351        InEdgeIt() { }
352        /// Copy constructor.
353
354        /// Copy constructor.
355        ///
356        InEdgeIt(const InEdgeIt& e) : Edge(e) { }
357        /// Initialize the iterator to be invalid.
358
359        /// Initialize the iterator to be invalid.
360        ///
361        InEdgeIt(Invalid) { }
362        /// This constructor sets the iterator to first incoming edge.
363   
364        /// This constructor set the iterator to the first incoming edge of
365        /// the node.
366        ///@param n the node
367        ///@param g the graph
368        InEdgeIt(const StaticGraph&, const Node&) { }
369        /// Edge -> InEdgeIt conversion
370
371        /// Sets the iterator to the value of the trivial iterator \c e.
372        /// This feature necessitates that each time we
373        /// iterate the edge-set, the iteration order is the same.
374        InEdgeIt(const StaticGraph&, const Edge&) { }
375        /// Next incoming edge
376
377        /// Assign the iterator to the next inedge of the corresponding node.
378        ///
379        InEdgeIt& operator++() { return *this; }
380      };
381      /// This iterator goes through each edge.
382
383      /// This iterator goes through each edge of a graph.
384      /// Its usage is quite simple, for example you can count the number
385      /// of edges in a graph \c g of type \c Graph as follows:
386      /// \code
387      /// int count=0;
388      /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
389      /// \endcode
390      class EdgeIt : public Edge {
391      public:
392        /// Default constructor
393
394        /// @warning The default constructor sets the iterator
395        /// to an undefined value.
396        EdgeIt() { }
397        /// Copy constructor.
398
399        /// Copy constructor.
400        ///
401        EdgeIt(const EdgeIt& e) : Edge(e) { }
402        /// Initialize the iterator to be invalid.
403
404        /// Initialize the iterator to be invalid.
405        ///
406        EdgeIt(Invalid) { }
407        /// This constructor sets the iterator to the first edge.
408   
409        /// This constructor sets the iterator to the first edge of \c g.
410        ///@param g the graph
411        EdgeIt(const StaticGraph&) { }
412        /// Edge -> EdgeIt conversion
413
414        /// Sets the iterator to the value of the trivial iterator \c e.
415        /// This feature necessitates that each time we
416        /// iterate the edge-set, the iteration order is the same.
417        EdgeIt(const StaticGraph&, const Edge&) { }
418        ///Next edge
419       
420        /// Assign the iterator to the next edge.
421        EdgeIt& operator++() { return *this; }
422      };
423      ///Gives back the target node of an edge.
424
425      ///Gives back the target node of an edge.
426      ///
427      Node target(Edge) const { return INVALID; }
428      ///Gives back the source node of an edge.
429
430      ///Gives back the source node of an edge.
431      ///
432      Node source(Edge) const { return INVALID; }
433
434      /// Gives back the first Node in the iterating order.
435     
436      /// Gives back the first Node in the iterating order.
437      ///     
438      void first(Node&) const {}
439
440      /// Gives back the next Node in the iterating order.
441     
442      /// Gives back the next Node in the iterating order.
443      ///     
444      void next(Node&) const {}
445
446      /// Gives back the first Edge in the iterating order.
447     
448      /// Gives back the first Edge in the iterating order.
449      ///     
450      void first(Edge&) const {}
451      /// Gives back the next Edge in the iterating order.
452     
453      /// Gives back the next Edge in the iterating order.
454      ///     
455      void next(Edge&) const {}
456
457
458      /// Gives back the first of the Edges point to the given Node.
459     
460      /// Gives back the first of the Edges point to the given Node.
461      ///     
462      void firstIn(Edge&, const Node&) const {}
463
464      /// Gives back the next of the Edges points to the given Node.
465
466
467      /// Gives back the next of the Edges points to the given Node.
468      ///
469      void nextIn(Edge&) const {}
470
471      /// Gives back the first of the Edges start from the given Node.
472     
473      /// Gives back the first of the Edges start from the given Node.
474      ///     
475      void firstOut(Edge&, const Node&) const {}
476
477      /// Gives back the next of the Edges start from the given Node.
478     
479      /// Gives back the next of the Edges start from the given Node.
480      ///     
481      void nextOut(Edge&) const {}
482
483      /// \brief The base node of the iterator.
484      ///
485      /// Gives back the base node of the iterator.
486      Node baseNode(const InEdgeIt&) const { return INVALID; }
487
488      /// \brief The running node of the iterator.
489      ///
490      /// Gives back the running node of the iterator.
491      Node runningNode(const InEdgeIt&) const { return INVALID; }
492
493      /// \brief The base node of the iterator.
494      ///
495      /// Gives back the base node of the iterator.
496      Node baseNode(const OutEdgeIt&) const { return INVALID; }
497
498      /// \brief The running node of the iterator.
499      ///
500      /// Gives back the running node of the iterator.
501      Node runningNode(const OutEdgeIt&) const { return INVALID; }
502      /// Read write map of the nodes to type \c T.
503
504      /// \ingroup concept
505      /// ReadWrite map of the nodes to type \c T.
506      /// \sa Reference
507      /// \warning Making maps that can handle bool type (NodeMap<bool>)
508      /// needs some extra attention!
509      template<class T>
510      class NodeMap : public ReadWriteMap< Node, T >
511      {
512      public:
513
514        ///\e
515        NodeMap(const StaticGraph&) { }
516        ///\e
517        NodeMap(const StaticGraph&, T) { }
518
519        ///Copy constructor
520        NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
521        ///Assignment operator
522        NodeMap& operator=(const NodeMap&) { return *this; }
523        // \todo fix this concept
524      };
525
526      /// Read write map of the edges to type \c T.
527
528      /// \ingroup concept
529      ///Reference map of the edges to type \c T.
530      /// \sa Reference
531      /// \warning Making maps that can handle bool type (EdgeMap<bool>)
532      /// needs some extra attention!
533      template<class T>
534      class EdgeMap : public ReadWriteMap<Edge,T>
535      {
536      public:
537
538        ///\e
539        EdgeMap(const StaticGraph&) { }
540        ///\e
541        EdgeMap(const StaticGraph&, T) { }
542        ///Copy constructor
543        EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
544        ///Assignment operator
545        EdgeMap& operator=(const EdgeMap&) { return *this; }
546        // \todo fix this concept   
547      };
548
549      template <typename _Graph>
550      struct Constraints : public _StaticGraph::Constraints<_Graph> {};
551
552    };
553
554    /// An empty non-static graph class.
555   
556    /// This class provides everything that \ref StaticGraph does.
557    /// Additionally it enables building graphs from scratch.
558    class ExtendableGraph : public StaticGraph
559    {
560    public:
561      /// Defalult constructor.
562
563      /// Defalult constructor.
564      ///
565      ExtendableGraph() { }
566      ///Add a new node to the graph.
567
568      /// \return the new node.
569      ///
570      Node addNode() { return INVALID; }
571      ///Add a new edge to the graph.
572
573      ///Add a new edge to the graph with source node \c s
574      ///and target node \c t.
575      ///\return the new edge.
576      Edge addEdge(Node, Node) { return INVALID; }
577   
578      /// Resets the graph.
579
580      /// This function deletes all edges and nodes of the graph.
581      /// It also frees the memory allocated to store them.
582      /// \todo It might belong to \ref ErasableGraph.
583      void clear() { }
584
585      template <typename _Graph>
586      struct Constraints : public _ExtendableGraph::Constraints<_Graph> {};
587
588    };
589
590    /// An empty erasable graph class.
591 
592    /// This class is an extension of \ref ExtendableGraph. It makes it
593    /// possible to erase edges or nodes.
594    class ErasableGraph : public ExtendableGraph
595    {
596    public:
597      /// Defalult constructor.
598
599      /// Defalult constructor.
600      ///
601      ErasableGraph() { }
602      /// Deletes a node.
603
604      /// Deletes node \c n node.
605      ///
606      void erase(Node) { }
607      /// Deletes an edge.
608
609      /// Deletes edge \c e edge.
610      ///
611      void erase(Edge) { }
612
613      template <typename _Graph>
614      struct Constraints : public _ErasableGraph::Constraints<_Graph> {};
615
616    };
617
618   
619    /************* New GraphBase stuff **************/
620
621
622//     /// A minimal GraphBase concept
623
624//     /// This class describes a minimal concept which can be extended to a
625//     /// full-featured graph with \ref GraphFactory.
626//     class GraphBase {
627//     public:
628
629//       GraphBase() {}
630
631//       /// \bug Should we demand that Node and Edge be subclasses of the
632//       /// Graph class???
633
634//       typedef GraphItem<'n'> Node;
635//       typedef GraphItem<'e'> Edge;
636
637// //       class Node : public BaseGraphItem<'n'> {};
638// //       class Edge : public BaseGraphItem<'e'> {};
639
640//       // Graph operation
641//       void firstNode(Node &n) const { }
642//       void firstEdge(Edge &e) const { }
643
644//       void firstOutEdge(Edge &e, Node) const { }
645//       void firstInEdge(Edge &e, Node) const { }
646
647//       void nextNode(Node &n) const { }
648//       void nextEdge(Edge &e) const { }
649
650
651//       // Question: isn't it reasonable if this methods have a Node
652//       // parameter? Like this:
653//       // Edge& nextOut(Edge &e, Node) const { return e; }
654//       void nextOutEdge(Edge &e) const { }
655//       void nextInEdge(Edge &e) const { }
656
657//       Node target(Edge) const { return Node(); }
658//       Node source(Edge) const { return Node(); }
659     
660
661//       // Do we need id, nodeNum, edgeNum and co. in this basic graphbase
662//       // concept?
663
664
665//       // Maps.
666//       //
667//       // We need a special slimer concept which does not provide maps (it
668//       // wouldn't be strictly slimer, cause for map-factory id() & friends
669//       // a required...)
670
671//       template<typename T>
672//       class NodeMap : public GraphMap<GraphBase, Node, T> {};
673
674//       template<typename T>
675//       class EdgeMap : public GraphMap<GraphBase, Node, T> {};
676//     };
677
678    // @}
679  } //namespace concept 
680} //namespace lemon
681
682
683
684#endif // LEMON_CONCEPT_GRAPH_H
Note: See TracBrowser for help on using the repository browser.