1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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_CONCEPTS_GRAPH_H
24 #define LEMON_CONCEPTS_GRAPH_H
26 #include <lemon/concepts/graph_components.h>
27 #include <lemon/core.h>
32 /// \ingroup graph_concepts
34 /// \brief Class describing the concept of Undirected Graphs.
36 /// This class describes the common interface of all Undirected
39 /// As all concept describing classes it provides only interface
40 /// without any sensible implementation. So any algorithm for
41 /// undirected graph should compile with this class, but it will not
42 /// run properly, of course.
44 /// The LEMON undirected graphs also fulfill the concept of
45 /// directed graphs (\ref lemon::concepts::Digraph "Digraph
46 /// Concept"). Each edges can be seen as two opposite
47 /// directed arc and consequently the undirected graph can be
48 /// seen as the direceted graph of these directed arcs. The
49 /// Graph has the Edge inner class for the edges and
50 /// the Arc type for the directed arcs. The Arc type is
51 /// convertible to Edge or inherited from it so from a directed
52 /// arc we can get the represented edge.
54 /// In the sense of the LEMON each edge has a default
55 /// direction (it should be in every computer implementation,
56 /// because the order of edge's nodes defines an
57 /// orientation). With the default orientation we can define that
58 /// the directed arc is forward or backward directed. With the \c
59 /// direction() and \c direct() function we can get the direction
60 /// of the directed arc and we can direct an edge.
62 /// The EdgeIt is an iterator for the edges. We can use
63 /// the EdgeMap to map values for the edges. The InArcIt and
64 /// OutArcIt iterates on the same edges but with opposite
65 /// direction. The IncEdgeIt iterates also on the same edges
66 /// as the OutArcIt and InArcIt but it is not convertible to Arc just
70 /// \brief The undirected graph should be tagged by the
73 /// The undirected graph should be tagged by the UndirectedTag. This
74 /// tag helps the enable_if technics to make compile time
75 /// specializations for undirected graphs.
76 typedef True UndirectedTag;
78 /// \brief The base type of node iterators,
79 /// or in other words, the trivial node iterator.
81 /// This is the base type of each node iterator,
82 /// thus each kind of node iterator converts to this.
83 /// More precisely each kind of node iterator should be inherited
84 /// from the trivial node iterator.
87 /// Default constructor
89 /// @warning The default constructor sets the iterator
90 /// to an undefined value.
98 /// Invalid constructor \& conversion.
100 /// This constructor initializes the iterator to be invalid.
101 /// \sa Invalid for more details.
103 /// Equality operator
105 /// Two iterators are equal if and only if they point to the
106 /// same object or both are invalid.
107 bool operator==(Node) const { return true; }
109 /// Inequality operator
111 /// \sa operator==(Node n)
113 bool operator!=(Node) const { return true; }
115 /// Artificial ordering operator.
117 /// To allow the use of graph descriptors as key type in std::map or
118 /// similar associative container we require this.
120 /// \note This operator only have to define some strict ordering of
121 /// the items; this order has nothing to do with the iteration
122 /// ordering of the items.
123 bool operator<(Node) const { return false; }
127 /// This iterator goes through each node.
129 /// This iterator goes through each node.
130 /// Its usage is quite simple, for example you can count the number
131 /// of nodes in graph \c g of type \c Graph like this:
134 /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
136 class NodeIt : public Node {
138 /// Default constructor
140 /// @warning The default constructor sets the iterator
141 /// to an undefined value.
143 /// Copy constructor.
145 /// Copy constructor.
147 NodeIt(const NodeIt& n) : Node(n) { }
148 /// Invalid constructor \& conversion.
150 /// Initialize the iterator to be invalid.
151 /// \sa Invalid for more details.
153 /// Sets the iterator to the first node.
155 /// Sets the iterator to the first node of \c g.
157 NodeIt(const Graph&) { }
158 /// Node -> NodeIt conversion.
160 /// Sets the iterator to the node of \c the graph pointed by
161 /// the trivial iterator.
162 /// This feature necessitates that each time we
163 /// iterate the arc-set, the iteration order is the same.
164 NodeIt(const Graph&, const Node&) { }
167 /// Assign the iterator to the next node.
169 NodeIt& operator++() { return *this; }
173 /// The base type of the edge iterators.
175 /// The base type of the edge iterators.
179 /// Default constructor
181 /// @warning The default constructor sets the iterator
182 /// to an undefined value.
184 /// Copy constructor.
186 /// Copy constructor.
188 Edge(const Edge&) { }
189 /// Initialize the iterator to be invalid.
191 /// Initialize the iterator to be invalid.
194 /// Equality operator
196 /// Two iterators are equal if and only if they point to the
197 /// same object or both are invalid.
198 bool operator==(Edge) const { return true; }
199 /// Inequality operator
201 /// \sa operator==(Edge n)
203 bool operator!=(Edge) const { return true; }
205 /// Artificial ordering operator.
207 /// To allow the use of graph descriptors as key type in std::map or
208 /// similar associative container we require this.
210 /// \note This operator only have to define some strict ordering of
211 /// the items; this order has nothing to do with the iteration
212 /// ordering of the items.
213 bool operator<(Edge) const { return false; }
216 /// This iterator goes through each edge.
218 /// This iterator goes through each edge of a graph.
219 /// Its usage is quite simple, for example you can count the number
220 /// of edges in a graph \c g of type \c Graph as follows:
223 /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
225 class EdgeIt : public Edge {
227 /// Default constructor
229 /// @warning The default constructor sets the iterator
230 /// to an undefined value.
232 /// Copy constructor.
234 /// Copy constructor.
236 EdgeIt(const EdgeIt& e) : Edge(e) { }
237 /// Initialize the iterator to be invalid.
239 /// Initialize the iterator to be invalid.
242 /// This constructor sets the iterator to the first edge.
244 /// This constructor sets the iterator to the first edge.
245 EdgeIt(const Graph&) { }
246 /// Edge -> EdgeIt conversion
248 /// Sets the iterator to the value of the trivial iterator.
249 /// This feature necessitates that each time we
250 /// iterate the edge-set, the iteration order is the
252 EdgeIt(const Graph&, const Edge&) { }
255 /// Assign the iterator to the next edge.
256 EdgeIt& operator++() { return *this; }
259 /// \brief This iterator goes trough the incident undirected
262 /// This iterator goes trough the incident edges
263 /// of a certain node of a graph. You should assume that the
264 /// loop arcs will be iterated twice.
266 /// Its usage is quite simple, for example you can compute the
267 /// degree (i.e. count the number of incident arcs of a node \c n
268 /// in graph \c g of type \c Graph as follows.
272 /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
274 class IncEdgeIt : public Edge {
276 /// Default constructor
278 /// @warning The default constructor sets the iterator
279 /// to an undefined value.
281 /// Copy constructor.
283 /// Copy constructor.
285 IncEdgeIt(const IncEdgeIt& e) : Edge(e) { }
286 /// Initialize the iterator to be invalid.
288 /// Initialize the iterator to be invalid.
290 IncEdgeIt(Invalid) { }
291 /// This constructor sets the iterator to first incident arc.
293 /// This constructor set the iterator to the first incident arc of
295 IncEdgeIt(const Graph&, const Node&) { }
296 /// Edge -> IncEdgeIt conversion
298 /// Sets the iterator to the value of the trivial iterator \c e.
299 /// This feature necessitates that each time we
300 /// iterate the arc-set, the iteration order is the same.
301 IncEdgeIt(const Graph&, const Edge&) { }
302 /// Next incident arc
304 /// Assign the iterator to the next incident arc
305 /// of the corresponding node.
306 IncEdgeIt& operator++() { return *this; }
309 /// The directed arc type.
311 /// The directed arc type. It can be converted to the
312 /// edge or it should be inherited from the undirected
314 class Arc : public Edge {
316 /// Default constructor
318 /// @warning The default constructor sets the iterator
319 /// to an undefined value.
321 /// Copy constructor.
323 /// Copy constructor.
325 Arc(const Arc& e) : Edge(e) { }
326 /// Initialize the iterator to be invalid.
328 /// Initialize the iterator to be invalid.
331 /// Equality operator
333 /// Two iterators are equal if and only if they point to the
334 /// same object or both are invalid.
335 bool operator==(Arc) const { return true; }
336 /// Inequality operator
338 /// \sa operator==(Arc n)
340 bool operator!=(Arc) const { return true; }
342 /// Artificial ordering operator.
344 /// To allow the use of graph descriptors as key type in std::map or
345 /// similar associative container we require this.
347 /// \note This operator only have to define some strict ordering of
348 /// the items; this order has nothing to do with the iteration
349 /// ordering of the items.
350 bool operator<(Arc) const { return false; }
353 /// This iterator goes through each directed arc.
355 /// This iterator goes through each arc of a graph.
356 /// Its usage is quite simple, for example you can count the number
357 /// of arcs in a graph \c g of type \c Graph as follows:
360 /// for(Graph::ArcIt e(g); e!=INVALID; ++e) ++count;
362 class ArcIt : public Arc {
364 /// Default constructor
366 /// @warning The default constructor sets the iterator
367 /// to an undefined value.
369 /// Copy constructor.
371 /// Copy constructor.
373 ArcIt(const ArcIt& e) : Arc(e) { }
374 /// Initialize the iterator to be invalid.
376 /// Initialize the iterator to be invalid.
379 /// This constructor sets the iterator to the first arc.
381 /// This constructor sets the iterator to the first arc of \c g.
382 ///@param g the graph
383 ArcIt(const Graph &g) { ignore_unused_variable_warning(g); }
384 /// Arc -> ArcIt conversion
386 /// Sets the iterator to the value of the trivial iterator \c e.
387 /// This feature necessitates that each time we
388 /// iterate the arc-set, the iteration order is the same.
389 ArcIt(const Graph&, const Arc&) { }
392 /// Assign the iterator to the next arc.
393 ArcIt& operator++() { return *this; }
396 /// This iterator goes trough the outgoing directed arcs of a node.
398 /// This iterator goes trough the \e outgoing arcs of a certain node
400 /// Its usage is quite simple, for example you can count the number
401 /// of outgoing arcs of a node \c n
402 /// in graph \c g of type \c Graph as follows.
405 /// for (Graph::OutArcIt e(g, n); e!=INVALID; ++e) ++count;
408 class OutArcIt : public Arc {
410 /// Default constructor
412 /// @warning The default constructor sets the iterator
413 /// to an undefined value.
415 /// Copy constructor.
417 /// Copy constructor.
419 OutArcIt(const OutArcIt& e) : Arc(e) { }
420 /// Initialize the iterator to be invalid.
422 /// Initialize the iterator to be invalid.
424 OutArcIt(Invalid) { }
425 /// This constructor sets the iterator to the first outgoing arc.
427 /// This constructor sets the iterator to the first outgoing arc of
430 ///@param g the graph
431 OutArcIt(const Graph& n, const Node& g) {
432 ignore_unused_variable_warning(n);
433 ignore_unused_variable_warning(g);
435 /// Arc -> OutArcIt conversion
437 /// Sets the iterator to the value of the trivial iterator.
438 /// This feature necessitates that each time we
439 /// iterate the arc-set, the iteration order is the same.
440 OutArcIt(const Graph&, const Arc&) { }
443 /// Assign the iterator to the next
444 /// outgoing arc of the corresponding node.
445 OutArcIt& operator++() { return *this; }
448 /// This iterator goes trough the incoming directed arcs of a node.
450 /// This iterator goes trough the \e incoming arcs of a certain node
452 /// Its usage is quite simple, for example you can count the number
453 /// of outgoing arcs of a node \c n
454 /// in graph \c g of type \c Graph as follows.
457 /// for(Graph::InArcIt e(g, n); e!=INVALID; ++e) ++count;
460 class InArcIt : public Arc {
462 /// Default constructor
464 /// @warning The default constructor sets the iterator
465 /// to an undefined value.
467 /// Copy constructor.
469 /// Copy constructor.
471 InArcIt(const InArcIt& e) : Arc(e) { }
472 /// Initialize the iterator to be invalid.
474 /// Initialize the iterator to be invalid.
477 /// This constructor sets the iterator to first incoming arc.
479 /// This constructor set the iterator to the first incoming arc of
482 ///@param g the graph
483 InArcIt(const Graph& g, const Node& n) {
484 ignore_unused_variable_warning(n);
485 ignore_unused_variable_warning(g);
487 /// Arc -> InArcIt conversion
489 /// Sets the iterator to the value of the trivial iterator \c e.
490 /// This feature necessitates that each time we
491 /// iterate the arc-set, the iteration order is the same.
492 InArcIt(const Graph&, const Arc&) { }
493 /// Next incoming arc
495 /// Assign the iterator to the next inarc of the corresponding node.
497 InArcIt& operator++() { return *this; }
500 /// \brief Read write map of the nodes to type \c T.
502 /// ReadWrite map of the nodes to type \c T.
505 class NodeMap : public ReadWriteMap< Node, T >
510 NodeMap(const Graph&) { }
512 NodeMap(const Graph&, 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 arcs to type \c T.
527 /// Reference map of the directed arcs to type \c T.
530 class ArcMap : public ReadWriteMap<Arc,T>
535 ArcMap(const Graph&) { }
537 ArcMap(const Graph&, T) { }
540 ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { }
541 ///Assignment operator
542 template <typename CMap>
543 ArcMap& operator=(const CMap&) {
544 checkConcept<ReadMap<Arc, T>, CMap>();
549 /// Read write map of the edges to type \c T.
551 /// Reference map of the arcs to type \c T.
554 class EdgeMap : public ReadWriteMap<Edge,T>
559 EdgeMap(const Graph&) { }
561 EdgeMap(const Graph&, T) { }
564 EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) {}
565 ///Assignment operator
566 template <typename CMap>
567 EdgeMap& operator=(const CMap&) {
568 checkConcept<ReadMap<Edge, T>, CMap>();
573 /// \brief Direct the given edge.
575 /// Direct the given edge. The returned arc source
576 /// will be the given node.
577 Arc direct(const Edge&, const Node&) const {
581 /// \brief Direct the given edge.
583 /// Direct the given edge. The returned arc
584 /// represents the given edge and the direction comes
585 /// from the bool parameter. The source of the edge and
586 /// the directed arc is the same when the given bool is true.
587 Arc direct(const Edge&, bool) const {
591 /// \brief Returns true if the arc has default orientation.
593 /// Returns whether the given directed arc is same orientation as
594 /// the corresponding edge's default orientation.
595 bool direction(Arc) const { return true; }
597 /// \brief Returns the opposite directed arc.
599 /// Returns the opposite directed arc.
600 Arc oppositeArc(Arc) const { return INVALID; }
602 /// \brief Opposite node on an arc
604 /// \return the opposite of the given Node on the given Edge
605 Node oppositeNode(Node, Edge) const { return INVALID; }
607 /// \brief First node of the edge.
609 /// \return the first node of the given Edge.
611 /// Naturally edges don't have direction and thus
612 /// don't have source and target node. But we use these two methods
613 /// to query the two nodes of the arc. The direction of the arc
614 /// which arises this way is called the inherent direction of the
615 /// edge, and is used to define the "default" direction
616 /// of the directed versions of the arcs.
618 Node u(Edge) const { return INVALID; }
620 /// \brief Second node of the edge.
621 Node v(Edge) const { return INVALID; }
623 /// \brief Source node of the directed arc.
624 Node source(Arc) const { return INVALID; }
626 /// \brief Target node of the directed arc.
627 Node target(Arc) const { return INVALID; }
629 /// \brief Returns the id of the node.
630 int id(Node) const { return -1; }
632 /// \brief Returns the id of the edge.
633 int id(Edge) const { return -1; }
635 /// \brief Returns the id of the arc.
636 int id(Arc) const { return -1; }
638 /// \brief Returns the node with the given id.
640 /// \pre The argument should be a valid node id in the graph.
641 Node nodeFromId(int) const { return INVALID; }
643 /// \brief Returns the edge with the given id.
645 /// \pre The argument should be a valid edge id in the graph.
646 Edge edgeFromId(int) const { return INVALID; }
648 /// \brief Returns the arc with the given id.
650 /// \pre The argument should be a valid arc id in the graph.
651 Arc arcFromId(int) const { return INVALID; }
653 /// \brief Returns an upper bound on the node IDs.
654 int maxNodeId() const { return -1; }
656 /// \brief Returns an upper bound on the edge IDs.
657 int maxEdgeId() const { return -1; }
659 /// \brief Returns an upper bound on the arc IDs.
660 int maxArcId() const { return -1; }
662 void first(Node&) const {}
663 void next(Node&) const {}
665 void first(Edge&) const {}
666 void next(Edge&) const {}
668 void first(Arc&) const {}
669 void next(Arc&) const {}
671 void firstOut(Arc&, Node) const {}
672 void nextOut(Arc&) const {}
674 void firstIn(Arc&, Node) const {}
675 void nextIn(Arc&) const {}
677 void firstInc(Edge &, bool &, const Node &) const {}
678 void nextInc(Edge &, bool &) const {}
680 // The second parameter is dummy.
681 Node fromId(int, Node) const { return INVALID; }
682 // The second parameter is dummy.
683 Edge fromId(int, Edge) const { return INVALID; }
684 // The second parameter is dummy.
685 Arc fromId(int, Arc) const { return INVALID; }
688 int maxId(Node) const { return -1; }
690 int maxId(Edge) const { return -1; }
692 int maxId(Arc) const { return -1; }
694 /// \brief Base node of the iterator
696 /// Returns the base node (the source in this case) of the iterator
697 Node baseNode(OutArcIt e) const {
700 /// \brief Running node of the iterator
702 /// Returns the running node (the target in this case) of the
704 Node runningNode(OutArcIt e) const {
708 /// \brief Base node of the iterator
710 /// Returns the base node (the target in this case) of the iterator
711 Node baseNode(InArcIt e) const {
714 /// \brief Running node of the iterator
716 /// Returns the running node (the source in this case) of the
718 Node runningNode(InArcIt e) const {
722 /// \brief Base node of the iterator
724 /// Returns the base node of the iterator
725 Node baseNode(IncEdgeIt) const {
729 /// \brief Running node of the iterator
731 /// Returns the running node of the iterator
732 Node runningNode(IncEdgeIt) const {
736 template <typename _Graph>
739 checkConcept<IterableGraphComponent<>, _Graph>();
740 checkConcept<IDableGraphComponent<>, _Graph>();
741 checkConcept<MappableGraphComponent<>, _Graph>();