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, 14 years ago

Ordering in the graph concept.

File size: 20.6 KB
RevLine 
[959]1/* -*- C++ -*-
[1435]2 * lemon/concept/graph.h - Part of LEMON, a generic C++ optimization library
[959]3 *
[1164]4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[959]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
[1030]20///\ingroup graph_concepts
[959]21///\file
22///\brief Declaration of Graph.
23
24#include <lemon/invalid.h>
[1448]25#include <lemon/utility.h>
[959]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 {
[1136]32
[959]33   
[961]34    /**************** The full-featured graph concepts ****************/
[959]35
[1136]36
[1426]37    /// \brief Modular static graph class.
[1136]38    ///     
39    /// It should be the same as the \c StaticGraph class.
40    class _StaticGraph
[961]41      :  virtual public BaseGraphComponent,
[1426]42         public IterableGraphComponent, public MappableGraphComponent {
[959]43    public:
[1448]44      ///\e
45
46      ///\todo undocumented
47      ///
48      typedef False UndirTag;
49     
[959]50      typedef BaseGraphComponent::Node Node;
51      typedef BaseGraphComponent::Edge Edge;
52
[989]53      template <typename _Graph>
54      struct Constraints {
[1426]55        void constraints() {
56          checkConcept<IterableGraphComponent, _Graph>();
57          checkConcept<MappableGraphComponent, _Graph>();
58        }
[989]59      };
[959]60    };
61
[1426]62    /// \brief Modular extendable graph class.
[1136]63    ///     
64    /// It should be the same as the \c ExtendableGraph class.
65    class _ExtendableGraph
66      :  virtual public BaseGraphComponent, public _StaticGraph,
[1426]67         public ExtendableGraphComponent, public ClearableGraphComponent {
[959]68    public:
69      typedef BaseGraphComponent::Node Node;
70      typedef BaseGraphComponent::Edge Edge;
71
[989]72      template <typename _Graph>
73      struct Constraints {
[1426]74        void constraints() {
75          checkConcept<_StaticGraph, _Graph >();
76          checkConcept<ExtendableGraphComponent, _Graph >();
77          checkConcept<ClearableGraphComponent, _Graph >();
78        }
[989]79      };
[959]80    };
81
[1426]82    /// \brief Modular erasable graph class.
[1136]83    ///     
84    /// It should be the same as the \c ErasableGraph class.
85    class _ErasableGraph
86      :  virtual public BaseGraphComponent, public _ExtendableGraph,
[1426]87         public ErasableGraphComponent {
[959]88    public:
89      typedef BaseGraphComponent::Node Node;
90      typedef BaseGraphComponent::Edge Edge;
91
[989]92      template <typename _Graph>
93      struct Constraints {
[1426]94        void constraints() {
95          checkConcept<_ExtendableGraph, _Graph >();
96          checkConcept<ErasableGraphComponent, _Graph >();
97        }
[989]98      };
[959]99    };
100
[1620]101    /// \addtogroup graph_concepts
102    /// @{
103
[1136]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:
[1448]125      ///\e
126
127      ///\todo undocumented
128      ///
129      typedef False UndirTag;
130
[1136]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:
[1426]151        /// Default constructor
[1136]152
[1426]153        /// @warning The default constructor sets the iterator
154        /// to an undefined value.
155        Node() { }
156        /// Copy constructor.
[1136]157
[1426]158        /// Copy constructor.
159        ///
160        Node(const Node&) { }
[1136]161
[1426]162        /// Invalid constructor \& conversion.
[1136]163
[1426]164        /// This constructor initializes the iterator to be invalid.
165        /// \sa Invalid for more details.
166        Node(Invalid) { }
167        /// Equality operator
[1136]168
[1426]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; }
[1136]172
[1426]173        /// Inequality operator
174       
175        /// \sa operator==(Node n)
176        ///
177        bool operator!=(Node) const { return true; }
[1136]178
[1622]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
[1136]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;
[1426]200      /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
[1136]201      /// \endcode
202      class NodeIt : public Node {
203      public:
[1426]204        /// Default constructor
[1136]205
[1426]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.
[1136]215
[1426]216        /// Initialize the iterator to be invalid.
217        /// \sa Invalid for more details.
218        NodeIt(Invalid) { }
219        /// Sets the iterator to the first node.
[1136]220
[1426]221        /// Sets the iterator to the first node of \c g.
222        ///
223        NodeIt(const StaticGraph&) { }
224        /// Node -> NodeIt conversion.
[1136]225
[1470]226        /// Sets the iterator to the node of \c the graph pointed by
227        /// the trivial iterator.
[1426]228        /// This feature necessitates that each time we
229        /// iterate the edge-set, the iteration order is the same.
[1470]230        NodeIt(const StaticGraph&, const Node&) { }
[1426]231        /// Next node.
[1136]232
[1426]233        /// Assign the iterator to the next node.
234        ///
235        NodeIt& operator++() { return *this; }
[1136]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:
[1426]245        /// Default constructor
[1136]246
[1426]247        /// @warning The default constructor sets the iterator
248        /// to an undefined value.
249        Edge() { }
250        /// Copy constructor.
[1136]251
[1426]252        /// Copy constructor.
253        ///
254        Edge(const Edge&) { }
255        /// Initialize the iterator to be invalid.
[1136]256
[1426]257        /// Initialize the iterator to be invalid.
258        ///
259        Edge(Invalid) { }
260        /// Equality operator
[1136]261
[1426]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
[1136]266
[1620]267        /// \sa operator==(Edge n)
[1426]268        ///
269        bool operator!=(Edge) const { return true; }
[1622]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; }
[1136]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:
[1426]298        /// Default constructor
[1136]299
[1426]300        /// @warning The default constructor sets the iterator
301        /// to an undefined value.
302        OutEdgeIt() { }
303        /// Copy constructor.
[1136]304
[1426]305        /// Copy constructor.
306        ///
307        OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
308        /// Initialize the iterator to be invalid.
[1136]309
[1426]310        /// Initialize the iterator to be invalid.
311        ///
312        OutEdgeIt(Invalid) { }
313        /// This constructor sets the iterator to the first outgoing edge.
[1136]314   
[1426]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
[1136]321
[1470]322        /// Sets the iterator to the value of the trivial iterator.
323        /// This feature necessitates that each time we
[1426]324        /// iterate the edge-set, the iteration order is the same.
[1470]325        OutEdgeIt(const StaticGraph&, const Edge&) { }
[1426]326        ///Next outgoing edge
327       
328        /// Assign the iterator to the next
329        /// outgoing edge of the corresponding node.
330        OutEdgeIt& operator++() { return *this; }
[1136]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:
[1426]347        /// Default constructor
[1136]348
[1426]349        /// @warning The default constructor sets the iterator
350        /// to an undefined value.
351        InEdgeIt() { }
352        /// Copy constructor.
[1136]353
[1426]354        /// Copy constructor.
355        ///
356        InEdgeIt(const InEdgeIt& e) : Edge(e) { }
357        /// Initialize the iterator to be invalid.
[1136]358
[1426]359        /// Initialize the iterator to be invalid.
360        ///
361        InEdgeIt(Invalid) { }
362        /// This constructor sets the iterator to first incoming edge.
[1136]363   
[1426]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
[1136]370
[1426]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
[1136]376
[1426]377        /// Assign the iterator to the next inedge of the corresponding node.
378        ///
379        InEdgeIt& operator++() { return *this; }
[1136]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:
[1426]392        /// Default constructor
[1136]393
[1426]394        /// @warning The default constructor sets the iterator
395        /// to an undefined value.
396        EdgeIt() { }
397        /// Copy constructor.
[1136]398
[1426]399        /// Copy constructor.
400        ///
401        EdgeIt(const EdgeIt& e) : Edge(e) { }
402        /// Initialize the iterator to be invalid.
[1136]403
[1426]404        /// Initialize the iterator to be invalid.
405        ///
406        EdgeIt(Invalid) { }
407        /// This constructor sets the iterator to the first edge.
[1136]408   
[1426]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
[1136]413
[1426]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; }
[1136]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; }
[1563]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; }
[1136]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
[1426]514        ///\e
515        NodeMap(const StaticGraph&) { }
516        ///\e
517        NodeMap(const StaticGraph&, T) { }
[1136]518
[1426]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
[1136]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
[1426]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   
[1136]547      };
548
549      template <typename _Graph>
550      struct Constraints : public _StaticGraph::Constraints<_Graph> {};
551
552    };
553
554    /// An empty non-static graph class.
555   
[1426]556    /// This class provides everything that \ref StaticGraph does.
557    /// Additionally it enables building graphs from scratch.
[1136]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.
[1367]576      Edge addEdge(Node, Node) { return INVALID; }
[1136]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 
[1426]592    /// This class is an extension of \ref ExtendableGraph. It makes it
[1136]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      ///
[1367]606      void erase(Node) { }
[1136]607      /// Deletes an edge.
608
609      /// Deletes edge \c e edge.
610      ///
[1367]611      void erase(Edge) { }
[1136]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
[959]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.