alpar@906: /* -*- C++ -*- alpar@906: * src/hugo/skeletons/graph.h - Part of HUGOlib, a generic C++ optimization library alpar@906: * alpar@906: * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@906: * (Egervary Combinatorial Optimization Research Group, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: alpar@503: #ifndef HUGO_SKELETON_GRAPH_H alpar@503: #define HUGO_SKELETON_GRAPH_H alpar@52: alpar@794: ///\ingroup skeletons alpar@242: ///\file alpar@880: ///\brief Declaration of Graph. alpar@242: ladanyi@542: #include alpar@732: #include alpar@145: alpar@163: namespace hugo { alpar@732: namespace skeleton { alpar@732: alpar@794: /// \addtogroup skeletons alpar@794: /// @{ alpar@163: alpar@732: /// An empty static graph class. alpar@732: alpar@732: /// This class provides all the common features of a graph structure, alpar@732: /// however completely without implementations and real data structures alpar@732: /// behind the interface. alpar@732: /// All graph algorithms should compile with this class, but it will not alpar@732: /// run properly, of course. alpar@732: /// alpar@732: /// It can be used for checking the interface compatibility, alpar@732: /// or it can serve as a skeleton of a new graph structure. alpar@732: /// alpar@732: /// Also, you will find here the full documentation of a certain graph alpar@732: /// feature, the documentation of a real graph imlementation alpar@732: /// like @ref ListGraph or alpar@732: /// @ref SmartGraph will just refer to this structure. alpar@880: class StaticGraph alpar@732: { alpar@732: public: alpar@732: /// Defalult constructor. alpar@801: alpar@801: /// Defalult constructor. alpar@801: /// alpar@880: StaticGraph() { } alpar@732: ///Copy consructor. alpar@163: alpar@801: // ///\todo It is not clear, what we expect from a copy constructor. alpar@801: // ///E.g. How to assign the nodes/edges to each other? What about maps? alpar@880: // StaticGraph(const StaticGraph& g) { } alpar@732: alpar@774: /// The base type of node iterators, alpar@774: /// or in other words, the trivial node iterator. alpar@732: alpar@774: /// This is the base type of each node iterator, alpar@774: /// thus each kind of node iterator converts to this. alpar@801: /// More precisely each kind of node iterator should be inherited alpar@774: /// from the trivial node iterator. alpar@732: class Node { alpar@732: public: alpar@801: /// Default constructor alpar@801: alpar@732: /// @warning The default constructor sets the iterator alpar@732: /// to an undefined value. alpar@774: Node() { } alpar@774: /// Copy constructor. alpar@801: alpar@801: /// Copy constructor. alpar@801: /// alpar@774: Node(const Node&) { } alpar@801: alpar@732: /// Invalid constructor \& conversion. alpar@732: alpar@732: /// This constructor initializes the iterator to be invalid. alpar@732: /// \sa Invalid for more details. alpar@774: Node(Invalid) { } alpar@801: /// Equality operator alpar@801: alpar@732: /// Two iterators are equal if and only if they point to the alpar@732: /// same object or both are invalid. alpar@732: bool operator==(Node) const { return true; } alpar@732: alpar@801: /// Inequality operator alpar@801: alpar@911: /// \sa operator==(Node n) alpar@732: /// alpar@732: bool operator!=(Node) const { return true; } alpar@732: alpar@801: ///Comparison operator. alpar@801: alpar@801: ///This is a strict ordering between the nodes. alpar@801: /// alpar@801: ///This ordering can be different from the order in which NodeIt alpar@801: ///goes through the nodes. alpar@801: ///\todo Possibly we don't need it. alpar@732: bool operator<(Node) const { return true; } alpar@732: }; alpar@732: alpar@732: /// This iterator goes through each node. alpar@732: alpar@732: /// This iterator goes through each node. alpar@732: /// Its usage is quite simple, for example you can count the number alpar@774: /// of nodes in graph \c g of type \c Graph like this: alpar@732: /// \code alpar@774: /// int count=0; alpar@801: /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count; alpar@732: /// \endcode alpar@732: class NodeIt : public Node { alpar@732: public: alpar@801: /// Default constructor alpar@801: alpar@732: /// @warning The default constructor sets the iterator alpar@732: /// to an undefined value. alpar@774: NodeIt() { } alpar@774: /// Copy constructor. alpar@801: alpar@801: /// Copy constructor. alpar@801: /// alpar@774: NodeIt(const NodeIt&) { } alpar@732: /// Invalid constructor \& conversion. alpar@732: alpar@774: /// Initialize the iterator to be invalid. alpar@732: /// \sa Invalid for more details. alpar@774: NodeIt(Invalid) { } alpar@801: /// Sets the iterator to the first node. alpar@801: alpar@774: /// Sets the iterator to the first node of \c g. alpar@801: /// alpar@880: NodeIt(const StaticGraph& g) { } alpar@801: /// Node -> NodeIt conversion. alpar@801: alpar@774: /// Sets the iterator to the node of \c g pointed by the trivial alpar@801: /// iterator n. alpar@801: /// This feature necessitates that each time we alpar@801: /// iterate the edge-set, the iteration order is the same. alpar@880: NodeIt(const StaticGraph& g, const Node& n) { } alpar@801: /// Next node. alpar@801: alpar@774: /// Assign the iterator to the next node. alpar@801: /// alpar@774: NodeIt& operator++() { return *this; } alpar@732: }; alpar@732: alpar@732: alpar@732: /// The base type of the edge iterators. alpar@801: alpar@801: /// The base type of the edge iterators. alpar@801: /// alpar@732: class Edge { alpar@732: public: alpar@801: /// Default constructor alpar@801: alpar@732: /// @warning The default constructor sets the iterator alpar@732: /// to an undefined value. alpar@774: Edge() { } alpar@774: /// Copy constructor. alpar@801: alpar@801: /// Copy constructor. alpar@801: /// alpar@774: Edge(const Edge&) { } alpar@774: /// Initialize the iterator to be invalid. alpar@801: alpar@801: /// Initialize the iterator to be invalid. alpar@801: /// alpar@774: Edge(Invalid) { } alpar@801: /// Equality operator alpar@801: alpar@732: /// Two iterators are equal if and only if they point to the alpar@732: /// same object or both are invalid. alpar@732: bool operator==(Edge) const { return true; } alpar@801: /// Inequality operator alpar@801: alpar@911: /// \sa operator==(Node n) alpar@801: /// alpar@732: bool operator!=(Edge) const { return true; } alpar@801: ///Comparison operator. alpar@801: alpar@801: ///This is a strict ordering between the nodes. alpar@801: /// alpar@801: ///This ordering can be different from the order in which NodeIt alpar@801: ///goes through the nodes. alpar@801: ///\todo Possibly we don't need it. alpar@801: bool operator<(Edge) const { return true; } alpar@732: }; alpar@732: alpar@732: /// This iterator goes trough the outgoing edges of a node. alpar@732: alpar@732: /// This iterator goes trough the \e outgoing edges of a certain node alpar@732: /// of a graph. alpar@732: /// Its usage is quite simple, for example you can count the number alpar@732: /// of outgoing edges of a node \c n alpar@774: /// in graph \c g of type \c Graph as follows. alpar@732: /// \code alpar@774: /// int count=0; alpar@801: /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count; alpar@732: /// \endcode alpar@732: alpar@732: class OutEdgeIt : public Edge { alpar@732: public: alpar@801: /// Default constructor alpar@801: alpar@732: /// @warning The default constructor sets the iterator alpar@732: /// to an undefined value. alpar@774: OutEdgeIt() { } alpar@774: /// Copy constructor. alpar@801: alpar@801: /// Copy constructor. alpar@801: /// alpar@774: OutEdgeIt(const OutEdgeIt&) { } alpar@774: /// Initialize the iterator to be invalid. alpar@801: alpar@801: /// Initialize the iterator to be invalid. alpar@801: /// alpar@774: OutEdgeIt(Invalid) { } alpar@732: /// This constructor sets the iterator to first outgoing edge. alpar@732: alpar@732: /// This constructor set the iterator to the first outgoing edge of alpar@732: /// node alpar@732: ///@param n the node alpar@774: ///@param g the graph alpar@880: OutEdgeIt(const StaticGraph& g, const Node& n) { } alpar@801: /// Edge -> OutEdgeIt conversion alpar@801: alpar@774: /// Sets the iterator to the value of the trivial iterator \c e. alpar@774: /// This feature necessitates that each time we alpar@774: /// iterate the edge-set, the iteration order is the same. alpar@880: OutEdgeIt(const StaticGraph& g, const Edge& e) { } alpar@801: ///Next outgoing edge alpar@801: alpar@801: /// Assign the iterator to the next alpar@801: /// outgoing edge of the corresponding node. alpar@774: OutEdgeIt& operator++() { return *this; } alpar@732: }; alpar@732: alpar@732: /// This iterator goes trough the incoming edges of a node. alpar@732: alpar@732: /// This iterator goes trough the \e incoming edges of a certain node alpar@732: /// of a graph. alpar@732: /// Its usage is quite simple, for example you can count the number alpar@732: /// of outgoing edges of a node \c n alpar@774: /// in graph \c g of type \c Graph as follows. alpar@732: /// \code alpar@774: /// int count=0; alpar@801: /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count; alpar@732: /// \endcode alpar@732: alpar@732: class InEdgeIt : public Edge { alpar@732: public: alpar@801: /// Default constructor alpar@801: alpar@732: /// @warning The default constructor sets the iterator alpar@732: /// to an undefined value. alpar@774: InEdgeIt() { } alpar@774: /// Copy constructor. alpar@801: alpar@801: /// Copy constructor. alpar@801: /// alpar@774: InEdgeIt(const InEdgeIt&) { } alpar@774: /// Initialize the iterator to be invalid. alpar@801: alpar@801: /// Initialize the iterator to be invalid. alpar@801: /// alpar@774: InEdgeIt(Invalid) { } alpar@801: /// This constructor sets the iterator to first incoming edge. alpar@801: alpar@801: /// This constructor set the iterator to the first incoming edge of alpar@801: /// node alpar@801: ///@param n the node alpar@801: ///@param g the graph alpar@880: InEdgeIt(const StaticGraph& g, const Node& n) { } alpar@801: /// Edge -> InEdgeIt conversion alpar@801: alpar@801: /// Sets the iterator to the value of the trivial iterator \c e. alpar@801: /// This feature necessitates that each time we alpar@801: /// iterate the edge-set, the iteration order is the same. alpar@880: InEdgeIt(const StaticGraph& g, const Edge& n) { } alpar@801: /// Next incoming edge alpar@801: alpar@774: /// Assign the iterator to the next inedge of the corresponding node. alpar@801: /// alpar@774: InEdgeIt& operator++() { return *this; } alpar@732: }; alpar@732: /// This iterator goes through each edge. alpar@732: alpar@732: /// This iterator goes through each edge of a graph. alpar@732: /// Its usage is quite simple, for example you can count the number alpar@774: /// of edges in a graph \c g of type \c Graph as follows: alpar@732: /// \code alpar@774: /// int count=0; alpar@801: /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count; alpar@732: /// \endcode alpar@732: class EdgeIt : public Edge { alpar@732: public: alpar@801: /// Default constructor alpar@801: alpar@732: /// @warning The default constructor sets the iterator alpar@732: /// to an undefined value. alpar@774: EdgeIt() { } alpar@774: /// Copy constructor. alpar@801: alpar@801: /// Copy constructor. alpar@801: /// alpar@774: EdgeIt(const EdgeIt&) { } alpar@774: /// Initialize the iterator to be invalid. alpar@801: alpar@801: /// Initialize the iterator to be invalid. alpar@801: /// alpar@774: EdgeIt(Invalid) { } alpar@801: /// This constructor sets the iterator to first edge. alpar@801: alpar@801: /// This constructor set the iterator to the first edge of alpar@801: /// node alpar@801: ///@param g the graph alpar@880: EdgeIt(const StaticGraph& g) { } alpar@801: /// Edge -> EdgeIt conversion alpar@801: alpar@801: /// Sets the iterator to the value of the trivial iterator \c e. alpar@801: /// This feature necessitates that each time we alpar@801: /// iterate the edge-set, the iteration order is the same. alpar@880: EdgeIt(const StaticGraph&, const Edge&) { } alpar@801: ///Next edge alpar@801: alpar@801: /// Assign the iterator to the next alpar@801: /// edge of the corresponding node. alpar@774: EdgeIt& operator++() { return *this; } alpar@732: }; alpar@732: alpar@732: /// First node of the graph. alpar@732: alpar@732: /// \retval i the first node. alpar@732: /// \return the first node. alpar@732: /// alpar@774: NodeIt& first(NodeIt& i) const { return i; } alpar@732: alpar@732: /// The first incoming edge. alpar@801: alpar@801: /// The first incoming edge. alpar@801: /// alpar@774: InEdgeIt& first(InEdgeIt &i, Node) const { return i; } alpar@732: /// The first outgoing edge. alpar@801: alpar@801: /// The first outgoing edge. alpar@801: /// alpar@774: OutEdgeIt& first(OutEdgeIt& i, Node) const { return i; } alpar@732: /// The first edge of the Graph. alpar@801: alpar@801: /// The first edge of the Graph. alpar@801: /// alpar@774: EdgeIt& first(EdgeIt& i) const { return i; } alpar@732: alpar@801: ///Gives back the head node of an edge. alpar@732: alpar@732: ///Gives back the head node of an edge. alpar@801: /// alpar@732: Node head(Edge) const { return INVALID; } alpar@732: ///Gives back the tail node of an edge. alpar@801: alpar@801: ///Gives back the tail node of an edge. alpar@801: /// alpar@732: Node tail(Edge) const { return INVALID; } alpar@163: alpar@732: ///Gives back the \e id of a node. alpar@182: alpar@732: ///\warning Not all graph structures provide this feature. alpar@732: /// alpar@801: ///\todo Should each graph provide \c id? alpar@774: int id(const Node&) const { return 0; } alpar@732: ///Gives back the \e id of an edge. alpar@182: alpar@732: ///\warning Not all graph structures provide this feature. alpar@182: /// alpar@801: ///\todo Should each graph provide \c id? alpar@774: int id(const Edge&) const { return 0; } alpar@182: alpar@911: ///\e alpar@801: alpar@880: ///\todo Should it be in the concept? alpar@801: /// alpar@774: int nodeNum() const { return 0; } alpar@911: ///\e alpar@880: alpar@880: ///\todo Should it be in the concept? alpar@801: /// alpar@774: int edgeNum() const { return 0; } alpar@732: alpar@732: alpar@732: ///Reference map of the nodes to type \c T. alpar@732: alpar@880: /// \ingroup skeletons alpar@732: ///Reference map of the nodes to type \c T. alpar@880: /// \sa Reference alpar@732: /// \warning Making maps that can handle bool type (NodeMap) alpar@801: /// needs some extra attention! alpar@880: template class NodeMap : public ReferenceMap< Node, T > alpar@732: { alpar@732: public: alpar@732: alpar@911: ///\e alpar@880: NodeMap(const StaticGraph&) { } alpar@911: ///\e alpar@880: NodeMap(const StaticGraph&, T) { } alpar@732: alpar@732: ///Copy constructor alpar@774: template NodeMap(const NodeMap&) { } alpar@732: ///Assignment operator alpar@774: template NodeMap& operator=(const NodeMap&) alpar@774: { return *this; } alpar@732: }; alpar@732: alpar@732: ///Reference map of the edges to type \c T. alpar@732: alpar@880: /// \ingroup skeletons alpar@732: ///Reference map of the edges to type \c T. alpar@880: /// \sa Reference alpar@732: /// \warning Making maps that can handle bool type (EdgeMap) alpar@801: /// needs some extra attention! alpar@732: template class EdgeMap alpar@732: : public ReferenceMap alpar@732: { alpar@732: public: alpar@732: alpar@911: ///\e alpar@880: EdgeMap(const StaticGraph&) { } alpar@911: ///\e alpar@880: EdgeMap(const StaticGraph&, T) { } alpar@147: alpar@732: ///Copy constructor alpar@774: template EdgeMap(const EdgeMap&) { } alpar@732: ///Assignment operator alpar@774: template EdgeMap &operator=(const EdgeMap&) alpar@774: { return *this; } alpar@732: }; alpar@163: }; alpar@163: alpar@186: alpar@732: alpar@801: /// An empty non-static graph class. alpar@186: alpar@880: /// This class provides everything that \ref StaticGraph alpar@732: /// with additional functionality which enables to build a alpar@732: /// graph from scratch. alpar@880: class ExtendableGraph : public StaticGraph alpar@732: { alpar@163: public: alpar@732: /// Defalult constructor. alpar@801: alpar@801: /// Defalult constructor. alpar@801: /// alpar@880: ExtendableGraph() { } alpar@732: ///Add a new node to the graph. alpar@732: alpar@732: /// \return the new node. alpar@732: /// alpar@774: Node addNode() { return INVALID; } alpar@732: ///Add a new edge to the graph. alpar@732: alpar@880: ///Add a new edge to the graph with tail node \c t alpar@880: ///and head node \c h. alpar@732: ///\return the new edge. alpar@880: Edge addEdge(Node h, Node t) { return INVALID; } alpar@732: alpar@732: /// Resets the graph. alpar@732: alpar@732: /// This function deletes all edges and nodes of the graph. alpar@732: /// It also frees the memory allocated to store them. alpar@880: /// \todo It might belong to \ref ErasableGraph. alpar@774: void clear() { } alpar@163: }; alpar@163: alpar@826: /// An empty erasable graph class. alpar@52: alpar@880: /// This class is an extension of \ref ExtendableGraph. It also makes it alpar@732: /// possible to erase edges or nodes. alpar@880: class ErasableGraph : public ExtendableGraph alpar@163: { alpar@163: public: alpar@801: /// Defalult constructor. alpar@801: alpar@801: /// Defalult constructor. alpar@801: /// alpar@880: ErasableGraph() { } alpar@732: /// Deletes a node. alpar@801: alpar@801: /// Deletes node \c n node. alpar@801: /// alpar@774: void erase(Node n) { } alpar@732: /// Deletes an edge. alpar@801: alpar@801: /// Deletes edge \c e edge. alpar@801: /// alpar@774: void erase(Edge e) { } alpar@163: }; alpar@163: alpar@732: // @} alpar@801: } //namespace skeleton marci@174: } //namespace hugo alpar@52: alpar@145: alpar@145: alpar@503: #endif // HUGO_SKELETON_GRAPH_H