3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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 Undirected Graphs.
23 #ifndef LEMON_CONCEPT_UGRAPH_H
24 #define LEMON_CONCEPT_UGRAPH_H
26 #include <lemon/concepts/graph_components.h>
27 #include <lemon/concepts/graph.h>
28 #include <lemon/bits/utility.h>
33 /// \ingroup graph_concepts
35 /// \brief Class describing the concept of Undirected Graphs.
37 /// This class describes the common interface of all Undirected
40 /// As all concept describing classes it provides only interface
41 /// without any sensible implementation. So any algorithm for
42 /// undirected graph should compile with this class, but it will not
43 /// run properly, of course.
45 /// The LEMON undirected graphs also fulfill the concept of
46 /// directed graphs (\ref lemon::concepts::Graph "Graph
47 /// Concept"). Each undirected edges can be seen as two opposite
48 /// directed edge and consequently the undirected graph can be
49 /// seen as the direceted graph of these directed edges. The
50 /// UGraph has the UEdge inner class for the undirected edges and
51 /// the Edge type for the directed edges. The Edge type is
52 /// convertible to UEdge or inherited from it so from a directed
53 /// edge we can get the represented undirected edge.
55 /// In the sense of the LEMON each undirected edge has a default
56 /// direction (it should be in every computer implementation,
57 /// because the order of undirected edge's nodes defines an
58 /// orientation). With the default orientation we can define that
59 /// the directed edge is forward or backward directed. With the \c
60 /// direction() and \c direct() function we can get the direction
61 /// of the directed edge and we can direct an undirected edge.
63 /// The UEdgeIt is an iterator for the undirected edges. We can use
64 /// the UEdgeMap to map values for the undirected edges. The InEdgeIt and
65 /// OutEdgeIt iterates on the same undirected edges but with opposite
66 /// direction. The IncEdgeIt iterates also on the same undirected edges
67 /// as the OutEdgeIt and InEdgeIt but it is not convertible to Edge just
71 /// \brief The undirected graph should be tagged by the
74 /// The undirected graph should be tagged by the UndirectedTag. This
75 /// tag helps the enable_if technics to make compile time
76 /// specializations for undirected graphs.
77 typedef True UndirectedTag;
79 /// \brief The base type of node iterators,
80 /// or in other words, the trivial node iterator.
82 /// This is the base type of each node iterator,
83 /// thus each kind of node iterator converts to this.
84 /// More precisely each kind of node iterator should be inherited
85 /// from the trivial node iterator.
88 /// Default constructor
90 /// @warning The default constructor sets the iterator
91 /// to an undefined value.
99 /// Invalid constructor \& conversion.
101 /// This constructor initializes the iterator to be invalid.
102 /// \sa Invalid for more details.
104 /// Equality operator
106 /// Two iterators are equal if and only if they point to the
107 /// same object or both are invalid.
108 bool operator==(Node) const { return true; }
110 /// Inequality operator
112 /// \sa operator==(Node n)
114 bool operator!=(Node) const { return true; }
116 /// Artificial ordering operator.
118 /// To allow the use of graph descriptors as key type in std::map or
119 /// similar associative container we require this.
121 /// \note This operator only have to define some strict ordering of
122 /// the items; this order has nothing to do with the iteration
123 /// ordering of the items.
124 bool operator<(Node) const { return false; }
128 /// This iterator goes through each node.
130 /// This iterator goes through each node.
131 /// Its usage is quite simple, for example you can count the number
132 /// of nodes in graph \c g of type \c Graph like this:
135 /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
137 class NodeIt : public Node {
139 /// Default constructor
141 /// @warning The default constructor sets the iterator
142 /// to an undefined value.
144 /// Copy constructor.
146 /// Copy constructor.
148 NodeIt(const NodeIt& n) : Node(n) { }
149 /// Invalid constructor \& conversion.
151 /// Initialize the iterator to be invalid.
152 /// \sa Invalid for more details.
154 /// Sets the iterator to the first node.
156 /// Sets the iterator to the first node of \c g.
158 NodeIt(const UGraph&) { }
159 /// Node -> NodeIt conversion.
161 /// Sets the iterator to the node of \c the graph pointed by
162 /// the trivial iterator.
163 /// This feature necessitates that each time we
164 /// iterate the edge-set, the iteration order is the same.
165 NodeIt(const UGraph&, const Node&) { }
168 /// Assign the iterator to the next node.
170 NodeIt& operator++() { return *this; }
174 /// The base type of the undirected edge iterators.
176 /// The base type of the undirected edge iterators.
180 /// Default constructor
182 /// @warning The default constructor sets the iterator
183 /// to an undefined value.
185 /// Copy constructor.
187 /// Copy constructor.
189 UEdge(const UEdge&) { }
190 /// Initialize the iterator to be invalid.
192 /// Initialize the iterator to be invalid.
195 /// Equality operator
197 /// Two iterators are equal if and only if they point to the
198 /// same object or both are invalid.
199 bool operator==(UEdge) const { return true; }
200 /// Inequality operator
202 /// \sa operator==(UEdge n)
204 bool operator!=(UEdge) const { return true; }
206 /// Artificial ordering operator.
208 /// To allow the use of graph descriptors as key type in std::map or
209 /// similar associative container we require this.
211 /// \note This operator only have to define some strict ordering of
212 /// the items; this order has nothing to do with the iteration
213 /// ordering of the items.
214 bool operator<(UEdge) const { return false; }
217 /// This iterator goes through each undirected edge.
219 /// This iterator goes through each undirected edge of a graph.
220 /// Its usage is quite simple, for example you can count the number
221 /// of undirected edges in a graph \c g of type \c Graph as follows:
224 /// for(Graph::UEdgeIt e(g); e!=INVALID; ++e) ++count;
226 class UEdgeIt : public UEdge {
228 /// Default constructor
230 /// @warning The default constructor sets the iterator
231 /// to an undefined value.
233 /// Copy constructor.
235 /// Copy constructor.
237 UEdgeIt(const UEdgeIt& e) : UEdge(e) { }
238 /// Initialize the iterator to be invalid.
240 /// Initialize the iterator to be invalid.
243 /// This constructor sets the iterator to the first undirected edge.
245 /// This constructor sets the iterator to the first undirected edge.
246 UEdgeIt(const UGraph&) { }
247 /// UEdge -> UEdgeIt conversion
249 /// Sets the iterator to the value of the trivial iterator.
250 /// This feature necessitates that each time we
251 /// iterate the undirected edge-set, the iteration order is the
253 UEdgeIt(const UGraph&, const UEdge&) { }
254 /// Next undirected edge
256 /// Assign the iterator to the next undirected edge.
257 UEdgeIt& operator++() { return *this; }
260 /// \brief This iterator goes trough the incident undirected
263 /// This iterator goes trough the incident undirected edges
264 /// of a certain node of a graph. You should assume that the
265 /// loop edges will be iterated twice.
267 /// Its usage is quite simple, for example you can compute the
268 /// degree (i.e. count the number of incident edges of a node \c n
269 /// in graph \c g of type \c Graph as follows.
273 /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
275 class IncEdgeIt : public UEdge {
277 /// Default constructor
279 /// @warning The default constructor sets the iterator
280 /// to an undefined value.
282 /// Copy constructor.
284 /// Copy constructor.
286 IncEdgeIt(const IncEdgeIt& e) : UEdge(e) { }
287 /// Initialize the iterator to be invalid.
289 /// Initialize the iterator to be invalid.
291 IncEdgeIt(Invalid) { }
292 /// This constructor sets the iterator to first incident edge.
294 /// This constructor set the iterator to the first incident edge of
296 IncEdgeIt(const UGraph&, const Node&) { }
297 /// UEdge -> IncEdgeIt conversion
299 /// Sets the iterator to the value of the trivial iterator \c e.
300 /// This feature necessitates that each time we
301 /// iterate the edge-set, the iteration order is the same.
302 IncEdgeIt(const UGraph&, const UEdge&) { }
303 /// Next incident edge
305 /// Assign the iterator to the next incident edge
306 /// of the corresponding node.
307 IncEdgeIt& operator++() { return *this; }
310 /// The directed edge type.
312 /// The directed edge type. It can be converted to the
313 /// undirected edge or it should be inherited from the undirected
315 class Edge : public UEdge {
317 /// Default constructor
319 /// @warning The default constructor sets the iterator
320 /// to an undefined value.
322 /// Copy constructor.
324 /// Copy constructor.
326 Edge(const Edge& e) : UEdge(e) { }
327 /// Initialize the iterator to be invalid.
329 /// Initialize the iterator to be invalid.
332 /// Equality operator
334 /// Two iterators are equal if and only if they point to the
335 /// same object or both are invalid.
336 bool operator==(Edge) const { return true; }
337 /// Inequality operator
339 /// \sa operator==(Edge n)
341 bool operator!=(Edge) const { return true; }
343 /// Artificial ordering operator.
345 /// To allow the use of graph descriptors as key type in std::map or
346 /// similar associative container we require this.
348 /// \note This operator only have to define some strict ordering of
349 /// the items; this order has nothing to do with the iteration
350 /// ordering of the items.
351 bool operator<(Edge) const { return false; }
354 /// This iterator goes through each directed edge.
356 /// This iterator goes through each edge of a graph.
357 /// Its usage is quite simple, for example you can count the number
358 /// of edges in a graph \c g of type \c Graph as follows:
361 /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
363 class EdgeIt : public Edge {
365 /// Default constructor
367 /// @warning The default constructor sets the iterator
368 /// to an undefined value.
370 /// Copy constructor.
372 /// Copy constructor.
374 EdgeIt(const EdgeIt& e) : Edge(e) { }
375 /// Initialize the iterator to be invalid.
377 /// Initialize the iterator to be invalid.
380 /// This constructor sets the iterator to the first edge.
382 /// This constructor sets the iterator to the first edge of \c g.
383 ///@param g the graph
384 EdgeIt(const UGraph &g) { ignore_unused_variable_warning(g); }
385 /// Edge -> EdgeIt conversion
387 /// Sets the iterator to the value of the trivial iterator \c e.
388 /// This feature necessitates that each time we
389 /// iterate the edge-set, the iteration order is the same.
390 EdgeIt(const UGraph&, const Edge&) { }
393 /// Assign the iterator to the next edge.
394 EdgeIt& operator++() { return *this; }
397 /// This iterator goes trough the outgoing directed edges of a node.
399 /// This iterator goes trough the \e outgoing edges of a certain node
401 /// Its usage is quite simple, for example you can count the number
402 /// of outgoing edges of a node \c n
403 /// in graph \c g of type \c Graph as follows.
406 /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
409 class OutEdgeIt : public Edge {
411 /// Default constructor
413 /// @warning The default constructor sets the iterator
414 /// to an undefined value.
416 /// Copy constructor.
418 /// Copy constructor.
420 OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
421 /// Initialize the iterator to be invalid.
423 /// Initialize the iterator to be invalid.
425 OutEdgeIt(Invalid) { }
426 /// This constructor sets the iterator to the first outgoing edge.
428 /// This constructor sets the iterator to the first outgoing edge of
431 ///@param g the graph
432 OutEdgeIt(const UGraph& n, const Node& g) {
433 ignore_unused_variable_warning(n);
434 ignore_unused_variable_warning(g);
436 /// Edge -> OutEdgeIt conversion
438 /// Sets the iterator to the value of the trivial iterator.
439 /// This feature necessitates that each time we
440 /// iterate the edge-set, the iteration order is the same.
441 OutEdgeIt(const UGraph&, const Edge&) { }
442 ///Next outgoing edge
444 /// Assign the iterator to the next
445 /// outgoing edge of the corresponding node.
446 OutEdgeIt& operator++() { return *this; }
449 /// This iterator goes trough the incoming directed edges of a node.
451 /// This iterator goes trough the \e incoming edges of a certain node
453 /// Its usage is quite simple, for example you can count the number
454 /// of outgoing edges of a node \c n
455 /// in graph \c g of type \c Graph as follows.
458 /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
461 class InEdgeIt : public Edge {
463 /// Default constructor
465 /// @warning The default constructor sets the iterator
466 /// to an undefined value.
468 /// Copy constructor.
470 /// Copy constructor.
472 InEdgeIt(const InEdgeIt& e) : Edge(e) { }
473 /// Initialize the iterator to be invalid.
475 /// Initialize the iterator to be invalid.
477 InEdgeIt(Invalid) { }
478 /// This constructor sets the iterator to first incoming edge.
480 /// This constructor set the iterator to the first incoming edge of
483 ///@param g the graph
484 InEdgeIt(const UGraph& g, const Node& n) {
485 ignore_unused_variable_warning(n);
486 ignore_unused_variable_warning(g);
488 /// Edge -> InEdgeIt conversion
490 /// Sets the iterator to the value of the trivial iterator \c e.
491 /// This feature necessitates that each time we
492 /// iterate the edge-set, the iteration order is the same.
493 InEdgeIt(const UGraph&, const Edge&) { }
494 /// Next incoming edge
496 /// Assign the iterator to the next inedge of the corresponding node.
498 InEdgeIt& operator++() { return *this; }
501 /// \brief Read write map of the nodes to type \c T.
503 /// ReadWrite map of the nodes to type \c T.
506 class NodeMap : public ReadWriteMap< Node, T >
511 NodeMap(const UGraph&) { }
513 NodeMap(const UGraph&, T) { }
516 NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
517 ///Assignment operator
518 template <typename CMap>
519 NodeMap& operator=(const CMap&) {
520 checkConcept<ReadMap<Node, T>, CMap>();
525 /// \brief Read write map of the directed edges to type \c T.
527 /// Reference map of the directed edges to type \c T.
530 class EdgeMap : public ReadWriteMap<Edge,T>
535 EdgeMap(const UGraph&) { }
537 EdgeMap(const UGraph&, T) { }
539 EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
540 ///Assignment operator
541 template <typename CMap>
542 EdgeMap& operator=(const CMap&) {
543 checkConcept<ReadMap<Edge, T>, CMap>();
548 /// Read write map of the undirected edges to type \c T.
550 /// Reference map of the edges to type \c T.
553 class UEdgeMap : public ReadWriteMap<UEdge,T>
558 UEdgeMap(const UGraph&) { }
560 UEdgeMap(const UGraph&, T) { }
562 UEdgeMap(const UEdgeMap& em) : ReadWriteMap<UEdge,T>(em) {}
563 ///Assignment operator
564 template <typename CMap>
565 UEdgeMap& operator=(const CMap&) {
566 checkConcept<ReadMap<UEdge, T>, CMap>();
571 /// \brief Direct the given undirected edge.
573 /// Direct the given undirected edge. The returned edge source
574 /// will be the given node.
575 Edge direct(const UEdge&, const Node&) const {
579 /// \brief Direct the given undirected edge.
581 /// Direct the given undirected edge. The returned edge
582 /// represents the given undirected edge and the direction comes
583 /// from the given bool. The source of the undirected edge and
584 /// the directed edge is the same when the given bool is true.
585 Edge direct(const UEdge&, bool) const {
589 /// \brief Returns true if the edge has default orientation.
591 /// Returns whether the given directed edge is same orientation as
592 /// the corresponding undirected edge's default orientation.
593 bool direction(Edge) const { return true; }
595 /// \brief Returns the opposite directed edge.
597 /// Returns the opposite directed edge.
598 Edge oppositeEdge(Edge) const { return INVALID; }
600 /// \brief Opposite node on an edge
602 /// \return the opposite of the given Node on the given UEdge
603 Node oppositeNode(Node, UEdge) const { return INVALID; }
605 /// \brief First node of the undirected edge.
607 /// \return the first node of the given UEdge.
609 /// Naturally undirected edges don't have direction and thus
610 /// don't have source and target node. But we use these two methods
611 /// to query the two nodes of the edge. The direction of the edge
612 /// which arises this way is called the inherent direction of the
613 /// undirected edge, and is used to define the "default" direction
614 /// of the directed versions of the edges.
616 Node source(UEdge) const { return INVALID; }
618 /// \brief Second node of the undirected edge.
619 Node target(UEdge) const { return INVALID; }
621 /// \brief Source node of the directed edge.
622 Node source(Edge) const { return INVALID; }
624 /// \brief Target node of the directed edge.
625 Node target(Edge) const { return INVALID; }
627 void first(Node&) const {}
628 void next(Node&) const {}
630 void first(UEdge&) const {}
631 void next(UEdge&) const {}
633 void first(Edge&) const {}
634 void next(Edge&) const {}
636 void firstOut(Edge&, Node) const {}
637 void nextOut(Edge&) const {}
639 void firstIn(Edge&, Node) const {}
640 void nextIn(Edge&) const {}
643 void firstInc(UEdge &, bool &, const Node &) const {}
644 void nextInc(UEdge &, bool &) const {}
646 /// \brief Base node of the iterator
648 /// Returns the base node (the source in this case) of the iterator
649 Node baseNode(OutEdgeIt e) const {
652 /// \brief Running node of the iterator
654 /// Returns the running node (the target in this case) of the
656 Node runningNode(OutEdgeIt e) const {
660 /// \brief Base node of the iterator
662 /// Returns the base node (the target in this case) of the iterator
663 Node baseNode(InEdgeIt e) const {
666 /// \brief Running node of the iterator
668 /// Returns the running node (the source in this case) of the
670 Node runningNode(InEdgeIt e) const {
674 /// \brief Base node of the iterator
676 /// Returns the base node of the iterator
677 Node baseNode(IncEdgeIt) const {
681 /// \brief Running node of the iterator
683 /// Returns the running node of the iterator
684 Node runningNode(IncEdgeIt) const {
688 template <typename Graph>
691 checkConcept<IterableUGraphComponent<>, Graph>();
692 checkConcept<MappableUGraphComponent<>, Graph>();