src/lemon/concept/graph.h
changeset 1435 8e85e6bbefdf
parent 1367 a490662291b9
equal deleted inserted replaced
10:f78244d75571 -1:000000000000
     1 /* -*- C++ -*-
       
     2  * src/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/concept/maps.h>
       
    26 #include <lemon/concept_check.h>
       
    27 #include <lemon/concept/graph_component.h>
       
    28 
       
    29 namespace lemon {
       
    30   namespace concept {
       
    31 
       
    32     
       
    33     /// \addtogroup graph_concepts
       
    34     /// @{
       
    35 
       
    36     /**************** The full-featured graph concepts ****************/
       
    37 
       
    38 
       
    39     /// \brief Modular static graph class.
       
    40     ///     
       
    41     /// It should be the same as the \c StaticGraph class.
       
    42     class _StaticGraph 
       
    43       :  virtual public BaseGraphComponent,
       
    44          public IterableGraphComponent, public MappableGraphComponent {
       
    45     public:
       
    46       typedef BaseGraphComponent::Node Node;
       
    47       typedef BaseGraphComponent::Edge Edge;
       
    48 
       
    49       template <typename _Graph>
       
    50       struct Constraints {
       
    51         void constraints() {
       
    52           checkConcept<IterableGraphComponent, _Graph>();
       
    53           checkConcept<MappableGraphComponent, _Graph>();
       
    54         }
       
    55       };
       
    56     };
       
    57 
       
    58     /// \brief Modular extendable graph class.
       
    59     ///     
       
    60     /// It should be the same as the \c ExtendableGraph class.
       
    61     class _ExtendableGraph 
       
    62       :  virtual public BaseGraphComponent, public _StaticGraph,
       
    63          public ExtendableGraphComponent, public ClearableGraphComponent {
       
    64     public:
       
    65       typedef BaseGraphComponent::Node Node;
       
    66       typedef BaseGraphComponent::Edge Edge;
       
    67 
       
    68       template <typename _Graph>
       
    69       struct Constraints {
       
    70         void constraints() {
       
    71           checkConcept<_StaticGraph, _Graph >();
       
    72           checkConcept<ExtendableGraphComponent, _Graph >();
       
    73           checkConcept<ClearableGraphComponent, _Graph >();
       
    74         }
       
    75       };
       
    76     };
       
    77 
       
    78     /// \brief Modular erasable graph class.
       
    79     ///     
       
    80     /// It should be the same as the \c ErasableGraph class.
       
    81     class _ErasableGraph 
       
    82       :  virtual public BaseGraphComponent, public _ExtendableGraph,
       
    83          public ErasableGraphComponent {
       
    84     public:
       
    85       typedef BaseGraphComponent::Node Node;
       
    86       typedef BaseGraphComponent::Edge Edge;
       
    87 
       
    88       template <typename _Graph>
       
    89       struct Constraints {
       
    90         void constraints() {
       
    91           checkConcept<_ExtendableGraph, _Graph >();
       
    92           checkConcept<ErasableGraphComponent, _Graph >();
       
    93         }
       
    94       };
       
    95     };
       
    96 
       
    97     /// An empty static graph class.
       
    98   
       
    99     /// This class provides all the common features of a graph structure,
       
   100     /// however completely without implementations and real data structures
       
   101     /// behind the interface.
       
   102     /// All graph algorithms should compile with this class, but it will not
       
   103     /// run properly, of course.
       
   104     ///
       
   105     /// It can be used for checking the interface compatibility,
       
   106     /// or it can serve as a skeleton of a new graph structure.
       
   107     /// 
       
   108     /// Also, you will find here the full documentation of a certain graph
       
   109     /// feature, the documentation of a real graph imlementation
       
   110     /// like @ref ListGraph or
       
   111     /// @ref SmartGraph will just refer to this structure.
       
   112     ///
       
   113     /// \todo A pages describing the concept of concept description would
       
   114     /// be nice.
       
   115     class StaticGraph
       
   116     {
       
   117     public:
       
   118       /// Defalult constructor.
       
   119 
       
   120       /// Defalult constructor.
       
   121       ///
       
   122       StaticGraph() { }
       
   123       ///Copy consructor.
       
   124 
       
   125 //       ///\todo It is not clear, what we expect from a copy constructor.
       
   126 //       ///E.g. How to assign the nodes/edges to each other? What about maps?
       
   127 //       StaticGraph(const StaticGraph& g) { }
       
   128 
       
   129       /// The base type of node iterators, 
       
   130       /// or in other words, the trivial node iterator.
       
   131 
       
   132       /// This is the base type of each node iterator,
       
   133       /// thus each kind of node iterator converts to this.
       
   134       /// More precisely each kind of node iterator should be inherited 
       
   135       /// from the trivial node iterator.
       
   136       class Node {
       
   137       public:
       
   138         /// Default constructor
       
   139 
       
   140         /// @warning The default constructor sets the iterator
       
   141         /// to an undefined value.
       
   142         Node() { }
       
   143         /// Copy constructor.
       
   144 
       
   145         /// Copy constructor.
       
   146         ///
       
   147         Node(const Node&) { }
       
   148 
       
   149         /// Invalid constructor \& conversion.
       
   150 
       
   151         /// This constructor initializes the iterator to be invalid.
       
   152         /// \sa Invalid for more details.
       
   153         Node(Invalid) { }
       
   154         /// Equality operator
       
   155 
       
   156         /// Two iterators are equal if and only if they point to the
       
   157         /// same object or both are invalid.
       
   158         bool operator==(Node) const { return true; }
       
   159 
       
   160         /// Inequality operator
       
   161         
       
   162         /// \sa operator==(Node n)
       
   163         ///
       
   164         bool operator!=(Node) const { return true; }
       
   165 
       
   166       };
       
   167     
       
   168       /// This iterator goes through each node.
       
   169 
       
   170       /// This iterator goes through each node.
       
   171       /// Its usage is quite simple, for example you can count the number
       
   172       /// of nodes in graph \c g of type \c Graph like this:
       
   173       /// \code
       
   174       /// int count=0;
       
   175       /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
       
   176       /// \endcode
       
   177       class NodeIt : public Node {
       
   178       public:
       
   179         /// Default constructor
       
   180 
       
   181         /// @warning The default constructor sets the iterator
       
   182         /// to an undefined value.
       
   183         NodeIt() { }
       
   184         /// Copy constructor.
       
   185         
       
   186         /// Copy constructor.
       
   187         ///
       
   188         NodeIt(const NodeIt& n) : Node(n) { }
       
   189         /// Invalid constructor \& conversion.
       
   190 
       
   191         /// Initialize the iterator to be invalid.
       
   192         /// \sa Invalid for more details.
       
   193         NodeIt(Invalid) { }
       
   194         /// Sets the iterator to the first node.
       
   195 
       
   196         /// Sets the iterator to the first node of \c g.
       
   197         ///
       
   198         NodeIt(const StaticGraph&) { }
       
   199         /// Node -> NodeIt conversion.
       
   200 
       
   201         /// Sets the iterator to the node of \c g pointed by the trivial 
       
   202         /// iterator n.
       
   203         /// This feature necessitates that each time we 
       
   204         /// iterate the edge-set, the iteration order is the same.
       
   205         NodeIt(const StaticGraph& g, const Node& n) { }
       
   206         /// Next node.
       
   207 
       
   208         /// Assign the iterator to the next node.
       
   209         ///
       
   210         NodeIt& operator++() { return *this; }
       
   211       };
       
   212     
       
   213     
       
   214       /// The base type of the edge iterators.
       
   215 
       
   216       /// The base type of the edge iterators.
       
   217       ///
       
   218       class Edge {
       
   219       public:
       
   220         /// Default constructor
       
   221 
       
   222         /// @warning The default constructor sets the iterator
       
   223         /// to an undefined value.
       
   224         Edge() { }
       
   225         /// Copy constructor.
       
   226 
       
   227         /// Copy constructor.
       
   228         ///
       
   229         Edge(const Edge&) { }
       
   230         /// Initialize the iterator to be invalid.
       
   231 
       
   232         /// Initialize the iterator to be invalid.
       
   233         ///
       
   234         Edge(Invalid) { }
       
   235         /// Equality operator
       
   236 
       
   237         /// Two iterators are equal if and only if they point to the
       
   238         /// same object or both are invalid.
       
   239         bool operator==(Edge) const { return true; }
       
   240         /// Inequality operator
       
   241 
       
   242         /// \sa operator==(Node n)
       
   243         ///
       
   244         bool operator!=(Edge) const { return true; }
       
   245       };
       
   246     
       
   247       /// This iterator goes trough the outgoing edges of a node.
       
   248 
       
   249       /// This iterator goes trough the \e outgoing edges of a certain node
       
   250       /// of a graph.
       
   251       /// Its usage is quite simple, for example you can count the number
       
   252       /// of outgoing edges of a node \c n
       
   253       /// in graph \c g of type \c Graph as follows.
       
   254       /// \code
       
   255       /// int count=0;
       
   256       /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
       
   257       /// \endcode
       
   258     
       
   259       class OutEdgeIt : public Edge {
       
   260       public:
       
   261         /// Default constructor
       
   262 
       
   263         /// @warning The default constructor sets the iterator
       
   264         /// to an undefined value.
       
   265         OutEdgeIt() { }
       
   266         /// Copy constructor.
       
   267 
       
   268         /// Copy constructor.
       
   269         ///
       
   270         OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
       
   271         /// Initialize the iterator to be invalid.
       
   272 
       
   273         /// Initialize the iterator to be invalid.
       
   274         ///
       
   275         OutEdgeIt(Invalid) { }
       
   276         /// This constructor sets the iterator to the first outgoing edge.
       
   277     
       
   278         /// This constructor sets the iterator to the first outgoing edge of
       
   279         /// the node.
       
   280         ///@param n the node
       
   281         ///@param g the graph
       
   282         OutEdgeIt(const StaticGraph&, const Node&) { }
       
   283         /// Edge -> OutEdgeIt conversion
       
   284 
       
   285         /// Sets the iterator to the value of the trivial iterator \c e.
       
   286         /// This feature necessitates that each time we 
       
   287         /// iterate the edge-set, the iteration order is the same.
       
   288         OutEdgeIt(const StaticGraph& g, const Edge& e) { }
       
   289         ///Next outgoing edge
       
   290         
       
   291         /// Assign the iterator to the next 
       
   292         /// outgoing edge of the corresponding node.
       
   293         OutEdgeIt& operator++() { return *this; }
       
   294       };
       
   295 
       
   296       /// This iterator goes trough the incoming edges of a node.
       
   297 
       
   298       /// This iterator goes trough the \e incoming edges of a certain node
       
   299       /// of a graph.
       
   300       /// Its usage is quite simple, for example you can count the number
       
   301       /// of outgoing edges of a node \c n
       
   302       /// in graph \c g of type \c Graph as follows.
       
   303       /// \code
       
   304       /// int count=0;
       
   305       /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
       
   306       /// \endcode
       
   307 
       
   308       class InEdgeIt : public Edge {
       
   309       public:
       
   310         /// Default constructor
       
   311 
       
   312         /// @warning The default constructor sets the iterator
       
   313         /// to an undefined value.
       
   314         InEdgeIt() { }
       
   315         /// Copy constructor.
       
   316 
       
   317         /// Copy constructor.
       
   318         ///
       
   319         InEdgeIt(const InEdgeIt& e) : Edge(e) { }
       
   320         /// Initialize the iterator to be invalid.
       
   321 
       
   322         /// Initialize the iterator to be invalid.
       
   323         ///
       
   324         InEdgeIt(Invalid) { }
       
   325         /// This constructor sets the iterator to first incoming edge.
       
   326     
       
   327         /// This constructor set the iterator to the first incoming edge of
       
   328         /// the node.
       
   329         ///@param n the node
       
   330         ///@param g the graph
       
   331         InEdgeIt(const StaticGraph&, const Node&) { }
       
   332         /// Edge -> InEdgeIt conversion
       
   333 
       
   334         /// Sets the iterator to the value of the trivial iterator \c e.
       
   335         /// This feature necessitates that each time we 
       
   336         /// iterate the edge-set, the iteration order is the same.
       
   337         InEdgeIt(const StaticGraph&, const Edge&) { }
       
   338         /// Next incoming edge
       
   339 
       
   340         /// Assign the iterator to the next inedge of the corresponding node.
       
   341         ///
       
   342         InEdgeIt& operator++() { return *this; }
       
   343       };
       
   344       /// This iterator goes through each edge.
       
   345 
       
   346       /// This iterator goes through each edge of a graph.
       
   347       /// Its usage is quite simple, for example you can count the number
       
   348       /// of edges in a graph \c g of type \c Graph as follows:
       
   349       /// \code
       
   350       /// int count=0;
       
   351       /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
       
   352       /// \endcode
       
   353       class EdgeIt : public Edge {
       
   354       public:
       
   355         /// Default constructor
       
   356 
       
   357         /// @warning The default constructor sets the iterator
       
   358         /// to an undefined value.
       
   359         EdgeIt() { }
       
   360         /// Copy constructor.
       
   361 
       
   362         /// Copy constructor.
       
   363         ///
       
   364         EdgeIt(const EdgeIt& e) : Edge(e) { }
       
   365         /// Initialize the iterator to be invalid.
       
   366 
       
   367         /// Initialize the iterator to be invalid.
       
   368         ///
       
   369         EdgeIt(Invalid) { }
       
   370         /// This constructor sets the iterator to the first edge.
       
   371     
       
   372         /// This constructor sets the iterator to the first edge of \c g.
       
   373         ///@param g the graph
       
   374         EdgeIt(const StaticGraph&) { }
       
   375         /// Edge -> EdgeIt conversion
       
   376 
       
   377         /// Sets the iterator to the value of the trivial iterator \c e.
       
   378         /// This feature necessitates that each time we 
       
   379         /// iterate the edge-set, the iteration order is the same.
       
   380         EdgeIt(const StaticGraph&, const Edge&) { } 
       
   381         ///Next edge
       
   382         
       
   383         /// Assign the iterator to the next edge.
       
   384         EdgeIt& operator++() { return *this; }
       
   385       };
       
   386       ///Gives back the target node of an edge.
       
   387 
       
   388       ///Gives back the target node of an edge.
       
   389       ///
       
   390       Node target(Edge) const { return INVALID; }
       
   391       ///Gives back the source node of an edge.
       
   392 
       
   393       ///Gives back the source node of an edge.
       
   394       ///
       
   395       Node source(Edge) const { return INVALID; }
       
   396       /// Read write map of the nodes to type \c T.
       
   397 
       
   398       /// \ingroup concept
       
   399       /// ReadWrite map of the nodes to type \c T.
       
   400       /// \sa Reference
       
   401       /// \warning Making maps that can handle bool type (NodeMap<bool>)
       
   402       /// needs some extra attention!
       
   403       template<class T> 
       
   404       class NodeMap : public ReadWriteMap< Node, T >
       
   405       {
       
   406       public:
       
   407 
       
   408         ///\e
       
   409         NodeMap(const StaticGraph&) { }
       
   410         ///\e
       
   411         NodeMap(const StaticGraph&, T) { }
       
   412 
       
   413         ///Copy constructor
       
   414         NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
       
   415         ///Assignment operator
       
   416         NodeMap& operator=(const NodeMap&) { return *this; }
       
   417         // \todo fix this concept
       
   418       };
       
   419 
       
   420       /// Read write map of the edges to type \c T.
       
   421 
       
   422       /// \ingroup concept
       
   423       ///Reference map of the edges to type \c T.
       
   424       /// \sa Reference
       
   425       /// \warning Making maps that can handle bool type (EdgeMap<bool>)
       
   426       /// needs some extra attention!
       
   427       template<class T> 
       
   428       class EdgeMap : public ReadWriteMap<Edge,T>
       
   429       {
       
   430       public:
       
   431 
       
   432         ///\e
       
   433         EdgeMap(const StaticGraph&) { }
       
   434         ///\e
       
   435         EdgeMap(const StaticGraph&, T) { }
       
   436         ///Copy constructor
       
   437         EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
       
   438         ///Assignment operator
       
   439         EdgeMap& operator=(const EdgeMap&) { return *this; }
       
   440         // \todo fix this concept    
       
   441       };
       
   442 
       
   443       template <typename _Graph>
       
   444       struct Constraints : public _StaticGraph::Constraints<_Graph> {};
       
   445 
       
   446     };
       
   447 
       
   448     /// An empty non-static graph class.
       
   449     
       
   450     /// This class provides everything that \ref StaticGraph does.
       
   451     /// Additionally it enables building graphs from scratch.
       
   452     class ExtendableGraph : public StaticGraph
       
   453     {
       
   454     public:
       
   455       /// Defalult constructor.
       
   456 
       
   457       /// Defalult constructor.
       
   458       ///
       
   459       ExtendableGraph() { }
       
   460       ///Add a new node to the graph.
       
   461 
       
   462       /// \return the new node.
       
   463       ///
       
   464       Node addNode() { return INVALID; }
       
   465       ///Add a new edge to the graph.
       
   466 
       
   467       ///Add a new edge to the graph with source node \c s
       
   468       ///and target node \c t.
       
   469       ///\return the new edge.
       
   470       Edge addEdge(Node, Node) { return INVALID; }
       
   471     
       
   472       /// Resets the graph.
       
   473 
       
   474       /// This function deletes all edges and nodes of the graph.
       
   475       /// It also frees the memory allocated to store them.
       
   476       /// \todo It might belong to \ref ErasableGraph.
       
   477       void clear() { }
       
   478 
       
   479       template <typename _Graph>
       
   480       struct Constraints : public _ExtendableGraph::Constraints<_Graph> {};
       
   481 
       
   482     };
       
   483 
       
   484     /// An empty erasable graph class.
       
   485   
       
   486     /// This class is an extension of \ref ExtendableGraph. It makes it
       
   487     /// possible to erase edges or nodes.
       
   488     class ErasableGraph : public ExtendableGraph
       
   489     {
       
   490     public:
       
   491       /// Defalult constructor.
       
   492 
       
   493       /// Defalult constructor.
       
   494       ///
       
   495       ErasableGraph() { }
       
   496       /// Deletes a node.
       
   497 
       
   498       /// Deletes node \c n node.
       
   499       ///
       
   500       void erase(Node) { }
       
   501       /// Deletes an edge.
       
   502 
       
   503       /// Deletes edge \c e edge.
       
   504       ///
       
   505       void erase(Edge) { }
       
   506 
       
   507       template <typename _Graph>
       
   508       struct Constraints : public _ErasableGraph::Constraints<_Graph> {};
       
   509 
       
   510     };
       
   511 
       
   512     
       
   513     /************* New GraphBase stuff **************/
       
   514 
       
   515 
       
   516 //     /// A minimal GraphBase concept
       
   517 
       
   518 //     /// This class describes a minimal concept which can be extended to a
       
   519 //     /// full-featured graph with \ref GraphFactory.
       
   520 //     class GraphBase {
       
   521 //     public:
       
   522 
       
   523 //       GraphBase() {}
       
   524 
       
   525 //       /// \bug Should we demand that Node and Edge be subclasses of the
       
   526 //       /// Graph class???
       
   527 
       
   528 //       typedef GraphItem<'n'> Node;
       
   529 //       typedef GraphItem<'e'> Edge;
       
   530 
       
   531 // //       class Node : public BaseGraphItem<'n'> {};
       
   532 // //       class Edge : public BaseGraphItem<'e'> {};
       
   533 
       
   534 //       // Graph operation
       
   535 //       void firstNode(Node &n) const { }
       
   536 //       void firstEdge(Edge &e) const { }
       
   537 
       
   538 //       void firstOutEdge(Edge &e, Node) const { }
       
   539 //       void firstInEdge(Edge &e, Node) const { }
       
   540 
       
   541 //       void nextNode(Node &n) const { }
       
   542 //       void nextEdge(Edge &e) const { }
       
   543 
       
   544 
       
   545 //       // Question: isn't it reasonable if this methods have a Node
       
   546 //       // parameter? Like this:
       
   547 //       // Edge& nextOut(Edge &e, Node) const { return e; }
       
   548 //       void nextOutEdge(Edge &e) const { }
       
   549 //       void nextInEdge(Edge &e) const { }
       
   550 
       
   551 //       Node target(Edge) const { return Node(); }
       
   552 //       Node source(Edge) const { return Node(); }
       
   553       
       
   554 
       
   555 //       // Do we need id, nodeNum, edgeNum and co. in this basic graphbase
       
   556 //       // concept?
       
   557 
       
   558 
       
   559 //       // Maps.
       
   560 //       //
       
   561 //       // We need a special slimer concept which does not provide maps (it
       
   562 //       // wouldn't be strictly slimer, cause for map-factory id() & friends
       
   563 //       // a required...)
       
   564 
       
   565 //       template<typename T>
       
   566 //       class NodeMap : public GraphMap<GraphBase, Node, T> {};
       
   567 
       
   568 //       template<typename T>
       
   569 //       class EdgeMap : public GraphMap<GraphBase, Node, T> {};
       
   570 //     };
       
   571 
       
   572     // @}
       
   573   } //namespace concept  
       
   574 } //namespace lemon
       
   575 
       
   576 
       
   577 
       
   578 #endif // LEMON_CONCEPT_GRAPH_H