3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 ///\ingroup graph_concepts
21 ///\brief The concept of the undirected graphs.
24 #ifndef LEMON_CONCEPT_UGRAPH_H
25 #define LEMON_CONCEPT_UGRAPH_H
27 #include <lemon/concepts/graph_components.h>
28 #include <lemon/concepts/graph.h>
29 #include <lemon/bits/utility.h>
34 /// \addtogroup graph_concepts
38 /// \brief Class describing the concept of Undirected Graphs.
40 /// This class describes the common interface of all Undirected
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 course.
48 /// The LEMON undirected graphs also fulfill the concept of
49 /// directed graphs (\ref lemon::concepts::Graph "Graph
50 /// Concept"). Each undirected edges can be seen as two opposite
51 /// directed edge and consequently the undirected graph can be
52 /// seen as the direceted graph of these directed edges. The
53 /// UGraph has the UEdge inner class for the undirected edges and
54 /// the Edge type for the directed edges. The Edge type is
55 /// convertible to UEdge or inherited from it so from a directed
56 /// edge we can get the represented undirected edge.
58 /// In the sense of the LEMON each undirected edge has a default
59 /// direction (it should be in every computer implementation,
60 /// because the order of undirected edge's nodes defines an
61 /// orientation). With the default orientation we can define that
62 /// the directed edge is forward or backward directed. With the \c
63 /// direction() and \c direct() function we can get the direction
64 /// of the directed edge and we can direct an undirected edge.
66 /// The UEdgeIt is an iterator for the undirected edges. We can use
67 /// the UEdgeMap to map values for the undirected edges. The InEdgeIt and
68 /// OutEdgeIt iterates on the same undirected edges but with opposite
69 /// direction. The IncEdgeIt iterates also on the same undirected edges
70 /// as the OutEdgeIt and InEdgeIt but it is not convertible to Edge just
74 /// \brief The undirected graph should be tagged by the
77 /// The undirected graph should be tagged by the UndirectedTag. This
78 /// tag helps the enable_if technics to make compile time
79 /// specializations for undirected graphs.
80 typedef True UndirectedTag;
82 /// \brief The base type of node iterators,
83 /// or in other words, the trivial node iterator.
85 /// This is the base type of each node iterator,
86 /// thus each kind of node iterator converts to this.
87 /// More precisely each kind of node iterator should be inherited
88 /// from the trivial node iterator.
91 /// Default constructor
93 /// @warning The default constructor sets the iterator
94 /// to an undefined value.
100 Node(const Node&) { }
102 /// Invalid constructor \& conversion.
104 /// This constructor initializes the iterator to be invalid.
105 /// \sa Invalid for more details.
107 /// Equality operator
109 /// Two iterators are equal if and only if they point to the
110 /// same object or both are invalid.
111 bool operator==(Node) const { return true; }
113 /// Inequality operator
115 /// \sa operator==(Node n)
117 bool operator!=(Node) const { return true; }
119 /// Artificial ordering operator.
121 /// To allow the use of graph descriptors as key type in std::map or
122 /// similar associative container we require this.
124 /// \note This operator only have to define some strict ordering of
125 /// the items; this order has nothing to do with the iteration
126 /// ordering of the items.
127 bool operator<(Node) const { return false; }
131 /// This iterator goes through each node.
133 /// This iterator goes through each node.
134 /// Its usage is quite simple, for example you can count the number
135 /// of nodes in graph \c g of type \c Graph like this:
138 /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
140 class NodeIt : public Node {
142 /// Default constructor
144 /// @warning The default constructor sets the iterator
145 /// to an undefined value.
147 /// Copy constructor.
149 /// Copy constructor.
151 NodeIt(const NodeIt& n) : Node(n) { }
152 /// Invalid constructor \& conversion.
154 /// Initialize the iterator to be invalid.
155 /// \sa Invalid for more details.
157 /// Sets the iterator to the first node.
159 /// Sets the iterator to the first node of \c g.
161 NodeIt(const UGraph&) { }
162 /// Node -> NodeIt conversion.
164 /// Sets the iterator to the node of \c the graph pointed by
165 /// the trivial iterator.
166 /// This feature necessitates that each time we
167 /// iterate the edge-set, the iteration order is the same.
168 NodeIt(const UGraph&, const Node&) { }
171 /// Assign the iterator to the next node.
173 NodeIt& operator++() { return *this; }
177 /// The base type of the undirected edge iterators.
179 /// The base type of the undirected edge iterators.
183 /// Default constructor
185 /// @warning The default constructor sets the iterator
186 /// to an undefined value.
188 /// Copy constructor.
190 /// Copy constructor.
192 UEdge(const UEdge&) { }
193 /// Initialize the iterator to be invalid.
195 /// Initialize the iterator to be invalid.
198 /// Equality operator
200 /// Two iterators are equal if and only if they point to the
201 /// same object or both are invalid.
202 bool operator==(UEdge) const { return true; }
203 /// Inequality operator
205 /// \sa operator==(UEdge n)
207 bool operator!=(UEdge) const { return true; }
209 /// Artificial ordering operator.
211 /// To allow the use of graph descriptors as key type in std::map or
212 /// similar associative container we require this.
214 /// \note This operator only have to define some strict ordering of
215 /// the items; this order has nothing to do with the iteration
216 /// ordering of the items.
217 bool operator<(UEdge) const { return false; }
220 /// This iterator goes through each undirected edge.
222 /// This iterator goes through each undirected edge of a graph.
223 /// Its usage is quite simple, for example you can count the number
224 /// of undirected edges in a graph \c g of type \c Graph as follows:
227 /// for(Graph::UEdgeIt e(g); e!=INVALID; ++e) ++count;
229 class UEdgeIt : public UEdge {
231 /// Default constructor
233 /// @warning The default constructor sets the iterator
234 /// to an undefined value.
236 /// Copy constructor.
238 /// Copy constructor.
240 UEdgeIt(const UEdgeIt& e) : UEdge(e) { }
241 /// Initialize the iterator to be invalid.
243 /// Initialize the iterator to be invalid.
246 /// This constructor sets the iterator to the first undirected edge.
248 /// This constructor sets the iterator to the first undirected edge.
249 UEdgeIt(const UGraph&) { }
250 /// UEdge -> UEdgeIt conversion
252 /// Sets the iterator to the value of the trivial iterator.
253 /// This feature necessitates that each time we
254 /// iterate the undirected edge-set, the iteration order is the
256 UEdgeIt(const UGraph&, const UEdge&) { }
257 /// Next undirected edge
259 /// Assign the iterator to the next undirected edge.
260 UEdgeIt& operator++() { return *this; }
263 /// \brief This iterator goes trough the incident undirected
266 /// This iterator goes trough the incident undirected edges
267 /// of a certain node of a graph. You should assume that the
268 /// loop edges will be iterated twice.
270 /// Its usage is quite simple, for example you can compute the
271 /// degree (i.e. count the number of incident edges of a node \c n
272 /// in graph \c g of type \c Graph as follows.
276 /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
278 class IncEdgeIt : public UEdge {
280 /// Default constructor
282 /// @warning The default constructor sets the iterator
283 /// to an undefined value.
285 /// Copy constructor.
287 /// Copy constructor.
289 IncEdgeIt(const IncEdgeIt& e) : UEdge(e) { }
290 /// Initialize the iterator to be invalid.
292 /// Initialize the iterator to be invalid.
294 IncEdgeIt(Invalid) { }
295 /// This constructor sets the iterator to first incident edge.
297 /// This constructor set the iterator to the first incident edge of
299 IncEdgeIt(const UGraph&, const Node&) { }
300 /// UEdge -> IncEdgeIt conversion
302 /// Sets the iterator to the value of the trivial iterator \c e.
303 /// This feature necessitates that each time we
304 /// iterate the edge-set, the iteration order is the same.
305 IncEdgeIt(const UGraph&, const UEdge&) { }
306 /// Next incident edge
308 /// Assign the iterator to the next incident edge
309 /// of the corresponding node.
310 IncEdgeIt& operator++() { return *this; }
313 /// The directed edge type.
315 /// The directed edge type. It can be converted to the
316 /// undirected edge or it should be inherited from the undirected
318 class Edge : public UEdge {
320 /// Default constructor
322 /// @warning The default constructor sets the iterator
323 /// to an undefined value.
325 /// Copy constructor.
327 /// Copy constructor.
329 Edge(const Edge& e) : UEdge(e) { }
330 /// Initialize the iterator to be invalid.
332 /// Initialize the iterator to be invalid.
335 /// Equality operator
337 /// Two iterators are equal if and only if they point to the
338 /// same object or both are invalid.
339 bool operator==(Edge) const { return true; }
340 /// Inequality operator
342 /// \sa operator==(Edge n)
344 bool operator!=(Edge) const { return true; }
346 /// Artificial ordering operator.
348 /// To allow the use of graph descriptors as key type in std::map or
349 /// similar associative container we require this.
351 /// \note This operator only have to define some strict ordering of
352 /// the items; this order has nothing to do with the iteration
353 /// ordering of the items.
354 bool operator<(Edge) const { return false; }
357 /// This iterator goes through each directed edge.
359 /// This iterator goes through each edge of a graph.
360 /// Its usage is quite simple, for example you can count the number
361 /// of edges in a graph \c g of type \c Graph as follows:
364 /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
366 class EdgeIt : public Edge {
368 /// Default constructor
370 /// @warning The default constructor sets the iterator
371 /// to an undefined value.
373 /// Copy constructor.
375 /// Copy constructor.
377 EdgeIt(const EdgeIt& e) : Edge(e) { }
378 /// Initialize the iterator to be invalid.
380 /// Initialize the iterator to be invalid.
383 /// This constructor sets the iterator to the first edge.
385 /// This constructor sets the iterator to the first edge of \c g.
386 ///@param g the graph
387 EdgeIt(const UGraph &g) { ignore_unused_variable_warning(g); }
388 /// Edge -> EdgeIt conversion
390 /// Sets the iterator to the value of the trivial iterator \c e.
391 /// This feature necessitates that each time we
392 /// iterate the edge-set, the iteration order is the same.
393 EdgeIt(const UGraph&, const Edge&) { }
396 /// Assign the iterator to the next edge.
397 EdgeIt& operator++() { return *this; }
400 /// This iterator goes trough the outgoing directed edges of a node.
402 /// This iterator goes trough the \e outgoing edges of a certain node
404 /// Its usage is quite simple, for example you can count the number
405 /// of outgoing edges of a node \c n
406 /// in graph \c g of type \c Graph as follows.
409 /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
412 class OutEdgeIt : public Edge {
414 /// Default constructor
416 /// @warning The default constructor sets the iterator
417 /// to an undefined value.
419 /// Copy constructor.
421 /// Copy constructor.
423 OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
424 /// Initialize the iterator to be invalid.
426 /// Initialize the iterator to be invalid.
428 OutEdgeIt(Invalid) { }
429 /// This constructor sets the iterator to the first outgoing edge.
431 /// This constructor sets the iterator to the first outgoing edge of
434 ///@param g the graph
435 OutEdgeIt(const UGraph& n, const Node& g) {
436 ignore_unused_variable_warning(n);
437 ignore_unused_variable_warning(g);
439 /// Edge -> OutEdgeIt conversion
441 /// Sets the iterator to the value of the trivial iterator.
442 /// This feature necessitates that each time we
443 /// iterate the edge-set, the iteration order is the same.
444 OutEdgeIt(const UGraph&, const Edge&) { }
445 ///Next outgoing edge
447 /// Assign the iterator to the next
448 /// outgoing edge of the corresponding node.
449 OutEdgeIt& operator++() { return *this; }
452 /// This iterator goes trough the incoming directed edges of a node.
454 /// This iterator goes trough the \e incoming edges of a certain node
456 /// Its usage is quite simple, for example you can count the number
457 /// of outgoing edges of a node \c n
458 /// in graph \c g of type \c Graph as follows.
461 /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
464 class InEdgeIt : public Edge {
466 /// Default constructor
468 /// @warning The default constructor sets the iterator
469 /// to an undefined value.
471 /// Copy constructor.
473 /// Copy constructor.
475 InEdgeIt(const InEdgeIt& e) : Edge(e) { }
476 /// Initialize the iterator to be invalid.
478 /// Initialize the iterator to be invalid.
480 InEdgeIt(Invalid) { }
481 /// This constructor sets the iterator to first incoming edge.
483 /// This constructor set the iterator to the first incoming edge of
486 ///@param g the graph
487 InEdgeIt(const UGraph& g, const Node& n) {
488 ignore_unused_variable_warning(n);
489 ignore_unused_variable_warning(g);
491 /// Edge -> InEdgeIt conversion
493 /// Sets the iterator to the value of the trivial iterator \c e.
494 /// This feature necessitates that each time we
495 /// iterate the edge-set, the iteration order is the same.
496 InEdgeIt(const UGraph&, const Edge&) { }
497 /// Next incoming edge
499 /// Assign the iterator to the next inedge of the corresponding node.
501 InEdgeIt& operator++() { return *this; }
504 /// \brief Read write map of the nodes to type \c T.
506 /// ReadWrite map of the nodes to type \c T.
509 class NodeMap : public ReadWriteMap< Node, T >
514 NodeMap(const UGraph&) { }
516 NodeMap(const UGraph&, T) { }
519 NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
520 ///Assignment operator
521 template <typename CMap>
522 NodeMap& operator=(const CMap&) {
523 checkConcept<ReadMap<Node, T>, CMap>();
528 /// \brief Read write map of the directed edges to type \c T.
530 /// Reference map of the directed edges to type \c T.
533 class EdgeMap : public ReadWriteMap<Edge,T>
538 EdgeMap(const UGraph&) { }
540 EdgeMap(const UGraph&, T) { }
542 EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
543 ///Assignment operator
544 template <typename CMap>
545 EdgeMap& operator=(const CMap&) {
546 checkConcept<ReadMap<Edge, T>, CMap>();
551 /// Read write map of the undirected edges to type \c T.
553 /// Reference map of the edges to type \c T.
556 class UEdgeMap : public ReadWriteMap<UEdge,T>
561 UEdgeMap(const UGraph&) { }
563 UEdgeMap(const UGraph&, T) { }
565 UEdgeMap(const UEdgeMap& em) : ReadWriteMap<UEdge,T>(em) {}
566 ///Assignment operator
567 template <typename CMap>
568 UEdgeMap& operator=(const CMap&) {
569 checkConcept<ReadMap<UEdge, T>, CMap>();
574 /// \brief Direct the given undirected edge.
576 /// Direct the given undirected edge. The returned edge source
577 /// will be the given node.
578 Edge direct(const UEdge&, const Node&) const {
582 /// \brief Direct the given undirected edge.
584 /// Direct the given undirected edge. The returned edge
585 /// represents the given undirected edge and the direction comes
586 /// from the given bool. The source of the undirected edge and
587 /// the directed edge is the same when the given bool is true.
588 Edge direct(const UEdge&, bool) const {
592 /// \brief Returns true if the edge has default orientation.
594 /// Returns whether the given directed edge is same orientation as
595 /// the corresponding undirected edge's default orientation.
596 bool direction(Edge) const { return true; }
598 /// \brief Returns the opposite directed edge.
600 /// Returns the opposite directed edge.
601 Edge oppositeEdge(Edge) const { return INVALID; }
603 /// \brief Opposite node on an edge
605 /// \return the opposite of the given Node on the given UEdge
606 Node oppositeNode(Node, UEdge) const { return INVALID; }
608 /// \brief First node of the undirected edge.
610 /// \return the first node of the given UEdge.
612 /// Naturally undirected edges don't have direction and thus
613 /// don't have source and target node. But we use these two methods
614 /// to query the two nodes of the edge. The direction of the edge
615 /// which arises this way is called the inherent direction of the
616 /// undirected edge, and is used to define the "default" direction
617 /// of the directed versions of the edges.
619 Node source(UEdge) const { return INVALID; }
621 /// \brief Second node of the undirected edge.
622 Node target(UEdge) const { return INVALID; }
624 /// \brief Source node of the directed edge.
625 Node source(Edge) const { return INVALID; }
627 /// \brief Target node of the directed edge.
628 Node target(Edge) const { return INVALID; }
630 void first(Node&) const {}
631 void next(Node&) const {}
633 void first(UEdge&) const {}
634 void next(UEdge&) const {}
636 void first(Edge&) const {}
637 void next(Edge&) const {}
639 void firstOut(Edge&, Node) const {}
640 void nextOut(Edge&) const {}
642 void firstIn(Edge&, Node) const {}
643 void nextIn(Edge&) const {}
646 void firstInc(UEdge &, bool &, const Node &) const {}
647 void nextInc(UEdge &, bool &) const {}
649 /// \brief Base node of the iterator
651 /// Returns the base node (the source in this case) of the iterator
652 Node baseNode(OutEdgeIt e) const {
655 /// \brief Running node of the iterator
657 /// Returns the running node (the target in this case) of the
659 Node runningNode(OutEdgeIt e) const {
663 /// \brief Base node of the iterator
665 /// Returns the base node (the target in this case) of the iterator
666 Node baseNode(InEdgeIt e) const {
669 /// \brief Running node of the iterator
671 /// Returns the running node (the source in this case) of the
673 Node runningNode(InEdgeIt e) const {
677 /// \brief Base node of the iterator
679 /// Returns the base node of the iterator
680 Node baseNode(IncEdgeIt) const {
684 /// \brief Running node of the iterator
686 /// Returns the running node of the iterator
687 Node runningNode(IncEdgeIt) const {
691 template <typename Graph>
694 checkConcept<IterableUGraphComponent<>, Graph>();
695 checkConcept<MappableUGraphComponent<>, Graph>();