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_component.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.
111 /// \bug This is a technical requirement. Do we really need this?
112 bool operator<(Node) const { return false; }
116 /// This iterator goes through each node.
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:
123 /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
125 class NodeIt : public Node {
127 /// Default constructor
129 /// @warning The default constructor sets the iterator
130 /// to an undefined value.
132 /// Copy constructor.
134 /// Copy constructor.
136 NodeIt(const NodeIt& n) : Node(n) { }
137 /// Invalid constructor \& conversion.
139 /// Initialize the iterator to be invalid.
140 /// \sa Invalid for more details.
142 /// Sets the iterator to the first node.
144 /// Sets the iterator to the first node of \c g.
146 NodeIt(const UGraph&) { }
147 /// Node -> NodeIt conversion.
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&) { }
156 /// Assign the iterator to the next node.
158 NodeIt& operator++() { return *this; }
162 /// The base type of the undirected edge iterators.
164 /// The base type of the undirected edge iterators.
168 /// Default constructor
170 /// @warning The default constructor sets the iterator
171 /// to an undefined value.
173 /// Copy constructor.
175 /// Copy constructor.
177 UEdge(const UEdge&) { }
178 /// Initialize the iterator to be invalid.
180 /// Initialize the iterator to be invalid.
183 /// Equality operator
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
190 /// \sa operator==(UEdge n)
192 bool operator!=(UEdge) const { return true; }
194 /// Artificial ordering operator.
196 /// To allow the use of graph descriptors as key type in std::map or
197 /// similar associative container we require this.
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.
203 /// \bug This is a technical requirement. Do we really need this?
204 bool operator<(UEdge) const { return false; }
207 /// This iterator goes through each undirected edge.
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:
214 /// for(Graph::UEdgeIt e(g); e!=INVALID; ++e) ++count;
216 class UEdgeIt : public UEdge {
218 /// Default constructor
220 /// @warning The default constructor sets the iterator
221 /// to an undefined value.
223 /// Copy constructor.
225 /// Copy constructor.
227 UEdgeIt(const UEdgeIt& e) : UEdge(e) { }
228 /// Initialize the iterator to be invalid.
230 /// Initialize the iterator to be invalid.
233 /// This constructor sets the iterator to the first undirected edge.
235 /// This constructor sets the iterator to the first undirected edge.
236 UEdgeIt(const UGraph&) { }
237 /// UEdge -> UEdgeIt conversion
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
243 UEdgeIt(const UGraph&, const UEdge&) { }
244 /// Next undirected edge
246 /// Assign the iterator to the next undirected edge.
247 UEdgeIt& operator++() { return *this; }
250 /// \brief This iterator goes trough the incident undirected
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.
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.
263 /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
265 class IncEdgeIt : public UEdge {
267 /// Default constructor
269 /// @warning The default constructor sets the iterator
270 /// to an undefined value.
272 /// Copy constructor.
274 /// Copy constructor.
276 IncEdgeIt(const IncEdgeIt& e) : UEdge(e) { }
277 /// Initialize the iterator to be invalid.
279 /// Initialize the iterator to be invalid.
281 IncEdgeIt(Invalid) { }
282 /// This constructor sets the iterator to first incident edge.
284 /// This constructor set the iterator to the first incident edge of
286 IncEdgeIt(const UGraph&, const Node&) { }
287 /// UEdge -> IncEdgeIt conversion
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
295 /// Assign the iterator to the next incident edge
296 /// of the corresponding node.
297 IncEdgeIt& operator++() { return *this; }
300 /// The directed edge type.
302 /// The directed edge type. It can be converted to the
304 class Edge : public UEdge {
306 /// Default constructor
308 /// @warning The default constructor sets the iterator
309 /// to an undefined value.
311 /// Copy constructor.
313 /// Copy constructor.
315 Edge(const Edge& e) : UEdge(e) { }
316 /// Initialize the iterator to be invalid.
318 /// Initialize the iterator to be invalid.
321 /// Equality operator
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
328 /// \sa operator==(Edge n)
330 bool operator!=(Edge) const { return true; }
332 /// Artificial ordering operator.
334 /// To allow the use of graph descriptors as key type in std::map or
335 /// similar associative container we require this.
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.
341 /// \bug This is a technical requirement. Do we really need this?
342 bool operator<(Edge) const { return false; }
345 /// This iterator goes through each directed edge.
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:
352 /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
354 class EdgeIt : public Edge {
356 /// Default constructor
358 /// @warning The default constructor sets the iterator
359 /// to an undefined value.
361 /// Copy constructor.
363 /// Copy constructor.
365 EdgeIt(const EdgeIt& e) : Edge(e) { }
366 /// Initialize the iterator to be invalid.
368 /// Initialize the iterator to be invalid.
371 /// This constructor sets the iterator to the first edge.
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
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&) { }
384 /// Assign the iterator to the next edge.
385 EdgeIt& operator++() { return *this; }
388 /// This iterator goes trough the outgoing directed edges of a node.
390 /// This iterator goes trough the \e outgoing edges of a certain node
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.
397 /// for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) ++count;
400 class OutEdgeIt : public Edge {
402 /// Default constructor
404 /// @warning The default constructor sets the iterator
405 /// to an undefined value.
407 /// Copy constructor.
409 /// Copy constructor.
411 OutEdgeIt(const OutEdgeIt& e) : Edge(e) { }
412 /// Initialize the iterator to be invalid.
414 /// Initialize the iterator to be invalid.
416 OutEdgeIt(Invalid) { }
417 /// This constructor sets the iterator to the first outgoing edge.
419 /// This constructor sets the iterator to the first outgoing edge of
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);
427 /// Edge -> OutEdgeIt conversion
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
435 /// Assign the iterator to the next
436 /// outgoing edge of the corresponding node.
437 OutEdgeIt& operator++() { return *this; }
440 /// This iterator goes trough the incoming directed edges of a node.
442 /// This iterator goes trough the \e incoming edges of a certain node
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.
449 /// for(Graph::InEdgeIt e(g, n); e!=INVALID; ++e) ++count;
452 class InEdgeIt : public Edge {
454 /// Default constructor
456 /// @warning The default constructor sets the iterator
457 /// to an undefined value.
459 /// Copy constructor.
461 /// Copy constructor.
463 InEdgeIt(const InEdgeIt& e) : Edge(e) { }
464 /// Initialize the iterator to be invalid.
466 /// Initialize the iterator to be invalid.
468 InEdgeIt(Invalid) { }
469 /// This constructor sets the iterator to first incoming edge.
471 /// This constructor set the iterator to the first incoming edge of
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);
479 /// Edge -> InEdgeIt conversion
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
487 /// Assign the iterator to the next inedge of the corresponding node.
489 InEdgeIt& operator++() { return *this; }
492 /// \brief Read write map of the nodes to type \c T.
494 /// ReadWrite map of the nodes to type \c T.
496 /// \warning Making maps that can handle bool type (NodeMap<bool>)
497 /// needs some extra attention!
498 /// \todo Wrong documentation
500 class NodeMap : public ReadWriteMap< Node, T >
505 NodeMap(const UGraph&) { }
507 NodeMap(const UGraph&, T) { }
510 NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
511 ///Assignment operator
512 NodeMap& operator=(const NodeMap&) { return *this; }
513 // \todo fix this concept
516 /// \brief Read write map of the directed edges to type \c T.
518 /// Reference map of the directed edges to type \c T.
520 /// \warning Making maps that can handle bool type (EdgeMap<bool>)
521 /// needs some extra attention!
522 /// \todo Wrong documentation
524 class EdgeMap : public ReadWriteMap<Edge,T>
529 EdgeMap(const UGraph&) { }
531 EdgeMap(const UGraph&, T) { }
533 EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
534 ///Assignment operator
535 EdgeMap& operator=(const EdgeMap&) { return *this; }
536 // \todo fix this concept
539 /// Read write map of the undirected edges to type \c T.
541 /// Reference map of the edges to type \c T.
543 /// \warning Making maps that can handle bool type (UEdgeMap<bool>)
544 /// needs some extra attention!
545 /// \todo Wrong documentation
547 class UEdgeMap : public ReadWriteMap<UEdge,T>
552 UEdgeMap(const UGraph&) { }
554 UEdgeMap(const UGraph&, T) { }
556 UEdgeMap(const UEdgeMap& em) : ReadWriteMap<UEdge,T>(em) {}
557 ///Assignment operator
558 UEdgeMap &operator=(const UEdgeMap&) { return *this; }
559 // \todo fix this concept
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<BaseIterableUGraphConcept, Graph>();
682 checkConcept<IterableUGraphConcept, Graph>();
683 checkConcept<MappableUGraphConcept, Graph>();