lemon/concept/graph.h
author deba
Tue, 14 Feb 2006 10:41:16 +0000
changeset 1968 78e6e2d1fd96
parent 1946 17eb3eaad9f8
child 1979 c2992fd74dad
permissions -rw-r--r--
Name modification
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_CONCEPT_GRAPH_H
    20 #define LEMON_CONCEPT_GRAPH_H
    21 
    22 ///\ingroup graph_concepts
    23 ///\file
    24 ///\brief Declaration of Graph.
    25 
    26 #include <lemon/invalid.h>
    27 #include <lemon/utility.h>
    28 #include <lemon/concept/maps.h>
    29 #include <lemon/concept_check.h>
    30 #include <lemon/concept/graph_component.h>
    31 
    32 namespace lemon {
    33   namespace concept {
    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 
    47       typedef False UTag;
    48       
    49       typedef BaseGraphComponent::Node Node;
    50       typedef BaseGraphComponent::Edge Edge;
    51 
    52       template <typename _Graph>
    53       struct Constraints {
    54         void constraints() {
    55           checkConcept<IterableGraphComponent, _Graph>();
    56           checkConcept<MappableGraphComponent, _Graph>();
    57         }
    58       };
    59     };
    60 
    61     // \brief Modular extendable graph class.
    62     //     
    63     // It should be the same as the \c ExtendableGraph class.
    64     class _ExtendableGraph 
    65       :  virtual public BaseGraphComponent, public _StaticGraph,
    66          public ExtendableGraphComponent, public ClearableGraphComponent {
    67     public:
    68       typedef BaseGraphComponent::Node Node;
    69       typedef BaseGraphComponent::Edge Edge;
    70 
    71       template <typename _Graph>
    72       struct Constraints {
    73         void constraints() {
    74           checkConcept<_StaticGraph, _Graph >();
    75           checkConcept<ExtendableGraphComponent, _Graph >();
    76           checkConcept<ClearableGraphComponent, _Graph >();
    77         }
    78       };
    79     };
    80 
    81     // \brief Modular erasable graph class.
    82     //     
    83     // It should be the same as the \c ErasableGraph class.
    84     class _ErasableGraph 
    85       :  virtual public BaseGraphComponent, public _ExtendableGraph,
    86          public ErasableGraphComponent {
    87     public:
    88       typedef BaseGraphComponent::Node Node;
    89       typedef BaseGraphComponent::Edge Edge;
    90 
    91       template <typename _Graph>
    92       struct Constraints {
    93         void constraints() {
    94           checkConcept<_ExtendableGraph, _Graph >();
    95           checkConcept<ErasableGraphComponent, _Graph >();
    96         }
    97       };
    98     };
    99 
   100     /// \addtogroup graph_concepts
   101     /// @{
   102 
   103     /// An empty static graph class.
   104   
   105     /// This class provides all the common features of a graph structure,
   106     /// however completely without implementations and real data structures
   107     /// behind the interface.
   108     /// All graph algorithms should compile with this class, but it will not
   109     /// run properly, of course.
   110     ///
   111     /// It can be used for checking the interface compatibility,
   112     /// or it can serve as a skeleton of a new graph structure.
   113     /// 
   114     /// Also, you will find here the full documentation of a certain graph
   115     /// feature, the documentation of a real graph imlementation
   116     /// like @ref ListGraph or
   117     /// @ref SmartGraph will just refer to this structure.
   118     ///
   119     /// \todo A pages describing the concept of concept description would
   120     /// be nice.
   121     class StaticGraph
   122     {
   123     public:
   124       ///\e
   125 
   126       ///\todo undocumented
   127       ///
   128       typedef False UTag;
   129 
   130       /// Defalult constructor.
   131 
   132       /// Defalult constructor.
   133       ///
   134       StaticGraph() { }
   135       ///Copy consructor.
   136 
   137 //       ///\todo It is not clear, what we expect from a copy constructor.
   138 //       ///E.g. How to assign the nodes/edges to each other? What about maps?
   139 //       StaticGraph(const StaticGraph& g) { }
   140 
   141       /// The base type of node iterators, 
   142       /// or in other words, the trivial node iterator.
   143 
   144       /// This is the base type of each node iterator,
   145       /// thus each kind of node iterator converts to this.
   146       /// More precisely each kind of node iterator should be inherited 
   147       /// from the trivial node iterator.
   148       class Node {
   149       public:
   150         /// Default constructor
   151 
   152         /// @warning The default constructor sets the iterator
   153         /// to an undefined value.
   154         Node() { }
   155         /// Copy constructor.
   156 
   157         /// Copy constructor.
   158         ///
   159         Node(const Node&) { }
   160 
   161         /// Invalid constructor \& conversion.
   162 
   163         /// This constructor initializes the iterator to be invalid.
   164         /// \sa Invalid for more details.
   165         Node(Invalid) { }
   166         /// Equality operator
   167 
   168         /// Two iterators are equal if and only if they point to the
   169         /// same object or both are invalid.
   170         bool operator==(Node) const { return true; }
   171 
   172         /// Inequality operator
   173         
   174         /// \sa operator==(Node n)
   175         ///
   176         bool operator!=(Node) const { return true; }
   177 
   178 	/// Artificial ordering operator.
   179 	
   180 	/// To allow the use of graph descriptors as key type in std::map or
   181 	/// similar associative container we require this.
   182 	///
   183 	/// \note This operator only have to define some strict ordering of
   184 	/// the items; this order has nothing to do with the iteration
   185 	/// ordering of the items.
   186 	///
   187 	/// \bug This is a technical requirement. Do we really need this?
   188 	bool operator<(Node) const { return false; }
   189 
   190       };
   191     
   192       /// This iterator goes through each node.
   193 
   194       /// This iterator goes through each node.
   195       /// Its usage is quite simple, for example you can count the number
   196       /// of nodes in graph \c g of type \c Graph like this:
   197       ///\code
   198       /// int count=0;
   199       /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
   200       ///\endcode
   201       class NodeIt : public Node {
   202       public:
   203         /// Default constructor
   204 
   205         /// @warning The default constructor sets the iterator
   206         /// to an undefined value.
   207         NodeIt() { }
   208         /// Copy constructor.
   209         
   210         /// Copy constructor.
   211         ///
   212         NodeIt(const NodeIt& n) : Node(n) { }
   213         /// Invalid constructor \& conversion.
   214 
   215         /// Initialize the iterator to be invalid.
   216         /// \sa Invalid for more details.
   217         NodeIt(Invalid) { }
   218         /// Sets the iterator to the first node.
   219 
   220         /// Sets the iterator to the first node of \c g.
   221         ///
   222         NodeIt(const StaticGraph&) { }
   223         /// Node -> NodeIt conversion.
   224 
   225         /// Sets the iterator to the node of \c the graph pointed by 
   226 	/// the trivial iterator.
   227         /// This feature necessitates that each time we 
   228         /// iterate the edge-set, the iteration order is the same.
   229         NodeIt(const StaticGraph&, const Node&) { }
   230         /// Next node.
   231 
   232         /// Assign the iterator to the next node.
   233         ///
   234         NodeIt& operator++() { return *this; }
   235       };
   236     
   237     
   238       /// The base type of the edge iterators.
   239 
   240       /// The base type of the edge iterators.
   241       ///
   242       class Edge {
   243       public:
   244         /// Default constructor
   245 
   246         /// @warning The default constructor sets the iterator
   247         /// to an undefined value.
   248         Edge() { }
   249         /// Copy constructor.
   250 
   251         /// Copy constructor.
   252         ///
   253         Edge(const Edge&) { }
   254         /// Initialize the iterator to be invalid.
   255 
   256         /// Initialize the iterator to be invalid.
   257         ///
   258         Edge(Invalid) { }
   259         /// Equality operator
   260 
   261         /// Two iterators are equal if and only if they point to the
   262         /// same object or both are invalid.
   263         bool operator==(Edge) const { return true; }
   264         /// Inequality operator
   265 
   266         /// \sa operator==(Edge n)
   267         ///
   268         bool operator!=(Edge) const { return true; }
   269 
   270 	/// Artificial ordering operator.
   271 	
   272 	/// To allow the use of graph descriptors as key type in std::map or
   273 	/// similar associative container we require this.
   274 	///
   275 	/// \note This operator only have to define some strict ordering of
   276 	/// the items; this order has nothing to do with the iteration
   277 	/// ordering of the items.
   278 	///
   279 	/// \bug This is a technical requirement. Do we really need this?
   280 	bool operator<(Edge) const { return false; }
   281       };
   282     
   283       /// This iterator goes trough the outgoing edges of a node.
   284 
   285       /// This iterator goes trough the \e outgoing edges of a certain node
   286       /// of a graph.
   287       /// Its usage is quite simple, for example you can count the number
   288       /// of outgoing edges of a node \c n
   289       /// in graph \c g of type \c Graph as follows.
   290       ///\code
   291       /// int count=0;
   292       /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   293       ///\endcode
   294     
   295       class OutEdgeIt : public Edge {
   296       public:
   297         /// Default constructor
   298 
   299         /// @warning The default constructor sets the iterator
   300         /// to an undefined value.
   301         OutEdgeIt() { }
   302         /// Copy constructor.
   303 
   304         /// Copy constructor.
   305         ///
   306         OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
   307         /// Initialize the iterator to be invalid.
   308 
   309         /// Initialize the iterator to be invalid.
   310         ///
   311         OutEdgeIt(Invalid) { }
   312         /// This constructor sets the iterator to the first outgoing edge.
   313     
   314         /// This constructor sets the iterator to the first outgoing edge of
   315         /// the node.
   316         OutEdgeIt(const StaticGraph&, const Node&) { }
   317         /// Edge -> OutEdgeIt conversion
   318 
   319         /// Sets the iterator to the value of the trivial iterator.
   320 	/// This feature necessitates that each time we 
   321         /// iterate the edge-set, the iteration order is the same.
   322         OutEdgeIt(const StaticGraph&, const Edge&) { }
   323         ///Next outgoing edge
   324         
   325         /// Assign the iterator to the next 
   326         /// outgoing edge of the corresponding node.
   327         OutEdgeIt& operator++() { return *this; }
   328       };
   329 
   330       /// This iterator goes trough the incoming edges of a node.
   331 
   332       /// This iterator goes trough the \e incoming edges of a certain node
   333       /// of a graph.
   334       /// Its usage is quite simple, for example you can count the number
   335       /// of outgoing edges of a node \c n
   336       /// in graph \c g of type \c Graph as follows.
   337       ///\code
   338       /// int count=0;
   339       /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   340       ///\endcode
   341 
   342       class InEdgeIt : public Edge {
   343       public:
   344         /// Default constructor
   345 
   346         /// @warning The default constructor sets the iterator
   347         /// to an undefined value.
   348         InEdgeIt() { }
   349         /// Copy constructor.
   350 
   351         /// Copy constructor.
   352         ///
   353         InEdgeIt(const InEdgeIt& e) : Edge(e) { }
   354         /// Initialize the iterator to be invalid.
   355 
   356         /// Initialize the iterator to be invalid.
   357         ///
   358         InEdgeIt(Invalid) { }
   359         /// This constructor sets the iterator to first incoming edge.
   360     
   361         /// This constructor set the iterator to the first incoming edge of
   362         /// the node.
   363         InEdgeIt(const StaticGraph&, const Node&) { }
   364         /// Edge -> InEdgeIt conversion
   365 
   366         /// Sets the iterator to the value of the trivial iterator \c e.
   367         /// This feature necessitates that each time we 
   368         /// iterate the edge-set, the iteration order is the same.
   369         InEdgeIt(const StaticGraph&, const Edge&) { }
   370         /// Next incoming edge
   371 
   372         /// Assign the iterator to the next inedge of the corresponding node.
   373         ///
   374         InEdgeIt& operator++() { return *this; }
   375       };
   376       /// This iterator goes through each edge.
   377 
   378       /// This iterator goes through each edge of a graph.
   379       /// Its usage is quite simple, for example you can count the number
   380       /// of edges in a graph \c g of type \c Graph as follows:
   381       ///\code
   382       /// int count=0;
   383       /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
   384       ///\endcode
   385       class EdgeIt : public Edge {
   386       public:
   387         /// Default constructor
   388 
   389         /// @warning The default constructor sets the iterator
   390         /// to an undefined value.
   391         EdgeIt() { }
   392         /// Copy constructor.
   393 
   394         /// Copy constructor.
   395         ///
   396         EdgeIt(const EdgeIt& e) : Edge(e) { }
   397         /// Initialize the iterator to be invalid.
   398 
   399         /// Initialize the iterator to be invalid.
   400         ///
   401         EdgeIt(Invalid) { }
   402         /// This constructor sets the iterator to the first edge.
   403     
   404         /// This constructor sets the iterator to the first edge of \c g.
   405         ///@param g the graph
   406         EdgeIt(const StaticGraph& g) { ignore_unused_variable_warning(g); }
   407         /// Edge -> EdgeIt conversion
   408 
   409         /// Sets the iterator to the value of the trivial iterator \c e.
   410         /// This feature necessitates that each time we 
   411         /// iterate the edge-set, the iteration order is the same.
   412         EdgeIt(const StaticGraph&, const Edge&) { } 
   413         ///Next edge
   414         
   415         /// Assign the iterator to the next edge.
   416         EdgeIt& operator++() { return *this; }
   417       };
   418       ///Gives back the target node of an edge.
   419 
   420       ///Gives back the target node of an edge.
   421       ///
   422       Node target(Edge) const { return INVALID; }
   423       ///Gives back the source node of an edge.
   424 
   425       ///Gives back the source node of an edge.
   426       ///
   427       Node source(Edge) const { return INVALID; }
   428 
   429 //       /// Gives back the first Node in the iterating order.
   430       
   431 //       /// Gives back the first Node in the iterating order.
   432 //       ///     
   433       void first(Node&) const {}
   434 
   435 //       /// Gives back the next Node in the iterating order.
   436       
   437 //       /// Gives back the next Node in the iterating order.
   438 //       ///     
   439       void next(Node&) const {}
   440 
   441 //       /// Gives back the first Edge in the iterating order.
   442       
   443 //       /// Gives back the first Edge in the iterating order.
   444 //       ///     
   445       void first(Edge&) const {}
   446 //       /// Gives back the next Edge in the iterating order.
   447       
   448 //       /// Gives back the next Edge in the iterating order.
   449 //       ///     
   450       void next(Edge&) const {}
   451 
   452 
   453 //       /// Gives back the first of the Edges point to the given Node.
   454       
   455 //       /// Gives back the first of the Edges point to the given Node.
   456 //       ///     
   457       void firstIn(Edge&, const Node&) const {}
   458 
   459 //       /// Gives back the next of the Edges points to the given Node.
   460 
   461 
   462 //       /// Gives back the next of the Edges points to the given Node.
   463 //       ///
   464       void nextIn(Edge&) const {}
   465 
   466 //       /// Gives back the first of the Edges start from the given Node.
   467       
   468 //       /// Gives back the first of the Edges start from the given Node.
   469 //       ///     
   470       void firstOut(Edge&, const Node&) const {}
   471 
   472 //       /// Gives back the next of the Edges start from the given Node.
   473       
   474 //       /// Gives back the next of the Edges start from the given Node.
   475 //       ///     
   476       void nextOut(Edge&) const {}
   477 
   478       /// \brief The base node of the iterator.
   479       ///
   480       /// Gives back the base node of the iterator.
   481       /// It is always the target of the pointed edge.
   482       Node baseNode(const InEdgeIt&) const { return INVALID; }
   483 
   484       /// \brief The running node of the iterator.
   485       ///
   486       /// Gives back the running node of the iterator.
   487       /// It is always the source of the pointed edge.
   488       Node runningNode(const InEdgeIt&) const { return INVALID; }
   489 
   490       /// \brief The base node of the iterator.
   491       ///
   492       /// Gives back the base node of the iterator.
   493       /// It is always the source of the pointed edge.
   494       Node baseNode(const OutEdgeIt&) const { return INVALID; }
   495 
   496       /// \brief The running node of the iterator.
   497       ///
   498       /// Gives back the running node of the iterator.
   499       /// It is always the target of the pointed edge.
   500       Node runningNode(const OutEdgeIt&) const { return INVALID; }
   501 
   502       /// \brief The opposite node on the given edge.
   503       ///
   504       /// Gives back the opposite node on the given edge.
   505       Node oppositeNode(const Node&, const Edge&) const { return INVALID; }
   506 
   507       /// \brief Read write map of the nodes to type \c T.
   508       /// 
   509       /// ReadWrite map of the nodes to type \c T.
   510       /// \sa Reference
   511       /// \warning Making maps that can handle bool type (NodeMap<bool>)
   512       /// needs some extra attention!
   513       /// \todo Wrong documentation
   514       template<class T> 
   515       class NodeMap : public ReadWriteMap< Node, T >
   516       {
   517       public:
   518 
   519         ///\e
   520         NodeMap(const StaticGraph&) { }
   521         ///\e
   522         NodeMap(const StaticGraph&, T) { }
   523 
   524         ///Copy constructor
   525         NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
   526         ///Assignment operator
   527         NodeMap& operator=(const NodeMap&) { return *this; }
   528         // \todo fix this concept
   529       };
   530 
   531       /// \brief Read write map of the edges to type \c T.
   532       ///
   533       /// Reference map of the edges to type \c T.
   534       /// \sa Reference
   535       /// \warning Making maps that can handle bool type (EdgeMap<bool>)
   536       /// needs some extra attention!
   537       /// \todo Wrong documentation
   538       template<class T> 
   539       class EdgeMap : public ReadWriteMap<Edge,T>
   540       {
   541       public:
   542 
   543         ///\e
   544         EdgeMap(const StaticGraph&) { }
   545         ///\e
   546         EdgeMap(const StaticGraph&, T) { }
   547         ///Copy constructor
   548         EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
   549         ///Assignment operator
   550         EdgeMap& operator=(const EdgeMap&) { return *this; }
   551         // \todo fix this concept    
   552       };
   553 
   554       template <typename _Graph>
   555       struct Constraints : public _StaticGraph::Constraints<_Graph> {};
   556 
   557     };
   558 
   559     /// An empty non-static graph class.
   560     
   561     /// This class provides everything that \ref StaticGraph does.
   562     /// Additionally it enables building graphs from scratch.
   563     class ExtendableGraph : public StaticGraph
   564     {
   565     public:
   566       /// Defalult constructor.
   567 
   568       /// Defalult constructor.
   569       ///
   570       ExtendableGraph() { }
   571       ///Add a new node to the graph.
   572 
   573       /// \return the new node.
   574       ///
   575       Node addNode() { return INVALID; }
   576       ///Add a new edge to the graph.
   577 
   578       ///Add a new edge to the graph with source node \c s
   579       ///and target node \c t.
   580       ///\return the new edge.
   581       Edge addEdge(Node, Node) { return INVALID; }
   582     
   583       /// Resets the graph.
   584 
   585       /// This function deletes all edges and nodes of the graph.
   586       /// It also frees the memory allocated to store them.
   587       /// \todo It might belong to \ref ErasableGraph.
   588       void clear() { }
   589 
   590       template <typename _Graph>
   591       struct Constraints : public _ExtendableGraph::Constraints<_Graph> {};
   592 
   593     };
   594 
   595     /// An empty erasable graph class.
   596   
   597     /// This class is an extension of \ref ExtendableGraph. It makes it
   598     /// possible to erase edges or nodes.
   599     class ErasableGraph : public ExtendableGraph
   600     {
   601     public:
   602       /// Defalult constructor.
   603 
   604       /// Defalult constructor.
   605       ///
   606       ErasableGraph() { }
   607       /// Deletes a node.
   608 
   609       /// Deletes node \c n node.
   610       ///
   611       void erase(Node) { }
   612       /// Deletes an edge.
   613 
   614       /// Deletes edge \c e edge.
   615       ///
   616       void erase(Edge) { }
   617 
   618       template <typename _Graph>
   619       struct Constraints : public _ErasableGraph::Constraints<_Graph> {};
   620 
   621     };
   622     
   623     // @}
   624   } //namespace concept  
   625 } //namespace lemon
   626 
   627 
   628 
   629 #endif // LEMON_CONCEPT_GRAPH_H