1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
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 #ifndef LEMON_CONCEPTS_DIGRAPH_H
20 #define LEMON_CONCEPTS_DIGRAPH_H
22 ///\ingroup graph_concepts
24 ///\brief The concept of directed graphs.
26 #include <lemon/core.h>
27 #include <lemon/concepts/maps.h>
28 #include <lemon/concept_check.h>
29 #include <lemon/concepts/graph_components.h>
30 #include <lemon/bits/stl_iterators.h>
35 /// \ingroup graph_concepts
37 /// \brief Class describing the concept of directed graphs.
39 /// This class describes the common interface of all directed
40 /// graphs (digraphs).
42 /// Like all concept classes, it only provides an interface
43 /// without any sensible implementation. So any general algorithm for
44 /// directed graphs should compile with this class, but it will not
45 /// run properly, of course.
46 /// An actual digraph implementation like \ref ListDigraph or
47 /// \ref SmartDigraph may have additional functionality.
52 /// Diraphs are \e not copy constructible. Use DigraphCopy instead.
53 Digraph(const Digraph &) {}
54 /// \brief Assignment of a digraph to another one is \e not allowed.
55 /// Use DigraphCopy instead.
56 void operator=(const Digraph &) {}
59 /// Default constructor.
62 /// The node type of the digraph
64 /// This class identifies a node of the digraph. It also serves
65 /// as a base class of the node iterators,
66 /// thus they convert to this type.
69 /// Default constructor
71 /// Default constructor.
72 /// \warning It sets the object to an undefined value.
80 /// %Invalid constructor \& conversion.
82 /// Initializes the object to be invalid.
83 /// \sa Invalid for more details.
87 /// Equality operator.
89 /// Two iterators are equal if and only if they point to the
90 /// same object or both are \c INVALID.
91 bool operator==(Node) const { return true; }
93 /// Inequality operator
95 /// Inequality operator.
96 bool operator!=(Node) const { return true; }
98 /// Artificial ordering operator.
100 /// Artificial ordering operator.
102 /// \note This operator only has to define some strict ordering of
103 /// the nodes; this order has nothing to do with the iteration
104 /// ordering of the nodes.
105 bool operator<(Node) const { return false; }
108 /// Iterator class for the nodes.
110 /// This iterator goes through each node of the digraph.
111 /// Its usage is quite simple, for example, you can count the number
112 /// of nodes in a digraph \c g of type \c %Digraph like this:
115 /// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count;
117 class NodeIt : public Node {
119 /// Default constructor
121 /// Default constructor.
122 /// \warning It sets the iterator to an undefined value.
124 /// Copy constructor.
126 /// Copy constructor.
128 NodeIt(const NodeIt& n) : Node(n) { }
129 /// %Invalid constructor \& conversion.
131 /// Initializes the iterator to be invalid.
132 /// \sa Invalid for more details.
134 /// Sets the iterator to the first node.
136 /// Sets the iterator to the first node of the given digraph.
138 explicit NodeIt(const Digraph&) { }
139 /// Sets the iterator to the given node.
141 /// Sets the iterator to the given node of the given digraph.
143 NodeIt(const Digraph&, const Node&) { }
146 /// Assign the iterator to the next node.
148 NodeIt& operator++() { return *this; }
151 /// \brief Gets the collection of the nodes of the digraph.
153 /// This function can be used for iterating on
154 /// the nodes of the digraph. It returns a wrapped NodeIt, which looks
155 /// like an STL container (by having begin() and end())
156 /// which you can use in range-based for loops, STL algorithms, etc.
157 /// For example you can write:
160 /// for(auto v: g.nodes())
163 /// //Using an STL algorithm:
164 /// copy(g.nodes().begin(), g.nodes().end(), vect.begin());
166 LemonRangeWrapper1<NodeIt, Digraph> nodes() const {
167 return LemonRangeWrapper1<NodeIt, Digraph>(*this);
171 /// The arc type of the digraph
173 /// This class identifies an arc of the digraph. It also serves
174 /// as a base class of the arc iterators,
175 /// thus they will convert to this type.
178 /// Default constructor
180 /// Default constructor.
181 /// \warning It sets the object to an undefined value.
183 /// Copy constructor.
185 /// Copy constructor.
188 /// %Invalid constructor \& conversion.
190 /// Initializes the object to be invalid.
191 /// \sa Invalid for more details.
193 /// Equality operator
195 /// Equality operator.
197 /// Two iterators are equal if and only if they point to the
198 /// same object or both are \c INVALID.
199 bool operator==(Arc) const { return true; }
200 /// Inequality operator
202 /// Inequality operator.
203 bool operator!=(Arc) const { return true; }
205 /// Artificial ordering operator.
207 /// Artificial ordering operator.
209 /// \note This operator only has to define some strict ordering of
210 /// the arcs; this order has nothing to do with the iteration
211 /// ordering of the arcs.
212 bool operator<(Arc) const { return false; }
215 /// Iterator class for the outgoing arcs of a node.
217 /// This iterator goes trough the \e outgoing arcs of a certain node
219 /// Its usage is quite simple, for example, you can count the number
220 /// of outgoing arcs of a node \c n
221 /// in a digraph \c g of type \c %Digraph as follows.
224 /// for (Digraph::OutArcIt a(g, n); a!=INVALID; ++a) ++count;
226 class OutArcIt : public Arc {
228 /// Default constructor
230 /// Default constructor.
231 /// \warning It sets the iterator to an undefined value.
233 /// Copy constructor.
235 /// Copy constructor.
237 OutArcIt(const OutArcIt& e) : Arc(e) { }
238 /// %Invalid constructor \& conversion.
240 /// Initializes the iterator to be invalid.
241 /// \sa Invalid for more details.
242 OutArcIt(Invalid) { }
243 /// Sets the iterator to the first outgoing arc.
245 /// Sets the iterator to the first outgoing arc of the given node.
247 OutArcIt(const Digraph&, const Node&) { }
248 /// Sets the iterator to the given arc.
250 /// Sets the iterator to the given arc of the given digraph.
252 OutArcIt(const Digraph&, const Arc&) { }
253 /// Next outgoing arc
255 /// Assign the iterator to the next
256 /// outgoing arc of the corresponding node.
257 OutArcIt& operator++() { return *this; }
260 /// \brief Gets the collection of the outgoing arcs of a certain node
263 /// This function can be used for iterating on the
264 /// outgoing arcs of a certain node of the digraph. It returns a wrapped
265 /// OutArcIt, which looks like an STL container
266 /// (by having begin() and end()) which you can use in range-based
267 /// for loops, STL algorithms, etc.
268 /// For example if g is a Digraph and u is a node, you can write:
270 /// for(auto a: g.outArcs(u))
273 /// //Using an STL algorithm:
274 /// copy(g.outArcs(u).begin(), g.outArcs(u).end(), vect.begin());
276 LemonRangeWrapper2<OutArcIt, Digraph, Node> outArcs(const Node& u) const {
277 return LemonRangeWrapper2<OutArcIt, Digraph, Node>(*this, u);
281 /// Iterator class for the incoming arcs of a node.
283 /// This iterator goes trough the \e incoming arcs of a certain node
285 /// Its usage is quite simple, for example, you can count the number
286 /// of incoming arcs of a node \c n
287 /// in a digraph \c g of type \c %Digraph as follows.
290 /// for(Digraph::InArcIt a(g, n); a!=INVALID; ++a) ++count;
292 class InArcIt : public Arc {
294 /// Default constructor
296 /// Default constructor.
297 /// \warning It sets the iterator to an undefined value.
299 /// Copy constructor.
301 /// Copy constructor.
303 InArcIt(const InArcIt& e) : Arc(e) { }
304 /// %Invalid constructor \& conversion.
306 /// Initializes the iterator to be invalid.
307 /// \sa Invalid for more details.
309 /// Sets the iterator to the first incoming arc.
311 /// Sets the iterator to the first incoming arc of the given node.
313 InArcIt(const Digraph&, const Node&) { }
314 /// Sets the iterator to the given arc.
316 /// Sets the iterator to the given arc of the given digraph.
318 InArcIt(const Digraph&, const Arc&) { }
319 /// Next incoming arc
321 /// Assign the iterator to the next
322 /// incoming arc of the corresponding node.
323 InArcIt& operator++() { return *this; }
326 /// \brief Gets the collection of the incoming arcs of a certain node
329 /// This function can be used for iterating on the
330 /// incoming arcs of a certain node of the digraph. It returns a wrapped
331 /// InArcIt, which looks like an STL container
332 /// (by having begin() and end()) which you can use in range-based
333 /// for loops, STL algorithms, etc.
334 /// For example if g is a Digraph and u is a node, you can write:
336 /// for(auto a: g.inArcs(u))
339 /// //Using an STL algorithm:
340 /// copy(g.inArcs(u).begin(), g.inArcs(u).end(), vect.begin());
342 LemonRangeWrapper2<InArcIt, Digraph, Node> inArcs(const Node& u) const {
343 return LemonRangeWrapper2<InArcIt, Digraph, Node>(*this, u);
347 /// Iterator class for the arcs.
349 /// This iterator goes through each arc of the digraph.
350 /// Its usage is quite simple, for example, you can count the number
351 /// of arcs in a digraph \c g of type \c %Digraph as follows:
354 /// for(Digraph::ArcIt a(g); a!=INVALID; ++a) ++count;
356 class ArcIt : public Arc {
358 /// Default constructor
360 /// Default constructor.
361 /// \warning It sets the iterator to an undefined value.
363 /// Copy constructor.
365 /// Copy constructor.
367 ArcIt(const ArcIt& e) : Arc(e) { }
368 /// %Invalid constructor \& conversion.
370 /// Initializes the iterator to be invalid.
371 /// \sa Invalid for more details.
373 /// Sets the iterator to the first arc.
375 /// Sets the iterator to the first arc of the given digraph.
377 explicit ArcIt(const Digraph& g) {
378 ::lemon::ignore_unused_variable_warning(g);
380 /// Sets the iterator to the given arc.
382 /// Sets the iterator to the given arc of the given digraph.
384 ArcIt(const Digraph&, const Arc&) { }
387 /// Assign the iterator to the next arc.
389 ArcIt& operator++() { return *this; }
392 /// \brief Gets the collection of the arcs of the digraph.
394 /// This function can be used for iterating on the
395 /// arcs of the digraph. It returns a wrapped
396 /// ArcIt, which looks like an STL container
397 /// (by having begin() and end()) which you can use in range-based
398 /// for loops, STL algorithms, etc.
399 /// For example you can write:
402 /// for(auto a: g.arcs())
405 /// //Using an STL algorithm:
406 /// copy(g.arcs().begin(), g.arcs().end(), vect.begin());
408 LemonRangeWrapper1<ArcIt, Digraph> arcs() const {
409 return LemonRangeWrapper1<ArcIt, Digraph>(*this);
413 /// \brief The source node of the arc.
415 /// Returns the source node of the given arc.
416 Node source(Arc) const { return INVALID; }
418 /// \brief The target node of the arc.
420 /// Returns the target node of the given arc.
421 Node target(Arc) const { return INVALID; }
423 /// \brief The ID of the node.
425 /// Returns the ID of the given node.
426 int id(Node) const { return -1; }
428 /// \brief The ID of the arc.
430 /// Returns the ID of the given arc.
431 int id(Arc) const { return -1; }
433 /// \brief The node with the given ID.
435 /// Returns the node with the given ID.
436 /// \pre The argument should be a valid node ID in the digraph.
437 Node nodeFromId(int) const { return INVALID; }
439 /// \brief The arc with the given ID.
441 /// Returns the arc with the given ID.
442 /// \pre The argument should be a valid arc ID in the digraph.
443 Arc arcFromId(int) const { return INVALID; }
445 /// \brief An upper bound on the node IDs.
447 /// Returns an upper bound on the node IDs.
448 int maxNodeId() const { return -1; }
450 /// \brief An upper bound on the arc IDs.
452 /// Returns an upper bound on the arc IDs.
453 int maxArcId() const { return -1; }
455 void first(Node&) const {}
456 void next(Node&) const {}
458 void first(Arc&) const {}
459 void next(Arc&) const {}
462 void firstIn(Arc&, const Node&) const {}
463 void nextIn(Arc&) const {}
465 void firstOut(Arc&, const Node&) const {}
466 void nextOut(Arc&) const {}
468 // The second parameter is dummy.
469 Node fromId(int, Node) const { return INVALID; }
470 // The second parameter is dummy.
471 Arc fromId(int, Arc) const { return INVALID; }
474 int maxId(Node) const { return -1; }
476 int maxId(Arc) const { return -1; }
478 /// \brief The opposite node on the arc.
480 /// Returns the opposite node on the given arc.
481 Node oppositeNode(Node, Arc) const { return INVALID; }
483 /// \brief The base node of the iterator.
485 /// Returns the base node of the given outgoing arc iterator
486 /// (i.e. the source node of the corresponding arc).
487 Node baseNode(OutArcIt) const { return INVALID; }
489 /// \brief The running node of the iterator.
491 /// Returns the running node of the given outgoing arc iterator
492 /// (i.e. the target node of the corresponding arc).
493 Node runningNode(OutArcIt) const { return INVALID; }
495 /// \brief The base node of the iterator.
497 /// Returns the base node of the given incoming arc iterator
498 /// (i.e. the target node of the corresponding arc).
499 Node baseNode(InArcIt) const { return INVALID; }
501 /// \brief The running node of the iterator.
503 /// Returns the running node of the given incoming arc iterator
504 /// (i.e. the source node of the corresponding arc).
505 Node runningNode(InArcIt) const { return INVALID; }
507 /// \brief Standard graph map type for the nodes.
509 /// Standard graph map type for the nodes.
510 /// It conforms to the ReferenceMap concept.
512 class NodeMap : public ReferenceMap<Node, T, T&, const T&> {
516 explicit NodeMap(const Digraph&) { }
517 /// Constructor with given initial value
518 NodeMap(const Digraph&, T) { }
522 NodeMap(const NodeMap& nm) :
523 ReferenceMap<Node, T, T&, const T&>(nm) { }
524 ///Assignment operator
525 template <typename CMap>
526 NodeMap& operator=(const CMap&) {
527 checkConcept<ReadMap<Node, T>, CMap>();
532 /// \brief Standard graph map type for the arcs.
534 /// Standard graph map type for the arcs.
535 /// It conforms to the ReferenceMap concept.
537 class ArcMap : public ReferenceMap<Arc, T, T&, const T&> {
541 explicit ArcMap(const Digraph&) { }
542 /// Constructor with given initial value
543 ArcMap(const Digraph&, T) { }
547 ArcMap(const ArcMap& em) :
548 ReferenceMap<Arc, T, T&, const T&>(em) { }
549 ///Assignment operator
550 template <typename CMap>
551 ArcMap& operator=(const CMap&) {
552 checkConcept<ReadMap<Arc, T>, CMap>();
557 template <typename _Digraph>
560 checkConcept<BaseDigraphComponent, _Digraph>();
561 checkConcept<IterableDigraphComponent<>, _Digraph>();
562 checkConcept<IDableDigraphComponent<>, _Digraph>();
563 checkConcept<MappableDigraphComponent<>, _Digraph>();
569 } //namespace concepts