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/concept/graph_components.h>
28 #include <lemon/concept/graph.h>
29 #include <lemon/bits/utility.h>
34 /// \addtogroup graph_concepts
38 /// 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 couse.
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.
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.
63 typedef True UndirectedTag;
65 /// \brief The base type of node iterators,
66 /// or in other words, the trivial node iterator.
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.
74 /// Default constructor
76 /// @warning The default constructor sets the iterator
77 /// to an undefined value.
85 /// Invalid constructor \& conversion.
87 /// This constructor initializes the iterator to be invalid.
88 /// \sa Invalid for more details.
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; }
96 /// Inequality operator
98 /// \sa operator==(Node n)
100 bool operator!=(Node) const { return true; }
102 /// Artificial ordering operator.
104 /// To allow the use of graph descriptors as key type in std::map or
105 /// similar associative container we require this.
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 bool operator<(Node) const { return false; }
114 /// This iterator goes through each node.
116 /// This iterator goes through each node.
117 /// Its usage is quite simple, for example you can count the number
118 /// of nodes in graph \c g of type \c Graph like this:
121 /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
123 class NodeIt : public Node {
125 /// Default constructor
127 /// @warning The default constructor sets the iterator
128 /// to an undefined value.
130 /// Copy constructor.
132 /// Copy constructor.
134 NodeIt(const NodeIt& n) : Node(n) { }
135 /// Invalid constructor \& conversion.
137 /// Initialize the iterator to be invalid.
138 /// \sa Invalid for more details.
140 /// Sets the iterator to the first node.
142 /// Sets the iterator to the first node of \c g.
144 NodeIt(const UGraph&) { }
145 /// Node -> NodeIt conversion.
147 /// Sets the iterator to the node of \c the graph pointed by
148 /// the trivial iterator.
149 /// This feature necessitates that each time we
150 /// iterate the edge-set, the iteration order is the same.
151 NodeIt(const UGraph&, const Node&) { }
154 /// Assign the iterator to the next node.
156 NodeIt& operator++() { return *this; }
160 /// The base type of the undirected edge iterators.
162 /// The base type of the undirected edge iterators.
166 /// Default constructor
168 /// @warning The default constructor sets the iterator
169 /// to an undefined value.
171 /// Copy constructor.
173 /// Copy constructor.
175 UEdge(const UEdge&) { }
176 /// Initialize the iterator to be invalid.
178 /// Initialize the iterator to be invalid.
181 /// Equality operator
183 /// Two iterators are equal if and only if they point to the
184 /// same object or both are invalid.
185 bool operator==(UEdge) const { return true; }
186 /// Inequality operator
188 /// \sa operator==(UEdge n)
190 bool operator!=(UEdge) const { return true; }
192 /// Artificial ordering operator.
194 /// To allow the use of graph descriptors as key type in std::map or
195 /// similar associative container we require this.
197 /// \note This operator only have to define some strict ordering of
198 /// the items; this order has nothing to do with the iteration
199 /// ordering of the items.
200 bool operator<(UEdge) const { return false; }
203 /// This iterator goes through each undirected edge.
205 /// This iterator goes through each undirected edge of a graph.
206 /// Its usage is quite simple, for example you can count the number
207 /// of undirected edges in a graph \c g of type \c Graph as follows:
210 /// for(Graph::UEdgeIt e(g); e!=INVALID; ++e) ++count;
212 class UEdgeIt : public UEdge {
214 /// Default constructor
216 /// @warning The default constructor sets the iterator
217 /// to an undefined value.
219 /// Copy constructor.
221 /// Copy constructor.
223 UEdgeIt(const UEdgeIt& e) : UEdge(e) { }
224 /// Initialize the iterator to be invalid.
226 /// Initialize the iterator to be invalid.
229 /// This constructor sets the iterator to the first undirected edge.
231 /// This constructor sets the iterator to the first undirected edge.
232 UEdgeIt(const UGraph&) { }
233 /// UEdge -> UEdgeIt conversion
235 /// Sets the iterator to the value of the trivial iterator.
236 /// This feature necessitates that each time we
237 /// iterate the undirected edge-set, the iteration order is the
239 UEdgeIt(const UGraph&, const UEdge&) { }
240 /// Next undirected edge
242 /// Assign the iterator to the next undirected edge.
243 UEdgeIt& operator++() { return *this; }
246 /// \brief This iterator goes trough the incident undirected
249 /// This iterator goes trough the incident undirected edges
250 /// of a certain node of a graph. You should assume that the
251 /// loop edges will be iterated twice.
253 /// Its usage is quite simple, for example you can compute the
254 /// degree (i.e. count the number of incident edges of a node \c n
255 /// in graph \c g of type \c Graph as follows.
259 /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
261 class IncEdgeIt : public UEdge {
263 /// Default constructor
265 /// @warning The default constructor sets the iterator
266 /// to an undefined value.
268 /// Copy constructor.
270 /// Copy constructor.
272 IncEdgeIt(const IncEdgeIt& e) : UEdge(e) { }
273 /// Initialize the iterator to be invalid.
275 /// Initialize the iterator to be invalid.
277 IncEdgeIt(Invalid) { }
278 /// This constructor sets the iterator to first incident edge.
280 /// This constructor set the iterator to the first incident edge of
282 IncEdgeIt(const UGraph&, const Node&) { }
283 /// UEdge -> IncEdgeIt conversion
285 /// Sets the iterator to the value of the trivial iterator \c e.
286 /// This feature necessitates that each time we
287 /// iterate the edge-set, the iteration order is the same.
288 IncEdgeIt(const UGraph&, const UEdge&) { }
289 /// Next incident edge
291 /// Assign the iterator to the next incident edge
292 /// of the corresponding node.
293 IncEdgeIt& operator++() { return *this; }
296 /// The directed edge type.
298 /// The directed edge type. It can be converted to the
300 class Edge : public UEdge {
302 /// Default constructor
304 /// @warning The default constructor sets the iterator
305 /// to an undefined value.
307 /// Copy constructor.
309 /// Copy constructor.
311 Edge(const Edge& e) : UEdge(e) { }
312 /// Initialize the iterator to be invalid.
314 /// Initialize the iterator to be invalid.
317 /// Equality operator
319 /// Two iterators are equal if and only if they point to the
320 /// same object or both are invalid.
321 bool operator==(Edge) const { return true; }
322 /// Inequality operator
324 /// \sa operator==(Edge n)
326 bool operator!=(Edge) const { return true; }
328 /// Artificial ordering operator.
330 /// To allow the use of graph descriptors as key type in std::map or
331 /// similar associative container we require this.
333 /// \note This operator only have to define some strict ordering of
334 /// the items; this order has nothing to do with the iteration
335 /// ordering of the items.
336 bool operator<(Edge) const { return false; }
339 /// This iterator goes through each directed edge.
341 /// This iterator goes through each edge of a graph.
342 /// Its usage is quite simple, for example you can count the number
343 /// of edges in a graph \c g of type \c Graph as follows:
346 /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
348 class EdgeIt : public Edge {
350 /// Default constructor
352 /// @warning The default constructor sets the iterator
353 /// to an undefined value.
355 /// Copy constructor.
357 /// Copy constructor.
359 EdgeIt(const EdgeIt& e) : Edge(e) { }
360 /// Initialize the iterator to be invalid.
362 /// Initialize the iterator to be invalid.
365 /// This constructor sets the iterator to the first edge.
367 /// This constructor sets the iterator to the first edge of \c g.
368 ///@param g the graph
369 EdgeIt(const UGraph &g) { ignore_unused_variable_warning(g); }
370 /// Edge -> EdgeIt conversion
372 /// Sets the iterator to the value of the trivial iterator \c e.
373 /// This feature necessitates that each time we
374 /// iterate the edge-set, the iteration order is the same.
375 EdgeIt(const UGraph&, const Edge&) { }
378 /// Assign the iterator to the next edge.
379 EdgeIt& operator++() { return *this; }
382 /// This iterator goes trough the outgoing directed edges of a node.
384 /// This iterator goes trough the \e outgoing edges of a certain node
386 /// Its usage is quite simple, for example you can count the number
387 /// of outgoing edges of a node \c n
388 /// in graph \c g of type \c Graph as follows.
391 /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
394 class OutEdgeIt : public Edge {
396 /// Default constructor
398 /// @warning The default constructor sets the iterator
399 /// to an undefined value.
401 /// Copy constructor.
403 /// Copy constructor.
405 OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
406 /// Initialize the iterator to be invalid.
408 /// Initialize the iterator to be invalid.
410 OutEdgeIt(Invalid) { }
411 /// This constructor sets the iterator to the first outgoing edge.
413 /// This constructor sets the iterator to the first outgoing edge of
416 ///@param g the graph
417 OutEdgeIt(const UGraph& n, const Node& g) {
418 ignore_unused_variable_warning(n);
419 ignore_unused_variable_warning(g);
421 /// Edge -> OutEdgeIt conversion
423 /// Sets the iterator to the value of the trivial iterator.
424 /// This feature necessitates that each time we
425 /// iterate the edge-set, the iteration order is the same.
426 OutEdgeIt(const UGraph&, const Edge&) { }
427 ///Next outgoing edge
429 /// Assign the iterator to the next
430 /// outgoing edge of the corresponding node.
431 OutEdgeIt& operator++() { return *this; }
434 /// This iterator goes trough the incoming directed edges of a node.
436 /// This iterator goes trough the \e incoming edges of a certain node
438 /// Its usage is quite simple, for example you can count the number
439 /// of outgoing edges of a node \c n
440 /// in graph \c g of type \c Graph as follows.
443 /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
446 class InEdgeIt : public Edge {
448 /// Default constructor
450 /// @warning The default constructor sets the iterator
451 /// to an undefined value.
453 /// Copy constructor.
455 /// Copy constructor.
457 InEdgeIt(const InEdgeIt& e) : Edge(e) { }
458 /// Initialize the iterator to be invalid.
460 /// Initialize the iterator to be invalid.
462 InEdgeIt(Invalid) { }
463 /// This constructor sets the iterator to first incoming edge.
465 /// This constructor set the iterator to the first incoming edge of
468 ///@param g the graph
469 InEdgeIt(const UGraph& g, const Node& n) {
470 ignore_unused_variable_warning(n);
471 ignore_unused_variable_warning(g);
473 /// Edge -> InEdgeIt conversion
475 /// Sets the iterator to the value of the trivial iterator \c e.
476 /// This feature necessitates that each time we
477 /// iterate the edge-set, the iteration order is the same.
478 InEdgeIt(const UGraph&, const Edge&) { }
479 /// Next incoming edge
481 /// Assign the iterator to the next inedge of the corresponding node.
483 InEdgeIt& operator++() { return *this; }
486 /// \brief Read write map of the nodes to type \c T.
488 /// ReadWrite map of the nodes to type \c T.
490 /// \warning Making maps that can handle bool type (NodeMap<bool>)
491 /// needs some extra attention!
493 class NodeMap : public ReadWriteMap< Node, T >
498 NodeMap(const UGraph&) { }
500 NodeMap(const UGraph&, T) { }
503 NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
504 ///Assignment operator
505 template <typename CMap>
506 NodeMap& operator=(const CMap&) {
507 checkConcept<ReadMap<Node, T>, CMap>();
512 /// \brief Read write map of the directed edges to type \c T.
514 /// Reference map of the directed edges to type \c T.
516 /// \warning Making maps that can handle bool type (EdgeMap<bool>)
517 /// needs some extra attention!
519 class EdgeMap : public ReadWriteMap<Edge,T>
524 EdgeMap(const UGraph&) { }
526 EdgeMap(const UGraph&, T) { }
528 EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
529 ///Assignment operator
530 template <typename CMap>
531 EdgeMap& operator=(const CMap&) {
532 checkConcept<ReadMap<Edge, T>, CMap>();
537 /// Read write map of the undirected edges to type \c T.
539 /// Reference map of the edges to type \c T.
541 /// \warning Making maps that can handle bool type (UEdgeMap<bool>)
542 /// needs some extra attention!
544 class UEdgeMap : public ReadWriteMap<UEdge,T>
549 UEdgeMap(const UGraph&) { }
551 UEdgeMap(const UGraph&, T) { }
553 UEdgeMap(const UEdgeMap& em) : ReadWriteMap<UEdge,T>(em) {}
554 ///Assignment operator
555 template <typename CMap>
556 UEdgeMap& operator=(const CMap&) {
557 checkConcept<ReadMap<UEdge, T>, CMap>();
562 /// \brief Direct the given undirected edge.
564 /// Direct the given undirected edge. The returned edge source
565 /// will be the given edge.
566 Edge direct(const UEdge&, const Node&) const {
570 /// \brief Direct the given undirected edge.
572 /// Direct the given undirected edge. The returned edge source
573 /// will be the source of the undirected edge if the given bool
575 Edge direct(const UEdge&, bool) const {
579 /// \brief Returns true if the edge has default orientation.
581 /// Returns whether the given directed edge is same orientation as
582 /// the corresponding undirected edge.
583 bool direction(Edge) const { return true; }
585 /// \brief Returns the opposite directed edge.
587 /// Returns the opposite directed edge.
588 Edge oppositeEdge(Edge) const { return INVALID; }
590 /// \brief Opposite node on an edge
592 /// \return the opposite of the given Node on the given Edge
593 Node oppositeNode(Node, UEdge) const { return INVALID; }
595 /// \brief First node of the undirected edge.
597 /// \return the first node of the given UEdge.
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.
606 Node source(UEdge) const { return INVALID; }
608 /// \brief Second node of the undirected edge.
609 Node target(UEdge) const { return INVALID; }
611 /// \brief Source node of the directed edge.
612 Node source(Edge) const { return INVALID; }
614 /// \brief Target node of the directed edge.
615 Node target(Edge) const { return INVALID; }
617 void first(Node&) const {}
618 void next(Node&) const {}
620 void first(UEdge&) const {}
621 void next(UEdge&) const {}
623 void first(Edge&) const {}
624 void next(Edge&) const {}
626 void firstOut(Edge&, Node) const {}
627 void nextOut(Edge&) const {}
629 void firstIn(Edge&, Node) const {}
630 void nextIn(Edge&) const {}
633 void firstInc(UEdge &, bool &, const Node &) const {}
634 void nextInc(UEdge &, bool &) const {}
636 /// \brief Base node of the iterator
638 /// Returns the base node (the source in this case) of the iterator
639 Node baseNode(OutEdgeIt e) const {
642 /// \brief Running node of the iterator
644 /// Returns the running node (the target in this case) of the
646 Node runningNode(OutEdgeIt e) const {
650 /// \brief Base node of the iterator
652 /// Returns the base node (the target in this case) of the iterator
653 Node baseNode(InEdgeIt e) const {
656 /// \brief Running node of the iterator
658 /// Returns the running node (the source in this case) of the
660 Node runningNode(InEdgeIt e) const {
664 /// \brief Base node of the iterator
666 /// Returns the base node of the iterator
667 Node baseNode(IncEdgeIt) const {
671 /// \brief Running node of the iterator
673 /// Returns the running node of the iterator
674 Node runningNode(IncEdgeIt) const {
678 template <typename Graph>
681 checkConcept<BaseIterableUGraphComponent<>, Graph>();
682 checkConcept<IterableUGraphComponent<>, Graph>();
683 checkConcept<MappableUGraphComponent<>, Graph>();