lemon/concept/ugraph.h
author alpar
Wed, 05 Jul 2006 16:59:45 +0000
changeset 2120 a907fb95f1e0
parent 2111 ea1fa1bc3f6d
child 2121 09a07a851506
permissions -rw-r--r--
As we agreed, Node/Edge::operator<() is required by the concept
     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 ///\ingroup graph_concepts
    20 ///\file
    21 ///\brief The concept of the undirected graphs.
    22 
    23 
    24 #ifndef LEMON_CONCEPT_UGRAPH_H
    25 #define LEMON_CONCEPT_UGRAPH_H
    26 
    27 #include <lemon/concept/graph_component.h>
    28 #include <lemon/concept/graph.h>
    29 #include <lemon/bits/utility.h>
    30 
    31 namespace lemon {
    32   namespace concept {
    33 
    34     /// \addtogroup graph_concepts
    35     /// @{
    36 
    37 
    38     /// Class describing the concept of Undirected Graphs.
    39 
    40     /// This class describes the common interface of all Undirected
    41     /// Graphs.
    42     ///
    43     /// As all concept describing classes it provides only interface
    44     /// without any sensible implementation. So any algorithm for
    45     /// undirected graph should compile with this class, but it will not
    46     /// run properly, of couse.
    47     ///
    48     /// In LEMON undirected graphs also fulfill the concept of directed
    49     /// graphs (\ref lemon::concept::Graph "Graph Concept"). For
    50     /// explanation of this and more see also the page \ref graphs,
    51     /// a tutorial about graphs.
    52     ///
    53     /// You can assume that all undirected graph can be handled
    54     /// as a directed graph. This way it is fully conform
    55     /// to the Graph concept.
    56 
    57     class UGraph {
    58     public:
    59       ///\e
    60 
    61       ///\todo undocumented
    62       ///
    63       typedef True UndirectedTag;
    64 
    65       /// \brief The base type of node iterators, 
    66       /// or in other words, the trivial node iterator.
    67       ///
    68       /// This is the base type of each node iterator,
    69       /// thus each kind of node iterator converts to this.
    70       /// More precisely each kind of node iterator should be inherited 
    71       /// from the trivial node iterator.
    72       class Node {
    73       public:
    74         /// Default constructor
    75 
    76         /// @warning The default constructor sets the iterator
    77         /// to an undefined value.
    78         Node() { }
    79         /// Copy constructor.
    80 
    81         /// Copy constructor.
    82         ///
    83         Node(const Node&) { }
    84 
    85         /// Invalid constructor \& conversion.
    86 
    87         /// This constructor initializes the iterator to be invalid.
    88         /// \sa Invalid for more details.
    89         Node(Invalid) { }
    90         /// Equality operator
    91 
    92         /// Two iterators are equal if and only if they point to the
    93         /// same object or both are invalid.
    94         bool operator==(Node) const { return true; }
    95 
    96         /// Inequality operator
    97         
    98         /// \sa operator==(Node n)
    99         ///
   100         bool operator!=(Node) const { return true; }
   101 
   102 	/// Artificial ordering operator.
   103 	
   104 	/// To allow the use of graph descriptors as key type in std::map or
   105 	/// similar associative container we require this.
   106 	///
   107 	/// \note This operator only have to define some strict ordering of
   108 	/// the items; this order has nothing to do with the iteration
   109 	/// ordering of the items.
   110 	bool operator<(Node) const { return false; }
   111 
   112       };
   113     
   114       /// This iterator goes through each node.
   115 
   116       /// This iterator goes through each node.
   117       /// Its usage is quite simple, for example you can count the number
   118       /// of nodes in graph \c g of type \c Graph like this:
   119       ///\code
   120       /// int count=0;
   121       /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
   122       ///\endcode
   123       class NodeIt : public Node {
   124       public:
   125         /// Default constructor
   126 
   127         /// @warning The default constructor sets the iterator
   128         /// to an undefined value.
   129         NodeIt() { }
   130         /// Copy constructor.
   131         
   132         /// Copy constructor.
   133         ///
   134         NodeIt(const NodeIt& n) : Node(n) { }
   135         /// Invalid constructor \& conversion.
   136 
   137         /// Initialize the iterator to be invalid.
   138         /// \sa Invalid for more details.
   139         NodeIt(Invalid) { }
   140         /// Sets the iterator to the first node.
   141 
   142         /// Sets the iterator to the first node of \c g.
   143         ///
   144         NodeIt(const UGraph&) { }
   145         /// Node -> NodeIt conversion.
   146 
   147         /// Sets the iterator to the node of \c the graph pointed by 
   148 	/// the trivial iterator.
   149         /// This feature necessitates that each time we 
   150         /// iterate the edge-set, the iteration order is the same.
   151         NodeIt(const UGraph&, const Node&) { }
   152         /// Next node.
   153 
   154         /// Assign the iterator to the next node.
   155         ///
   156         NodeIt& operator++() { return *this; }
   157       };
   158     
   159     
   160       /// The base type of the undirected edge iterators.
   161 
   162       /// The base type of the undirected edge iterators.
   163       ///
   164       class UEdge {
   165       public:
   166         /// Default constructor
   167 
   168         /// @warning The default constructor sets the iterator
   169         /// to an undefined value.
   170         UEdge() { }
   171         /// Copy constructor.
   172 
   173         /// Copy constructor.
   174         ///
   175         UEdge(const UEdge&) { }
   176         /// Initialize the iterator to be invalid.
   177 
   178         /// Initialize the iterator to be invalid.
   179         ///
   180         UEdge(Invalid) { }
   181         /// Equality operator
   182 
   183         /// Two iterators are equal if and only if they point to the
   184         /// same object or both are invalid.
   185         bool operator==(UEdge) const { return true; }
   186         /// Inequality operator
   187 
   188         /// \sa operator==(UEdge n)
   189         ///
   190         bool operator!=(UEdge) const { return true; }
   191 
   192 	/// Artificial ordering operator.
   193 	
   194 	/// To allow the use of graph descriptors as key type in std::map or
   195 	/// similar associative container we require this.
   196 	///
   197 	/// \note This operator only have to define some strict ordering of
   198 	/// the items; this order has nothing to do with the iteration
   199 	/// ordering of the items.
   200 	bool operator<(UEdge) const { return false; }
   201       };
   202 
   203       /// This iterator goes through each undirected edge.
   204 
   205       /// This iterator goes through each undirected edge of a graph.
   206       /// Its usage is quite simple, for example you can count the number
   207       /// of undirected edges in a graph \c g of type \c Graph as follows:
   208       ///\code
   209       /// int count=0;
   210       /// for(Graph::UEdgeIt e(g); e!=INVALID; ++e) ++count;
   211       ///\endcode
   212       class UEdgeIt : public UEdge {
   213       public:
   214         /// Default constructor
   215 
   216         /// @warning The default constructor sets the iterator
   217         /// to an undefined value.
   218         UEdgeIt() { }
   219         /// Copy constructor.
   220 
   221         /// Copy constructor.
   222         ///
   223         UEdgeIt(const UEdgeIt& e) : UEdge(e) { }
   224         /// Initialize the iterator to be invalid.
   225 
   226         /// Initialize the iterator to be invalid.
   227         ///
   228         UEdgeIt(Invalid) { }
   229         /// This constructor sets the iterator to the first undirected edge.
   230     
   231         /// This constructor sets the iterator to the first undirected edge.
   232         UEdgeIt(const UGraph&) { }
   233         /// UEdge -> UEdgeIt conversion
   234 
   235         /// Sets the iterator to the value of the trivial iterator.
   236         /// This feature necessitates that each time we
   237         /// iterate the undirected edge-set, the iteration order is the 
   238 	/// same.
   239         UEdgeIt(const UGraph&, const UEdge&) { } 
   240         /// Next undirected edge
   241         
   242         /// Assign the iterator to the next undirected edge.
   243         UEdgeIt& operator++() { return *this; }
   244       };
   245 
   246       /// \brief This iterator goes trough the incident undirected 
   247       /// edges of a node.
   248       ///
   249       /// This iterator goes trough the incident undirected edges
   250       /// of a certain node of a graph. You should assume that the 
   251       /// loop edges will be iterated twice.
   252       /// 
   253       /// Its usage is quite simple, for example you can compute the
   254       /// degree (i.e. count the number of incident edges of a node \c n
   255       /// in graph \c g of type \c Graph as follows. 
   256       ///
   257       ///\code
   258       /// int count=0;
   259       /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   260       ///\endcode
   261       class IncEdgeIt : public UEdge {
   262       public:
   263         /// Default constructor
   264 
   265         /// @warning The default constructor sets the iterator
   266         /// to an undefined value.
   267         IncEdgeIt() { }
   268         /// Copy constructor.
   269 
   270         /// Copy constructor.
   271         ///
   272         IncEdgeIt(const IncEdgeIt& e) : UEdge(e) { }
   273         /// Initialize the iterator to be invalid.
   274 
   275         /// Initialize the iterator to be invalid.
   276         ///
   277         IncEdgeIt(Invalid) { }
   278         /// This constructor sets the iterator to first incident edge.
   279     
   280         /// This constructor set the iterator to the first incident edge of
   281         /// the node.
   282         IncEdgeIt(const UGraph&, const Node&) { }
   283         /// UEdge -> IncEdgeIt 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         IncEdgeIt(const UGraph&, const UEdge&) { }
   289         /// Next incident edge
   290 
   291         /// Assign the iterator to the next incident edge
   292 	/// of the corresponding node.
   293         IncEdgeIt& operator++() { return *this; }
   294       };
   295 
   296       /// The directed edge type.
   297 
   298       /// The directed edge type. It can be converted to the
   299       /// undirected edge.
   300       class Edge : public UEdge {
   301       public:
   302         /// Default constructor
   303 
   304         /// @warning The default constructor sets the iterator
   305         /// to an undefined value.
   306         Edge() { }
   307         /// Copy constructor.
   308 
   309         /// Copy constructor.
   310         ///
   311         Edge(const Edge& e) : UEdge(e) { }
   312         /// Initialize the iterator to be invalid.
   313 
   314         /// Initialize the iterator to be invalid.
   315         ///
   316         Edge(Invalid) { }
   317         /// Equality operator
   318 
   319         /// Two iterators are equal if and only if they point to the
   320         /// same object or both are invalid.
   321         bool operator==(Edge) const { return true; }
   322         /// Inequality operator
   323 
   324         /// \sa operator==(Edge n)
   325         ///
   326         bool operator!=(Edge) const { return true; }
   327 
   328 	/// Artificial ordering operator.
   329 	
   330 	/// To allow the use of graph descriptors as key type in std::map or
   331 	/// similar associative container we require this.
   332 	///
   333 	/// \note This operator only have to define some strict ordering of
   334 	/// the items; this order has nothing to do with the iteration
   335 	/// ordering of the items.
   336 	bool operator<(Edge) const { return false; }
   337 	
   338       }; 
   339       /// This iterator goes through each directed edge.
   340 
   341       /// This iterator goes through each edge of a graph.
   342       /// Its usage is quite simple, for example you can count the number
   343       /// of edges in a graph \c g of type \c Graph as follows:
   344       ///\code
   345       /// int count=0;
   346       /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
   347       ///\endcode
   348       class EdgeIt : public Edge {
   349       public:
   350         /// Default constructor
   351 
   352         /// @warning The default constructor sets the iterator
   353         /// to an undefined value.
   354         EdgeIt() { }
   355         /// Copy constructor.
   356 
   357         /// Copy constructor.
   358         ///
   359         EdgeIt(const EdgeIt& e) : Edge(e) { }
   360         /// Initialize the iterator to be invalid.
   361 
   362         /// Initialize the iterator to be invalid.
   363         ///
   364         EdgeIt(Invalid) { }
   365         /// This constructor sets the iterator to the first edge.
   366     
   367         /// This constructor sets the iterator to the first edge of \c g.
   368         ///@param g the graph
   369         EdgeIt(const UGraph &g) { ignore_unused_variable_warning(g); }
   370         /// Edge -> EdgeIt conversion
   371 
   372         /// Sets the iterator to the value of the trivial iterator \c e.
   373         /// This feature necessitates that each time we 
   374         /// iterate the edge-set, the iteration order is the same.
   375         EdgeIt(const UGraph&, const Edge&) { } 
   376         ///Next edge
   377         
   378         /// Assign the iterator to the next edge.
   379         EdgeIt& operator++() { return *this; }
   380       };
   381    
   382       /// This iterator goes trough the outgoing directed edges of a node.
   383 
   384       /// This iterator goes trough the \e outgoing edges of a certain node
   385       /// of a graph.
   386       /// Its usage is quite simple, for example you can count the number
   387       /// of outgoing edges of a node \c n
   388       /// in graph \c g of type \c Graph as follows.
   389       ///\code
   390       /// int count=0;
   391       /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   392       ///\endcode
   393     
   394       class OutEdgeIt : public Edge {
   395       public:
   396         /// Default constructor
   397 
   398         /// @warning The default constructor sets the iterator
   399         /// to an undefined value.
   400         OutEdgeIt() { }
   401         /// Copy constructor.
   402 
   403         /// Copy constructor.
   404         ///
   405         OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
   406         /// Initialize the iterator to be invalid.
   407 
   408         /// Initialize the iterator to be invalid.
   409         ///
   410         OutEdgeIt(Invalid) { }
   411         /// This constructor sets the iterator to the first outgoing edge.
   412     
   413         /// This constructor sets the iterator to the first outgoing edge of
   414         /// the node.
   415         ///@param n the node
   416         ///@param g the graph
   417         OutEdgeIt(const UGraph& n, const Node& g) {
   418 	  ignore_unused_variable_warning(n);
   419 	  ignore_unused_variable_warning(g);
   420 	}
   421         /// Edge -> OutEdgeIt conversion
   422 
   423         /// Sets the iterator to the value of the trivial iterator.
   424 	/// This feature necessitates that each time we 
   425         /// iterate the edge-set, the iteration order is the same.
   426         OutEdgeIt(const UGraph&, const Edge&) { }
   427         ///Next outgoing edge
   428         
   429         /// Assign the iterator to the next 
   430         /// outgoing edge of the corresponding node.
   431         OutEdgeIt& operator++() { return *this; }
   432       };
   433 
   434       /// This iterator goes trough the incoming directed edges of a node.
   435 
   436       /// This iterator goes trough the \e incoming edges of a certain node
   437       /// of a graph.
   438       /// Its usage is quite simple, for example you can count the number
   439       /// of outgoing edges of a node \c n
   440       /// in graph \c g of type \c Graph as follows.
   441       ///\code
   442       /// int count=0;
   443       /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
   444       ///\endcode
   445 
   446       class InEdgeIt : public Edge {
   447       public:
   448         /// Default constructor
   449 
   450         /// @warning The default constructor sets the iterator
   451         /// to an undefined value.
   452         InEdgeIt() { }
   453         /// Copy constructor.
   454 
   455         /// Copy constructor.
   456         ///
   457         InEdgeIt(const InEdgeIt& e) : Edge(e) { }
   458         /// Initialize the iterator to be invalid.
   459 
   460         /// Initialize the iterator to be invalid.
   461         ///
   462         InEdgeIt(Invalid) { }
   463         /// This constructor sets the iterator to first incoming edge.
   464     
   465         /// This constructor set the iterator to the first incoming edge of
   466         /// the node.
   467         ///@param n the node
   468         ///@param g the graph
   469         InEdgeIt(const UGraph& g, const Node& n) { 
   470 	  ignore_unused_variable_warning(n);
   471 	  ignore_unused_variable_warning(g);
   472 	}
   473         /// Edge -> InEdgeIt conversion
   474 
   475         /// Sets the iterator to the value of the trivial iterator \c e.
   476         /// This feature necessitates that each time we 
   477         /// iterate the edge-set, the iteration order is the same.
   478         InEdgeIt(const UGraph&, const Edge&) { }
   479         /// Next incoming edge
   480 
   481         /// Assign the iterator to the next inedge of the corresponding node.
   482         ///
   483         InEdgeIt& operator++() { return *this; }
   484       };
   485 
   486       /// \brief Read write map of the nodes to type \c T.
   487       /// 
   488       /// ReadWrite map of the nodes to type \c T.
   489       /// \sa Reference
   490       /// \warning Making maps that can handle bool type (NodeMap<bool>)
   491       /// needs some extra attention!
   492       /// \todo Wrong documentation
   493       template<class T> 
   494       class NodeMap : public ReadWriteMap< Node, T >
   495       {
   496       public:
   497 
   498         ///\e
   499         NodeMap(const UGraph&) { }
   500         ///\e
   501         NodeMap(const UGraph&, T) { }
   502 
   503         ///Copy constructor
   504         NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
   505         ///Assignment operator
   506         NodeMap& operator=(const NodeMap&) { return *this; }
   507         // \todo fix this concept
   508       };
   509 
   510       /// \brief Read write map of the directed edges to type \c T.
   511       ///
   512       /// Reference map of the directed edges to type \c T.
   513       /// \sa Reference
   514       /// \warning Making maps that can handle bool type (EdgeMap<bool>)
   515       /// needs some extra attention!
   516       /// \todo Wrong documentation
   517       template<class T> 
   518       class EdgeMap : public ReadWriteMap<Edge,T>
   519       {
   520       public:
   521 
   522         ///\e
   523         EdgeMap(const UGraph&) { }
   524         ///\e
   525         EdgeMap(const UGraph&, T) { }
   526         ///Copy constructor
   527         EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
   528         ///Assignment operator
   529         EdgeMap& operator=(const EdgeMap&) { return *this; }
   530         // \todo fix this concept    
   531       };
   532 
   533       /// Read write map of the undirected edges to type \c T.
   534 
   535       /// Reference map of the edges to type \c T.
   536       /// \sa Reference
   537       /// \warning Making maps that can handle bool type (UEdgeMap<bool>)
   538       /// needs some extra attention!
   539       /// \todo Wrong documentation
   540       template<class T> 
   541       class UEdgeMap : public ReadWriteMap<UEdge,T>
   542       {
   543       public:
   544 
   545         ///\e
   546         UEdgeMap(const UGraph&) { }
   547         ///\e
   548         UEdgeMap(const UGraph&, T) { }
   549         ///Copy constructor
   550         UEdgeMap(const UEdgeMap& em) : ReadWriteMap<UEdge,T>(em) {}
   551         ///Assignment operator
   552         UEdgeMap &operator=(const UEdgeMap&) { return *this; }
   553         // \todo fix this concept    
   554       };
   555 
   556       /// \brief Direct the given undirected edge.
   557       ///
   558       /// Direct the given undirected edge. The returned edge source
   559       /// will be the given edge.
   560       Edge direct(const UEdge&, const Node&) const {
   561 	return INVALID;
   562       }
   563 
   564       /// \brief Direct the given undirected edge.
   565       ///
   566       /// Direct the given undirected edge. The returned edge source
   567       /// will be the source of the undirected edge if the given bool
   568       /// is true.
   569       Edge direct(const UEdge&, bool) const {
   570 	return INVALID;
   571       }
   572 
   573       /// \brief Returns true if the edge has default orientation.
   574       ///
   575       /// Returns whether the given directed edge is same orientation as
   576       /// the corresponding undirected edge.
   577       bool direction(Edge) const { return true; }
   578 
   579       /// \brief Returns the opposite directed edge.
   580       ///
   581       /// Returns the opposite directed edge.
   582       Edge oppositeEdge(Edge) const { return INVALID; }
   583 
   584       /// \brief Opposite node on an edge
   585       ///
   586       /// \return the opposite of the given Node on the given Edge
   587       Node oppositeNode(Node, UEdge) const { return INVALID; }
   588 
   589       /// \brief First node of the undirected edge.
   590       ///
   591       /// \return the first node of the given UEdge.
   592       ///
   593       /// Naturally uectected edges don't have direction and thus
   594       /// don't have source and target node. But we use these two methods
   595       /// to query the two endnodes of the edge. The direction of the edge
   596       /// which arises this way is called the inherent direction of the
   597       /// undirected edge, and is used to define the "default" direction
   598       /// of the directed versions of the edges.
   599       /// \sa direction
   600       Node source(UEdge) const { return INVALID; }
   601 
   602       /// \brief Second node of the undirected edge.
   603       Node target(UEdge) const { return INVALID; }
   604 
   605       /// \brief Source node of the directed edge.
   606       Node source(Edge) const { return INVALID; }
   607 
   608       /// \brief Target node of the directed edge.
   609       Node target(Edge) const { return INVALID; }
   610 
   611       void first(Node&) const {}
   612       void next(Node&) const {}
   613 
   614       void first(UEdge&) const {}
   615       void next(UEdge&) const {}
   616 
   617       void first(Edge&) const {}
   618       void next(Edge&) const {}
   619 
   620       void firstOut(Edge&, Node) const {}
   621       void nextOut(Edge&) const {}
   622 
   623       void firstIn(Edge&, Node) const {}
   624       void nextIn(Edge&) const {}
   625 
   626 
   627       void firstInc(UEdge &, bool &, const Node &) const {}
   628       void nextInc(UEdge &, bool &) const {}
   629 
   630       /// \brief Base node of the iterator
   631       ///
   632       /// Returns the base node (the source in this case) of the iterator
   633       Node baseNode(OutEdgeIt e) const {
   634 	return source(e);
   635       }
   636       /// \brief Running node of the iterator
   637       ///
   638       /// Returns the running node (the target in this case) of the
   639       /// iterator
   640       Node runningNode(OutEdgeIt e) const {
   641 	return target(e);
   642       }
   643 
   644       /// \brief Base node of the iterator
   645       ///
   646       /// Returns the base node (the target in this case) of the iterator
   647       Node baseNode(InEdgeIt e) const {
   648 	return target(e);
   649       }
   650       /// \brief Running node of the iterator
   651       ///
   652       /// Returns the running node (the source in this case) of the
   653       /// iterator
   654       Node runningNode(InEdgeIt e) const {
   655 	return source(e);
   656       }
   657 
   658       /// \brief Base node of the iterator
   659       ///
   660       /// Returns the base node of the iterator
   661       Node baseNode(IncEdgeIt) const {
   662 	return INVALID;
   663       }
   664       
   665       /// \brief Running node of the iterator
   666       ///
   667       /// Returns the running node of the iterator
   668       Node runningNode(IncEdgeIt) const {
   669 	return INVALID;
   670       }
   671 
   672       template <typename Graph>
   673       struct Constraints {
   674 	void constraints() {
   675 	  checkConcept<BaseIterableUGraphConcept, Graph>();
   676 	  checkConcept<IterableUGraphConcept, Graph>();
   677 	  checkConcept<MappableUGraphConcept, Graph>();
   678 	}
   679       };
   680 
   681     };
   682 
   683     /// @}
   684 
   685   }
   686 
   687 }
   688 
   689 #endif