COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/skeletons/graph.h @ 937:d4e911acef3d

Last change on this file since 937:d4e911acef3d was 921:818510fa3d99, checked in by Alpar Juttner, 20 years ago

hugo -> lemon

File size: 13.9 KB
RevLine 
[906]1/* -*- C++ -*-
[921]2 * src/lemon/skeletons/graph.h - Part of LEMON, a generic C++ optimization library
[906]3 *
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, 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
[921]17#ifndef LEMON_SKELETON_GRAPH_H
18#define LEMON_SKELETON_GRAPH_H
[52]19
[794]20///\ingroup skeletons
[242]21///\file
[880]22///\brief Declaration of Graph.
[242]23
[921]24#include <lemon/invalid.h>
25#include <lemon/skeletons/maps.h>
[145]26
[921]27namespace lemon {
[732]28  namespace skeleton {
29   
[794]30    /// \addtogroup skeletons
31    /// @{
[163]32
[732]33    /// An empty static graph class.
34 
35    /// This class provides all the common features of a graph structure,
36    /// however completely without implementations and real data structures
37    /// behind the interface.
38    /// All graph algorithms should compile with this class, but it will not
39    /// run properly, of course.
40    ///
41    /// It can be used for checking the interface compatibility,
42    /// or it can serve as a skeleton of a new graph structure.
43    ///
44    /// Also, you will find here the full documentation of a certain graph
45    /// feature, the documentation of a real graph imlementation
46    /// like @ref ListGraph or
47    /// @ref SmartGraph will just refer to this structure.
[880]48    class StaticGraph
[732]49    {
50    public:
51      /// Defalult constructor.
[801]52
53      /// Defalult constructor.
54      ///
[880]55      StaticGraph() { }
[732]56      ///Copy consructor.
[163]57
[801]58//       ///\todo It is not clear, what we expect from a copy constructor.
59//       ///E.g. How to assign the nodes/edges to each other? What about maps?
[880]60//       StaticGraph(const StaticGraph& g) { }
[732]61
[774]62      /// The base type of node iterators,
63      /// or in other words, the trivial node iterator.
[732]64
[774]65      /// This is the base type of each node iterator,
66      /// thus each kind of node iterator converts to this.
[801]67      /// More precisely each kind of node iterator should be inherited
[774]68      /// from the trivial node iterator.
[732]69      class Node {
70      public:
[801]71        /// Default constructor
72
[732]73        /// @warning The default constructor sets the iterator
74        /// to an undefined value.
[774]75        Node() { }
76        /// Copy constructor.
[801]77
78        /// Copy constructor.
79        ///
[774]80        Node(const Node&) { }
[801]81
[732]82        /// Invalid constructor \& conversion.
83
84        /// This constructor initializes the iterator to be invalid.
85        /// \sa Invalid for more details.
[774]86        Node(Invalid) { }
[801]87        /// Equality operator
88
[732]89        /// Two iterators are equal if and only if they point to the
90        /// same object or both are invalid.
91        bool operator==(Node) const { return true; }
92
[801]93        /// Inequality operator
94       
[911]95        /// \sa operator==(Node n)
[732]96        ///
97        bool operator!=(Node) const { return true; }
98
[801]99        ///Comparison operator.
100
101        ///This is a strict ordering between the nodes.
102        ///
103        ///This ordering can be different from the order in which NodeIt
104        ///goes through the nodes.
105        ///\todo Possibly we don't need it.
[732]106        bool operator<(Node) const { return true; }
107      };
108   
109      /// This iterator goes through each node.
110
111      /// This iterator goes through each node.
112      /// Its usage is quite simple, for example you can count the number
[774]113      /// of nodes in graph \c g of type \c Graph like this:
[732]114      /// \code
[774]115      /// int count=0;
[801]116      /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
[732]117      /// \endcode
118      class NodeIt : public Node {
119      public:
[801]120        /// Default constructor
121
[732]122        /// @warning The default constructor sets the iterator
123        /// to an undefined value.
[774]124        NodeIt() { }
125        /// Copy constructor.
[801]126       
127        /// Copy constructor.
128        ///
[774]129        NodeIt(const NodeIt&) { }
[732]130        /// Invalid constructor \& conversion.
131
[774]132        /// Initialize the iterator to be invalid.
[732]133        /// \sa Invalid for more details.
[774]134        NodeIt(Invalid) { }
[801]135        /// Sets the iterator to the first node.
136
[774]137        /// Sets the iterator to the first node of \c g.
[801]138        ///
[880]139        NodeIt(const StaticGraph& g) { }
[801]140        /// Node -> NodeIt conversion.
141
[774]142        /// Sets the iterator to the node of \c g pointed by the trivial
[801]143        /// iterator n.
144        /// This feature necessitates that each time we
145        /// iterate the edge-set, the iteration order is the same.
[880]146        NodeIt(const StaticGraph& g, const Node& n) { }
[801]147        /// Next node.
148
[774]149        /// Assign the iterator to the next node.
[801]150        ///
[774]151        NodeIt& operator++() { return *this; }
[732]152      };
153   
154   
155      /// The base type of the edge iterators.
[801]156
157      /// The base type of the edge iterators.
158      ///
[732]159      class Edge {
160      public:
[801]161        /// Default constructor
162
[732]163        /// @warning The default constructor sets the iterator
164        /// to an undefined value.
[774]165        Edge() { }
166        /// Copy constructor.
[801]167
168        /// Copy constructor.
169        ///
[774]170        Edge(const Edge&) { }
171        /// Initialize the iterator to be invalid.
[801]172
173        /// Initialize the iterator to be invalid.
174        ///
[774]175        Edge(Invalid) { }
[801]176        /// Equality operator
177
[732]178        /// Two iterators are equal if and only if they point to the
179        /// same object or both are invalid.
180        bool operator==(Edge) const { return true; }
[801]181        /// Inequality operator
182
[911]183        /// \sa operator==(Node n)
[801]184        ///
[732]185        bool operator!=(Edge) const { return true; }
[801]186        ///Comparison operator.
187
188        ///This is a strict ordering between the nodes.
189        ///
190        ///This ordering can be different from the order in which NodeIt
191        ///goes through the nodes.
192        ///\todo Possibly we don't need it.
193        bool operator<(Edge) const { return true; }
[732]194      };
195   
196      /// This iterator goes trough the outgoing edges of a node.
197
198      /// This iterator goes trough the \e outgoing edges of a certain node
199      /// of a graph.
200      /// Its usage is quite simple, for example you can count the number
201      /// of outgoing edges of a node \c n
[774]202      /// in graph \c g of type \c Graph as follows.
[732]203      /// \code
[774]204      /// int count=0;
[801]205      /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
[732]206      /// \endcode
207   
208      class OutEdgeIt : public Edge {
209      public:
[801]210        /// Default constructor
211
[732]212        /// @warning The default constructor sets the iterator
213        /// to an undefined value.
[774]214        OutEdgeIt() { }
215        /// Copy constructor.
[801]216
217        /// Copy constructor.
218        ///
[774]219        OutEdgeIt(const OutEdgeIt&) { }
220        /// Initialize the iterator to be invalid.
[801]221
222        /// Initialize the iterator to be invalid.
223        ///
[774]224        OutEdgeIt(Invalid) { }
[732]225        /// This constructor sets the iterator to first outgoing edge.
226   
227        /// This constructor set the iterator to the first outgoing edge of
228        /// node
229        ///@param n the node
[774]230        ///@param g the graph
[880]231        OutEdgeIt(const StaticGraph& g, const Node& n) { }
[801]232        /// Edge -> OutEdgeIt conversion
233
[774]234        /// Sets the iterator to the value of the trivial iterator \c e.
235        /// This feature necessitates that each time we
236        /// iterate the edge-set, the iteration order is the same.
[880]237        OutEdgeIt(const StaticGraph& g, const Edge& e) { }
[801]238        ///Next outgoing edge
239       
240        /// Assign the iterator to the next
241        /// outgoing edge of the corresponding node.
[774]242        OutEdgeIt& operator++() { return *this; }
[732]243      };
244
245      /// This iterator goes trough the incoming edges of a node.
246
247      /// This iterator goes trough the \e incoming edges of a certain node
248      /// of a graph.
249      /// Its usage is quite simple, for example you can count the number
250      /// of outgoing edges of a node \c n
[774]251      /// in graph \c g of type \c Graph as follows.
[732]252      /// \code
[774]253      /// int count=0;
[801]254      /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
[732]255      /// \endcode
256
257      class InEdgeIt : public Edge {
258      public:
[801]259        /// Default constructor
260
[732]261        /// @warning The default constructor sets the iterator
262        /// to an undefined value.
[774]263        InEdgeIt() { }
264        /// Copy constructor.
[801]265
266        /// Copy constructor.
267        ///
[774]268        InEdgeIt(const InEdgeIt&) { }
269        /// Initialize the iterator to be invalid.
[801]270
271        /// Initialize the iterator to be invalid.
272        ///
[774]273        InEdgeIt(Invalid) { }
[801]274        /// This constructor sets the iterator to first incoming edge.
275   
276        /// This constructor set the iterator to the first incoming edge of
277        /// node
278        ///@param n the node
279        ///@param g the graph
[880]280        InEdgeIt(const StaticGraph& g, const Node& n) { }
[801]281        /// Edge -> InEdgeIt conversion
282
283        /// Sets the iterator to the value of the trivial iterator \c e.
284        /// This feature necessitates that each time we
285        /// iterate the edge-set, the iteration order is the same.
[880]286        InEdgeIt(const StaticGraph& g, const Edge& n) { }
[801]287        /// Next incoming edge
288
[774]289        /// Assign the iterator to the next inedge of the corresponding node.
[801]290        ///
[774]291        InEdgeIt& operator++() { return *this; }
[732]292      };
293      /// This iterator goes through each edge.
294
295      /// This iterator goes through each edge of a graph.
296      /// Its usage is quite simple, for example you can count the number
[774]297      /// of edges in a graph \c g of type \c Graph as follows:
[732]298      /// \code
[774]299      /// int count=0;
[801]300      /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
[732]301      /// \endcode
302      class EdgeIt : public Edge {
303      public:
[801]304        /// Default constructor
305
[732]306        /// @warning The default constructor sets the iterator
307        /// to an undefined value.
[774]308        EdgeIt() { }
309        /// Copy constructor.
[801]310
311        /// Copy constructor.
312        ///
[774]313        EdgeIt(const EdgeIt&) { }
314        /// Initialize the iterator to be invalid.
[801]315
316        /// Initialize the iterator to be invalid.
317        ///
[774]318        EdgeIt(Invalid) { }
[801]319        /// This constructor sets the iterator to first edge.
320   
321        /// This constructor set the iterator to the first edge of
322        /// node
323        ///@param g the graph
[880]324        EdgeIt(const StaticGraph& g) { }
[801]325        /// Edge -> EdgeIt conversion
326
327        /// Sets the iterator to the value of the trivial iterator \c e.
328        /// This feature necessitates that each time we
329        /// iterate the edge-set, the iteration order is the same.
[880]330        EdgeIt(const StaticGraph&, const Edge&) { }
[801]331        ///Next edge
332       
333        /// Assign the iterator to the next
334        /// edge of the corresponding node.
[774]335        EdgeIt& operator++() { return *this; }
[732]336      };
337
338      /// First node of the graph.
339
340      /// \retval i the first node.
341      /// \return the first node.
342      ///
[774]343      NodeIt& first(NodeIt& i) const { return i; }
[732]344
345      /// The first incoming edge.
[801]346
347      /// The first incoming edge.
348      ///
[774]349      InEdgeIt& first(InEdgeIt &i, Node) const { return i; }
[732]350      /// The first outgoing edge.
[801]351
352      /// The first outgoing edge.
353      ///
[774]354      OutEdgeIt& first(OutEdgeIt& i, Node) const { return i; }
[732]355      /// The first edge of the Graph.
[801]356
357      /// The first edge of the Graph.
358      ///
[774]359      EdgeIt& first(EdgeIt& i) const { return i; }
[732]360
[801]361      ///Gives back the head node of an edge.
[732]362
363      ///Gives back the head node of an edge.
[801]364      ///
[732]365      Node head(Edge) const { return INVALID; }
366      ///Gives back the tail node of an edge.
[801]367
368      ///Gives back the tail node of an edge.
369      ///
[732]370      Node tail(Edge) const { return INVALID; }
[163]371 
[732]372      ///Gives back the \e id of a node.
[182]373
[732]374      ///\warning Not all graph structures provide this feature.
375      ///
[801]376      ///\todo Should each graph provide \c id?
[774]377      int id(const Node&) const { return 0; }
[732]378      ///Gives back the \e id of an edge.
[182]379
[732]380      ///\warning Not all graph structures provide this feature.
[182]381      ///
[801]382      ///\todo Should each graph provide \c id?
[774]383      int id(const Edge&) const { return 0; }
[182]384
[911]385      ///\e
[801]386     
[880]387      ///\todo Should it be in the concept?
[801]388      ///
[774]389      int nodeNum() const { return 0; }
[911]390      ///\e
[880]391
392      ///\todo Should it be in the concept?
[801]393      ///
[774]394      int edgeNum() const { return 0; }
[732]395
396
397      ///Reference map of the nodes to type \c T.
398
[880]399      /// \ingroup skeletons
[732]400      ///Reference map of the nodes to type \c T.
[880]401      /// \sa Reference
[732]402      /// \warning Making maps that can handle bool type (NodeMap<bool>)
[801]403      /// needs some extra attention!
[880]404      template<class T> class NodeMap : public ReferenceMap< Node, T >
[732]405      {
406      public:
407
[911]408        ///\e
[880]409        NodeMap(const StaticGraph&) { }
[911]410        ///\e
[880]411        NodeMap(const StaticGraph&, T) { }
[732]412
413        ///Copy constructor
[774]414        template<typename TT> NodeMap(const NodeMap<TT>&) { }
[732]415        ///Assignment operator
[774]416        template<typename TT> NodeMap& operator=(const NodeMap<TT>&)
417        { return *this; }
[732]418      };
419
420      ///Reference map of the edges to type \c T.
421
[880]422      /// \ingroup skeletons
[732]423      ///Reference map of the edges to type \c T.
[880]424      /// \sa Reference
[732]425      /// \warning Making maps that can handle bool type (EdgeMap<bool>)
[801]426      /// needs some extra attention!
[732]427      template<class T> class EdgeMap
428        : public ReferenceMap<Edge,T>
429      {
430      public:
431
[911]432        ///\e
[880]433        EdgeMap(const StaticGraph&) { }
[911]434        ///\e
[880]435        EdgeMap(const StaticGraph&, T) { }
[147]436   
[732]437        ///Copy constructor
[774]438        template<typename TT> EdgeMap(const EdgeMap<TT>&) { }
[732]439        ///Assignment operator
[774]440        template<typename TT> EdgeMap &operator=(const EdgeMap<TT>&)
441        { return *this; }
[732]442      };
[163]443    };
444
[186]445
[732]446 
[801]447    /// An empty non-static graph class.
[186]448
[880]449    /// This class provides everything that \ref StaticGraph
[732]450    /// with additional functionality which enables to build a
451    /// graph from scratch.
[880]452    class ExtendableGraph : public StaticGraph
[732]453    {
[163]454    public:
[732]455      /// Defalult constructor.
[801]456
457      /// Defalult constructor.
458      ///
[880]459      ExtendableGraph() { }
[732]460      ///Add a new node to the graph.
461
462      /// \return the new node.
463      ///
[774]464      Node addNode() { return INVALID; }
[732]465      ///Add a new edge to the graph.
466
[880]467      ///Add a new edge to the graph with tail node \c t
468      ///and head node \c h.
[732]469      ///\return the new edge.
[880]470      Edge addEdge(Node h, Node t) { return INVALID; }
[732]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.
[880]476      /// \todo It might belong to \ref ErasableGraph.
[774]477      void clear() { }
[163]478    };
479
[826]480    /// An empty erasable graph class.
[52]481 
[880]482    /// This class is an extension of \ref ExtendableGraph. It also makes it
[732]483    /// possible to erase edges or nodes.
[880]484    class ErasableGraph : public ExtendableGraph
[163]485    {
486    public:
[801]487      /// Defalult constructor.
488
489      /// Defalult constructor.
490      ///
[880]491      ErasableGraph() { }
[732]492      /// Deletes a node.
[801]493
494      /// Deletes node \c n node.
495      ///
[774]496      void erase(Node n) { }
[732]497      /// Deletes an edge.
[801]498
499      /// Deletes edge \c e edge.
500      ///
[774]501      void erase(Edge e) { }
[163]502    };
503
[732]504    // @}
[801]505  } //namespace skeleton 
[921]506} //namespace lemon
[52]507
[145]508
509
[921]510#endif // LEMON_SKELETON_GRAPH_H
Note: See TracBrowser for help on using the repository browser.