Better doc.
2 * lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_GRAPH_UTILS_H
18 #define LEMON_GRAPH_UTILS_H
25 #include <lemon/invalid.h>
26 #include <lemon/utility.h>
27 #include <lemon/maps.h>
28 #include <lemon/traits.h>
29 #include <lemon/bits/alteration_notifier.h>
33 ///\brief Graph utilities.
40 /// \addtogroup gutils
43 ///Creates convenience typedefs for the graph types and iterators
45 ///This \c \#define creates convenience typedefs for the following types
46 ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
48 ///\note If \c G it a template parameter, it should be used in this way.
50 /// GRAPH_TYPEDEFS(typename G)
53 ///\warning There are no typedefs for the graph maps because of the lack of
54 ///template typedefs in C++.
55 #define GRAPH_TYPEDEFS(Graph) \
56 typedef Graph:: Node Node; \
57 typedef Graph:: NodeIt NodeIt; \
58 typedef Graph:: Edge Edge; \
59 typedef Graph:: EdgeIt EdgeIt; \
60 typedef Graph:: InEdgeIt InEdgeIt; \
61 typedef Graph::OutEdgeIt OutEdgeIt;
63 ///Creates convenience typedefs for the undirected graph types and iterators
65 ///This \c \#define creates the same convenience typedefs as defined by
66 ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
67 ///\c UndirEdge, \c UndirEdgeIt, \c IncEdgeIt,
69 ///\note If \c G it a template parameter, it should be used in this way.
71 /// UNDIRGRAPH_TYPEDEFS(typename G)
74 ///\warning There are no typedefs for the graph maps because of the lack of
75 ///template typedefs in C++.
76 #define UNDIRGRAPH_TYPEDEFS(Graph) \
77 GRAPH_TYPEDEFS(Graph) \
78 typedef Graph:: UndirEdge UndirEdge; \
79 typedef Graph:: UndirEdgeIt UndirEdgeIt; \
80 typedef Graph:: IncEdgeIt IncEdgeIt;
83 /// \brief Function to count the items in the graph.
85 /// This function counts the items (nodes, edges etc) in the graph.
86 /// The complexity of the function is O(n) because
87 /// it iterates on all of the items.
89 template <typename Graph, typename ItemIt>
90 inline int countItems(const Graph& g) {
92 for (ItemIt it(g); it != INVALID; ++it) {
100 template <typename Graph>
102 typename enable_if<typename Graph::NodeNumTag, int>::type
103 _countNodes(const Graph &g) {
107 template <typename Graph>
108 inline int _countNodes(Wrap<Graph> w) {
109 return countItems<Graph, typename Graph::NodeIt>(w.value);
112 /// \brief Function to count the nodes in the graph.
114 /// This function counts the nodes in the graph.
115 /// The complexity of the function is O(n) but for some
116 /// graph structures it is specialized to run in O(1).
118 /// \todo refer how to specialize it
120 template <typename Graph>
121 inline int countNodes(const Graph& g) {
122 return _countNodes<Graph>(g);
127 template <typename Graph>
129 typename enable_if<typename Graph::EdgeNumTag, int>::type
130 _countEdges(const Graph &g) {
134 template <typename Graph>
135 inline int _countEdges(Wrap<Graph> w) {
136 return countItems<Graph, typename Graph::EdgeIt>(w.value);
139 /// \brief Function to count the edges in the graph.
141 /// This function counts the edges in the graph.
142 /// The complexity of the function is O(e) but for some
143 /// graph structures it is specialized to run in O(1).
145 template <typename Graph>
146 inline int countEdges(const Graph& g) {
147 return _countEdges<Graph>(g);
150 // Undirected edge counting:
152 template <typename Graph>
154 typename enable_if<typename Graph::EdgeNumTag, int>::type
155 _countUndirEdges(const Graph &g) {
156 return g.undirEdgeNum();
159 template <typename Graph>
160 inline int _countUndirEdges(Wrap<Graph> w) {
161 return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
164 /// \brief Function to count the undirected edges in the graph.
166 /// This function counts the undirected edges in the graph.
167 /// The complexity of the function is O(e) but for some
168 /// graph structures it is specialized to run in O(1).
170 template <typename Graph>
171 inline int countUndirEdges(const Graph& g) {
172 return _countUndirEdges<Graph>(g);
177 template <typename Graph, typename DegIt>
178 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
180 for (DegIt it(_g, _n); it != INVALID; ++it) {
186 /// \brief Function to count the number of the out-edges from node \c n.
188 /// This function counts the number of the out-edges from node \c n
190 template <typename Graph>
191 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
192 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
195 /// \brief Function to count the number of the in-edges to node \c n.
197 /// This function counts the number of the in-edges to node \c n
199 template <typename Graph>
200 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
201 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
204 /// \brief Function to count the number of the inc-edges to node \c n.
206 /// This function counts the number of the inc-edges to node \c n
208 template <typename Graph>
209 inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
210 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
214 template <typename Graph>
216 typename enable_if<typename Graph::FindEdgeTag, typename Graph::Edge>::type
217 _findEdge(const Graph &g,
218 typename Graph::Node u, typename Graph::Node v,
219 typename Graph::Edge prev = INVALID) {
220 return g.findEdge(u, v, prev);
223 template <typename Graph>
224 inline typename Graph::Edge
225 _findEdge(Wrap<Graph> w,
226 typename Graph::Node u,
227 typename Graph::Node v,
228 typename Graph::Edge prev = INVALID) {
229 const Graph& g = w.value;
230 if (prev == INVALID) {
231 typename Graph::OutEdgeIt e(g, u);
232 while (e != INVALID && g.target(e) != v) ++e;
235 typename Graph::OutEdgeIt e(g, prev); ++e;
236 while (e != INVALID && g.target(e) != v) ++e;
241 /// \brief Finds an edge between two nodes of a graph.
243 /// Finds an edge from node \c u to node \c v in graph \c g.
245 /// If \c prev is \ref INVALID (this is the default value), then
246 /// it finds the first edge from \c u to \c v. Otherwise it looks for
247 /// the next edge from \c u to \c v after \c prev.
248 /// \return The found edge or \ref INVALID if there is no such an edge.
250 /// Thus you can iterate through each edge from \c u to \c v as it follows.
252 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
256 // /// \todo We may want to use the "GraphBase"
257 // /// interface here...
258 template <typename Graph>
259 inline typename Graph::Edge findEdge(const Graph &g,
260 typename Graph::Node u,
261 typename Graph::Node v,
262 typename Graph::Edge prev = INVALID) {
263 return _findEdge<Graph>(g, u, v, prev);
266 /// \brief Iterator for iterating on edges connected the same nodes.
268 /// Iterator for iterating on edges connected the same nodes. It is
269 /// higher level interface for the findEdge() function. You can
270 /// use it the following way:
272 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
277 /// \author Balazs Dezso
278 template <typename _Graph>
279 class ConEdgeIt : public _Graph::Edge {
282 typedef _Graph Graph;
283 typedef typename Graph::Edge Parent;
285 typedef typename Graph::Edge Edge;
286 typedef typename Graph::Node Node;
288 /// \brief Constructor.
290 /// Construct a new ConEdgeIt iterating on the edges which
291 /// connects the \c u and \c v node.
292 ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
293 Parent::operator=(findEdge(graph, u, v));
296 /// \brief Constructor.
298 /// Construct a new ConEdgeIt which continues the iterating from
300 ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
302 /// \brief Increment operator.
304 /// It increments the iterator and gives back the next edge.
305 ConEdgeIt& operator++() {
306 Parent::operator=(findEdge(graph, graph.source(*this),
307 graph.target(*this), *this));
314 template <typename Graph>
317 typename Graph::FindEdgeTag,
318 typename Graph::UndirEdge>::type
319 _findUndirEdge(const Graph &g,
320 typename Graph::Node u, typename Graph::Node v,
321 typename Graph::UndirEdge prev = INVALID) {
322 return g.findUndirEdge(u, v, prev);
325 template <typename Graph>
326 inline typename Graph::UndirEdge
327 _findUndirEdge(Wrap<Graph> w,
328 typename Graph::Node u,
329 typename Graph::Node v,
330 typename Graph::UndirEdge prev = INVALID) {
331 const Graph& g = w.value;
332 if (prev == INVALID) {
333 typename Graph::OutEdgeIt e(g, u);
334 while (e != INVALID && g.target(e) != v) ++e;
337 typename Graph::OutEdgeIt e(g, g.direct(prev, u)); ++e;
338 while (e != INVALID && g.target(e) != v) ++e;
343 /// \brief Finds an undir edge between two nodes of a graph.
345 /// Finds an undir edge from node \c u to node \c v in graph \c g.
347 /// If \c prev is \ref INVALID (this is the default value), then
348 /// it finds the first edge from \c u to \c v. Otherwise it looks for
349 /// the next edge from \c u to \c v after \c prev.
350 /// \return The found edge or \ref INVALID if there is no such an edge.
352 /// Thus you can iterate through each edge from \c u to \c v as it follows.
354 /// for(UndirEdge e = findUndirEdge(g,u,v); e != INVALID;
355 /// e = findUndirEdge(g,u,v,e)) {
359 // /// \todo We may want to use the "GraphBase"
360 // /// interface here...
361 template <typename Graph>
362 inline typename Graph::UndirEdge
363 findUndirEdge(const Graph &g,
364 typename Graph::Node u,
365 typename Graph::Node v,
366 typename Graph::UndirEdge prev = INVALID) {
367 return _findUndirEdge<Graph>(g, u, v, prev);
370 /// \brief Iterator for iterating on undir edges connected the same nodes.
372 /// Iterator for iterating on undir edges connected the same nodes. It is
373 /// higher level interface for the findUndirEdge() function. You can
374 /// use it the following way:
376 /// for (ConUndirEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
381 /// \author Balazs Dezso
382 template <typename _Graph>
383 class ConUndirEdgeIt : public _Graph::UndirEdge {
386 typedef _Graph Graph;
387 typedef typename Graph::UndirEdge Parent;
389 typedef typename Graph::UndirEdge UndirEdge;
390 typedef typename Graph::Node Node;
392 /// \brief Constructor.
394 /// Construct a new ConUndirEdgeIt iterating on the edges which
395 /// connects the \c u and \c v node.
396 ConUndirEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
397 Parent::operator=(findUndirEdge(graph, u, v));
400 /// \brief Constructor.
402 /// Construct a new ConUndirEdgeIt which continues the iterating from
404 ConUndirEdgeIt(const Graph& g, UndirEdge e) : Parent(e), graph(g) {}
406 /// \brief Increment operator.
408 /// It increments the iterator and gives back the next edge.
409 ConUndirEdgeIt& operator++() {
410 Parent::operator=(findUndirEdge(graph, graph.source(*this),
411 graph.target(*this), *this));
418 /// \brief Copy a map.
420 /// This function copies the \c source map to the \c target map. It uses the
421 /// given iterator to iterate on the data structure and it uses the \c ref
422 /// mapping to convert the source's keys to the target's keys.
423 template <typename Target, typename Source,
424 typename ItemIt, typename Ref>
425 void copyMap(Target& target, const Source& source,
426 ItemIt it, const Ref& ref) {
427 for (; it != INVALID; ++it) {
428 target[ref[it]] = source[it];
432 /// \brief Copy the source map to the target map.
434 /// Copy the \c source map to the \c target map. It uses the given iterator
435 /// to iterate on the data structure.
436 template <typename Target, typename Source,
438 void copyMap(Target& target, const Source& source, ItemIt it) {
439 for (; it != INVALID; ++it) {
440 target[it] = source[it];
444 /// \brief Class to copy a graph.
446 /// Class to copy a graph to an other graph (duplicate a graph). The
447 /// simplest way of using it is through the \c copyGraph() function.
448 template <typename Target, typename Source>
451 typedef typename Source::Node Node;
452 typedef typename Source::NodeIt NodeIt;
453 typedef typename Source::Edge Edge;
454 typedef typename Source::EdgeIt EdgeIt;
456 typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
457 typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
459 /// \brief Constructor for the GraphCopy.
461 /// It copies the content of the \c _source graph into the
462 /// \c _target graph. It creates also two references, one beetween
463 /// the two nodeset and one beetween the two edgesets.
464 GraphCopy(Target& _target, const Source& _source)
465 : source(_source), target(_target),
466 nodeRefMap(_source), edgeRefMap(_source) {
467 for (NodeIt it(source); it != INVALID; ++it) {
468 nodeRefMap[it] = target.addNode();
470 for (EdgeIt it(source); it != INVALID; ++it) {
471 edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
472 nodeRefMap[source.target(it)]);
476 /// \brief Copies the node references into the given map.
478 /// Copies the node references into the given map.
479 template <typename NodeRef>
480 const GraphCopy& nodeRef(NodeRef& map) const {
481 for (NodeIt it(source); it != INVALID; ++it) {
482 map.set(it, nodeRefMap[it]);
487 /// \brief Reverse and copies the node references into the given map.
489 /// Reverse and copies the node references into the given map.
490 template <typename NodeRef>
491 const GraphCopy& nodeCrossRef(NodeRef& map) const {
492 for (NodeIt it(source); it != INVALID; ++it) {
493 map.set(nodeRefMap[it], it);
498 /// \brief Copies the edge references into the given map.
500 /// Copies the edge references into the given map.
501 template <typename EdgeRef>
502 const GraphCopy& edgeRef(EdgeRef& map) const {
503 for (EdgeIt it(source); it != INVALID; ++it) {
504 map.set(it, edgeRefMap[it]);
509 /// \brief Reverse and copies the edge references into the given map.
511 /// Reverse and copies the edge references into the given map.
512 template <typename EdgeRef>
513 const GraphCopy& edgeCrossRef(EdgeRef& map) const {
514 for (EdgeIt it(source); it != INVALID; ++it) {
515 map.set(edgeRefMap[it], it);
520 /// \brief Make copy of the given map.
522 /// Makes copy of the given map for the newly created graph.
523 /// The new map's key type is the target graph's node type,
524 /// and the copied map's key type is the source graph's node
526 template <typename TargetMap, typename SourceMap>
527 const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
528 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
532 /// \brief Make copy of the given map.
534 /// Makes copy of the given map for the newly created graph.
535 /// The new map's key type is the target graph's edge type,
536 /// and the copied map's key type is the source graph's edge
538 template <typename TargetMap, typename SourceMap>
539 const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
540 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
544 /// \brief Gives back the stored node references.
546 /// Gives back the stored node references.
547 const NodeRefMap& nodeRef() const {
551 /// \brief Gives back the stored edge references.
553 /// Gives back the stored edge references.
554 const EdgeRefMap& edgeRef() const {
562 const Source& source;
565 NodeRefMap nodeRefMap;
566 EdgeRefMap edgeRefMap;
569 /// \brief Copy a graph to an other graph.
571 /// Copy a graph to an other graph.
572 /// The usage of the function:
575 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
578 /// After the copy the \c nr map will contain the mapping from the
579 /// source graph's nodes to the target graph's nodes and the \c ecr will
580 /// contain the mapping from the target graph's edges to the source's
582 template <typename Target, typename Source>
583 GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
584 return GraphCopy<Target, Source>(target, source);
587 /// \brief Class to copy an undirected graph.
589 /// Class to copy an undirected graph to an other graph (duplicate a graph).
590 /// The simplest way of using it is through the \c copyUndirGraph() function.
591 template <typename Target, typename Source>
592 class UndirGraphCopy {
594 typedef typename Source::Node Node;
595 typedef typename Source::NodeIt NodeIt;
596 typedef typename Source::Edge Edge;
597 typedef typename Source::EdgeIt EdgeIt;
598 typedef typename Source::UndirEdge UndirEdge;
599 typedef typename Source::UndirEdgeIt UndirEdgeIt;
601 typedef typename Source::
602 template NodeMap<typename Target::Node> NodeRefMap;
604 typedef typename Source::
605 template UndirEdgeMap<typename Target::UndirEdge> UndirEdgeRefMap;
610 EdgeRefMap(UndirGraphCopy& _gc) : gc(_gc) {}
611 typedef typename Source::Edge Key;
612 typedef typename Target::Edge Value;
614 Value operator[](const Key& key) {
615 return gc.target.direct(gc.undirEdgeRef[key],
616 gc.target.direction(key));
624 /// \brief Constructor for the UndirGraphCopy.
626 /// It copies the content of the \c _source graph into the
627 /// \c _target graph. It creates also two references, one beetween
628 /// the two nodeset and one beetween the two edgesets.
629 UndirGraphCopy(Target& _target, const Source& _source)
630 : source(_source), target(_target),
631 nodeRefMap(_source), edgeRefMap(*this), undirEdgeRefMap(_source) {
632 for (NodeIt it(source); it != INVALID; ++it) {
633 nodeRefMap[it] = target.addNode();
635 for (UndirEdgeIt it(source); it != INVALID; ++it) {
636 undirEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
637 nodeRefMap[source.target(it)]);
641 /// \brief Copies the node references into the given map.
643 /// Copies the node references into the given map.
644 template <typename NodeRef>
645 const UndirGraphCopy& nodeRef(NodeRef& map) const {
646 for (NodeIt it(source); it != INVALID; ++it) {
647 map.set(it, nodeRefMap[it]);
652 /// \brief Reverse and copies the node references into the given map.
654 /// Reverse and copies the node references into the given map.
655 template <typename NodeRef>
656 const UndirGraphCopy& nodeCrossRef(NodeRef& map) const {
657 for (NodeIt it(source); it != INVALID; ++it) {
658 map.set(nodeRefMap[it], it);
663 /// \brief Copies the edge references into the given map.
665 /// Copies the edge references into the given map.
666 template <typename EdgeRef>
667 const UndirGraphCopy& edgeRef(EdgeRef& map) const {
668 for (EdgeIt it(source); it != INVALID; ++it) {
669 map.set(edgeRefMap[it], it);
674 /// \brief Reverse and copies the undirected edge references into the
677 /// Reverse and copies the undirected edge references into the given map.
678 template <typename EdgeRef>
679 const UndirGraphCopy& edgeCrossRef(EdgeRef& map) const {
680 for (EdgeIt it(source); it != INVALID; ++it) {
681 map.set(it, edgeRefMap[it]);
686 /// \brief Copies the undirected edge references into the given map.
688 /// Copies the undirected edge references into the given map.
689 template <typename EdgeRef>
690 const UndirGraphCopy& undirEdgeRef(EdgeRef& map) const {
691 for (UndirEdgeIt it(source); it != INVALID; ++it) {
692 map.set(it, undirEdgeRefMap[it]);
697 /// \brief Reverse and copies the undirected edge references into the
700 /// Reverse and copies the undirected edge references into the given map.
701 template <typename EdgeRef>
702 const UndirGraphCopy& undirEdgeCrossRef(EdgeRef& map) const {
703 for (UndirEdgeIt it(source); it != INVALID; ++it) {
704 map.set(undirEdgeRefMap[it], it);
709 /// \brief Make copy of the given map.
711 /// Makes copy of the given map for the newly created graph.
712 /// The new map's key type is the target graph's node type,
713 /// and the copied map's key type is the source graph's node
715 template <typename TargetMap, typename SourceMap>
716 const UndirGraphCopy& nodeMap(TargetMap& tMap,
717 const SourceMap& sMap) const {
718 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
722 /// \brief Make copy of the given map.
724 /// Makes copy of the given map for the newly created graph.
725 /// The new map's key type is the target graph's edge type,
726 /// and the copied map's key type is the source graph's edge
728 template <typename TargetMap, typename SourceMap>
729 const UndirGraphCopy& edgeMap(TargetMap& tMap,
730 const SourceMap& sMap) const {
731 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
735 /// \brief Make copy of the given map.
737 /// Makes copy of the given map for the newly created graph.
738 /// The new map's key type is the target graph's edge type,
739 /// and the copied map's key type is the source graph's edge
741 template <typename TargetMap, typename SourceMap>
742 const UndirGraphCopy& undirEdgeMap(TargetMap& tMap,
743 const SourceMap& sMap) const {
744 copyMap(tMap, sMap, UndirEdgeIt(source), undirEdgeRefMap);
748 /// \brief Gives back the stored node references.
750 /// Gives back the stored node references.
751 const NodeRefMap& nodeRef() const {
755 /// \brief Gives back the stored edge references.
757 /// Gives back the stored edge references.
758 const EdgeRefMap& edgeRef() const {
762 /// \brief Gives back the stored undir edge references.
764 /// Gives back the stored undir edge references.
765 const UndirEdgeRefMap& undirEdgeRef() const {
766 return undirEdgeRefMap;
773 const Source& source;
776 NodeRefMap nodeRefMap;
777 EdgeRefMap edgeRefMap;
778 UndirEdgeRefMap undirEdgeRefMap;
781 /// \brief Copy a graph to an other graph.
783 /// Copy a graph to an other graph.
784 /// The usage of the function:
787 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
790 /// After the copy the \c nr map will contain the mapping from the
791 /// source graph's nodes to the target graph's nodes and the \c ecr will
792 /// contain the mapping from the target graph's edges to the source's
794 template <typename Target, typename Source>
795 UndirGraphCopy<Target, Source>
796 copyUndirGraph(Target& target, const Source& source) {
797 return UndirGraphCopy<Target, Source>(target, source);
803 /// \addtogroup graph_maps
806 /// Provides an immutable and unique id for each item in the graph.
808 /// The IdMap class provides a unique and immutable id for each item of the
809 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
810 /// different items (nodes) get different ids <li>\b immutable: the id of an
811 /// item (node) does not change (even if you delete other nodes). </ul>
812 /// Through this map you get access (i.e. can read) the inner id values of
813 /// the items stored in the graph. This map can be inverted with its member
814 /// class \c InverseMap.
816 template <typename _Graph, typename _Item>
819 typedef _Graph Graph;
824 /// \brief Constructor.
826 /// Constructor for creating id map.
827 IdMap(const Graph& _graph) : graph(&_graph) {}
829 /// \brief Gives back the \e id of the item.
831 /// Gives back the immutable and unique \e id of the map.
832 int operator[](const Item& item) const { return graph->id(item);}
840 /// \brief The class represents the inverse of its owner (IdMap).
842 /// The class represents the inverse of its owner (IdMap).
847 /// \brief Constructor.
849 /// Constructor for creating an id-to-item map.
850 InverseMap(const Graph& _graph) : graph(&_graph) {}
852 /// \brief Constructor.
854 /// Constructor for creating an id-to-item map.
855 InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
857 /// \brief Gives back the given item from its id.
859 /// Gives back the given item from its id.
861 Item operator[](int id) const { return graph->fromId(id, Item());}
866 /// \brief Gives back the inverse of the map.
868 /// Gives back the inverse of the IdMap.
869 InverseMap inverse() const { return InverseMap(*graph);}
874 /// \brief General invertable graph-map type.
876 /// This type provides simple invertable graph-maps.
877 /// The InvertableMap wraps an arbitrary ReadWriteMap
878 /// and if a key is set to a new value then store it
879 /// in the inverse map.
880 /// \param _Graph The graph type.
881 /// \param _Map The map to extend with invertable functionality.
887 = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent
889 class InvertableMap : protected _Map {
894 typedef _Graph Graph;
896 /// The key type of InvertableMap (Node, Edge, UndirEdge).
897 typedef typename _Map::Key Key;
898 /// The value type of the InvertableMap.
899 typedef typename _Map::Value Value;
901 /// \brief Constructor.
903 /// Construct a new InvertableMap for the graph.
905 InvertableMap(const Graph& graph) : Map(graph) {}
907 /// \brief The setter function of the map.
909 /// Sets the mapped value.
910 void set(const Key& key, const Value& val) {
911 Value oldval = Map::operator[](key);
912 typename Container::iterator it = invMap.find(oldval);
913 if (it != invMap.end() && it->second == key) {
916 invMap.insert(make_pair(val, key));
920 /// \brief The getter function of the map.
922 /// It gives back the value associated with the key.
923 Value operator[](const Key& key) const {
924 return Map::operator[](key);
929 /// \brief Add a new key to the map.
931 /// Add a new key to the map. It is called by the
932 /// \c AlterationNotifier.
933 virtual void add(const Key& key) {
937 /// \brief Erase the key from the map.
939 /// Erase the key to the map. It is called by the
940 /// \c AlterationNotifier.
941 virtual void erase(const Key& key) {
942 Value val = Map::operator[](key);
943 typename Container::iterator it = invMap.find(val);
944 if (it != invMap.end() && it->second == key) {
950 /// \brief Clear the keys from the map and inverse map.
952 /// Clear the keys from the map and inverse map. It is called by the
953 /// \c AlterationNotifier.
954 virtual void clear() {
961 typedef std::map<Value, Key> Container;
966 /// \brief The inverse map type.
968 /// The inverse of this map. The subscript operator of the map
969 /// gives back always the item what was last assigned to the value.
972 /// \brief Constructor of the InverseMap.
974 /// Constructor of the InverseMap.
975 InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
977 /// The value type of the InverseMap.
978 typedef typename InvertableMap::Key Value;
979 /// The key type of the InverseMap.
980 typedef typename InvertableMap::Value Key;
982 /// \brief Subscript operator.
984 /// Subscript operator. It gives back always the item
985 /// what was last assigned to the value.
986 Value operator[](const Key& key) const {
987 typename Container::const_iterator it = inverted.invMap.find(key);
992 const InvertableMap& inverted;
995 /// \brief It gives back the just readeable inverse map.
997 /// It gives back the just readeable inverse map.
998 InverseMap inverse() const {
999 return InverseMap(*this);
1006 /// \brief Provides a mutable, continuous and unique descriptor for each
1007 /// item in the graph.
1009 /// The DescriptorMap class provides a unique and continuous (but mutable)
1010 /// descriptor (id) for each item of the same type (e.g. node) in the
1011 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1012 /// different ids <li>\b continuous: the range of the ids is the set of
1013 /// integers between 0 and \c n-1, where \c n is the number of the items of
1014 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1015 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1016 /// with its member class \c InverseMap.
1018 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1019 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
1021 /// \param _Map A ReadWriteMap mapping from the item type to integer.
1026 = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
1028 class DescriptorMap : protected _Map {
1034 /// The graph class of DescriptorMap.
1035 typedef _Graph Graph;
1037 /// The key type of DescriptorMap (Node, Edge, UndirEdge).
1038 typedef typename _Map::Key Key;
1039 /// The value type of DescriptorMap.
1040 typedef typename _Map::Value Value;
1042 /// \brief Constructor.
1044 /// Constructor for descriptor map.
1045 DescriptorMap(const Graph& _graph) : Map(_graph) {
1051 /// \brief Add a new key to the map.
1053 /// Add a new key to the map. It is called by the
1054 /// \c AlterationNotifier.
1055 virtual void add(const Item& item) {
1057 Map::set(item, invMap.size());
1058 invMap.push_back(item);
1061 /// \brief Erase the key from the map.
1063 /// Erase the key to the map. It is called by the
1064 /// \c AlterationNotifier.
1065 virtual void erase(const Item& item) {
1066 Map::set(invMap.back(), Map::operator[](item));
1067 invMap[Map::operator[](item)] = invMap.back();
1072 /// \brief Build the unique map.
1074 /// Build the unique map. It is called by the
1075 /// \c AlterationNotifier.
1076 virtual void build() {
1079 const typename Map::Graph* graph = Map::getGraph();
1080 for (graph->first(it); it != INVALID; graph->next(it)) {
1081 Map::set(it, invMap.size());
1082 invMap.push_back(it);
1086 /// \brief Clear the keys from the map.
1088 /// Clear the keys from the map. It is called by the
1089 /// \c AlterationNotifier.
1090 virtual void clear() {
1097 /// \brief Swaps the position of the two items in the map.
1099 /// Swaps the position of the two items in the map.
1100 void swap(const Item& p, const Item& q) {
1101 int pi = Map::operator[](p);
1102 int qi = Map::operator[](q);
1109 /// \brief Gives back the \e descriptor of the item.
1111 /// Gives back the mutable and unique \e descriptor of the map.
1112 int operator[](const Item& item) const {
1113 return Map::operator[](item);
1118 typedef std::vector<Item> Container;
1122 /// \brief The inverse map type of DescriptorMap.
1124 /// The inverse map type of DescriptorMap.
1127 /// \brief Constructor of the InverseMap.
1129 /// Constructor of the InverseMap.
1130 InverseMap(const DescriptorMap& _inverted)
1131 : inverted(_inverted) {}
1134 /// The value type of the InverseMap.
1135 typedef typename DescriptorMap::Key Value;
1136 /// The key type of the InverseMap.
1137 typedef typename DescriptorMap::Value Key;
1139 /// \brief Subscript operator.
1141 /// Subscript operator. It gives back the item
1142 /// that the descriptor belongs to currently.
1143 Value operator[](const Key& key) const {
1144 return inverted.invMap[key];
1147 /// \brief Size of the map.
1149 /// Returns the size of the map.
1151 return inverted.invMap.size();
1155 const DescriptorMap& inverted;
1158 /// \brief Gives back the inverse of the map.
1160 /// Gives back the inverse of the map.
1161 const InverseMap inverse() const {
1162 return InverseMap(*this);
1166 /// \brief Returns the source of the given edge.
1168 /// The SourceMap gives back the source Node of the given edge.
1169 /// \author Balazs Dezso
1170 template <typename Graph>
1174 typedef typename Graph::Node Value;
1175 typedef typename Graph::Edge Key;
1177 /// \brief Constructor
1180 /// \param _graph The graph that the map belongs to.
1181 SourceMap(const Graph& _graph) : graph(_graph) {}
1183 /// \brief The subscript operator.
1185 /// The subscript operator.
1186 /// \param edge The edge
1187 /// \return The source of the edge
1188 Value operator[](const Key& edge) const {
1189 return graph.source(edge);
1196 /// \brief Returns a \ref SourceMap class
1198 /// This function just returns an \ref SourceMap class.
1199 /// \relates SourceMap
1200 template <typename Graph>
1201 inline SourceMap<Graph> sourceMap(const Graph& graph) {
1202 return SourceMap<Graph>(graph);
1205 /// \brief Returns the target of the given edge.
1207 /// The TargetMap gives back the target Node of the given edge.
1208 /// \author Balazs Dezso
1209 template <typename Graph>
1213 typedef typename Graph::Node Value;
1214 typedef typename Graph::Edge Key;
1216 /// \brief Constructor
1219 /// \param _graph The graph that the map belongs to.
1220 TargetMap(const Graph& _graph) : graph(_graph) {}
1222 /// \brief The subscript operator.
1224 /// The subscript operator.
1225 /// \param e The edge
1226 /// \return The target of the edge
1227 Value operator[](const Key& e) const {
1228 return graph.target(e);
1235 /// \brief Returns a \ref TargetMap class
1237 /// This function just returns a \ref TargetMap class.
1238 /// \relates TargetMap
1239 template <typename Graph>
1240 inline TargetMap<Graph> targetMap(const Graph& graph) {
1241 return TargetMap<Graph>(graph);
1244 /// \brief Returns the "forward" directed edge view of an undirected edge.
1246 /// Returns the "forward" directed edge view of an undirected edge.
1247 /// \author Balazs Dezso
1248 template <typename Graph>
1252 typedef typename Graph::Edge Value;
1253 typedef typename Graph::UndirEdge Key;
1255 /// \brief Constructor
1258 /// \param _graph The graph that the map belongs to.
1259 ForwardMap(const Graph& _graph) : graph(_graph) {}
1261 /// \brief The subscript operator.
1263 /// The subscript operator.
1264 /// \param key An undirected edge
1265 /// \return The "forward" directed edge view of undirected edge
1266 Value operator[](const Key& key) const {
1267 return graph.direct(key, true);
1274 /// \brief Returns a \ref ForwardMap class
1276 /// This function just returns an \ref ForwardMap class.
1277 /// \relates ForwardMap
1278 template <typename Graph>
1279 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
1280 return ForwardMap<Graph>(graph);
1283 /// \brief Returns the "backward" directed edge view of an undirected edge.
1285 /// Returns the "backward" directed edge view of an undirected edge.
1286 /// \author Balazs Dezso
1287 template <typename Graph>
1291 typedef typename Graph::Edge Value;
1292 typedef typename Graph::UndirEdge Key;
1294 /// \brief Constructor
1297 /// \param _graph The graph that the map belongs to.
1298 BackwardMap(const Graph& _graph) : graph(_graph) {}
1300 /// \brief The subscript operator.
1302 /// The subscript operator.
1303 /// \param key An undirected edge
1304 /// \return The "backward" directed edge view of undirected edge
1305 Value operator[](const Key& key) const {
1306 return graph.direct(key, false);
1313 /// \brief Returns a \ref BackwardMap class
1315 /// This function just returns a \ref BackwardMap class.
1316 /// \relates BackwardMap
1317 template <typename Graph>
1318 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
1319 return BackwardMap<Graph>(graph);
1322 /// \brief Potential difference map
1324 /// If there is an potential map on the nodes then we
1325 /// can get an edge map as we get the substraction of the
1326 /// values of the target and source.
1327 template <typename Graph, typename NodeMap>
1328 class PotentialDifferenceMap {
1330 typedef typename Graph::Edge Key;
1331 typedef typename NodeMap::Value Value;
1333 /// \brief Constructor
1335 /// Contructor of the map
1336 PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential)
1337 : graph(_graph), potential(_potential) {}
1339 /// \brief Const subscription operator
1341 /// Const subscription operator
1342 Value operator[](const Key& edge) const {
1343 return potential[graph.target(edge)] - potential[graph.source(edge)];
1348 const NodeMap& potential;
1351 /// \brief Just returns a PotentialDifferenceMap
1353 /// Just returns a PotentialDifferenceMap
1354 /// \relates PotentialDifferenceMap
1355 template <typename Graph, typename NodeMap>
1356 PotentialDifferenceMap<Graph, NodeMap>
1357 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
1358 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
1361 /// \brief Map of the node in-degrees.
1363 /// This map returns the in-degree of a node. Once it is constructed,
1364 /// the degrees are stored in a standard NodeMap, so each query is done
1365 /// in constant time. On the other hand, the values are updated automatically
1366 /// whenever the graph changes.
1368 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1369 /// alternative ways to modify the graph. The correct behavior of InDegMap
1370 /// is not guarantied if these additional featureas are used. For example
1371 /// the funstions \ref ListGraph::changeSource() "changeSource()",
1372 /// \ref ListGraph::changeTarget() "changeTarget()" and
1373 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1374 /// of \ref ListGraph will \e not update the degree values correctly.
1378 template <typename _Graph>
1380 : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
1384 typedef _Graph Graph;
1386 typedef typename Graph::Node Key;
1390 class AutoNodeMap : public Graph::template NodeMap<int> {
1393 typedef typename Graph::template NodeMap<int> Parent;
1395 typedef typename Parent::Key Key;
1396 typedef typename Parent::Value Value;
1398 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1400 void add(const Key& key) {
1402 Parent::set(key, 0);
1408 /// \brief Constructor.
1410 /// Constructor for creating in-degree map.
1411 InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1412 AlterationNotifier<typename _Graph::Edge>
1413 ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
1415 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1416 deg[it] = countInEdges(graph, it);
1420 virtual ~InDegMap() {
1421 AlterationNotifier<typename _Graph::Edge>::
1422 ObserverBase::detach();
1425 /// Gives back the in-degree of a Node.
1426 int operator[](const Key& key) const {
1432 typedef typename Graph::Edge Edge;
1434 virtual void add(const Edge& edge) {
1435 ++deg[graph.target(edge)];
1438 virtual void erase(const Edge& edge) {
1439 --deg[graph.target(edge)];
1442 virtual void signalChange(const Edge& edge) {
1446 virtual void commitChange(const Edge& edge) {
1450 virtual void build() {
1451 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1452 deg[it] = countInEdges(graph, it);
1456 virtual void clear() {
1457 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1463 const _Graph& graph;
1467 /// \brief Map of the node out-degrees.
1469 /// This map returns the out-degree of a node. Once it is constructed,
1470 /// the degrees are stored in a standard NodeMap, so each query is done
1471 /// in constant time. On the other hand, the values are updated automatically
1472 /// whenever the graph changes.
1474 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1475 /// alternative ways to modify the graph. The correct behavior of OutDegMap
1476 /// is not guarantied if these additional featureas are used. For example
1477 /// the funstions \ref ListGraph::changeSource() "changeSource()",
1478 /// \ref ListGraph::changeTarget() "changeTarget()" and
1479 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1480 /// of \ref ListGraph will \e not update the degree values correctly.
1484 template <typename _Graph>
1486 : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
1490 typedef _Graph Graph;
1492 typedef typename Graph::Node Key;
1496 class AutoNodeMap : public Graph::template NodeMap<int> {
1499 typedef typename Graph::template NodeMap<int> Parent;
1501 typedef typename Parent::Key Key;
1502 typedef typename Parent::Value Value;
1504 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1506 void add(const Key& key) {
1508 Parent::set(key, 0);
1514 /// \brief Constructor.
1516 /// Constructor for creating out-degree map.
1517 OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1518 AlterationNotifier<typename _Graph::Edge>
1519 ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
1521 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1522 deg[it] = countOutEdges(graph, it);
1526 virtual ~OutDegMap() {
1527 AlterationNotifier<typename _Graph::Edge>::
1528 ObserverBase::detach();
1531 /// Gives back the in-degree of a Node.
1532 int operator[](const Key& key) const {
1538 typedef typename Graph::Edge Edge;
1540 virtual void add(const Edge& edge) {
1541 ++deg[graph.source(edge)];
1544 virtual void erase(const Edge& edge) {
1545 --deg[graph.source(edge)];
1548 virtual void signalChange(const Edge& edge) {
1552 virtual void commitChange(const Edge& edge) {
1557 virtual void build() {
1558 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1559 deg[it] = countOutEdges(graph, it);
1563 virtual void clear() {
1564 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1570 const _Graph& graph;
1577 } //END OF NAMESPACE LEMON