COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/concepts/ugraph.h @ 2474:e6368948d5f7

Last change on this file since 2474:e6368948d5f7 was 2474:e6368948d5f7, checked in by Peter Kovacs, 17 years ago

Small bug fixes and changes in the documentation.

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