1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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/concepts/maps.h>
28 #include <lemon/concept_check.h>
29 #include <lemon/core.h>
34 /// \ingroup graph_concepts
36 /// \brief Class describing the concept of undirected graphs.
38 /// This class describes the common interface of all undirected
41 /// Like all concept classes, it only provides an interface
42 /// without any sensible implementation. So any general algorithm for
43 /// undirected graphs should compile with this class, but it will not
44 /// run properly, of course.
45 /// An actual graph implementation like \ref ListGraph or
46 /// \ref SmartGraph may have additional functionality.
48 /// The undirected graphs also fulfill the concept of \ref Digraph
49 /// "directed graphs", since each edge can also be regarded as two
50 /// oppositely directed arcs.
51 /// Undirected graphs provide an Edge type for the undirected edges and
52 /// an Arc type for the directed arcs. The Arc type is convertible to
53 /// Edge or inherited from it, i.e. the corresponding edge can be
54 /// obtained from an arc.
55 /// EdgeIt and EdgeMap classes can be used for the edges, while ArcIt
56 /// and ArcMap classes can be used for the arcs (just like in digraphs).
57 /// Both InArcIt and OutArcIt iterates on the same edges but with
58 /// opposite direction. IncEdgeIt also iterates on the same edges
59 /// as OutArcIt and InArcIt, but it is not convertible to Arc,
62 /// In LEMON, each undirected edge has an inherent orientation.
63 /// Thus it can defined if an arc is forward or backward oriented in
64 /// an undirected graph with respect to this default oriantation of
65 /// the represented edge.
66 /// With the direction() and direct() functions the direction
67 /// of an arc can be obtained and set, respectively.
69 /// Only nodes and edges can be added to or removed from an undirected
70 /// graph and the corresponding arcs are added or removed automatically.
75 /// Graphs are \e not copy constructible. Use GraphCopy instead.
76 Graph(const Graph&) {}
77 /// \brief Assignment of a graph to another one is \e not allowed.
78 /// Use GraphCopy instead.
79 void operator=(const Graph&) {}
82 /// Default constructor.
85 /// \brief Undirected graphs should be tagged with \c UndirectedTag.
87 /// Undirected graphs should be tagged with \c UndirectedTag.
89 /// This tag helps the \c enable_if technics to make compile time
90 /// specializations for undirected graphs.
91 typedef True UndirectedTag;
93 /// The node type of the graph
95 /// This class identifies a node of the graph. It also serves
96 /// as a base class of the node iterators,
97 /// thus they convert to this type.
100 /// Default constructor
102 /// Default constructor.
103 /// \warning It sets the object to an undefined value.
105 /// Copy constructor.
107 /// Copy constructor.
109 Node(const Node&) { }
111 /// %Invalid constructor \& conversion.
113 /// Initializes the object to be invalid.
114 /// \sa Invalid for more details.
116 /// Equality operator
118 /// Equality operator.
120 /// Two iterators are equal if and only if they point to the
121 /// same object or both are \c INVALID.
122 bool operator==(Node) const { return true; }
124 /// Inequality operator
126 /// Inequality operator.
127 bool operator!=(Node) const { return true; }
129 /// Artificial ordering operator.
131 /// Artificial ordering operator.
133 /// \note This operator only has to define some strict ordering of
134 /// the items; this order has nothing to do with the iteration
135 /// ordering of the items.
136 bool operator<(Node) const { return false; }
140 /// Iterator class for the nodes.
142 /// This iterator goes through each node of the graph.
143 /// Its usage is quite simple, for example, you can count the number
144 /// of nodes in a graph \c g of type \c %Graph like this:
147 /// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count;
149 class NodeIt : public Node {
151 /// Default constructor
153 /// Default constructor.
154 /// \warning It sets the iterator to an undefined value.
156 /// Copy constructor.
158 /// Copy constructor.
160 NodeIt(const NodeIt& n) : Node(n) { }
161 /// %Invalid constructor \& conversion.
163 /// Initializes the iterator to be invalid.
164 /// \sa Invalid for more details.
166 /// Sets the iterator to the first node.
168 /// Sets the iterator to the first node of the given digraph.
170 explicit NodeIt(const Graph&) { }
171 /// Sets the iterator to the given node.
173 /// Sets the iterator to the given node of the given digraph.
175 NodeIt(const Graph&, const Node&) { }
178 /// Assign the iterator to the next node.
180 NodeIt& operator++() { return *this; }
184 /// The edge type of the graph
186 /// This class identifies an edge of the graph. It also serves
187 /// as a base class of the edge iterators,
188 /// thus they will convert to this type.
191 /// Default constructor
193 /// Default constructor.
194 /// \warning It sets the object to an undefined value.
196 /// Copy constructor.
198 /// Copy constructor.
200 Edge(const Edge&) { }
201 /// %Invalid constructor \& conversion.
203 /// Initializes the object to be invalid.
204 /// \sa Invalid for more details.
206 /// Equality operator
208 /// Equality operator.
210 /// Two iterators are equal if and only if they point to the
211 /// same object or both are \c INVALID.
212 bool operator==(Edge) const { return true; }
213 /// Inequality operator
215 /// Inequality operator.
216 bool operator!=(Edge) const { return true; }
218 /// Artificial ordering operator.
220 /// Artificial ordering operator.
222 /// \note This operator only has to define some strict ordering of
223 /// the edges; this order has nothing to do with the iteration
224 /// ordering of the edges.
225 bool operator<(Edge) const { return false; }
228 /// Iterator class for the edges.
230 /// This iterator goes through each edge of the graph.
231 /// Its usage is quite simple, for example, you can count the number
232 /// of edges in a graph \c g of type \c %Graph as follows:
235 /// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count;
237 class EdgeIt : public Edge {
239 /// Default constructor
241 /// Default constructor.
242 /// \warning It sets the iterator to an undefined value.
244 /// Copy constructor.
246 /// Copy constructor.
248 EdgeIt(const EdgeIt& e) : Edge(e) { }
249 /// %Invalid constructor \& conversion.
251 /// Initializes the iterator to be invalid.
252 /// \sa Invalid for more details.
254 /// Sets the iterator to the first edge.
256 /// Sets the iterator to the first edge of the given graph.
258 explicit EdgeIt(const Graph&) { }
259 /// Sets the iterator to the given edge.
261 /// Sets the iterator to the given edge of the given graph.
263 EdgeIt(const Graph&, const Edge&) { }
266 /// Assign the iterator to the next edge.
268 EdgeIt& operator++() { return *this; }
271 /// Iterator class for the incident edges of a node.
273 /// This iterator goes trough the incident undirected edges
274 /// of a certain node of a graph.
275 /// Its usage is quite simple, for example, you can compute the
276 /// degree (i.e. the number of incident edges) of a node \c n
277 /// in a graph \c g of type \c %Graph as follows.
281 /// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
284 /// \warning Loop edges will be iterated twice.
285 class IncEdgeIt : public Edge {
287 /// Default constructor
289 /// Default constructor.
290 /// \warning It sets the iterator to an undefined value.
292 /// Copy constructor.
294 /// Copy constructor.
296 IncEdgeIt(const IncEdgeIt& e) : Edge(e) { }
297 /// %Invalid constructor \& conversion.
299 /// Initializes the iterator to be invalid.
300 /// \sa Invalid for more details.
301 IncEdgeIt(Invalid) { }
302 /// Sets the iterator to the first incident edge.
304 /// Sets the iterator to the first incident edge of the given node.
306 IncEdgeIt(const Graph&, const Node&) { }
307 /// Sets the iterator to the given edge.
309 /// Sets the iterator to the given edge of the given graph.
311 IncEdgeIt(const Graph&, const Edge&) { }
312 /// Next incident edge
314 /// Assign the iterator to the next incident edge
315 /// of the corresponding node.
316 IncEdgeIt& operator++() { return *this; }
319 /// The arc type of the graph
321 /// This class identifies a directed arc of the graph. It also serves
322 /// as a base class of the arc iterators,
323 /// thus they will convert to this type.
326 /// Default constructor
328 /// Default constructor.
329 /// \warning It sets the object to an undefined value.
331 /// Copy constructor.
333 /// Copy constructor.
336 /// %Invalid constructor \& conversion.
338 /// Initializes the object to be invalid.
339 /// \sa Invalid for more details.
341 /// Equality operator
343 /// Equality operator.
345 /// Two iterators are equal if and only if they point to the
346 /// same object or both are \c INVALID.
347 bool operator==(Arc) const { return true; }
348 /// Inequality operator
350 /// Inequality operator.
351 bool operator!=(Arc) const { return true; }
353 /// Artificial ordering operator.
355 /// Artificial ordering operator.
357 /// \note This operator only has to define some strict ordering of
358 /// the arcs; this order has nothing to do with the iteration
359 /// ordering of the arcs.
360 bool operator<(Arc) const { return false; }
362 /// Converison to \c Edge
364 /// Converison to \c Edge.
366 operator Edge() const { return Edge(); }
369 /// Iterator class for the arcs.
371 /// This iterator goes through each directed arc of the graph.
372 /// Its usage is quite simple, for example, you can count the number
373 /// of arcs in a graph \c g of type \c %Graph as follows:
376 /// for(Graph::ArcIt a(g); a!=INVALID; ++a) ++count;
378 class ArcIt : public Arc {
380 /// Default constructor
382 /// Default constructor.
383 /// \warning It sets the iterator to an undefined value.
385 /// Copy constructor.
387 /// Copy constructor.
389 ArcIt(const ArcIt& e) : Arc(e) { }
390 /// %Invalid constructor \& conversion.
392 /// Initializes the iterator to be invalid.
393 /// \sa Invalid for more details.
395 /// Sets the iterator to the first arc.
397 /// Sets the iterator to the first arc of the given graph.
399 explicit ArcIt(const Graph &g) { ignore_unused_variable_warning(g); }
400 /// Sets the iterator to the given arc.
402 /// Sets the iterator to the given arc of the given graph.
404 ArcIt(const Graph&, const Arc&) { }
407 /// Assign the iterator to the next arc.
409 ArcIt& operator++() { return *this; }
412 /// Iterator class for the outgoing arcs of a node.
414 /// This iterator goes trough the \e outgoing directed arcs of a
415 /// certain node of a graph.
416 /// Its usage is quite simple, for example, you can count the number
417 /// of outgoing arcs of a node \c n
418 /// in a graph \c g of type \c %Graph as follows.
421 /// for (Digraph::OutArcIt a(g, n); a!=INVALID; ++a) ++count;
423 class OutArcIt : public Arc {
425 /// Default constructor
427 /// Default constructor.
428 /// \warning It sets the iterator to an undefined value.
430 /// Copy constructor.
432 /// Copy constructor.
434 OutArcIt(const OutArcIt& e) : Arc(e) { }
435 /// %Invalid constructor \& conversion.
437 /// Initializes the iterator to be invalid.
438 /// \sa Invalid for more details.
439 OutArcIt(Invalid) { }
440 /// Sets the iterator to the first outgoing arc.
442 /// Sets the iterator to the first outgoing arc of the given node.
444 OutArcIt(const Graph& n, const Node& g) {
445 ignore_unused_variable_warning(n);
446 ignore_unused_variable_warning(g);
448 /// Sets the iterator to the given arc.
450 /// Sets the iterator to the given arc of the given graph.
452 OutArcIt(const Graph&, const Arc&) { }
453 /// Next outgoing arc
455 /// Assign the iterator to the next
456 /// outgoing arc of the corresponding node.
457 OutArcIt& operator++() { return *this; }
460 /// Iterator class for the incoming arcs of a node.
462 /// This iterator goes trough the \e incoming directed arcs of a
463 /// certain node of a graph.
464 /// Its usage is quite simple, for example, you can count the number
465 /// of incoming arcs of a node \c n
466 /// in a graph \c g of type \c %Graph as follows.
469 /// for (Digraph::InArcIt a(g, n); a!=INVALID; ++a) ++count;
471 class InArcIt : public Arc {
473 /// Default constructor
475 /// Default constructor.
476 /// \warning It sets the iterator to an undefined value.
478 /// Copy constructor.
480 /// Copy constructor.
482 InArcIt(const InArcIt& e) : Arc(e) { }
483 /// %Invalid constructor \& conversion.
485 /// Initializes the iterator to be invalid.
486 /// \sa Invalid for more details.
488 /// Sets the iterator to the first incoming arc.
490 /// Sets the iterator to the first incoming arc of the given node.
492 InArcIt(const Graph& g, const Node& n) {
493 ignore_unused_variable_warning(n);
494 ignore_unused_variable_warning(g);
496 /// Sets the iterator to the given arc.
498 /// Sets the iterator to the given arc of the given graph.
500 InArcIt(const Graph&, const Arc&) { }
501 /// Next incoming arc
503 /// Assign the iterator to the next
504 /// incoming arc of the corresponding node.
505 InArcIt& operator++() { return *this; }
508 /// \brief Standard graph map type for the nodes.
510 /// Standard graph map type for the nodes.
511 /// It conforms to the ReferenceMap concept.
513 class NodeMap : public ReferenceMap<Node, T, T&, const T&>
518 explicit NodeMap(const Graph&) { }
519 /// Constructor with given initial value
520 NodeMap(const Graph&, T) { }
524 NodeMap(const NodeMap& nm) :
525 ReferenceMap<Node, T, T&, const T&>(nm) { }
526 ///Assignment operator
527 template <typename CMap>
528 NodeMap& operator=(const CMap&) {
529 checkConcept<ReadMap<Node, T>, CMap>();
534 /// \brief Standard graph map type for the arcs.
536 /// Standard graph map type for the arcs.
537 /// It conforms to the ReferenceMap concept.
539 class ArcMap : public ReferenceMap<Arc, T, T&, const T&>
544 explicit ArcMap(const Graph&) { }
545 /// Constructor with given initial value
546 ArcMap(const Graph&, T) { }
550 ArcMap(const ArcMap& em) :
551 ReferenceMap<Arc, T, T&, const T&>(em) { }
552 ///Assignment operator
553 template <typename CMap>
554 ArcMap& operator=(const CMap&) {
555 checkConcept<ReadMap<Arc, T>, CMap>();
560 /// \brief Standard graph map type for the edges.
562 /// Standard graph map type for the edges.
563 /// It conforms to the ReferenceMap concept.
565 class EdgeMap : public ReferenceMap<Edge, T, T&, const T&>
570 explicit EdgeMap(const Graph&) { }
571 /// Constructor with given initial value
572 EdgeMap(const Graph&, T) { }
576 EdgeMap(const EdgeMap& em) :
577 ReferenceMap<Edge, T, T&, const T&>(em) {}
578 ///Assignment operator
579 template <typename CMap>
580 EdgeMap& operator=(const CMap&) {
581 checkConcept<ReadMap<Edge, T>, CMap>();
586 /// \brief The first node of the edge.
588 /// Returns the first node of the given edge.
590 /// Edges don't have source and target nodes, however, methods
591 /// u() and v() are used to query the two end-nodes of an edge.
592 /// The orientation of an edge that arises this way is called
593 /// the inherent direction, it is used to define the default
594 /// direction for the corresponding arcs.
597 Node u(Edge) const { return INVALID; }
599 /// \brief The second node of the edge.
601 /// Returns the second node of the given edge.
603 /// Edges don't have source and target nodes, however, methods
604 /// u() and v() are used to query the two end-nodes of an edge.
605 /// The orientation of an edge that arises this way is called
606 /// the inherent direction, it is used to define the default
607 /// direction for the corresponding arcs.
610 Node v(Edge) const { return INVALID; }
612 /// \brief The source node of the arc.
614 /// Returns the source node of the given arc.
615 Node source(Arc) const { return INVALID; }
617 /// \brief The target node of the arc.
619 /// Returns the target node of the given arc.
620 Node target(Arc) const { return INVALID; }
622 /// \brief The ID of the node.
624 /// Returns the ID of the given node.
625 int id(Node) const { return -1; }
627 /// \brief The ID of the edge.
629 /// Returns the ID of the given edge.
630 int id(Edge) const { return -1; }
632 /// \brief The ID of the arc.
634 /// Returns the ID of the given arc.
635 int id(Arc) const { return -1; }
637 /// \brief The node with the given ID.
639 /// 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 The edge with the given ID.
645 /// Returns the edge with the given ID.
646 /// \pre The argument should be a valid edge ID in the graph.
647 Edge edgeFromId(int) const { return INVALID; }
649 /// \brief The arc with the given ID.
651 /// Returns the arc with the given ID.
652 /// \pre The argument should be a valid arc ID in the graph.
653 Arc arcFromId(int) const { return INVALID; }
655 /// \brief An upper bound on the node IDs.
657 /// Returns an upper bound on the node IDs.
658 int maxNodeId() const { return -1; }
660 /// \brief An upper bound on the edge IDs.
662 /// Returns an upper bound on the edge IDs.
663 int maxEdgeId() const { return -1; }
665 /// \brief An upper bound on the arc IDs.
667 /// Returns an upper bound on the arc IDs.
668 int maxArcId() const { return -1; }
670 /// \brief The direction of the arc.
672 /// Returns \c true if the direction of the given arc is the same as
673 /// the inherent orientation of the represented edge.
674 bool direction(Arc) const { return true; }
676 /// \brief Direct the edge.
678 /// Direct the given edge. The returned arc
679 /// represents the given edge and its direction comes
680 /// from the bool parameter. If it is \c true, then the direction
681 /// of the arc is the same as the inherent orientation of the edge.
682 Arc direct(Edge, bool) const {
686 /// \brief Direct the edge.
688 /// Direct the given edge. The returned arc represents the given
689 /// edge and its source node is the given node.
690 Arc direct(Edge, Node) const {
694 /// \brief The oppositely directed arc.
696 /// Returns the oppositely directed arc representing the same edge.
697 Arc oppositeArc(Arc) const { return INVALID; }
699 /// \brief The opposite node on the edge.
701 /// Returns the opposite node on the given edge.
702 Node oppositeNode(Node, Edge) const { return INVALID; }
704 void first(Node&) const {}
705 void next(Node&) const {}
707 void first(Edge&) const {}
708 void next(Edge&) const {}
710 void first(Arc&) const {}
711 void next(Arc&) const {}
713 void firstOut(Arc&, Node) const {}
714 void nextOut(Arc&) const {}
716 void firstIn(Arc&, Node) const {}
717 void nextIn(Arc&) const {}
719 void firstInc(Edge &, bool &, const Node &) const {}
720 void nextInc(Edge &, bool &) const {}
722 // The second parameter is dummy.
723 Node fromId(int, Node) const { return INVALID; }
724 // The second parameter is dummy.
725 Edge fromId(int, Edge) const { return INVALID; }
726 // The second parameter is dummy.
727 Arc fromId(int, Arc) const { return INVALID; }
730 int maxId(Node) const { return -1; }
732 int maxId(Edge) const { return -1; }
734 int maxId(Arc) const { return -1; }
736 /// \brief The base node of the iterator.
738 /// Returns the base node of the given incident edge iterator.
739 Node baseNode(IncEdgeIt) const { return INVALID; }
741 /// \brief The running node of the iterator.
743 /// Returns the running node of the given incident edge iterator.
744 Node runningNode(IncEdgeIt) const { return INVALID; }
746 /// \brief The base node of the iterator.
748 /// Returns the base node of the given outgoing arc iterator
749 /// (i.e. the source node of the corresponding arc).
750 Node baseNode(OutArcIt) const { return INVALID; }
752 /// \brief The running node of the iterator.
754 /// Returns the running node of the given outgoing arc iterator
755 /// (i.e. the target node of the corresponding arc).
756 Node runningNode(OutArcIt) const { return INVALID; }
758 /// \brief The base node of the iterator.
760 /// Returns the base node of the given incoming arc iterator
761 /// (i.e. the target node of the corresponding arc).
762 Node baseNode(InArcIt) const { return INVALID; }
764 /// \brief The running node of the iterator.
766 /// Returns the running node of the given incoming arc iterator
767 /// (i.e. the source node of the corresponding arc).
768 Node runningNode(InArcIt) const { return INVALID; }
770 template <typename _Graph>
773 checkConcept<BaseGraphComponent, _Graph>();
774 checkConcept<IterableGraphComponent<>, _Graph>();
775 checkConcept<IDableGraphComponent<>, _Graph>();
776 checkConcept<MappableGraphComponent<>, _Graph>();