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 /// \brief Function to count the items in the graph.
45 /// This function counts the items (nodes, edges etc) in the graph.
46 /// The complexity of the function is O(n) because
47 /// it iterates on all of the items.
49 template <typename Graph, typename ItemIt>
50 inline int countItems(const Graph& g) {
52 for (ItemIt it(g); it != INVALID; ++it) {
60 template <typename Graph>
62 typename enable_if<typename Graph::NodeNumTag, int>::type
63 _countNodes(const Graph &g) {
67 template <typename Graph>
68 inline int _countNodes(Wrap<Graph> w) {
69 return countItems<Graph, typename Graph::NodeIt>(w.value);
72 /// \brief Function to count the nodes in the graph.
74 /// This function counts the nodes in the graph.
75 /// The complexity of the function is O(n) but for some
76 /// graph structures it is specialized to run in O(1).
78 /// \todo refer how to specialize it
80 template <typename Graph>
81 inline int countNodes(const Graph& g) {
82 return _countNodes<Graph>(g);
87 template <typename Graph>
89 typename enable_if<typename Graph::EdgeNumTag, int>::type
90 _countEdges(const Graph &g) {
94 template <typename Graph>
95 inline int _countEdges(Wrap<Graph> w) {
96 return countItems<Graph, typename Graph::EdgeIt>(w.value);
99 /// \brief Function to count the edges in the graph.
101 /// This function counts the edges in the graph.
102 /// The complexity of the function is O(e) but for some
103 /// graph structures it is specialized to run in O(1).
105 template <typename Graph>
106 inline int countEdges(const Graph& g) {
107 return _countEdges<Graph>(g);
110 // Undirected edge counting:
112 template <typename Graph>
114 typename enable_if<typename Graph::EdgeNumTag, int>::type
115 _countUndirEdges(const Graph &g) {
116 return g.undirEdgeNum();
119 template <typename Graph>
120 inline int _countUndirEdges(Wrap<Graph> w) {
121 return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
124 /// \brief Function to count the undirected edges in the graph.
126 /// This function counts the undirected edges in the graph.
127 /// The complexity of the function is O(e) but for some
128 /// graph structures it is specialized to run in O(1).
130 template <typename Graph>
131 inline int countUndirEdges(const Graph& g) {
132 return _countUndirEdges<Graph>(g);
137 template <typename Graph, typename DegIt>
138 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
140 for (DegIt it(_g, _n); it != INVALID; ++it) {
146 /// \brief Function to count the number of the out-edges from node \c n.
148 /// This function counts the number of the out-edges from node \c n
150 template <typename Graph>
151 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
152 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
155 /// \brief Function to count the number of the in-edges to node \c n.
157 /// This function counts the number of the in-edges to node \c n
159 template <typename Graph>
160 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
161 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
164 /// \brief Function to count the number of the inc-edges to node \c n.
166 /// This function counts the number of the inc-edges to node \c n
168 template <typename Graph>
169 inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
170 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
174 template <typename Graph>
176 typename enable_if<typename Graph::FindEdgeTag, typename Graph::Edge>::type
177 _findEdge(const Graph &g,
178 typename Graph::Node u, typename Graph::Node v,
179 typename Graph::Edge prev = INVALID) {
180 return g.findEdge(u, v, prev);
183 template <typename Graph>
184 inline typename Graph::Edge
185 _findEdge(Wrap<Graph> w,
186 typename Graph::Node u,
187 typename Graph::Node v,
188 typename Graph::Edge prev = INVALID) {
189 const Graph& g = w.value;
190 if (prev == INVALID) {
191 typename Graph::OutEdgeIt e(g, u);
192 while (e != INVALID && g.target(e) != v) ++e;
195 typename Graph::OutEdgeIt e(g, prev); ++e;
196 while (e != INVALID && g.target(e) != v) ++e;
201 /// \brief Finds an edge between two nodes of a graph.
203 /// Finds an edge from node \c u to node \c v in graph \c g.
205 /// If \c prev is \ref INVALID (this is the default value), then
206 /// it finds the first edge from \c u to \c v. Otherwise it looks for
207 /// the next edge from \c u to \c v after \c prev.
208 /// \return The found edge or \ref INVALID if there is no such an edge.
210 /// Thus you can iterate through each edge from \c u to \c v as it follows.
212 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
216 // /// \todo We may want to use the "GraphBase"
217 // /// interface here...
218 template <typename Graph>
219 inline typename Graph::Edge findEdge(const Graph &g,
220 typename Graph::Node u,
221 typename Graph::Node v,
222 typename Graph::Edge prev = INVALID) {
223 return _findEdge<Graph>(g, u, v, prev);
226 /// \brief Iterator for iterating on edges connected the same nodes.
228 /// Iterator for iterating on edges connected the same nodes. It is
229 /// higher level interface for the findEdge() function. You can
230 /// use it the following way:
232 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
237 /// \author Balazs Dezso
238 template <typename _Graph>
239 class ConEdgeIt : public _Graph::Edge {
242 typedef _Graph Graph;
243 typedef typename Graph::Edge Parent;
245 typedef typename Graph::Edge Edge;
246 typedef typename Graph::Node Node;
248 /// \brief Constructor.
250 /// Construct a new ConEdgeIt iterating on the edges which
251 /// connects the \c u and \c v node.
252 ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
253 Parent::operator=(findEdge(graph, u, v));
256 /// \brief Constructor.
258 /// Construct a new ConEdgeIt which continues the iterating from
260 ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
262 /// \brief Increment operator.
264 /// It increments the iterator and gives back the next edge.
265 ConEdgeIt& operator++() {
266 Parent::operator=(findEdge(graph, graph.source(*this),
267 graph.target(*this), *this));
274 template <typename Graph>
277 typename Graph::FindEdgeTag,
278 typename Graph::UndirEdge>::type
279 _findUndirEdge(const Graph &g,
280 typename Graph::Node u, typename Graph::Node v,
281 typename Graph::UndirEdge prev = INVALID) {
282 return g.findUndirEdge(u, v, prev);
285 template <typename Graph>
286 inline typename Graph::UndirEdge
287 _findUndirEdge(Wrap<Graph> w,
288 typename Graph::Node u,
289 typename Graph::Node v,
290 typename Graph::UndirEdge prev = INVALID) {
291 const Graph& g = w.value;
292 if (prev == INVALID) {
293 typename Graph::OutEdgeIt e(g, u);
294 while (e != INVALID && g.target(e) != v) ++e;
297 typename Graph::OutEdgeIt e(g, g.direct(prev, u)); ++e;
298 while (e != INVALID && g.target(e) != v) ++e;
303 /// \brief Finds an undir edge between two nodes of a graph.
305 /// Finds an undir edge from node \c u to node \c v in graph \c g.
307 /// If \c prev is \ref INVALID (this is the default value), then
308 /// it finds the first edge from \c u to \c v. Otherwise it looks for
309 /// the next edge from \c u to \c v after \c prev.
310 /// \return The found edge or \ref INVALID if there is no such an edge.
312 /// Thus you can iterate through each edge from \c u to \c v as it follows.
314 /// for(UndirEdge e = findUndirEdge(g,u,v); e != INVALID;
315 /// e = findUndirEdge(g,u,v,e)) {
319 // /// \todo We may want to use the "GraphBase"
320 // /// interface here...
321 template <typename Graph>
322 inline typename Graph::UndirEdge
323 findUndirEdge(const Graph &g,
324 typename Graph::Node u,
325 typename Graph::Node v,
326 typename Graph::UndirEdge prev = INVALID) {
327 return _findUndirEdge<Graph>(g, u, v, prev);
330 /// \brief Iterator for iterating on undir edges connected the same nodes.
332 /// Iterator for iterating on undir edges connected the same nodes. It is
333 /// higher level interface for the findUndirEdge() function. You can
334 /// use it the following way:
336 /// for (ConUndirEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
341 /// \author Balazs Dezso
342 template <typename _Graph>
343 class ConUndirEdgeIt : public _Graph::UndirEdge {
346 typedef _Graph Graph;
347 typedef typename Graph::UndirEdge Parent;
349 typedef typename Graph::UndirEdge UndirEdge;
350 typedef typename Graph::Node Node;
352 /// \brief Constructor.
354 /// Construct a new ConUndirEdgeIt iterating on the edges which
355 /// connects the \c u and \c v node.
356 ConUndirEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
357 Parent::operator=(findUndirEdge(graph, u, v));
360 /// \brief Constructor.
362 /// Construct a new ConUndirEdgeIt which continues the iterating from
364 ConUndirEdgeIt(const Graph& g, UndirEdge e) : Parent(e), graph(g) {}
366 /// \brief Increment operator.
368 /// It increments the iterator and gives back the next edge.
369 ConUndirEdgeIt& operator++() {
370 Parent::operator=(findUndirEdge(graph, graph.source(*this),
371 graph.target(*this), *this));
378 /// \brief Copy a map.
380 /// This function copies the \c source map to the \c target map. It uses the
381 /// given iterator to iterate on the data structure and it uses the \c ref
382 /// mapping to convert the source's keys to the target's keys.
383 template <typename Target, typename Source,
384 typename ItemIt, typename Ref>
385 void copyMap(Target& target, const Source& source,
386 ItemIt it, const Ref& ref) {
387 for (; it != INVALID; ++it) {
388 target[ref[it]] = source[it];
392 /// \brief Copy the source map to the target map.
394 /// Copy the \c source map to the \c target map. It uses the given iterator
395 /// to iterate on the data structure.
396 template <typename Target, typename Source,
398 void copyMap(Target& target, const Source& source, ItemIt it) {
399 for (; it != INVALID; ++it) {
400 target[it] = source[it];
404 /// \brief Class to copy a graph.
406 /// Class to copy a graph to an other graph (duplicate a graph). The
407 /// simplest way of using it is through the \c copyGraph() function.
408 template <typename Target, typename Source>
411 typedef typename Source::Node Node;
412 typedef typename Source::NodeIt NodeIt;
413 typedef typename Source::Edge Edge;
414 typedef typename Source::EdgeIt EdgeIt;
416 typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
417 typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
419 /// \brief Constructor for the GraphCopy.
421 /// It copies the content of the \c _source graph into the
422 /// \c _target graph. It creates also two references, one beetween
423 /// the two nodeset and one beetween the two edgesets.
424 GraphCopy(Target& _target, const Source& _source)
425 : source(_source), target(_target),
426 nodeRefMap(_source), edgeRefMap(_source) {
427 for (NodeIt it(source); it != INVALID; ++it) {
428 nodeRefMap[it] = target.addNode();
430 for (EdgeIt it(source); it != INVALID; ++it) {
431 edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
432 nodeRefMap[source.target(it)]);
436 /// \brief Copies the node references into the given map.
438 /// Copies the node references into the given map.
439 template <typename NodeRef>
440 const GraphCopy& nodeRef(NodeRef& map) const {
441 for (NodeIt it(source); it != INVALID; ++it) {
442 map.set(it, nodeRefMap[it]);
447 /// \brief Reverse and copies the node references into the given map.
449 /// Reverse and copies the node references into the given map.
450 template <typename NodeRef>
451 const GraphCopy& nodeCrossRef(NodeRef& map) const {
452 for (NodeIt it(source); it != INVALID; ++it) {
453 map.set(nodeRefMap[it], it);
458 /// \brief Copies the edge references into the given map.
460 /// Copies the edge references into the given map.
461 template <typename EdgeRef>
462 const GraphCopy& edgeRef(EdgeRef& map) const {
463 for (EdgeIt it(source); it != INVALID; ++it) {
464 map.set(it, edgeRefMap[it]);
469 /// \brief Reverse and copies the edge references into the given map.
471 /// Reverse and copies the edge references into the given map.
472 template <typename EdgeRef>
473 const GraphCopy& edgeCrossRef(EdgeRef& map) const {
474 for (EdgeIt it(source); it != INVALID; ++it) {
475 map.set(edgeRefMap[it], it);
480 /// \brief Make copy of the given map.
482 /// Makes copy of the given map for the newly created graph.
483 /// The new map's key type is the target graph's node type,
484 /// and the copied map's key type is the source graph's node
486 template <typename TargetMap, typename SourceMap>
487 const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
488 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
492 /// \brief Make copy of the given map.
494 /// Makes copy of the given map for the newly created graph.
495 /// The new map's key type is the target graph's edge type,
496 /// and the copied map's key type is the source graph's edge
498 template <typename TargetMap, typename SourceMap>
499 const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
500 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
504 /// \brief Gives back the stored node references.
506 /// Gives back the stored node references.
507 const NodeRefMap& nodeRef() const {
511 /// \brief Gives back the stored edge references.
513 /// Gives back the stored edge references.
514 const EdgeRefMap& edgeRef() const {
522 const Source& source;
525 NodeRefMap nodeRefMap;
526 EdgeRefMap edgeRefMap;
529 /// \brief Copy a graph to an other graph.
531 /// Copy a graph to an other graph.
532 /// The usage of the function:
535 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
538 /// After the copy the \c nr map will contain the mapping from the
539 /// source graph's nodes to the target graph's nodes and the \c ecr will
540 /// contain the mapping from the target graph's edges to the source's
542 template <typename Target, typename Source>
543 GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
544 return GraphCopy<Target, Source>(target, source);
547 /// \brief Class to copy an undirected graph.
549 /// Class to copy an undirected graph to an other graph (duplicate a graph).
550 /// The simplest way of using it is through the \c copyUndirGraph() function.
551 template <typename Target, typename Source>
552 class UndirGraphCopy {
554 typedef typename Source::Node Node;
555 typedef typename Source::NodeIt NodeIt;
556 typedef typename Source::Edge Edge;
557 typedef typename Source::EdgeIt EdgeIt;
558 typedef typename Source::UndirEdge UndirEdge;
559 typedef typename Source::UndirEdgeIt UndirEdgeIt;
561 typedef typename Source::
562 template NodeMap<typename Target::Node> NodeRefMap;
564 typedef typename Source::
565 template UndirEdgeMap<typename Target::UndirEdge> UndirEdgeRefMap;
570 EdgeRefMap(UndirGraphCopy& _gc) : gc(_gc) {}
571 typedef typename Source::Edge Key;
572 typedef typename Target::Edge Value;
574 Value operator[](const Key& key) {
575 return gc.target.direct(gc.undirEdgeRef[key],
576 gc.target.direction(key));
584 /// \brief Constructor for the UndirGraphCopy.
586 /// It copies the content of the \c _source graph into the
587 /// \c _target graph. It creates also two references, one beetween
588 /// the two nodeset and one beetween the two edgesets.
589 UndirGraphCopy(Target& _target, const Source& _source)
590 : source(_source), target(_target),
591 nodeRefMap(_source), edgeRefMap(*this), undirEdgeRefMap(_source) {
592 for (NodeIt it(source); it != INVALID; ++it) {
593 nodeRefMap[it] = target.addNode();
595 for (UndirEdgeIt it(source); it != INVALID; ++it) {
596 undirEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
597 nodeRefMap[source.target(it)]);
601 /// \brief Copies the node references into the given map.
603 /// Copies the node references into the given map.
604 template <typename NodeRef>
605 const UndirGraphCopy& nodeRef(NodeRef& map) const {
606 for (NodeIt it(source); it != INVALID; ++it) {
607 map.set(it, nodeRefMap[it]);
612 /// \brief Reverse and copies the node references into the given map.
614 /// Reverse and copies the node references into the given map.
615 template <typename NodeRef>
616 const UndirGraphCopy& nodeCrossRef(NodeRef& map) const {
617 for (NodeIt it(source); it != INVALID; ++it) {
618 map.set(nodeRefMap[it], it);
623 /// \brief Copies the edge references into the given map.
625 /// Copies the edge references into the given map.
626 template <typename EdgeRef>
627 const UndirGraphCopy& edgeRef(EdgeRef& map) const {
628 for (EdgeIt it(source); it != INVALID; ++it) {
629 map.set(edgeRefMap[it], it);
634 /// \brief Reverse and copies the undirected edge references into the
637 /// Reverse and copies the undirected edge references into the given map.
638 template <typename EdgeRef>
639 const UndirGraphCopy& edgeCrossRef(EdgeRef& map) const {
640 for (EdgeIt it(source); it != INVALID; ++it) {
641 map.set(it, edgeRefMap[it]);
646 /// \brief Copies the undirected edge references into the given map.
648 /// Copies the undirected edge references into the given map.
649 template <typename EdgeRef>
650 const UndirGraphCopy& undirEdgeRef(EdgeRef& map) const {
651 for (UndirEdgeIt it(source); it != INVALID; ++it) {
652 map.set(it, undirEdgeRefMap[it]);
657 /// \brief Reverse and copies the undirected edge references into the
660 /// Reverse and copies the undirected edge references into the given map.
661 template <typename EdgeRef>
662 const UndirGraphCopy& undirEdgeCrossRef(EdgeRef& map) const {
663 for (UndirEdgeIt it(source); it != INVALID; ++it) {
664 map.set(undirEdgeRefMap[it], it);
669 /// \brief Make copy of the given map.
671 /// Makes copy of the given map for the newly created graph.
672 /// The new map's key type is the target graph's node type,
673 /// and the copied map's key type is the source graph's node
675 template <typename TargetMap, typename SourceMap>
676 const UndirGraphCopy& nodeMap(TargetMap& tMap,
677 const SourceMap& sMap) const {
678 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
682 /// \brief Make copy of the given map.
684 /// Makes copy of the given map for the newly created graph.
685 /// The new map's key type is the target graph's edge type,
686 /// and the copied map's key type is the source graph's edge
688 template <typename TargetMap, typename SourceMap>
689 const UndirGraphCopy& edgeMap(TargetMap& tMap,
690 const SourceMap& sMap) const {
691 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
695 /// \brief Make copy of the given map.
697 /// Makes copy of the given map for the newly created graph.
698 /// The new map's key type is the target graph's edge type,
699 /// and the copied map's key type is the source graph's edge
701 template <typename TargetMap, typename SourceMap>
702 const UndirGraphCopy& undirEdgeMap(TargetMap& tMap,
703 const SourceMap& sMap) const {
704 copyMap(tMap, sMap, UndirEdgeIt(source), undirEdgeRefMap);
708 /// \brief Gives back the stored node references.
710 /// Gives back the stored node references.
711 const NodeRefMap& nodeRef() const {
715 /// \brief Gives back the stored edge references.
717 /// Gives back the stored edge references.
718 const EdgeRefMap& edgeRef() const {
722 /// \brief Gives back the stored undir edge references.
724 /// Gives back the stored undir edge references.
725 const UndirEdgeRefMap& undirEdgeRef() const {
726 return undirEdgeRefMap;
733 const Source& source;
736 NodeRefMap nodeRefMap;
737 EdgeRefMap edgeRefMap;
738 UndirEdgeRefMap undirEdgeRefMap;
741 /// \brief Copy a graph to an other graph.
743 /// Copy a graph to an other graph.
744 /// The usage of the function:
747 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
750 /// After the copy the \c nr map will contain the mapping from the
751 /// source graph's nodes to the target graph's nodes and the \c ecr will
752 /// contain the mapping from the target graph's edges to the source's
754 template <typename Target, typename Source>
755 UndirGraphCopy<Target, Source>
756 copyUndirGraph(Target& target, const Source& source) {
757 return UndirGraphCopy<Target, Source>(target, source);
763 /// \addtogroup graph_maps
766 /// Provides an immutable and unique id for each item in the graph.
768 /// The IdMap class provides a unique and immutable id for each item of the
769 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
770 /// different items (nodes) get different ids <li>\b immutable: the id of an
771 /// item (node) does not change (even if you delete other nodes). </ul>
772 /// Through this map you get access (i.e. can read) the inner id values of
773 /// the items stored in the graph. This map can be inverted with its member
774 /// class \c InverseMap.
776 template <typename _Graph, typename _Item>
779 typedef _Graph Graph;
784 /// \brief Constructor.
786 /// Constructor for creating id map.
787 IdMap(const Graph& _graph) : graph(&_graph) {}
789 /// \brief Gives back the \e id of the item.
791 /// Gives back the immutable and unique \e id of the map.
792 int operator[](const Item& item) const { return graph->id(item);}
800 /// \brief The class represents the inverse of its owner (IdMap).
802 /// The class represents the inverse of its owner (IdMap).
807 /// \brief Constructor.
809 /// Constructor for creating an id-to-item map.
810 InverseMap(const Graph& _graph) : graph(&_graph) {}
812 /// \brief Constructor.
814 /// Constructor for creating an id-to-item map.
815 InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
817 /// \brief Gives back the given item from its id.
819 /// Gives back the given item from its id.
821 Item operator[](int id) const { return graph->fromId(id, Item());}
826 /// \brief Gives back the inverse of the map.
828 /// Gives back the inverse of the IdMap.
829 InverseMap inverse() const { return InverseMap(*graph);}
834 /// \brief General invertable graph-map type.
836 /// This type provides simple invertable graph-maps.
837 /// The InvertableMap wraps an arbitrary ReadWriteMap
838 /// and if a key is set to a new value then store it
839 /// in the inverse map.
840 /// \param _Graph The graph type.
841 /// \param _Map The map to extend with invertable functionality.
847 = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent
849 class InvertableMap : protected _Map {
854 typedef _Graph Graph;
856 /// The key type of InvertableMap (Node, Edge, UndirEdge).
857 typedef typename _Map::Key Key;
858 /// The value type of the InvertableMap.
859 typedef typename _Map::Value Value;
861 /// \brief Constructor.
863 /// Construct a new InvertableMap for the graph.
865 InvertableMap(const Graph& graph) : Map(graph) {}
867 /// \brief The setter function of the map.
869 /// Sets the mapped value.
870 void set(const Key& key, const Value& val) {
871 Value oldval = Map::operator[](key);
872 typename Container::iterator it = invMap.find(oldval);
873 if (it != invMap.end() && it->second == key) {
876 invMap.insert(make_pair(val, key));
880 /// \brief The getter function of the map.
882 /// It gives back the value associated with the key.
883 Value operator[](const Key& key) const {
884 return Map::operator[](key);
889 /// \brief Add a new key to the map.
891 /// Add a new key to the map. It is called by the
892 /// \c AlterationNotifier.
893 virtual void add(const Key& key) {
897 /// \brief Erase the key from the map.
899 /// Erase the key to the map. It is called by the
900 /// \c AlterationNotifier.
901 virtual void erase(const Key& key) {
902 Value val = Map::operator[](key);
903 typename Container::iterator it = invMap.find(val);
904 if (it != invMap.end() && it->second == key) {
910 /// \brief Clear the keys from the map and inverse map.
912 /// Clear the keys from the map and inverse map. It is called by the
913 /// \c AlterationNotifier.
914 virtual void clear() {
921 typedef std::map<Value, Key> Container;
926 /// \brief The inverse map type.
928 /// The inverse of this map. The subscript operator of the map
929 /// gives back always the item what was last assigned to the value.
932 /// \brief Constructor of the InverseMap.
934 /// Constructor of the InverseMap.
935 InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
937 /// The value type of the InverseMap.
938 typedef typename InvertableMap::Key Value;
939 /// The key type of the InverseMap.
940 typedef typename InvertableMap::Value Key;
942 /// \brief Subscript operator.
944 /// Subscript operator. It gives back always the item
945 /// what was last assigned to the value.
946 Value operator[](const Key& key) const {
947 typename Container::const_iterator it = inverted.invMap.find(key);
952 const InvertableMap& inverted;
955 /// \brief It gives back the just readeable inverse map.
957 /// It gives back the just readeable inverse map.
958 InverseMap inverse() const {
959 return InverseMap(*this);
966 /// \brief Provides a mutable, continuous and unique descriptor for each
967 /// item in the graph.
969 /// The DescriptorMap class provides a unique and continuous (but mutable)
970 /// descriptor (id) for each item of the same type (e.g. node) in the
971 /// graph. This id is <ul><li>\b unique: different items (nodes) get
972 /// different ids <li>\b continuous: the range of the ids is the set of
973 /// integers between 0 and \c n-1, where \c n is the number of the items of
974 /// this type (e.g. nodes) (so the id of a node can change if you delete an
975 /// other node, i.e. this id is mutable). </ul> This map can be inverted
976 /// with its member class \c InverseMap.
978 /// \param _Graph The graph class the \c DescriptorMap belongs to.
979 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
981 /// \param _Map A ReadWriteMap mapping from the item type to integer.
986 = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
988 class DescriptorMap : protected _Map {
994 /// The graph class of DescriptorMap.
995 typedef _Graph Graph;
997 /// The key type of DescriptorMap (Node, Edge, UndirEdge).
998 typedef typename _Map::Key Key;
999 /// The value type of DescriptorMap.
1000 typedef typename _Map::Value Value;
1002 /// \brief Constructor.
1004 /// Constructor for descriptor map.
1005 DescriptorMap(const Graph& _graph) : Map(_graph) {
1011 /// \brief Add a new key to the map.
1013 /// Add a new key to the map. It is called by the
1014 /// \c AlterationNotifier.
1015 virtual void add(const Item& item) {
1017 Map::set(item, invMap.size());
1018 invMap.push_back(item);
1021 /// \brief Erase the key from the map.
1023 /// Erase the key to the map. It is called by the
1024 /// \c AlterationNotifier.
1025 virtual void erase(const Item& item) {
1026 Map::set(invMap.back(), Map::operator[](item));
1027 invMap[Map::operator[](item)] = invMap.back();
1032 /// \brief Build the unique map.
1034 /// Build the unique map. It is called by the
1035 /// \c AlterationNotifier.
1036 virtual void build() {
1039 const typename Map::Graph* graph = Map::getGraph();
1040 for (graph->first(it); it != INVALID; graph->next(it)) {
1041 Map::set(it, invMap.size());
1042 invMap.push_back(it);
1046 /// \brief Clear the keys from the map.
1048 /// Clear the keys from the map. It is called by the
1049 /// \c AlterationNotifier.
1050 virtual void clear() {
1057 /// \brief Swaps the position of the two items in the map.
1059 /// Swaps the position of the two items in the map.
1060 void swap(const Item& p, const Item& q) {
1061 int pi = Map::operator[](p);
1062 int qi = Map::operator[](q);
1069 /// \brief Gives back the \e descriptor of the item.
1071 /// Gives back the mutable and unique \e descriptor of the map.
1072 int operator[](const Item& item) const {
1073 return Map::operator[](item);
1078 typedef std::vector<Item> Container;
1082 /// \brief The inverse map type of DescriptorMap.
1084 /// The inverse map type of DescriptorMap.
1087 /// \brief Constructor of the InverseMap.
1089 /// Constructor of the InverseMap.
1090 InverseMap(const DescriptorMap& _inverted)
1091 : inverted(_inverted) {}
1094 /// The value type of the InverseMap.
1095 typedef typename DescriptorMap::Key Value;
1096 /// The key type of the InverseMap.
1097 typedef typename DescriptorMap::Value Key;
1099 /// \brief Subscript operator.
1101 /// Subscript operator. It gives back the item
1102 /// that the descriptor belongs to currently.
1103 Value operator[](const Key& key) const {
1104 return inverted.invMap[key];
1107 /// \brief Size of the map.
1109 /// Returns the size of the map.
1111 return inverted.invMap.size();
1115 const DescriptorMap& inverted;
1118 /// \brief Gives back the inverse of the map.
1120 /// Gives back the inverse of the map.
1121 const InverseMap inverse() const {
1122 return InverseMap(*this);
1126 /// \brief Returns the source of the given edge.
1128 /// The SourceMap gives back the source Node of the given edge.
1129 /// \author Balazs Dezso
1130 template <typename Graph>
1134 typedef typename Graph::Node Value;
1135 typedef typename Graph::Edge Key;
1137 /// \brief Constructor
1140 /// \param _graph The graph that the map belongs to.
1141 SourceMap(const Graph& _graph) : graph(_graph) {}
1143 /// \brief The subscript operator.
1145 /// The subscript operator.
1146 /// \param edge The edge
1147 /// \return The source of the edge
1148 Value operator[](const Key& edge) const {
1149 return graph.source(edge);
1156 /// \brief Returns a \ref SourceMap class
1158 /// This function just returns an \ref SourceMap class.
1159 /// \relates SourceMap
1160 template <typename Graph>
1161 inline SourceMap<Graph> sourceMap(const Graph& graph) {
1162 return SourceMap<Graph>(graph);
1165 /// \brief Returns the target of the given edge.
1167 /// The TargetMap gives back the target Node of the given edge.
1168 /// \author Balazs Dezso
1169 template <typename Graph>
1173 typedef typename Graph::Node Value;
1174 typedef typename Graph::Edge Key;
1176 /// \brief Constructor
1179 /// \param _graph The graph that the map belongs to.
1180 TargetMap(const Graph& _graph) : graph(_graph) {}
1182 /// \brief The subscript operator.
1184 /// The subscript operator.
1185 /// \param e The edge
1186 /// \return The target of the edge
1187 Value operator[](const Key& e) const {
1188 return graph.target(e);
1195 /// \brief Returns a \ref TargetMap class
1197 /// This function just returns a \ref TargetMap class.
1198 /// \relates TargetMap
1199 template <typename Graph>
1200 inline TargetMap<Graph> targetMap(const Graph& graph) {
1201 return TargetMap<Graph>(graph);
1204 /// \brief Returns the "forward" directed edge view of an undirected edge.
1206 /// Returns the "forward" directed edge view of an undirected edge.
1207 /// \author Balazs Dezso
1208 template <typename Graph>
1212 typedef typename Graph::Edge Value;
1213 typedef typename Graph::UndirEdge Key;
1215 /// \brief Constructor
1218 /// \param _graph The graph that the map belongs to.
1219 ForwardMap(const Graph& _graph) : graph(_graph) {}
1221 /// \brief The subscript operator.
1223 /// The subscript operator.
1224 /// \param key An undirected edge
1225 /// \return The "forward" directed edge view of undirected edge
1226 Value operator[](const Key& key) const {
1227 return graph.direct(key, true);
1234 /// \brief Returns a \ref ForwardMap class
1236 /// This function just returns an \ref ForwardMap class.
1237 /// \relates ForwardMap
1238 template <typename Graph>
1239 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
1240 return ForwardMap<Graph>(graph);
1243 /// \brief Returns the "backward" directed edge view of an undirected edge.
1245 /// Returns the "backward" directed edge view of an undirected edge.
1246 /// \author Balazs Dezso
1247 template <typename Graph>
1251 typedef typename Graph::Edge Value;
1252 typedef typename Graph::UndirEdge Key;
1254 /// \brief Constructor
1257 /// \param _graph The graph that the map belongs to.
1258 BackwardMap(const Graph& _graph) : graph(_graph) {}
1260 /// \brief The subscript operator.
1262 /// The subscript operator.
1263 /// \param key An undirected edge
1264 /// \return The "backward" directed edge view of undirected edge
1265 Value operator[](const Key& key) const {
1266 return graph.direct(key, false);
1273 /// \brief Returns a \ref BackwardMap class
1275 /// This function just returns a \ref BackwardMap class.
1276 /// \relates BackwardMap
1277 template <typename Graph>
1278 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
1279 return BackwardMap<Graph>(graph);
1282 /// \brief Potential difference map
1284 /// If there is an potential map on the nodes then we
1285 /// can get an edge map as we get the substraction of the
1286 /// values of the target and source.
1287 template <typename Graph, typename NodeMap>
1288 class PotentialDifferenceMap {
1290 typedef typename Graph::Edge Key;
1291 typedef typename NodeMap::Value Value;
1293 /// \brief Constructor
1295 /// Contructor of the map
1296 PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential)
1297 : graph(_graph), potential(_potential) {}
1299 /// \brief Const subscription operator
1301 /// Const subscription operator
1302 Value operator[](const Key& edge) const {
1303 return potential[graph.target(edge)] - potential[graph.source(edge)];
1308 const NodeMap& potential;
1311 /// \brief Just returns a PotentialDifferenceMap
1313 /// Just returns a PotentialDifferenceMap
1314 /// \relates PotentialDifferenceMap
1315 template <typename Graph, typename NodeMap>
1316 PotentialDifferenceMap<Graph, NodeMap>
1317 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
1318 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
1321 /// \brief Map of the node in-degrees.
1323 /// This map returns the in-degree of a node. Once it is constructed,
1324 /// the degrees are stored in a standard NodeMap, so each query is done
1325 /// in constant time. On the other hand, the values are updated automatically
1326 /// whenever the graph changes.
1328 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1329 /// alternative ways to modify the graph. The correct behavior of InDegMap
1330 /// is not guarantied if these additional featureas are used. For example
1331 /// the funstions \ref ListGraph::changeSource() "changeSource()",
1332 /// \ref ListGraph::changeTarget() "changeTarget()" and
1333 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1334 /// of \ref ListGraph will \e not update the degree values correctly.
1338 template <typename _Graph>
1340 : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
1344 typedef _Graph Graph;
1346 typedef typename Graph::Node Key;
1350 class AutoNodeMap : public Graph::template NodeMap<int> {
1353 typedef typename Graph::template NodeMap<int> Parent;
1355 typedef typename Parent::Key Key;
1356 typedef typename Parent::Value Value;
1358 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1360 void add(const Key& key) {
1362 Parent::set(key, 0);
1368 /// \brief Constructor.
1370 /// Constructor for creating in-degree map.
1371 InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1372 AlterationNotifier<typename _Graph::Edge>
1373 ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
1375 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1376 deg[it] = countInEdges(graph, it);
1380 virtual ~InDegMap() {
1381 AlterationNotifier<typename _Graph::Edge>::
1382 ObserverBase::detach();
1385 /// Gives back the in-degree of a Node.
1386 int operator[](const Key& key) const {
1392 typedef typename Graph::Edge Edge;
1394 virtual void add(const Edge& edge) {
1395 ++deg[graph.target(edge)];
1398 virtual void erase(const Edge& edge) {
1399 --deg[graph.target(edge)];
1402 virtual void signalChange(const Edge& edge) {
1406 virtual void commitChange(const Edge& edge) {
1410 virtual void build() {
1411 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1412 deg[it] = countInEdges(graph, it);
1416 virtual void clear() {
1417 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1423 const _Graph& graph;
1427 /// \brief Map of the node out-degrees.
1429 /// This map returns the out-degree of a node. Once it is constructed,
1430 /// the degrees are stored in a standard NodeMap, so each query is done
1431 /// in constant time. On the other hand, the values are updated automatically
1432 /// whenever the graph changes.
1434 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1435 /// alternative ways to modify the graph. The correct behavior of OutDegMap
1436 /// is not guarantied if these additional featureas are used. For example
1437 /// the funstions \ref ListGraph::changeSource() "changeSource()",
1438 /// \ref ListGraph::changeTarget() "changeTarget()" and
1439 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1440 /// of \ref ListGraph will \e not update the degree values correctly.
1444 template <typename _Graph>
1446 : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
1450 typedef _Graph Graph;
1452 typedef typename Graph::Node Key;
1456 class AutoNodeMap : public Graph::template NodeMap<int> {
1459 typedef typename Graph::template NodeMap<int> Parent;
1461 typedef typename Parent::Key Key;
1462 typedef typename Parent::Value Value;
1464 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1466 void add(const Key& key) {
1468 Parent::set(key, 0);
1474 /// \brief Constructor.
1476 /// Constructor for creating out-degree map.
1477 OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1478 AlterationNotifier<typename _Graph::Edge>
1479 ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
1481 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1482 deg[it] = countOutEdges(graph, it);
1486 virtual ~OutDegMap() {
1487 AlterationNotifier<typename _Graph::Edge>::
1488 ObserverBase::detach();
1491 /// Gives back the in-degree of a Node.
1492 int operator[](const Key& key) const {
1498 typedef typename Graph::Edge Edge;
1500 virtual void add(const Edge& edge) {
1501 ++deg[graph.source(edge)];
1504 virtual void erase(const Edge& edge) {
1505 --deg[graph.source(edge)];
1508 virtual void signalChange(const Edge& edge) {
1512 virtual void commitChange(const Edge& edge) {
1517 virtual void build() {
1518 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1519 deg[it] = countOutEdges(graph, it);
1523 virtual void clear() {
1524 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1530 const _Graph& graph;
1537 } //END OF NAMESPACE LEMON