/* -*- C++ -*- * src/lemon/concept/graph.h - Part of LEMON, a generic C++ optimization library * * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Combinatorial Optimization Research Group, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ #ifndef LEMON_CONCEPT_GRAPH_H #define LEMON_CONCEPT_GRAPH_H ///\ingroup concept ///\file ///\brief Declaration of Graph. #include #include #include #include namespace lemon { namespace concept { /// \addtogroup concept /// @{ // /// An empty static graph class. // /// This class provides all the common features of a graph structure, // /// however completely without implementations and real data structures // /// behind the interface. // /// All graph algorithms should compile with this class, but it will not // /// run properly, of course. // /// // /// It can be used for checking the interface compatibility, // /// or it can serve as a skeleton of a new graph structure. // /// // /// Also, you will find here the full documentation of a certain graph // /// feature, the documentation of a real graph imlementation // /// like @ref ListGraph or // /// @ref SmartGraph will just refer to this structure. // /// // /// \todo A pages describing the concept of concept description would // /// be nice. // class StaticGraph // { // public: // /// Defalult constructor. // /// Defalult constructor. // /// // StaticGraph() { } // ///Copy consructor. // // ///\todo It is not clear, what we expect from a copy constructor. // // ///E.g. How to assign the nodes/edges to each other? What about maps? // // StaticGraph(const StaticGraph& g) { } // /// The base type of node iterators, // /// or in other words, the trivial node iterator. // /// This is the base type of each node iterator, // /// thus each kind of node iterator converts to this. // /// More precisely each kind of node iterator should be inherited // /// from the trivial node iterator. // class Node { // public: // /// Default constructor // /// @warning The default constructor sets the iterator // /// to an undefined value. // Node() { } // /// Copy constructor. // /// Copy constructor. // /// // Node(const Node&) { } // /// Invalid constructor \& conversion. // /// This constructor initializes the iterator to be invalid. // /// \sa Invalid for more details. // Node(Invalid) { } // /// Equality operator // /// Two iterators are equal if and only if they point to the // /// same object or both are invalid. // bool operator==(Node) const { return true; } // /// Inequality operator // /// \sa operator==(Node n) // /// // bool operator!=(Node) const { return true; } // ///Comparison operator. // ///This is a strict ordering between the nodes. // /// // ///This ordering can be different from the order in which NodeIt // ///goes through the nodes. // ///\todo Possibly we don't need it. // bool operator<(Node) const { return true; } // }; // /// This iterator goes through each node. // /// This iterator goes through each node. // /// Its usage is quite simple, for example you can count the number // /// of nodes in graph \c g of type \c Graph like this: // /// \code // /// int count=0; // /// for (Graph::NodeIt n(g); n!=INVALID ++n) ++count; // /// \endcode // class NodeIt : public Node { // public: // /// Default constructor // /// @warning The default constructor sets the iterator // /// to an undefined value. // NodeIt() { } // /// Copy constructor. // /// Copy constructor. // /// // NodeIt(const NodeIt&) { } // /// Invalid constructor \& conversion. // /// Initialize the iterator to be invalid. // /// \sa Invalid for more details. // NodeIt(Invalid) { } // /// Sets the iterator to the first node. // /// Sets the iterator to the first node of \c g. // /// // NodeIt(const StaticGraph& g) { } // /// Node -> NodeIt conversion. // /// Sets the iterator to the node of \c g pointed by the trivial // /// iterator n. // /// This feature necessitates that each time we // /// iterate the edge-set, the iteration order is the same. // NodeIt(const StaticGraph& g, const Node& n) { } // /// Next node. // /// Assign the iterator to the next node. // /// // NodeIt& operator++() { return *this; } // }; // /// The base type of the edge iterators. // /// The base type of the edge iterators. // /// // class Edge { // public: // /// Default constructor // /// @warning The default constructor sets the iterator // /// to an undefined value. // Edge() { } // /// Copy constructor. // /// Copy constructor. // /// // Edge(const Edge&) { } // /// Initialize the iterator to be invalid. // /// Initialize the iterator to be invalid. // /// // Edge(Invalid) { } // /// Equality operator // /// Two iterators are equal if and only if they point to the // /// same object or both are invalid. // bool operator==(Edge) const { return true; } // /// Inequality operator // /// \sa operator==(Node n) // /// // bool operator!=(Edge) const { return true; } // ///Comparison operator. // ///This is a strict ordering between the nodes. // /// // ///This ordering can be different from the order in which NodeIt // ///goes through the nodes. // ///\todo Possibly we don't need it. // bool operator<(Edge) const { return true; } // }; // /// This iterator goes trough the outgoing edges of a node. // /// This iterator goes trough the \e outgoing edges of a certain node // /// of a graph. // /// Its usage is quite simple, for example you can count the number // /// of outgoing edges of a node \c n // /// in graph \c g of type \c Graph as follows. // /// \code // /// int count=0; // /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count; // /// \endcode // class OutEdgeIt : public Edge { // public: // /// Default constructor // /// @warning The default constructor sets the iterator // /// to an undefined value. // OutEdgeIt() { } // /// Copy constructor. // /// Copy constructor. // /// // OutEdgeIt(const OutEdgeIt&) { } // /// Initialize the iterator to be invalid. // /// Initialize the iterator to be invalid. // /// // OutEdgeIt(Invalid) { } // /// This constructor sets the iterator to first outgoing edge. // /// This constructor set the iterator to the first outgoing edge of // /// node // ///@param n the node // ///@param g the graph // OutEdgeIt(const StaticGraph& g, const Node& n) { } // /// Edge -> OutEdgeIt conversion // /// Sets the iterator to the value of the trivial iterator \c e. // /// This feature necessitates that each time we // /// iterate the edge-set, the iteration order is the same. // OutEdgeIt(const StaticGraph& g, const Edge& e) { } // ///Next outgoing edge // /// Assign the iterator to the next // /// outgoing edge of the corresponding node. // OutEdgeIt& operator++() { return *this; } // }; // /// This iterator goes trough the incoming edges of a node. // /// This iterator goes trough the \e incoming edges of a certain node // /// of a graph. // /// Its usage is quite simple, for example you can count the number // /// of outgoing edges of a node \c n // /// in graph \c g of type \c Graph as follows. // /// \code // /// int count=0; // /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count; // /// \endcode // class InEdgeIt : public Edge { // public: // /// Default constructor // /// @warning The default constructor sets the iterator // /// to an undefined value. // InEdgeIt() { } // /// Copy constructor. // /// Copy constructor. // /// // InEdgeIt(const InEdgeIt&) { } // /// Initialize the iterator to be invalid. // /// Initialize the iterator to be invalid. // /// // InEdgeIt(Invalid) { } // /// This constructor sets the iterator to first incoming edge. // /// This constructor set the iterator to the first incoming edge of // /// node // ///@param n the node // ///@param g the graph // InEdgeIt(const StaticGraph& g, const Node& n) { } // /// Edge -> InEdgeIt conversion // /// Sets the iterator to the value of the trivial iterator \c e. // /// This feature necessitates that each time we // /// iterate the edge-set, the iteration order is the same. // InEdgeIt(const StaticGraph& g, const Edge& n) { } // /// Next incoming edge // /// Assign the iterator to the next inedge of the corresponding node. // /// // InEdgeIt& operator++() { return *this; } // }; // /// This iterator goes through each edge. // /// This iterator goes through each edge of a graph. // /// Its usage is quite simple, for example you can count the number // /// of edges in a graph \c g of type \c Graph as follows: // /// \code // /// int count=0; // /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count; // /// \endcode // class EdgeIt : public Edge { // public: // /// Default constructor // /// @warning The default constructor sets the iterator // /// to an undefined value. // EdgeIt() { } // /// Copy constructor. // /// Copy constructor. // /// // EdgeIt(const EdgeIt&) { } // /// Initialize the iterator to be invalid. // /// Initialize the iterator to be invalid. // /// // EdgeIt(Invalid) { } // /// This constructor sets the iterator to first edge. // /// This constructor set the iterator to the first edge of // /// node // ///@param g the graph // EdgeIt(const StaticGraph& g) { } // /// Edge -> EdgeIt conversion // /// Sets the iterator to the value of the trivial iterator \c e. // /// This feature necessitates that each time we // /// iterate the edge-set, the iteration order is the same. // EdgeIt(const StaticGraph&, const Edge&) { } // ///Next edge // /// Assign the iterator to the next // /// edge of the corresponding node. // EdgeIt& operator++() { return *this; } // }; // /// First node of the graph. // /// \retval i the first node. // /// \return the first node. // /// // NodeIt& first(NodeIt& i) const { return i; } // /// The first incoming edge. // /// The first incoming edge. // /// // InEdgeIt& first(InEdgeIt &i, Node) const { return i; } // /// The first outgoing edge. // /// The first outgoing edge. // /// // OutEdgeIt& first(OutEdgeIt& i, Node) const { return i; } // /// The first edge of the Graph. // /// The first edge of the Graph. // /// // EdgeIt& first(EdgeIt& i) const { return i; } // ///Gives back the head node of an edge. // ///Gives back the head node of an edge. // /// // Node head(Edge) const { return INVALID; } // ///Gives back the tail node of an edge. // ///Gives back the tail node of an edge. // /// // Node tail(Edge) const { return INVALID; } // ///Gives back the \e id of a node. // ///\warning Not all graph structures provide this feature. // /// // ///\todo Should each graph provide \c id? // int id(const Node&) const { return 0; } // ///Gives back the \e id of an edge. // ///\warning Not all graph structures provide this feature. // /// // ///\todo Should each graph provide \c id? // int id(const Edge&) const { return 0; } // ///\e // ///\todo Should it be in the concept? // /// // int nodeNum() const { return 0; } // ///\e // ///\todo Should it be in the concept? // /// // int edgeNum() const { return 0; } // ///Reference map of the nodes to type \c T. // /// \ingroup concept // ///Reference map of the nodes to type \c T. // /// \sa Reference // /// \warning Making maps that can handle bool type (NodeMap) // /// needs some extra attention! // template class NodeMap : public ReferenceMap< Node, T > // { // public: // ///\e // NodeMap(const StaticGraph&) { } // ///\e // NodeMap(const StaticGraph&, T) { } // ///Copy constructor // template NodeMap(const NodeMap&) { } // ///Assignment operator // template NodeMap& operator=(const NodeMap&) // { return *this; } // }; // ///Reference map of the edges to type \c T. // /// \ingroup concept // ///Reference map of the edges to type \c T. // /// \sa Reference // /// \warning Making maps that can handle bool type (EdgeMap) // /// needs some extra attention! // template class EdgeMap // : public ReferenceMap // { // public: // ///\e // EdgeMap(const StaticGraph&) { } // ///\e // EdgeMap(const StaticGraph&, T) { } // ///Copy constructor // template EdgeMap(const EdgeMap&) { } // ///Assignment operator // template EdgeMap &operator=(const EdgeMap&) // { return *this; } // }; // }; // struct DummyType { // int value; // DummyType() {} // DummyType(int p) : value(p) {} // DummyType& operator=(int p) { value = p; return *this;} // }; // ///\brief Checks whether \c G meets the // ///\ref lemon::concept::StaticGraph "StaticGraph" concept // template void checkCompileStaticGraph(Graph &G) // { // typedef typename Graph::Node Node; // typedef typename Graph::NodeIt NodeIt; // typedef typename Graph::Edge Edge; // typedef typename Graph::EdgeIt EdgeIt; // typedef typename Graph::InEdgeIt InEdgeIt; // typedef typename Graph::OutEdgeIt OutEdgeIt; // { // Node i; Node j(i); Node k(INVALID); // i=j; // bool b; b=true; // b=(i==INVALID); b=(i!=INVALID); // b=(i==j); b=(i!=j); b=(iNodeIt conversion // NodeIt ni(G,n); // } // { // Edge i; Edge j(i); Edge k(INVALID); // i=j; // bool b; b=true; // b=(i==INVALID); b=(i!=INVALID); // b=(i==j); b=(i!=j); b=(iEdgeIt conversion // EdgeIt ei(G,e); // } // { // Node n; // InEdgeIt i; InEdgeIt j(i); InEdgeIt k(INVALID); InEdgeIt l(G,n); // i=j; // j=G.first(i,n); // j=++i; // bool b; b=true; // b=(i==INVALID); b=(i!=INVALID); // Edge e(i); // e=i; // b=(i==j); b=(i!=j); b=(iInEdgeIt conversion // InEdgeIt ei(G,e); // } // { // Node n; // OutEdgeIt i; OutEdgeIt j(i); OutEdgeIt k(INVALID); OutEdgeIt l(G,n); // i=j; // j=G.first(i,n); // j=++i; // bool b; b=true; // b=(i==INVALID); b=(i!=INVALID); // Edge e(i); // e=i; // b=(i==j); b=(i!=j); b=(iOutEdgeIt conversion // OutEdgeIt ei(G,e); // } // { // Node n,m; // n=m=INVALID; // Edge e; // e=INVALID; // n=G.tail(e); // n=G.head(e); // } // // id tests // { Node n; int i=G.id(n); i=i; } // { Edge e; int i=G.id(e); i=i; } // //NodeMap tests // { // Node k; // typename Graph::template NodeMap m(G); // //Const map // typename Graph::template NodeMap const &cm = m; // //Inicialize with default value // typename Graph::template NodeMap mdef(G,12); // //Copy // typename Graph::template NodeMap mm(cm); // //Copy from another type // typename Graph::template NodeMap dm(cm); // //Copy to more complex type // typename Graph::template NodeMap em(cm); // int v; // v=m[k]; m[k]=v; m.set(k,v); // v=cm[k]; // m=cm; // dm=cm; //Copy from another type // em=cm; //Copy to more complex type // { // //Check the typedef's // typename Graph::template NodeMap::ValueType val; // val=1; // typename Graph::template NodeMap::KeyType key; // key = typename Graph::NodeIt(G); // } // } // { //bool NodeMap // Node k; // typename Graph::template NodeMap m(G); // typename Graph::template NodeMap const &cm = m; //Const map // //Inicialize with default value // typename Graph::template NodeMap mdef(G,12); // typename Graph::template NodeMap mm(cm); //Copy // typename Graph::template NodeMap dm(cm); //Copy from another type // bool v; // v=m[k]; m[k]=v; m.set(k,v); // v=cm[k]; // m=cm; // dm=cm; //Copy from another type // m=dm; //Copy to another type // { // //Check the typedef's // typename Graph::template NodeMap::ValueType val; // val=true; // typename Graph::template NodeMap::KeyType key; // key= typename Graph::NodeIt(G); // } // } // //EdgeMap tests // { // Edge k; // typename Graph::template EdgeMap m(G); // typename Graph::template EdgeMap const &cm = m; //Const map // //Inicialize with default value // typename Graph::template EdgeMap mdef(G,12); // typename Graph::template EdgeMap mm(cm); //Copy // typename Graph::template EdgeMap dm(cm);//Copy from another type // int v; // v=m[k]; m[k]=v; m.set(k,v); // v=cm[k]; // m=cm; // dm=cm; //Copy from another type // { // //Check the typedef's // typename Graph::template EdgeMap::ValueType val; // val=1; // typename Graph::template EdgeMap::KeyType key; // key= typename Graph::EdgeIt(G); // } // } // { //bool EdgeMap // Edge k; // typename Graph::template EdgeMap m(G); // typename Graph::template EdgeMap const &cm = m; //Const map // //Inicialize with default value // typename Graph::template EdgeMap mdef(G,12); // typename Graph::template EdgeMap mm(cm); //Copy // typename Graph::template EdgeMap dm(cm); //Copy from another type // bool v; // v=m[k]; m[k]=v; m.set(k,v); // v=cm[k]; // m=cm; // dm=cm; //Copy from another type // m=dm; //Copy to another type // { // //Check the typedef's // typename Graph::template EdgeMap::ValueType val; // val=true; // typename Graph::template EdgeMap::KeyType key; // key= typename Graph::EdgeIt(G); // } // } // } // /// An empty non-static graph class. // /// This class provides everything that \ref StaticGraph // /// with additional functionality which enables to build a // /// graph from scratch. // class ExtendableGraph : public StaticGraph // { // public: // /// Defalult constructor. // /// Defalult constructor. // /// // ExtendableGraph() { } // ///Add a new node to the graph. // /// \return the new node. // /// // Node addNode() { return INVALID; } // ///Add a new edge to the graph. // ///Add a new edge to the graph with tail node \c t // ///and head node \c h. // ///\return the new edge. // Edge addEdge(Node h, Node t) { return INVALID; } // /// Resets the graph. // /// This function deletes all edges and nodes of the graph. // /// It also frees the memory allocated to store them. // /// \todo It might belong to \ref ErasableGraph. // void clear() { } // }; // ///\brief Checks whether \c G meets the // ///\ref lemon::concept::ExtendableGraph "ExtendableGraph" concept // template void checkCompileExtendableGraph(Graph &G) // { // checkCompileStaticGraph(G); // typedef typename Graph::Node Node; // typedef typename Graph::NodeIt NodeIt; // typedef typename Graph::Edge Edge; // typedef typename Graph::EdgeIt EdgeIt; // typedef typename Graph::InEdgeIt InEdgeIt; // typedef typename Graph::OutEdgeIt OutEdgeIt; // Node n,m; // n=G.addNode(); // m=G.addNode(); // Edge e; // e=G.addEdge(n,m); // // G.clear(); // } // /// An empty erasable graph class. // /// This class is an extension of \ref ExtendableGraph. It also makes it // /// possible to erase edges or nodes. // class ErasableGraph : public ExtendableGraph // { // public: // /// Defalult constructor. // /// Defalult constructor. // /// // ErasableGraph() { } // /// Deletes a node. // /// Deletes node \c n node. // /// // void erase(Node n) { } // /// Deletes an edge. // /// Deletes edge \c e edge. // /// // void erase(Edge e) { } // }; // template void checkCompileGraphEraseEdge(Graph &G) // { // typename Graph::Edge e; // G.erase(e); // } // template void checkCompileGraphEraseNode(Graph &G) // { // typename Graph::Node n; // G.erase(n); // } // ///\brief Checks whether \c G meets the // ///\ref lemon::concept::EresableGraph "EresableGraph" concept // template void checkCompileErasableGraph(Graph &G) // { // checkCompileExtendableGraph(G); // checkCompileGraphEraseNode(G); // checkCompileGraphEraseEdge(G); // } // ///Checks whether a graph has findEdge() member function. // ///\todo findEdge() might be a global function. // /// // template void checkCompileGraphFindEdge(Graph &G) // { // typedef typename Graph::NodeIt Node; // typedef typename Graph::NodeIt NodeIt; // G.findEdge(NodeIt(G),++NodeIt(G),G.findEdge(NodeIt(G),++NodeIt(G))); // G.findEdge(Node(),Node(),G.findEdge(Node(),Node())); // } /************* New GraphBase stuff **************/ /// A minimal GraphBase concept /// This class describes a minimal concept which can be extended to a /// full-featured graph with \ref GraphFactory. class GraphBase { public: GraphBase() {} /// \bug Should we demand that Node and Edge be subclasses of the /// Graph class??? typedef GraphItem<'n'> Node; typedef GraphItem<'e'> Edge; // class Node : public BaseGraphItem<'n'> {}; // class Edge : public BaseGraphItem<'e'> {}; // Graph operation void firstNode(Node &n) const { } void firstEdge(Edge &e) const { } void firstOutEdge(Edge &e, Node) const { } void firstInEdge(Edge &e, Node) const { } void nextNode(Node &n) const { } void nextEdge(Edge &e) const { } // Question: isn't it reasonable if this methods have a Node // parameter? Like this: // Edge& nextOut(Edge &e, Node) const { return e; } void nextOutEdge(Edge &e) const { } void nextInEdge(Edge &e) const { } Node head(Edge) const { return Node(); } Node tail(Edge) const { return Node(); } // Do we need id, nodeNum, edgeNum and co. in this basic graphbase // concept? // Maps. // // We need a special slimer concept which does not provide maps (it // wouldn't be strictly slimer, cause for map-factory id() & friends // a required...) template class NodeMap : public GraphMap {}; template class EdgeMap : public GraphMap {}; }; /**************** The full-featured graph concepts ****************/ class StaticGraph : virtual public BaseGraphComponent, public IterableGraphComponent, public MappableGraphComponent { public: typedef BaseGraphComponent::Node Node; typedef BaseGraphComponent::Edge Edge; }; template struct StaticGraphConcept { void constraints() { function_requires >(); function_requires >(); } }; class ExtendableGraph : virtual public BaseGraphComponent, public StaticGraph, public ExtendableGraphComponent, public ClearableGraphComponent { public: typedef BaseGraphComponent::Node Node; typedef BaseGraphComponent::Edge Edge; }; template struct ExtendableGraphConcept { void constraints() { function_requires >(); function_requires >(); function_requires >(); } }; class ErasableGraph : virtual public BaseGraphComponent, public ExtendableGraph, public ErasableGraphComponent { public: typedef BaseGraphComponent::Node Node; typedef BaseGraphComponent::Edge Edge; }; template struct ErasableGraphConcept { void constraints() { function_requires >(); function_requires >(); } }; // @} } //namespace concept } //namespace lemon #endif // LEMON_CONCEPT_GRAPH_H