Graph imlementations actually provide ReferenceMaps.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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_GRAPH_UTILS_H
20 #define LEMON_GRAPH_UTILS_H
28 #include <lemon/bits/invalid.h>
29 #include <lemon/bits/utility.h>
30 #include <lemon/maps.h>
31 #include <lemon/bits/traits.h>
33 #include <lemon/bits/alteration_notifier.h>
34 #include <lemon/bits/default_map.h>
38 ///\brief Graph utilities.
45 /// \addtogroup gutils
48 ///Creates convenience typedefs for the graph types and iterators
50 ///This \c \#define creates convenience typedefs for the following types
51 ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
53 ///\note If \c G it a template parameter, it should be used in this way.
55 /// GRAPH_TYPEDEFS(typename G)
58 ///\warning There are no typedefs for the graph maps because of the lack of
59 ///template typedefs in C++.
60 #define GRAPH_TYPEDEFS(Graph) \
61 typedef Graph:: Node Node; \
62 typedef Graph:: NodeIt NodeIt; \
63 typedef Graph:: Edge Edge; \
64 typedef Graph:: EdgeIt EdgeIt; \
65 typedef Graph:: InEdgeIt InEdgeIt; \
66 typedef Graph::OutEdgeIt OutEdgeIt;
68 ///Creates convenience typedefs for the undirected graph types and iterators
70 ///This \c \#define creates the same convenience typedefs as defined by
71 ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
72 ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
74 ///\note If \c G it a template parameter, it should be used in this way.
76 /// UGRAPH_TYPEDEFS(typename G)
79 ///\warning There are no typedefs for the graph maps because of the lack of
80 ///template typedefs in C++.
81 #define UGRAPH_TYPEDEFS(Graph) \
82 GRAPH_TYPEDEFS(Graph) \
83 typedef Graph:: UEdge UEdge; \
84 typedef Graph:: UEdgeIt UEdgeIt; \
85 typedef Graph:: IncEdgeIt IncEdgeIt;
86 // typedef Graph::template UEdgeMap<bool> BoolUEdgeMap;
87 // typedef Graph::template UEdgeMap<int> IntUEdgeMap;
88 // typedef Graph::template UEdgeMap<double> DoubleUEdgeMap;
90 ///\brief Creates convenience typedefs for the bipartite undirected graph
91 ///types and iterators
93 ///This \c \#define creates the same convenience typedefs as defined by
94 ///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates
95 ///\c ANodeIt, \c BNodeIt,
97 ///\note If \c G it a template parameter, it should be used in this way.
99 /// BPUGRAPH_TYPEDEFS(typename G)
102 ///\warning There are no typedefs for the graph maps because of the lack of
103 ///template typedefs in C++.
104 #define BPUGRAPH_TYPEDEFS(Graph) \
105 UGRAPH_TYPEDEFS(Graph) \
106 typedef Graph::ANodeIt ANodeIt; \
107 typedef Graph::BNodeIt BNodeIt;
109 /// \brief Function to count the items in the graph.
111 /// This function counts the items (nodes, edges etc) in the graph.
112 /// The complexity of the function is O(n) because
113 /// it iterates on all of the items.
115 template <typename Graph, typename Item>
116 inline int countItems(const Graph& g) {
117 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
119 for (ItemIt it(g); it != INVALID; ++it) {
127 namespace _graph_utils_bits {
129 template <typename Graph, typename Enable = void>
130 struct CountNodesSelector {
131 static int count(const Graph &g) {
132 return countItems<Graph, typename Graph::Node>(g);
136 template <typename Graph>
137 struct CountNodesSelector<
139 enable_if<typename Graph::NodeNumTag, void>::type>
141 static int count(const Graph &g) {
147 /// \brief Function to count the nodes in the graph.
149 /// This function counts the nodes in the graph.
150 /// The complexity of the function is O(n) but for some
151 /// graph structures it is specialized to run in O(1).
153 /// \todo refer how to specialize it
155 template <typename Graph>
156 inline int countNodes(const Graph& g) {
157 return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
160 namespace _graph_utils_bits {
162 template <typename Graph, typename Enable = void>
163 struct CountANodesSelector {
164 static int count(const Graph &g) {
165 return countItems<Graph, typename Graph::ANode>(g);
169 template <typename Graph>
170 struct CountANodesSelector<
172 enable_if<typename Graph::NodeNumTag, void>::type>
174 static int count(const Graph &g) {
180 /// \brief Function to count the anodes in the graph.
182 /// This function counts the anodes in the graph.
183 /// The complexity of the function is O(an) but for some
184 /// graph structures it is specialized to run in O(1).
186 /// \todo refer how to specialize it
188 template <typename Graph>
189 inline int countANodes(const Graph& g) {
190 return _graph_utils_bits::CountANodesSelector<Graph>::count(g);
193 namespace _graph_utils_bits {
195 template <typename Graph, typename Enable = void>
196 struct CountBNodesSelector {
197 static int count(const Graph &g) {
198 return countItems<Graph, typename Graph::BNode>(g);
202 template <typename Graph>
203 struct CountBNodesSelector<
205 enable_if<typename Graph::NodeNumTag, void>::type>
207 static int count(const Graph &g) {
213 /// \brief Function to count the bnodes in the graph.
215 /// This function counts the bnodes in the graph.
216 /// The complexity of the function is O(bn) but for some
217 /// graph structures it is specialized to run in O(1).
219 /// \todo refer how to specialize it
221 template <typename Graph>
222 inline int countBNodes(const Graph& g) {
223 return _graph_utils_bits::CountBNodesSelector<Graph>::count(g);
229 namespace _graph_utils_bits {
231 template <typename Graph, typename Enable = void>
232 struct CountEdgesSelector {
233 static int count(const Graph &g) {
234 return countItems<Graph, typename Graph::Edge>(g);
238 template <typename Graph>
239 struct CountEdgesSelector<
241 typename enable_if<typename Graph::EdgeNumTag, void>::type>
243 static int count(const Graph &g) {
249 /// \brief Function to count the edges in the graph.
251 /// This function counts the edges in the graph.
252 /// The complexity of the function is O(e) but for some
253 /// graph structures it is specialized to run in O(1).
255 template <typename Graph>
256 inline int countEdges(const Graph& g) {
257 return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
260 // Undirected edge counting:
261 namespace _graph_utils_bits {
263 template <typename Graph, typename Enable = void>
264 struct CountUEdgesSelector {
265 static int count(const Graph &g) {
266 return countItems<Graph, typename Graph::UEdge>(g);
270 template <typename Graph>
271 struct CountUEdgesSelector<
273 typename enable_if<typename Graph::EdgeNumTag, void>::type>
275 static int count(const Graph &g) {
281 /// \brief Function to count the undirected edges in the graph.
283 /// This function counts the undirected edges in the graph.
284 /// The complexity of the function is O(e) but for some
285 /// graph structures it is specialized to run in O(1).
287 template <typename Graph>
288 inline int countUEdges(const Graph& g) {
289 return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
294 template <typename Graph, typename DegIt>
295 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
297 for (DegIt it(_g, _n); it != INVALID; ++it) {
303 /// \brief Function to count the number of the out-edges from node \c n.
305 /// This function counts the number of the out-edges from node \c n
307 template <typename Graph>
308 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
309 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
312 /// \brief Function to count the number of the in-edges to node \c n.
314 /// This function counts the number of the in-edges to node \c n
316 template <typename Graph>
317 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
318 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
321 /// \brief Function to count the number of the inc-edges to node \c n.
323 /// This function counts the number of the inc-edges to node \c n
325 template <typename Graph>
326 inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
327 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
330 namespace _graph_utils_bits {
332 template <typename Graph, typename Enable = void>
333 struct FindEdgeSelector {
334 typedef typename Graph::Node Node;
335 typedef typename Graph::Edge Edge;
336 static Edge find(const Graph &g, Node u, Node v, Edge e) {
342 while (e != INVALID && g.target(e) != v) {
349 template <typename Graph>
350 struct FindEdgeSelector<
352 typename enable_if<typename Graph::FindEdgeTag, void>::type>
354 typedef typename Graph::Node Node;
355 typedef typename Graph::Edge Edge;
356 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
357 return g.findEdge(u, v, prev);
362 /// \brief Finds an edge between two nodes of a graph.
364 /// Finds an edge from node \c u to node \c v in graph \c g.
366 /// If \c prev is \ref INVALID (this is the default value), then
367 /// it finds the first edge from \c u to \c v. Otherwise it looks for
368 /// the next edge from \c u to \c v after \c prev.
369 /// \return The found edge or \ref INVALID if there is no such an edge.
371 /// Thus you can iterate through each edge from \c u to \c v as it follows.
373 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
381 template <typename Graph>
382 inline typename Graph::Edge findEdge(const Graph &g,
383 typename Graph::Node u,
384 typename Graph::Node v,
385 typename Graph::Edge prev = INVALID) {
386 return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
389 /// \brief Iterator for iterating on edges connected the same nodes.
391 /// Iterator for iterating on edges connected the same nodes. It is
392 /// higher level interface for the findEdge() function. You can
393 /// use it the following way:
395 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
404 /// \author Balazs Dezso
405 template <typename _Graph>
406 class ConEdgeIt : public _Graph::Edge {
409 typedef _Graph Graph;
410 typedef typename Graph::Edge Parent;
412 typedef typename Graph::Edge Edge;
413 typedef typename Graph::Node Node;
415 /// \brief Constructor.
417 /// Construct a new ConEdgeIt iterating on the edges which
418 /// connects the \c u and \c v node.
419 ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
420 Parent::operator=(findEdge(graph, u, v));
423 /// \brief Constructor.
425 /// Construct a new ConEdgeIt which continues the iterating from
427 ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
429 /// \brief Increment operator.
431 /// It increments the iterator and gives back the next edge.
432 ConEdgeIt& operator++() {
433 Parent::operator=(findEdge(graph, graph.source(*this),
434 graph.target(*this), *this));
441 namespace _graph_utils_bits {
443 template <typename Graph, typename Enable = void>
444 struct FindUEdgeSelector {
445 typedef typename Graph::Node Node;
446 typedef typename Graph::UEdge UEdge;
447 static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
453 b = g.source(e) == u;
456 while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
466 while (e != INVALID && (!b || g.target(e) != v)) {
474 template <typename Graph>
475 struct FindUEdgeSelector<
477 typename enable_if<typename Graph::FindEdgeTag, void>::type>
479 typedef typename Graph::Node Node;
480 typedef typename Graph::UEdge UEdge;
481 static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
482 return g.findUEdge(u, v, prev);
487 /// \brief Finds an uedge between two nodes of a graph.
489 /// Finds an uedge from node \c u to node \c v in graph \c g.
490 /// If the node \c u and node \c v is equal then each loop edge
491 /// will be enumerated.
493 /// If \c prev is \ref INVALID (this is the default value), then
494 /// it finds the first edge from \c u to \c v. Otherwise it looks for
495 /// the next edge from \c u to \c v after \c prev.
496 /// \return The found edge or \ref INVALID if there is no such an edge.
498 /// Thus you can iterate through each edge from \c u to \c v as it follows.
500 /// for(UEdge e = findUEdge(g,u,v); e != INVALID;
501 /// e = findUEdge(g,u,v,e)) {
508 template <typename Graph>
509 inline typename Graph::UEdge findUEdge(const Graph &g,
510 typename Graph::Node u,
511 typename Graph::Node v,
512 typename Graph::UEdge p = INVALID) {
513 return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
516 /// \brief Iterator for iterating on uedges connected the same nodes.
518 /// Iterator for iterating on uedges connected the same nodes. It is
519 /// higher level interface for the findUEdge() function. You can
520 /// use it the following way:
522 /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
529 /// \author Balazs Dezso
530 template <typename _Graph>
531 class ConUEdgeIt : public _Graph::UEdge {
534 typedef _Graph Graph;
535 typedef typename Graph::UEdge Parent;
537 typedef typename Graph::UEdge UEdge;
538 typedef typename Graph::Node Node;
540 /// \brief Constructor.
542 /// Construct a new ConUEdgeIt iterating on the edges which
543 /// connects the \c u and \c v node.
544 ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
545 Parent::operator=(findUEdge(graph, u, v));
548 /// \brief Constructor.
550 /// Construct a new ConUEdgeIt which continues the iterating from
552 ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
554 /// \brief Increment operator.
556 /// It increments the iterator and gives back the next edge.
557 ConUEdgeIt& operator++() {
558 Parent::operator=(findUEdge(graph, graph.source(*this),
559 graph.target(*this), *this));
566 /// \brief Copy a map.
568 /// This function copies the \c source map to the \c target map. It uses the
569 /// given iterator to iterate on the data structure and it uses the \c ref
570 /// mapping to convert the source's keys to the target's keys.
571 template <typename Target, typename Source,
572 typename ItemIt, typename Ref>
573 void copyMap(Target& target, const Source& source,
574 ItemIt it, const Ref& ref) {
575 for (; it != INVALID; ++it) {
576 target[ref[it]] = source[it];
580 /// \brief Copy the source map to the target map.
582 /// Copy the \c source map to the \c target map. It uses the given iterator
583 /// to iterate on the data structure.
584 template <typename Target, typename Source, typename ItemIt>
585 void copyMap(Target& target, const Source& source, ItemIt it) {
586 for (; it != INVALID; ++it) {
587 target[it] = source[it];
591 /// \brief Class to copy a graph.
593 /// Class to copy a graph to another graph (duplicate a graph). The
594 /// simplest way of using it is through the \c copyGraph() function.
595 template <typename Target, typename Source>
598 typedef typename Source::Node Node;
599 typedef typename Source::NodeIt NodeIt;
600 typedef typename Source::Edge Edge;
601 typedef typename Source::EdgeIt EdgeIt;
603 typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
604 typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
606 /// \brief Constructor for the GraphCopy.
608 /// It copies the content of the \c _source graph into the
609 /// \c _target graph. It creates also two references, one beetween
610 /// the two nodeset and one beetween the two edgesets.
611 GraphCopy(Target& _target, const Source& _source)
612 : source(_source), target(_target),
613 nodeRefMap(_source), edgeRefMap(_source) {
614 for (NodeIt it(source); it != INVALID; ++it) {
615 nodeRefMap[it] = target.addNode();
617 for (EdgeIt it(source); it != INVALID; ++it) {
618 edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
619 nodeRefMap[source.target(it)]);
623 /// \brief Copies the node references into the given map.
625 /// Copies the node references into the given map.
626 template <typename NodeRef>
627 const GraphCopy& nodeRef(NodeRef& map) const {
628 for (NodeIt it(source); it != INVALID; ++it) {
629 map.set(it, nodeRefMap[it]);
634 /// \brief Reverse and copies the node references into the given map.
636 /// Reverse and copies the node references into the given map.
637 template <typename NodeRef>
638 const GraphCopy& nodeCrossRef(NodeRef& map) const {
639 for (NodeIt it(source); it != INVALID; ++it) {
640 map.set(nodeRefMap[it], it);
645 /// \brief Copies the edge references into the given map.
647 /// Copies the edge references into the given map.
648 template <typename EdgeRef>
649 const GraphCopy& edgeRef(EdgeRef& map) const {
650 for (EdgeIt it(source); it != INVALID; ++it) {
651 map.set(it, edgeRefMap[it]);
656 /// \brief Reverse and copies the edge references into the given map.
658 /// Reverse and copies the edge references into the given map.
659 template <typename EdgeRef>
660 const GraphCopy& edgeCrossRef(EdgeRef& map) const {
661 for (EdgeIt it(source); it != INVALID; ++it) {
662 map.set(edgeRefMap[it], it);
667 /// \brief Make copy of the given map.
669 /// Makes copy of the given map for the newly created graph.
670 /// The new map's key type is the target graph's node type,
671 /// and the copied map's key type is the source graph's node
673 template <typename TargetMap, typename SourceMap>
674 const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
675 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
679 /// \brief Make copy of the given map.
681 /// Makes copy of the given map for the newly created graph.
682 /// The new map's key type is the target graph's edge type,
683 /// and the copied map's key type is the source graph's edge
685 template <typename TargetMap, typename SourceMap>
686 const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
687 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
691 /// \brief Gives back the stored node references.
693 /// Gives back the stored node references.
694 const NodeRefMap& nodeRef() const {
698 /// \brief Gives back the stored edge references.
700 /// Gives back the stored edge references.
701 const EdgeRefMap& edgeRef() const {
709 const Source& source;
712 NodeRefMap nodeRefMap;
713 EdgeRefMap edgeRefMap;
716 /// \brief Copy a graph to another graph.
718 /// Copy a graph to another graph.
719 /// The usage of the function:
722 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
725 /// After the copy the \c nr map will contain the mapping from the
726 /// source graph's nodes to the target graph's nodes and the \c ecr will
727 /// contain the mapping from the target graph's edges to the source's
729 template <typename Target, typename Source>
730 GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
731 return GraphCopy<Target, Source>(target, source);
734 /// \brief Class to copy an undirected graph.
736 /// Class to copy an undirected graph to another graph (duplicate a graph).
737 /// The simplest way of using it is through the \c copyUGraph() function.
738 template <typename Target, typename Source>
741 typedef typename Source::Node Node;
742 typedef typename Source::NodeIt NodeIt;
743 typedef typename Source::Edge Edge;
744 typedef typename Source::EdgeIt EdgeIt;
745 typedef typename Source::UEdge UEdge;
746 typedef typename Source::UEdgeIt UEdgeIt;
748 typedef typename Source::
749 template NodeMap<typename Target::Node> NodeRefMap;
751 typedef typename Source::
752 template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
757 EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
758 typedef typename Source::Edge Key;
759 typedef typename Target::Edge Value;
761 Value operator[](const Key& key) {
762 return gc.target.direct(gc.uEdgeRef[key],
763 gc.target.direction(key));
771 /// \brief Constructor for the UGraphCopy.
773 /// It copies the content of the \c _source graph into the
774 /// \c _target graph. It creates also two references, one beetween
775 /// the two nodeset and one beetween the two edgesets.
776 UGraphCopy(Target& _target, const Source& _source)
777 : source(_source), target(_target),
778 nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
779 for (NodeIt it(source); it != INVALID; ++it) {
780 nodeRefMap[it] = target.addNode();
782 for (UEdgeIt it(source); it != INVALID; ++it) {
783 uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
784 nodeRefMap[source.target(it)]);
788 /// \brief Copies the node references into the given map.
790 /// Copies the node references into the given map.
791 template <typename NodeRef>
792 const UGraphCopy& nodeRef(NodeRef& map) const {
793 for (NodeIt it(source); it != INVALID; ++it) {
794 map.set(it, nodeRefMap[it]);
799 /// \brief Reverse and copies the node references into the given map.
801 /// Reverse and copies the node references into the given map.
802 template <typename NodeRef>
803 const UGraphCopy& nodeCrossRef(NodeRef& map) const {
804 for (NodeIt it(source); it != INVALID; ++it) {
805 map.set(nodeRefMap[it], it);
810 /// \brief Copies the edge references into the given map.
812 /// Copies the edge references into the given map.
813 template <typename EdgeRef>
814 const UGraphCopy& edgeRef(EdgeRef& map) const {
815 for (EdgeIt it(source); it != INVALID; ++it) {
816 map.set(edgeRefMap[it], it);
821 /// \brief Reverse and copies the undirected edge references into the
824 /// Reverse and copies the undirected edge references into the given map.
825 template <typename EdgeRef>
826 const UGraphCopy& edgeCrossRef(EdgeRef& map) const {
827 for (EdgeIt it(source); it != INVALID; ++it) {
828 map.set(it, edgeRefMap[it]);
833 /// \brief Copies the undirected edge references into the given map.
835 /// Copies the undirected edge references into the given map.
836 template <typename EdgeRef>
837 const UGraphCopy& uEdgeRef(EdgeRef& map) const {
838 for (UEdgeIt it(source); it != INVALID; ++it) {
839 map.set(it, uEdgeRefMap[it]);
844 /// \brief Reverse and copies the undirected edge references into the
847 /// Reverse and copies the undirected edge references into the given map.
848 template <typename EdgeRef>
849 const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
850 for (UEdgeIt it(source); it != INVALID; ++it) {
851 map.set(uEdgeRefMap[it], it);
856 /// \brief Make copy of the given map.
858 /// Makes copy of the given map for the newly created graph.
859 /// The new map's key type is the target graph's node type,
860 /// and the copied map's key type is the source graph's node
862 template <typename TargetMap, typename SourceMap>
863 const UGraphCopy& nodeMap(TargetMap& tMap,
864 const SourceMap& sMap) const {
865 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
869 /// \brief Make copy of the given map.
871 /// Makes copy of the given map for the newly created graph.
872 /// The new map's key type is the target graph's edge type,
873 /// and the copied map's key type is the source graph's edge
875 template <typename TargetMap, typename SourceMap>
876 const UGraphCopy& edgeMap(TargetMap& tMap,
877 const SourceMap& sMap) const {
878 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
882 /// \brief Make copy of the given map.
884 /// Makes copy of the given map for the newly created graph.
885 /// The new map's key type is the target graph's edge type,
886 /// and the copied map's key type is the source graph's edge
888 template <typename TargetMap, typename SourceMap>
889 const UGraphCopy& uEdgeMap(TargetMap& tMap,
890 const SourceMap& sMap) const {
891 copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
895 /// \brief Gives back the stored node references.
897 /// Gives back the stored node references.
898 const NodeRefMap& nodeRef() const {
902 /// \brief Gives back the stored edge references.
904 /// Gives back the stored edge references.
905 const EdgeRefMap& edgeRef() const {
909 /// \brief Gives back the stored uedge references.
911 /// Gives back the stored uedge references.
912 const UEdgeRefMap& uEdgeRef() const {
920 const Source& source;
923 NodeRefMap nodeRefMap;
924 EdgeRefMap edgeRefMap;
925 UEdgeRefMap uEdgeRefMap;
928 /// \brief Copy a graph to another graph.
930 /// Copy a graph to another graph.
931 /// The usage of the function:
934 /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
937 /// After the copy the \c nr map will contain the mapping from the
938 /// source graph's nodes to the target graph's nodes and the \c ecr will
939 /// contain the mapping from the target graph's edges to the source's
941 template <typename Target, typename Source>
942 UGraphCopy<Target, Source>
943 copyUGraph(Target& target, const Source& source) {
944 return UGraphCopy<Target, Source>(target, source);
950 /// \addtogroup graph_maps
953 /// Provides an immutable and unique id for each item in the graph.
955 /// The IdMap class provides a unique and immutable id for each item of the
956 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
957 /// different items (nodes) get different ids <li>\b immutable: the id of an
958 /// item (node) does not change (even if you delete other nodes). </ul>
959 /// Through this map you get access (i.e. can read) the inner id values of
960 /// the items stored in the graph. This map can be inverted with its member
961 /// class \c InverseMap.
963 template <typename _Graph, typename _Item>
966 typedef _Graph Graph;
971 /// \brief Constructor.
973 /// Constructor for creating id map.
974 IdMap(const Graph& _graph) : graph(&_graph) {}
976 /// \brief Gives back the \e id of the item.
978 /// Gives back the immutable and unique \e id of the map.
979 int operator[](const Item& item) const { return graph->id(item);}
987 /// \brief The class represents the inverse of its owner (IdMap).
989 /// The class represents the inverse of its owner (IdMap).
994 /// \brief Constructor.
996 /// Constructor for creating an id-to-item map.
997 InverseMap(const Graph& _graph) : graph(&_graph) {}
999 /// \brief Constructor.
1001 /// Constructor for creating an id-to-item map.
1002 InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
1004 /// \brief Gives back the given item from its id.
1006 /// Gives back the given item from its id.
1008 Item operator[](int id) const { return graph->fromId(id, Item());}
1013 /// \brief Gives back the inverse of the map.
1015 /// Gives back the inverse of the IdMap.
1016 InverseMap inverse() const { return InverseMap(*graph);}
1021 /// \brief General invertable graph-map type.
1023 /// This type provides simple invertable graph-maps.
1024 /// The InvertableMap wraps an arbitrary ReadWriteMap
1025 /// and if a key is set to a new value then store it
1026 /// in the inverse map.
1028 /// The values of the map can be accessed
1029 /// with stl compatible forward iterator.
1031 /// \param _Graph The graph type.
1032 /// \param _Item The item type of the graph.
1033 /// \param _Value The value type of the map.
1035 /// \see IterableValueMap
1037 /// \param _Map A ReadWriteMap mapping from the item type to integer.
1039 typename _Graph, typename _Item, typename _Value,
1040 typename _Map = DefaultMap<_Graph, _Item, _Value>
1043 template <typename _Graph, typename _Item, typename _Value>
1045 class InvertableMap : protected _Map {
1048 /// The key type of InvertableMap (Node, Edge, UEdge).
1049 typedef typename _Map::Key Key;
1050 /// The value type of the InvertableMap.
1051 typedef typename _Map::Value Value;
1056 typedef _Graph Graph;
1058 typedef std::map<Value, Key> Container;
1065 /// \brief Constructor.
1067 /// Construct a new InvertableMap for the graph.
1069 InvertableMap(const Graph& graph) : Map(graph) {}
1071 /// \brief Forward iterator for values.
1073 /// This iterator is an stl compatible forward
1074 /// iterator on the values of the map. The values can
1075 /// be accessed in the [beginValue, endValue) range.
1078 : public std::iterator<std::forward_iterator_tag, Value> {
1079 friend class InvertableMap;
1081 ValueIterator(typename Container::const_iterator _it)
1087 ValueIterator& operator++() { ++it; return *this; }
1088 ValueIterator operator++(int) {
1089 ValueIterator tmp(*this);
1094 const Value& operator*() const { return it->first; }
1095 const Value* operator->() const { return &(it->first); }
1097 bool operator==(ValueIterator jt) const { return it == jt.it; }
1098 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1101 typename Container::const_iterator it;
1104 /// \brief Returns an iterator to the first value.
1106 /// Returns an stl compatible iterator to the
1107 /// first value of the map. The values of the
1108 /// map can be accessed in the [beginValue, endValue)
1110 ValueIterator beginValue() const {
1111 return ValueIterator(invMap.begin());
1114 /// \brief Returns an iterator after the last value.
1116 /// Returns an stl compatible iterator after the
1117 /// last value of the map. The values of the
1118 /// map can be accessed in the [beginValue, endValue)
1120 ValueIterator endValue() const {
1121 return ValueIterator(invMap.end());
1124 /// \brief The setter function of the map.
1126 /// Sets the mapped value.
1127 void set(const Key& key, const Value& val) {
1128 Value oldval = Map::operator[](key);
1129 typename Container::iterator it = invMap.find(oldval);
1130 if (it != invMap.end() && it->second == key) {
1133 invMap.insert(make_pair(val, key));
1137 /// \brief The getter function of the map.
1139 /// It gives back the value associated with the key.
1140 typename MapTraits<Map>::ConstReturnValue
1141 operator[](const Key& key) const {
1142 return Map::operator[](key);
1147 /// \brief Erase the key from the map.
1149 /// Erase the key to the map. It is called by the
1150 /// \c AlterationNotifier.
1151 virtual void erase(const Key& key) {
1152 Value val = Map::operator[](key);
1153 typename Container::iterator it = invMap.find(val);
1154 if (it != invMap.end() && it->second == key) {
1160 /// \brief Erase more keys from the map.
1162 /// Erase more keys from the map. It is called by the
1163 /// \c AlterationNotifier.
1164 virtual void erase(const std::vector<Key>& keys) {
1165 for (int i = 0; i < (int)keys.size(); ++i) {
1166 Value val = Map::operator[](keys[i]);
1167 typename Container::iterator it = invMap.find(val);
1168 if (it != invMap.end() && it->second == keys[i]) {
1175 /// \brief Clear the keys from the map and inverse map.
1177 /// Clear the keys from the map and inverse map. It is called by the
1178 /// \c AlterationNotifier.
1179 virtual void clear() {
1186 /// \brief The inverse map type.
1188 /// The inverse of this map. The subscript operator of the map
1189 /// gives back always the item what was last assigned to the value.
1192 /// \brief Constructor of the InverseMap.
1194 /// Constructor of the InverseMap.
1195 InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
1197 /// The value type of the InverseMap.
1198 typedef typename InvertableMap::Key Value;
1199 /// The key type of the InverseMap.
1200 typedef typename InvertableMap::Value Key;
1202 /// \brief Subscript operator.
1204 /// Subscript operator. It gives back always the item
1205 /// what was last assigned to the value.
1206 Value operator[](const Key& key) const {
1207 typename Container::const_iterator it = inverted.invMap.find(key);
1212 const InvertableMap& inverted;
1215 /// \brief It gives back the just readable inverse map.
1217 /// It gives back the just readable inverse map.
1218 InverseMap inverse() const {
1219 return InverseMap(*this);
1226 /// \brief Provides a mutable, continuous and unique descriptor for each
1227 /// item in the graph.
1229 /// The DescriptorMap class provides a unique and continuous (but mutable)
1230 /// descriptor (id) for each item of the same type (e.g. node) in the
1231 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1232 /// different ids <li>\b continuous: the range of the ids is the set of
1233 /// integers between 0 and \c n-1, where \c n is the number of the items of
1234 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1235 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1236 /// with its member class \c InverseMap.
1238 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1239 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
1242 /// \param _Map A ReadWriteMap mapping from the item type to integer.
1244 typename _Graph, typename _Item,
1245 typename _Map = DefaultMap<_Graph, _Item, int>
1248 template <typename _Graph, typename _Item>
1250 class DescriptorMap : protected _Map {
1256 /// The graph class of DescriptorMap.
1257 typedef _Graph Graph;
1259 /// The key type of DescriptorMap (Node, Edge, UEdge).
1260 typedef typename _Map::Key Key;
1261 /// The value type of DescriptorMap.
1262 typedef typename _Map::Value Value;
1264 /// \brief Constructor.
1266 /// Constructor for descriptor map.
1267 DescriptorMap(const Graph& _graph) : Map(_graph) {
1269 const typename Map::Notifier* notifier = Map::getNotifier();
1270 for (notifier->first(it); it != INVALID; notifier->next(it)) {
1271 Map::set(it, invMap.size());
1272 invMap.push_back(it);
1278 /// \brief Add a new key to the map.
1280 /// Add a new key to the map. It is called by the
1281 /// \c AlterationNotifier.
1282 virtual void add(const Item& item) {
1284 Map::set(item, invMap.size());
1285 invMap.push_back(item);
1288 /// \brief Add more new keys to the map.
1290 /// Add more new keys to the map. It is called by the
1291 /// \c AlterationNotifier.
1292 virtual void add(const std::vector<Item>& items) {
1294 for (int i = 0; i < (int)items.size(); ++i) {
1295 Map::set(items[i], invMap.size());
1296 invMap.push_back(items[i]);
1300 /// \brief Erase the key from the map.
1302 /// Erase the key from the map. It is called by the
1303 /// \c AlterationNotifier.
1304 virtual void erase(const Item& item) {
1305 Map::set(invMap.back(), Map::operator[](item));
1306 invMap[Map::operator[](item)] = invMap.back();
1311 /// \brief Erase more keys from the map.
1313 /// Erase more keys from the map. It is called by the
1314 /// \c AlterationNotifier.
1315 virtual void erase(const std::vector<Item>& items) {
1316 for (int i = 0; i < (int)items.size(); ++i) {
1317 Map::set(invMap.back(), Map::operator[](items[i]));
1318 invMap[Map::operator[](items[i])] = invMap.back();
1324 /// \brief Build the unique map.
1326 /// Build the unique map. It is called by the
1327 /// \c AlterationNotifier.
1328 virtual void build() {
1331 const typename Map::Notifier* notifier = Map::getNotifier();
1332 for (notifier->first(it); it != INVALID; notifier->next(it)) {
1333 Map::set(it, invMap.size());
1334 invMap.push_back(it);
1338 /// \brief Clear the keys from the map.
1340 /// Clear the keys from the map. It is called by the
1341 /// \c AlterationNotifier.
1342 virtual void clear() {
1349 /// \brief Returns the maximal value plus one.
1351 /// Returns the maximal value plus one in the map.
1352 unsigned int size() const {
1353 return invMap.size();
1356 /// \brief Swaps the position of the two items in the map.
1358 /// Swaps the position of the two items in the map.
1359 void swap(const Item& p, const Item& q) {
1360 int pi = Map::operator[](p);
1361 int qi = Map::operator[](q);
1368 /// \brief Gives back the \e descriptor of the item.
1370 /// Gives back the mutable and unique \e descriptor of the map.
1371 int operator[](const Item& item) const {
1372 return Map::operator[](item);
1377 typedef std::vector<Item> Container;
1381 /// \brief The inverse map type of DescriptorMap.
1383 /// The inverse map type of DescriptorMap.
1386 /// \brief Constructor of the InverseMap.
1388 /// Constructor of the InverseMap.
1389 InverseMap(const DescriptorMap& _inverted)
1390 : inverted(_inverted) {}
1393 /// The value type of the InverseMap.
1394 typedef typename DescriptorMap::Key Value;
1395 /// The key type of the InverseMap.
1396 typedef typename DescriptorMap::Value Key;
1398 /// \brief Subscript operator.
1400 /// Subscript operator. It gives back the item
1401 /// that the descriptor belongs to currently.
1402 Value operator[](const Key& key) const {
1403 return inverted.invMap[key];
1406 /// \brief Size of the map.
1408 /// Returns the size of the map.
1409 unsigned int size() const {
1410 return inverted.invMap.size();
1414 const DescriptorMap& inverted;
1417 /// \brief Gives back the inverse of the map.
1419 /// Gives back the inverse of the map.
1420 const InverseMap inverse() const {
1421 return InverseMap(*this);
1425 /// \brief Returns the source of the given edge.
1427 /// The SourceMap gives back the source Node of the given edge.
1428 /// \author Balazs Dezso
1429 template <typename Graph>
1433 typedef typename Graph::Node Value;
1434 typedef typename Graph::Edge Key;
1436 /// \brief Constructor
1439 /// \param _graph The graph that the map belongs to.
1440 SourceMap(const Graph& _graph) : graph(_graph) {}
1442 /// \brief The subscript operator.
1444 /// The subscript operator.
1445 /// \param edge The edge
1446 /// \return The source of the edge
1447 Value operator[](const Key& edge) const {
1448 return graph.source(edge);
1455 /// \brief Returns a \ref SourceMap class
1457 /// This function just returns an \ref SourceMap class.
1458 /// \relates SourceMap
1459 template <typename Graph>
1460 inline SourceMap<Graph> sourceMap(const Graph& graph) {
1461 return SourceMap<Graph>(graph);
1464 /// \brief Returns the target of the given edge.
1466 /// The TargetMap gives back the target Node of the given edge.
1467 /// \author Balazs Dezso
1468 template <typename Graph>
1472 typedef typename Graph::Node Value;
1473 typedef typename Graph::Edge Key;
1475 /// \brief Constructor
1478 /// \param _graph The graph that the map belongs to.
1479 TargetMap(const Graph& _graph) : graph(_graph) {}
1481 /// \brief The subscript operator.
1483 /// The subscript operator.
1484 /// \param e The edge
1485 /// \return The target of the edge
1486 Value operator[](const Key& e) const {
1487 return graph.target(e);
1494 /// \brief Returns a \ref TargetMap class
1496 /// This function just returns a \ref TargetMap class.
1497 /// \relates TargetMap
1498 template <typename Graph>
1499 inline TargetMap<Graph> targetMap(const Graph& graph) {
1500 return TargetMap<Graph>(graph);
1503 /// \brief Returns the "forward" directed edge view of an undirected edge.
1505 /// Returns the "forward" directed edge view of an undirected edge.
1506 /// \author Balazs Dezso
1507 template <typename Graph>
1511 typedef typename Graph::Edge Value;
1512 typedef typename Graph::UEdge Key;
1514 /// \brief Constructor
1517 /// \param _graph The graph that the map belongs to.
1518 ForwardMap(const Graph& _graph) : graph(_graph) {}
1520 /// \brief The subscript operator.
1522 /// The subscript operator.
1523 /// \param key An undirected edge
1524 /// \return The "forward" directed edge view of undirected edge
1525 Value operator[](const Key& key) const {
1526 return graph.direct(key, true);
1533 /// \brief Returns a \ref ForwardMap class
1535 /// This function just returns an \ref ForwardMap class.
1536 /// \relates ForwardMap
1537 template <typename Graph>
1538 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
1539 return ForwardMap<Graph>(graph);
1542 /// \brief Returns the "backward" directed edge view of an undirected edge.
1544 /// Returns the "backward" directed edge view of an undirected edge.
1545 /// \author Balazs Dezso
1546 template <typename Graph>
1550 typedef typename Graph::Edge Value;
1551 typedef typename Graph::UEdge Key;
1553 /// \brief Constructor
1556 /// \param _graph The graph that the map belongs to.
1557 BackwardMap(const Graph& _graph) : graph(_graph) {}
1559 /// \brief The subscript operator.
1561 /// The subscript operator.
1562 /// \param key An undirected edge
1563 /// \return The "backward" directed edge view of undirected edge
1564 Value operator[](const Key& key) const {
1565 return graph.direct(key, false);
1572 /// \brief Returns a \ref BackwardMap class
1574 /// This function just returns a \ref BackwardMap class.
1575 /// \relates BackwardMap
1576 template <typename Graph>
1577 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
1578 return BackwardMap<Graph>(graph);
1581 /// \brief Potential difference map
1583 /// If there is an potential map on the nodes then we
1584 /// can get an edge map as we get the substraction of the
1585 /// values of the target and source.
1586 template <typename Graph, typename NodeMap>
1587 class PotentialDifferenceMap {
1589 typedef typename Graph::Edge Key;
1590 typedef typename NodeMap::Value Value;
1592 /// \brief Constructor
1594 /// Contructor of the map
1595 PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential)
1596 : graph(_graph), potential(_potential) {}
1598 /// \brief Const subscription operator
1600 /// Const subscription operator
1601 Value operator[](const Key& edge) const {
1602 return potential[graph.target(edge)] - potential[graph.source(edge)];
1607 const NodeMap& potential;
1610 /// \brief Just returns a PotentialDifferenceMap
1612 /// Just returns a PotentialDifferenceMap
1613 /// \relates PotentialDifferenceMap
1614 template <typename Graph, typename NodeMap>
1615 PotentialDifferenceMap<Graph, NodeMap>
1616 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
1617 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
1620 /// \brief Map of the node in-degrees.
1622 /// This map returns the in-degree of a node. Once it is constructed,
1623 /// the degrees are stored in a standard NodeMap, so each query is done
1624 /// in constant time. On the other hand, the values are updated automatically
1625 /// whenever the graph changes.
1627 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1628 /// alternative ways to modify the graph. The correct behavior of InDegMap
1629 /// is not guarantied if these additional features are used. For example
1630 /// the functions \ref ListGraph::changeSource() "changeSource()",
1631 /// \ref ListGraph::changeTarget() "changeTarget()" and
1632 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1633 /// of \ref ListGraph will \e not update the degree values correctly.
1637 template <typename _Graph>
1639 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
1640 ::ItemNotifier::ObserverBase {
1644 typedef _Graph Graph;
1646 typedef typename Graph::Node Key;
1648 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
1649 ::ItemNotifier::ObserverBase Parent;
1653 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
1656 typedef DefaultMap<_Graph, Key, int> Parent;
1657 typedef typename Parent::Graph Graph;
1659 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1661 virtual void add(const Key& key) {
1663 Parent::set(key, 0);
1666 virtual void add(const std::vector<Key>& keys) {
1668 for (int i = 0; i < (int)keys.size(); ++i) {
1669 Parent::set(keys[i], 0);
1676 /// \brief Constructor.
1678 /// Constructor for creating in-degree map.
1679 InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1680 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
1682 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1683 deg[it] = countInEdges(graph, it);
1687 /// Gives back the in-degree of a Node.
1688 int operator[](const Key& key) const {
1694 typedef typename Graph::Edge Edge;
1696 virtual void add(const Edge& edge) {
1697 ++deg[graph.target(edge)];
1700 virtual void add(const std::vector<Edge>& edges) {
1701 for (int i = 0; i < (int)edges.size(); ++i) {
1702 ++deg[graph.target(edges[i])];
1706 virtual void erase(const Edge& edge) {
1707 --deg[graph.target(edge)];
1710 virtual void erase(const std::vector<Edge>& edges) {
1711 for (int i = 0; i < (int)edges.size(); ++i) {
1712 --deg[graph.target(edges[i])];
1716 virtual void build() {
1717 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1718 deg[it] = countInEdges(graph, it);
1722 virtual void clear() {
1723 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1729 const _Graph& graph;
1733 /// \brief Map of the node out-degrees.
1735 /// This map returns the out-degree of a node. Once it is constructed,
1736 /// the degrees are stored in a standard NodeMap, so each query is done
1737 /// in constant time. On the other hand, the values are updated automatically
1738 /// whenever the graph changes.
1740 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1741 /// alternative ways to modify the graph. The correct behavior of OutDegMap
1742 /// is not guarantied if these additional features are used. For example
1743 /// the functions \ref ListGraph::changeSource() "changeSource()",
1744 /// \ref ListGraph::changeTarget() "changeTarget()" and
1745 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1746 /// of \ref ListGraph will \e not update the degree values correctly.
1750 template <typename _Graph>
1752 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
1753 ::ItemNotifier::ObserverBase {
1757 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
1758 ::ItemNotifier::ObserverBase Parent;
1760 typedef _Graph Graph;
1762 typedef typename Graph::Node Key;
1766 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
1769 typedef DefaultMap<_Graph, Key, int> Parent;
1770 typedef typename Parent::Graph Graph;
1772 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1774 virtual void add(const Key& key) {
1776 Parent::set(key, 0);
1778 virtual void add(const std::vector<Key>& keys) {
1780 for (int i = 0; i < (int)keys.size(); ++i) {
1781 Parent::set(keys[i], 0);
1788 /// \brief Constructor.
1790 /// Constructor for creating out-degree map.
1791 OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1792 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
1794 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1795 deg[it] = countOutEdges(graph, it);
1799 /// Gives back the out-degree of a Node.
1800 int operator[](const Key& key) const {
1806 typedef typename Graph::Edge Edge;
1808 virtual void add(const Edge& edge) {
1809 ++deg[graph.source(edge)];
1812 virtual void add(const std::vector<Edge>& edges) {
1813 for (int i = 0; i < (int)edges.size(); ++i) {
1814 ++deg[graph.source(edges[i])];
1818 virtual void erase(const Edge& edge) {
1819 --deg[graph.source(edge)];
1822 virtual void erase(const std::vector<Edge>& edges) {
1823 for (int i = 0; i < (int)edges.size(); ++i) {
1824 --deg[graph.source(edges[i])];
1828 virtual void build() {
1829 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1830 deg[it] = countOutEdges(graph, it);
1834 virtual void clear() {
1835 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1841 const _Graph& graph;
1846 ///Fast edge look up between given endpoints.
1849 ///Using this class, you can find an edge in a graph from a given
1850 ///source to a given target in time <em>O(log d)</em>,
1851 ///where <em>d</em> is the out-degree of the source node.
1853 ///It is not possible to find \e all parallel edges between two nodes.
1854 ///Use \ref AllEdgeLookUp for this purpose.
1856 ///\warning This class is static, so you should refresh() (or at least
1857 ///refresh(Node)) this data structure
1858 ///whenever the graph changes. This is a time consuming (superlinearly
1859 ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
1861 ///\param G The type of the underlying graph.
1863 ///\sa AllEdgeLookUp
1868 GRAPH_TYPEDEFS(typename G)
1873 typename Graph::template NodeMap<Edge> _head;
1874 typename Graph::template EdgeMap<Edge> _left;
1875 typename Graph::template EdgeMap<Edge> _right;
1880 EdgeLess(const Graph &_g) : g(_g) {}
1881 bool operator()(Edge a,Edge b) const
1883 return g.target(a)<g.target(b);
1893 ///It builds up the search database, which remains valid until the graph
1895 EdgeLookUp(const Graph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1898 Edge refresh_rec(std::vector<Edge> &v,int a,int b)
1902 _left[me] = a<m?refresh_rec(v,a,m-1):INVALID;
1903 _right[me] = m<b?refresh_rec(v,m+1,b):INVALID;
1907 ///Refresh the data structure at a node.
1909 ///Build up the search database of node \c n.
1911 ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
1912 ///the number of the outgoing edges of \c n.
1913 void refresh(Node n)
1915 std::vector<Edge> v;
1916 for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1918 std::sort(v.begin(),v.end(),EdgeLess(_g));
1919 _head[n]=refresh_rec(v,0,v.size()-1);
1921 else _head[n]=INVALID;
1923 ///Refresh the full data structure.
1925 ///Build up the full search database. In fact, it simply calls
1926 ///\ref refresh(Node) "refresh(n)" for each node \c n.
1928 ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
1929 ///the number of the edges of \c n and <em>D</em> is the maximum
1930 ///out-degree of the graph.
1934 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1937 ///Find an edge between two nodes.
1939 ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
1940 /// <em>d</em> is the number of outgoing edges of \c s.
1941 ///\param s The source node
1942 ///\param t The target node
1943 ///\return An edge from \c s to \c t if there exists,
1944 ///\ref INVALID otherwise.
1946 ///\warning If you change the graph, refresh() must be called before using
1947 ///this operator. If you change the outgoing edges of
1948 ///a single node \c n, then
1949 ///\ref refresh(Node) "refresh(n)" is enough.
1951 Edge operator()(Node s, Node t) const
1955 e!=INVALID&&_g.target(e)!=t;
1956 e = t < _g.target(e)?_left[e]:_right[e]) ;
1962 ///Fast look up of all edges between given endpoints.
1965 ///This class is the same as \ref EdgeLookUp, with the addition
1966 ///that it makes it possible to find all edges between given endpoints.
1968 ///\warning This class is static, so you should refresh() (or at least
1969 ///refresh(Node)) this data structure
1970 ///whenever the graph changes. This is a time consuming (superlinearly
1971 ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
1973 ///\param G The type of the underlying graph.
1977 class AllEdgeLookUp : public EdgeLookUp<G>
1979 using EdgeLookUp<G>::_g;
1980 using EdgeLookUp<G>::_right;
1981 using EdgeLookUp<G>::_left;
1982 using EdgeLookUp<G>::_head;
1984 GRAPH_TYPEDEFS(typename G)
1987 typename Graph::template EdgeMap<Edge> _next;
1989 Edge refreshNext(Edge head,Edge next=INVALID)
1991 if(head==INVALID) return next;
1993 next=refreshNext(_right[head],next);
1994 // _next[head]=next;
1995 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1997 return refreshNext(_left[head],head);
2003 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
2011 ///It builds up the search database, which remains valid until the graph
2013 AllEdgeLookUp(const Graph &g) : EdgeLookUp<G>(g), _next(g) {refreshNext();}
2015 ///Refresh the data structure at a node.
2017 ///Build up the search database of node \c n.
2019 ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2020 ///the number of the outgoing edges of \c n.
2022 void refresh(Node n)
2024 EdgeLookUp<G>::refresh(n);
2025 refreshNext(_head[n]);
2028 ///Refresh the full data structure.
2030 ///Build up the full search database. In fact, it simply calls
2031 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2033 ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2034 ///the number of the edges of \c n and <em>D</em> is the maximum
2035 ///out-degree of the graph.
2039 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
2042 ///Find an edge between two nodes.
2044 ///Find an edge between two nodes.
2045 ///\param s The source node
2046 ///\param t The target node
2047 ///\param prev The previous edge between \c s and \c t. It it is INVALID or
2048 ///not given, the operator finds the first appropriate edge.
2049 ///\return An edge from \c s to \c t after \prev or
2050 ///\ref INVALID if there is no more.
2052 ///For example, you can count the number of edges from \c u to \c v in the
2055 ///AllEdgeLookUp<ListGraph> ae(g);
2058 ///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
2061 ///Finding the first edge take <em>O(</em>log<em>d)</em> time, where
2062 /// <em>d</em> is the number of outgoing edges of \c s. Then, the
2063 ///consecutive edges are found in constant time.
2065 ///\warning If you change the graph, refresh() must be called before using
2066 ///this operator. If you change the outgoing edges of
2067 ///a single node \c n, then
2068 ///\ref refresh(Node) "refresh(n)" is enough.
2071 Edge operator()(Node s, Node t, Edge prev=INVALID) const {}
2073 using EdgeLookUp<G>::operator() ;
2074 Edge operator()(Node s, Node t, Edge prev) const
2076 return prev==INVALID?(*this)(s,t):_next[prev];
2084 } //END OF NAMESPACE LEMON