EdgeLookUp and AllEdgeLookUp tests added.
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 /// \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::concept::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.
508 /// \warning Making maps that can handle bool type (NodeMap<bool>)
509 /// needs some extra attention!
511 class NodeMap : public ReadWriteMap< Node, T >
516 NodeMap(const UGraph&) { }
518 NodeMap(const UGraph&, T) { }
521 NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { }
522 ///Assignment operator
523 template <typename CMap>
524 NodeMap& operator=(const CMap&) {
525 checkConcept<ReadMap<Node, T>, CMap>();
530 /// \brief Read write map of the directed edges to type \c T.
532 /// Reference map of the directed edges to type \c T.
534 /// \warning Making maps that can handle bool type (EdgeMap<bool>)
535 /// needs some extra attention!
537 class EdgeMap : public ReadWriteMap<Edge,T>
542 EdgeMap(const UGraph&) { }
544 EdgeMap(const UGraph&, T) { }
546 EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) { }
547 ///Assignment operator
548 template <typename CMap>
549 EdgeMap& operator=(const CMap&) {
550 checkConcept<ReadMap<Edge, T>, CMap>();
555 /// Read write map of the undirected edges to type \c T.
557 /// Reference map of the edges to type \c T.
559 /// \warning Making maps that can handle bool type (UEdgeMap<bool>)
560 /// needs some extra attention!
562 class UEdgeMap : public ReadWriteMap<UEdge,T>
567 UEdgeMap(const UGraph&) { }
569 UEdgeMap(const UGraph&, T) { }
571 UEdgeMap(const UEdgeMap& em) : ReadWriteMap<UEdge,T>(em) {}
572 ///Assignment operator
573 template <typename CMap>
574 UEdgeMap& operator=(const CMap&) {
575 checkConcept<ReadMap<UEdge, T>, CMap>();
580 /// \brief Direct the given undirected edge.
582 /// Direct the given undirected edge. The returned edge source
583 /// will be the given node.
584 Edge direct(const UEdge&, const Node&) const {
588 /// \brief Direct the given undirected edge.
590 /// Direct the given undirected edge. The returned edge
591 /// represents the given undireted edge and the direction comes
592 /// from the given bool. The source of the undirected edge and
593 /// the directed edge is the same when the given bool is true.
594 Edge direct(const UEdge&, bool) const {
598 /// \brief Returns true if the edge has default orientation.
600 /// Returns whether the given directed edge is same orientation as
601 /// the corresponding undirected edge's default orientation.
602 bool direction(Edge) const { return true; }
604 /// \brief Returns the opposite directed edge.
606 /// Returns the opposite directed edge.
607 Edge oppositeEdge(Edge) const { return INVALID; }
609 /// \brief Opposite node on an edge
611 /// \return the opposite of the given Node on the given UEdge
612 Node oppositeNode(Node, UEdge) const { return INVALID; }
614 /// \brief First node of the undirected edge.
616 /// \return the first node of the given UEdge.
618 /// Naturally undirected edges don't have direction and thus
619 /// don't have source and target node. But we use these two methods
620 /// to query the two nodes of the edge. The direction of the edge
621 /// which arises this way is called the inherent direction of the
622 /// undirected edge, and is used to define the "default" direction
623 /// of the directed versions of the edges.
625 Node source(UEdge) const { return INVALID; }
627 /// \brief Second node of the undirected edge.
628 Node target(UEdge) const { return INVALID; }
630 /// \brief Source node of the directed edge.
631 Node source(Edge) const { return INVALID; }
633 /// \brief Target node of the directed edge.
634 Node target(Edge) const { return INVALID; }
636 void first(Node&) const {}
637 void next(Node&) const {}
639 void first(UEdge&) const {}
640 void next(UEdge&) const {}
642 void first(Edge&) const {}
643 void next(Edge&) const {}
645 void firstOut(Edge&, Node) const {}
646 void nextOut(Edge&) const {}
648 void firstIn(Edge&, Node) const {}
649 void nextIn(Edge&) const {}
652 void firstInc(UEdge &, bool &, const Node &) const {}
653 void nextInc(UEdge &, bool &) const {}
655 /// \brief Base node of the iterator
657 /// Returns the base node (the source in this case) of the iterator
658 Node baseNode(OutEdgeIt e) const {
661 /// \brief Running node of the iterator
663 /// Returns the running node (the target in this case) of the
665 Node runningNode(OutEdgeIt e) const {
669 /// \brief Base node of the iterator
671 /// Returns the base node (the target in this case) of the iterator
672 Node baseNode(InEdgeIt e) const {
675 /// \brief Running node of the iterator
677 /// Returns the running node (the source in this case) of the
679 Node runningNode(InEdgeIt e) const {
683 /// \brief Base node of the iterator
685 /// Returns the base node of the iterator
686 Node baseNode(IncEdgeIt) const {
690 /// \brief Running node of the iterator
692 /// Returns the running node of the iterator
693 Node runningNode(IncEdgeIt) const {
697 template <typename Graph>
700 checkConcept<IterableUGraphComponent<>, Graph>();
701 checkConcept<MappableUGraphComponent<>, Graph>();