COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/concept/ugraph.h @ 2117:96efb4fa0736

Last change on this file since 2117:96efb4fa0736 was 2111:ea1fa1bc3f6d, checked in by Balazs Dezso, 18 years ago

Removing concepts for extendable and erasable graphs
Renaming StaticGraph? to Graph

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