3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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.
42 /// \addtogroup gutils
45 namespace _graph_utils_bits {
46 template <typename Graph>
47 struct Node { typedef typename Graph::Node type; };
49 template <typename Graph>
50 struct NodeIt { typedef typename Graph::NodeIt type; };
52 template <typename Graph>
53 struct Arc { typedef typename Graph::Arc type; };
55 template <typename Graph>
56 struct ArcIt { typedef typename Graph::ArcIt type; };
58 template <typename Graph>
59 struct Edge { typedef typename Graph::Edge type; };
61 template <typename Graph>
62 struct EdgeIt { typedef typename Graph::EdgeIt type; };
64 template <typename Graph>
65 struct OutArcIt { typedef typename Graph::OutArcIt type; };
67 template <typename Graph>
68 struct InArcIt { typedef typename Graph::InArcIt type; };
70 template <typename Graph>
71 struct IncEdgeIt { typedef typename Graph::IncEdgeIt type; };
73 template <typename Graph>
75 typedef typename Graph::template NodeMap<bool> type;
78 template <typename Graph>
80 typedef typename Graph::template NodeMap<int> type;
83 template <typename Graph>
84 struct DoubleNodeMap {
85 typedef typename Graph::template NodeMap<double> type;
88 template <typename Graph>
90 typedef typename Graph::template ArcMap<bool> type;
93 template <typename Graph>
95 typedef typename Graph::template ArcMap<int> type;
98 template <typename Graph>
100 typedef typename Graph::template ArcMap<double> type;
103 template <typename Graph>
105 typedef typename Graph::template EdgeMap<bool> type;
108 template <typename Graph>
110 typedef typename Graph::template EdgeMap<int> type;
113 template <typename Graph>
114 struct DoubleEdgeMap {
115 typedef typename Graph::template EdgeMap<double> type;
121 ///Creates convenience typedefs for the digraph types and iterators
123 ///This \c \#define creates convenience typedefs for the following types
124 ///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
125 ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
126 ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
127 #define DIGRAPH_TYPEDEFS(Digraph) \
128 typedef typename ::lemon::_graph_utils_bits:: \
129 Node<Digraph>::type Node; \
130 typedef typename ::lemon::_graph_utils_bits:: \
131 NodeIt<Digraph>::type NodeIt; \
132 typedef typename ::lemon::_graph_utils_bits:: \
133 Arc<Digraph>::type Arc; \
134 typedef typename ::lemon::_graph_utils_bits:: \
135 ArcIt<Digraph>::type ArcIt; \
136 typedef typename ::lemon::_graph_utils_bits:: \
137 OutArcIt<Digraph>::type OutArcIt; \
138 typedef typename ::lemon::_graph_utils_bits:: \
139 InArcIt<Digraph>::type InArcIt; \
140 typedef typename ::lemon::_graph_utils_bits:: \
141 BoolNodeMap<Digraph>::type BoolNodeMap; \
142 typedef typename ::lemon::_graph_utils_bits:: \
143 IntNodeMap<Digraph>::type IntNodeMap; \
144 typedef typename ::lemon::_graph_utils_bits:: \
145 DoubleNodeMap<Digraph>::type DoubleNodeMap; \
146 typedef typename ::lemon::_graph_utils_bits:: \
147 BoolArcMap<Digraph>::type BoolArcMap; \
148 typedef typename ::lemon::_graph_utils_bits:: \
149 IntArcMap<Digraph>::type IntArcMap; \
150 typedef typename ::lemon::_graph_utils_bits:: \
151 DoubleArcMap<Digraph>::type DoubleArcMap
154 ///Creates convenience typedefs for the graph types and iterators
156 ///This \c \#define creates the same convenience typedefs as defined
157 ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
158 ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
160 #define GRAPH_TYPEDEFS(Graph) \
161 DIGRAPH_TYPEDEFS(Graph); \
162 typedef typename ::lemon::_graph_utils_bits:: \
163 Edge<Graph>::type Edge; \
164 typedef typename ::lemon::_graph_utils_bits:: \
165 EdgeIt<Graph>::type EdgeIt; \
166 typedef typename ::lemon::_graph_utils_bits:: \
167 IncEdgeIt<Graph>::type IncEdgeIt \
168 typedef typename ::lemon::_graph_utils_bits:: \
169 BoolEdgeMap<Graph>::type BoolEdgeMap; \
170 typedef typename ::lemon::_graph_utils_bits:: \
171 IntEdgeMap<Graph>::type IntEdgeMap; \
172 typedef typename ::lemon::_graph_utils_bits:: \
173 DoubleEdgeMap<Graph>::type DoubleEdgeMap
176 /// \brief Function to count the items in the graph.
178 /// This function counts the items (nodes, arcs etc) in the graph.
179 /// The complexity of the function is O(n) because
180 /// it iterates on all of the items.
181 template <typename Graph, typename Item>
182 inline int countItems(const Graph& g) {
183 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
185 for (ItemIt it(g); it != INVALID; ++it) {
193 namespace _graph_utils_bits {
195 template <typename Graph, typename Enable = void>
196 struct CountNodesSelector {
197 static int count(const Graph &g) {
198 return countItems<Graph, typename Graph::Node>(g);
202 template <typename Graph>
203 struct CountNodesSelector<
205 enable_if<typename Graph::NodeNumTag, void>::type>
207 static int count(const Graph &g) {
213 /// \brief Function to count the nodes in the graph.
215 /// This function counts the nodes in the graph.
216 /// The complexity of the function is O(n) but for some
217 /// graph structures it is specialized to run in O(1).
219 /// If the graph contains a \e nodeNum() member function and a
220 /// \e NodeNumTag tag then this function calls directly the member
221 /// function to query the cardinality of the node set.
222 template <typename Graph>
223 inline int countNodes(const Graph& g) {
224 return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
229 namespace _graph_utils_bits {
231 template <typename Graph, typename Enable = void>
232 struct CountArcsSelector {
233 static int count(const Graph &g) {
234 return countItems<Graph, typename Graph::Arc>(g);
238 template <typename Graph>
239 struct CountArcsSelector<
241 typename enable_if<typename Graph::ArcNumTag, void>::type>
243 static int count(const Graph &g) {
249 /// \brief Function to count the arcs in the graph.
251 /// This function counts the arcs 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 /// If the graph contains a \e arcNum() member function and a
256 /// \e EdgeNumTag tag then this function calls directly the member
257 /// function to query the cardinality of the arc set.
258 template <typename Graph>
259 inline int countArcs(const Graph& g) {
260 return _graph_utils_bits::CountArcsSelector<Graph>::count(g);
264 namespace _graph_utils_bits {
266 template <typename Graph, typename Enable = void>
267 struct CountEdgesSelector {
268 static int count(const Graph &g) {
269 return countItems<Graph, typename Graph::Edge>(g);
273 template <typename Graph>
274 struct CountEdgesSelector<
276 typename enable_if<typename Graph::EdgeNumTag, void>::type>
278 static int count(const Graph &g) {
284 /// \brief Function to count the edges in the graph.
286 /// This function counts the edges in the graph.
287 /// The complexity of the function is O(m) but for some
288 /// graph structures it is specialized to run in O(1).
290 /// If the graph contains a \e edgeNum() member function and a
291 /// \e EdgeNumTag tag then this function calls directly the member
292 /// function to query the cardinality of the edge set.
293 template <typename Graph>
294 inline int countEdges(const Graph& g) {
295 return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
300 template <typename Graph, typename DegIt>
301 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
303 for (DegIt it(_g, _n); it != INVALID; ++it) {
309 /// \brief Function to count the number of the out-arcs from node \c n.
311 /// This function counts the number of the out-arcs from node \c n
313 template <typename Graph>
314 inline int countOutArcs(const Graph& _g, const typename Graph::Node& _n) {
315 return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n);
318 /// \brief Function to count the number of the in-arcs to node \c n.
320 /// This function counts the number of the in-arcs to node \c n
322 template <typename Graph>
323 inline int countInArcs(const Graph& _g, const typename Graph::Node& _n) {
324 return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n);
327 /// \brief Function to count the number of the inc-edges to node \c n.
329 /// This function counts the number of the inc-edges to node \c n
331 template <typename Graph>
332 inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
333 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
336 namespace _graph_utils_bits {
338 template <typename Graph, typename Enable = void>
339 struct FindArcSelector {
340 typedef typename Graph::Node Node;
341 typedef typename Graph::Arc Arc;
342 static Arc find(const Graph &g, Node u, Node v, Arc e) {
348 while (e != INVALID && g.target(e) != v) {
355 template <typename Graph>
356 struct FindArcSelector<
358 typename enable_if<typename Graph::FindEdgeTag, void>::type>
360 typedef typename Graph::Node Node;
361 typedef typename Graph::Arc Arc;
362 static Arc find(const Graph &g, Node u, Node v, Arc prev) {
363 return g.findArc(u, v, prev);
368 /// \brief Finds an arc between two nodes of a graph.
370 /// Finds an arc from node \c u to node \c v in graph \c g.
372 /// If \c prev is \ref INVALID (this is the default value), then
373 /// it finds the first arc from \c u to \c v. Otherwise it looks for
374 /// the next arc from \c u to \c v after \c prev.
375 /// \return The found arc or \ref INVALID if there is no such an arc.
377 /// Thus you can iterate through each arc from \c u to \c v as it follows.
379 /// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) {
388 template <typename Graph>
389 inline typename Graph::Arc
390 findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
391 typename Graph::Arc prev = INVALID) {
392 return _graph_utils_bits::FindArcSelector<Graph>::find(g, u, v, prev);
395 /// \brief Iterator for iterating on arcs connected the same nodes.
397 /// Iterator for iterating on arcs connected the same nodes. It is
398 /// higher level interface for the findArc() function. You can
399 /// use it the following way:
401 /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
411 /// \author Balazs Dezso
412 template <typename _Graph>
413 class ConArcIt : public _Graph::Arc {
416 typedef _Graph Graph;
417 typedef typename Graph::Arc Parent;
419 typedef typename Graph::Arc Arc;
420 typedef typename Graph::Node Node;
422 /// \brief Constructor.
424 /// Construct a new ConArcIt iterating on the arcs which
425 /// connects the \c u and \c v node.
426 ConArcIt(const Graph& g, Node u, Node v) : _graph(g) {
427 Parent::operator=(findArc(_graph, u, v));
430 /// \brief Constructor.
432 /// Construct a new ConArcIt which continues the iterating from
434 ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {}
436 /// \brief Increment operator.
438 /// It increments the iterator and gives back the next arc.
439 ConArcIt& operator++() {
440 Parent::operator=(findArc(_graph, _graph.source(*this),
441 _graph.target(*this), *this));
448 namespace _graph_utils_bits {
450 template <typename Graph, typename Enable = void>
451 struct FindEdgeSelector {
452 typedef typename Graph::Node Node;
453 typedef typename Graph::Edge Edge;
454 static Edge find(const Graph &g, Node u, Node v, Edge e) {
460 b = g.source(e) == u;
463 while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
473 while (e != INVALID && (!b || g.target(e) != v)) {
481 template <typename Graph>
482 struct FindEdgeSelector<
484 typename enable_if<typename Graph::FindEdgeTag, void>::type>
486 typedef typename Graph::Node Node;
487 typedef typename Graph::Edge Edge;
488 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
489 return g.findEdge(u, v, prev);
494 /// \brief Finds an edge between two nodes of a graph.
496 /// Finds an edge from node \c u to node \c v in graph \c g.
497 /// If the node \c u and node \c v is equal then each loop edge
498 /// will be enumerated once.
500 /// If \c prev is \ref INVALID (this is the default value), then
501 /// it finds the first arc from \c u to \c v. Otherwise it looks for
502 /// the next arc from \c u to \c v after \c prev.
503 /// \return The found arc or \ref INVALID if there is no such an arc.
505 /// Thus you can iterate through each arc from \c u to \c v as it follows.
507 /// for(Edge e = findEdge(g,u,v); e != INVALID;
508 /// e = findEdge(g,u,v,e)) {
515 template <typename Graph>
516 inline typename Graph::Edge
517 findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
518 typename Graph::Edge p = INVALID) {
519 return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
522 /// \brief Iterator for iterating on edges connected the same nodes.
524 /// Iterator for iterating on edges connected the same nodes. It is
525 /// higher level interface for the findEdge() function. You can
526 /// use it the following way:
528 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
535 /// \author Balazs Dezso
536 template <typename _Graph>
537 class ConEdgeIt : public _Graph::Edge {
540 typedef _Graph Graph;
541 typedef typename Graph::Edge Parent;
543 typedef typename Graph::Edge Edge;
544 typedef typename Graph::Node Node;
546 /// \brief Constructor.
548 /// Construct a new ConEdgeIt iterating on the edges which
549 /// connects the \c u and \c v node.
550 ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) {
551 Parent::operator=(findEdge(_graph, u, v));
554 /// \brief Constructor.
556 /// Construct a new ConEdgeIt which continues the iterating from
558 ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
560 /// \brief Increment operator.
562 /// It increments the iterator and gives back the next edge.
563 ConEdgeIt& operator++() {
564 Parent::operator=(findEdge(_graph, _graph.source(*this),
565 _graph.target(*this), *this));
572 namespace _graph_utils_bits {
574 template <typename Digraph, typename Item, typename RefMap>
577 virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
579 virtual ~MapCopyBase() {}
582 template <typename Digraph, typename Item, typename RefMap,
583 typename ToMap, typename FromMap>
584 class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
587 MapCopy(ToMap& tmap, const FromMap& map)
588 : _tmap(tmap), _map(map) {}
590 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
591 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
592 for (ItemIt it(digraph); it != INVALID; ++it) {
593 _tmap.set(refMap[it], _map[it]);
602 template <typename Digraph, typename Item, typename RefMap, typename It>
603 class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
606 ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
608 virtual void copy(const Digraph&, const RefMap& refMap) {
617 template <typename Digraph, typename Item, typename RefMap, typename Ref>
618 class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
621 RefCopy(Ref& map) : _map(map) {}
623 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
624 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
625 for (ItemIt it(digraph); it != INVALID; ++it) {
626 _map.set(it, refMap[it]);
634 template <typename Digraph, typename Item, typename RefMap,
636 class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
639 CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
641 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
642 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
643 for (ItemIt it(digraph); it != INVALID; ++it) {
644 _cmap.set(refMap[it], it);
652 template <typename Digraph, typename Enable = void>
653 struct DigraphCopySelector {
654 template <typename From, typename NodeRefMap, typename ArcRefMap>
655 static void copy(Digraph &to, const From& from,
656 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
657 for (typename From::NodeIt it(from); it != INVALID; ++it) {
658 nodeRefMap[it] = to.addNode();
660 for (typename From::ArcIt it(from); it != INVALID; ++it) {
661 arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
662 nodeRefMap[from.target(it)]);
667 template <typename Digraph>
668 struct DigraphCopySelector<
670 typename enable_if<typename Digraph::BuildTag, void>::type>
672 template <typename From, typename NodeRefMap, typename ArcRefMap>
673 static void copy(Digraph &to, const From& from,
674 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
675 to.build(from, nodeRefMap, arcRefMap);
679 template <typename Graph, typename Enable = void>
680 struct GraphCopySelector {
681 template <typename From, typename NodeRefMap, typename EdgeRefMap>
682 static void copy(Graph &to, const From& from,
683 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
684 for (typename From::NodeIt it(from); it != INVALID; ++it) {
685 nodeRefMap[it] = to.addNode();
687 for (typename From::EdgeIt it(from); it != INVALID; ++it) {
688 edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
689 nodeRefMap[from.target(it)]);
694 template <typename Graph>
695 struct GraphCopySelector<
697 typename enable_if<typename Graph::BuildTag, void>::type>
699 template <typename From, typename NodeRefMap, typename EdgeRefMap>
700 static void copy(Graph &to, const From& from,
701 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
702 to.build(from, nodeRefMap, edgeRefMap);
708 /// \brief Class to copy a digraph.
710 /// Class to copy a digraph to another digraph (duplicate a digraph). The
711 /// simplest way of using it is through the \c copyDigraph() function.
713 /// This class not just make a copy of a graph, but it can create
714 /// references and cross references between the nodes and arcs of
715 /// the two graphs, it can copy maps for use with the newly created
716 /// graph and copy nodes and arcs.
718 /// To make a copy from a graph, first an instance of DigraphCopy
719 /// should be created, then the data belongs to the graph should
720 /// assigned to copy. In the end, the \c run() member should be
723 /// The next code copies a graph with several data:
725 /// DigraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph);
726 /// // create a reference for the nodes
727 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
729 /// // create a cross reference (inverse) for the arcs
730 /// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
731 /// dc.arcCrossRef(acr);
732 /// // copy an arc map
733 /// OrigGraph::ArcMap<double> oamap(orig_graph);
734 /// NewGraph::ArcMap<double> namap(new_graph);
735 /// dc.arcMap(namap, oamap);
737 /// OrigGraph::Node on;
738 /// NewGraph::Node nn;
740 /// // Executions of copy
743 template <typename To, typename From>
747 typedef typename From::Node Node;
748 typedef typename From::NodeIt NodeIt;
749 typedef typename From::Arc Arc;
750 typedef typename From::ArcIt ArcIt;
752 typedef typename To::Node TNode;
753 typedef typename To::Arc TArc;
755 typedef typename From::template NodeMap<TNode> NodeRefMap;
756 typedef typename From::template ArcMap<TArc> ArcRefMap;
762 /// \brief Constructor for the DigraphCopy.
764 /// It copies the content of the \c _from digraph into the
766 DigraphCopy(To& to, const From& from)
767 : _from(from), _to(to) {}
769 /// \brief Destructor of the DigraphCopy
771 /// Destructor of the DigraphCopy
773 for (int i = 0; i < int(_node_maps.size()); ++i) {
774 delete _node_maps[i];
776 for (int i = 0; i < int(_arc_maps.size()); ++i) {
782 /// \brief Copies the node references into the given map.
784 /// Copies the node references into the given map. The parameter
785 /// should be a map, which key type is the Node type of the source
786 /// graph, while the value type is the Node type of the
787 /// destination graph.
788 template <typename NodeRef>
789 DigraphCopy& nodeRef(NodeRef& map) {
790 _node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node,
791 NodeRefMap, NodeRef>(map));
795 /// \brief Copies the node cross references into the given map.
797 /// Copies the node cross references (reverse references) into
798 /// the given map. The parameter should be a map, which key type
799 /// is the Node type of the destination graph, while the value type is
800 /// the Node type of the source graph.
801 template <typename NodeCrossRef>
802 DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
803 _node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node,
804 NodeRefMap, NodeCrossRef>(map));
808 /// \brief Make copy of the given map.
810 /// Makes copy of the given map for the newly created digraph.
811 /// The new map's key type is the destination graph's node type,
812 /// and the copied map's key type is the source graph's node type.
813 template <typename ToMap, typename FromMap>
814 DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
815 _node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node,
816 NodeRefMap, ToMap, FromMap>(tmap, map));
820 /// \brief Make a copy of the given node.
822 /// Make a copy of the given node.
823 DigraphCopy& node(TNode& tnode, const Node& snode) {
824 _node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node,
825 NodeRefMap, TNode>(tnode, snode));
829 /// \brief Copies the arc references into the given map.
831 /// Copies the arc references into the given map.
832 template <typename ArcRef>
833 DigraphCopy& arcRef(ArcRef& map) {
834 _arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc,
835 ArcRefMap, ArcRef>(map));
839 /// \brief Copies the arc cross references into the given map.
841 /// Copies the arc cross references (reverse references) into
843 template <typename ArcCrossRef>
844 DigraphCopy& arcCrossRef(ArcCrossRef& map) {
845 _arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc,
846 ArcRefMap, ArcCrossRef>(map));
850 /// \brief Make copy of the given map.
852 /// Makes copy of the given map for the newly created digraph.
853 /// The new map's key type is the to digraph's arc type,
854 /// and the copied map's key type is the from digraph's arc
856 template <typename ToMap, typename FromMap>
857 DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
858 _arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc,
859 ArcRefMap, ToMap, FromMap>(tmap, map));
863 /// \brief Make a copy of the given arc.
865 /// Make a copy of the given arc.
866 DigraphCopy& arc(TArc& tarc, const Arc& sarc) {
867 _arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc,
868 ArcRefMap, TArc>(tarc, sarc));
872 /// \brief Executes the copies.
874 /// Executes the copies.
876 NodeRefMap nodeRefMap(_from);
877 ArcRefMap arcRefMap(_from);
878 _graph_utils_bits::DigraphCopySelector<To>::
879 copy(_to, _from, nodeRefMap, arcRefMap);
880 for (int i = 0; i < int(_node_maps.size()); ++i) {
881 _node_maps[i]->copy(_from, nodeRefMap);
883 for (int i = 0; i < int(_arc_maps.size()); ++i) {
884 _arc_maps[i]->copy(_from, arcRefMap);
894 std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
897 std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
902 /// \brief Copy a digraph to another digraph.
904 /// Copy a digraph to another digraph. The complete usage of the
905 /// function is detailed in the DigraphCopy class, but a short
906 /// example shows a basic work:
908 /// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
911 /// After the copy the \c nr map will contain the mapping from the
912 /// nodes of the \c from digraph to the nodes of the \c to digraph and
913 /// \c ecr will contain the mapping from the arcs of the \c to digraph
914 /// to the arcs of the \c from digraph.
917 template <typename To, typename From>
918 DigraphCopy<To, From> copyDigraph(To& to, const From& from) {
919 return DigraphCopy<To, From>(to, from);
922 /// \brief Class to copy a graph.
924 /// Class to copy a graph to another graph (duplicate a graph). The
925 /// simplest way of using it is through the \c copyGraph() function.
927 /// This class not just make a copy of a graph, but it can create
928 /// references and cross references between the nodes, edges and arcs of
929 /// the two graphs, it can copy maps for use with the newly created
930 /// graph and copy nodes, edges and arcs.
932 /// To make a copy from a graph, first an instance of GraphCopy
933 /// should be created, then the data belongs to the graph should
934 /// assigned to copy. In the end, the \c run() member should be
937 /// The next code copies a graph with several data:
939 /// GraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph);
940 /// // create a reference for the nodes
941 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
943 /// // create a cross reference (inverse) for the edges
944 /// NewGraph::EdgeMap<OrigGraph::Arc> ecr(new_graph);
945 /// dc.edgeCrossRef(ecr);
946 /// // copy an arc map
947 /// OrigGraph::ArcMap<double> oamap(orig_graph);
948 /// NewGraph::ArcMap<double> namap(new_graph);
949 /// dc.arcMap(namap, oamap);
951 /// OrigGraph::Node on;
952 /// NewGraph::Node nn;
954 /// // Executions of copy
957 template <typename To, typename From>
961 typedef typename From::Node Node;
962 typedef typename From::NodeIt NodeIt;
963 typedef typename From::Arc Arc;
964 typedef typename From::ArcIt ArcIt;
965 typedef typename From::Edge Edge;
966 typedef typename From::EdgeIt EdgeIt;
968 typedef typename To::Node TNode;
969 typedef typename To::Arc TArc;
970 typedef typename To::Edge TEdge;
972 typedef typename From::template NodeMap<TNode> NodeRefMap;
973 typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
976 ArcRefMap(const To& to, const From& from,
977 const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
978 : _to(to), _from(from),
979 _edge_ref(edge_ref), _node_ref(node_ref) {}
981 typedef typename From::Arc Key;
982 typedef typename To::Arc Value;
984 Value operator[](const Key& key) const {
986 (_from.direction(key) ==
987 (_node_ref[_from.source(key)] == _to.source(_edge_ref[key])));
988 return _to.direct(_edge_ref[key], forward);
993 const EdgeRefMap& _edge_ref;
994 const NodeRefMap& _node_ref;
1001 /// \brief Constructor for the GraphCopy.
1003 /// It copies the content of the \c _from graph into the
1005 GraphCopy(To& to, const From& from)
1006 : _from(from), _to(to) {}
1008 /// \brief Destructor of the GraphCopy
1010 /// Destructor of the GraphCopy
1012 for (int i = 0; i < int(_node_maps.size()); ++i) {
1013 delete _node_maps[i];
1015 for (int i = 0; i < int(_arc_maps.size()); ++i) {
1016 delete _arc_maps[i];
1018 for (int i = 0; i < int(_edge_maps.size()); ++i) {
1019 delete _edge_maps[i];
1024 /// \brief Copies the node references into the given map.
1026 /// Copies the node references into the given map.
1027 template <typename NodeRef>
1028 GraphCopy& nodeRef(NodeRef& map) {
1029 _node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node,
1030 NodeRefMap, NodeRef>(map));
1034 /// \brief Copies the node cross references into the given map.
1036 /// Copies the node cross references (reverse references) into
1038 template <typename NodeCrossRef>
1039 GraphCopy& nodeCrossRef(NodeCrossRef& map) {
1040 _node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node,
1041 NodeRefMap, NodeCrossRef>(map));
1045 /// \brief Make copy of the given map.
1047 /// Makes copy of the given map for the newly created graph.
1048 /// The new map's key type is the to graph's node type,
1049 /// and the copied map's key type is the from graph's node
1051 template <typename ToMap, typename FromMap>
1052 GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
1053 _node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node,
1054 NodeRefMap, ToMap, FromMap>(tmap, map));
1058 /// \brief Make a copy of the given node.
1060 /// Make a copy of the given node.
1061 GraphCopy& node(TNode& tnode, const Node& snode) {
1062 _node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node,
1063 NodeRefMap, TNode>(tnode, snode));
1067 /// \brief Copies the arc references into the given map.
1069 /// Copies the arc references into the given map.
1070 template <typename ArcRef>
1071 GraphCopy& arcRef(ArcRef& map) {
1072 _arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc,
1073 ArcRefMap, ArcRef>(map));
1077 /// \brief Copies the arc cross references into the given map.
1079 /// Copies the arc cross references (reverse references) into
1081 template <typename ArcCrossRef>
1082 GraphCopy& arcCrossRef(ArcCrossRef& map) {
1083 _arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc,
1084 ArcRefMap, ArcCrossRef>(map));
1088 /// \brief Make copy of the given map.
1090 /// Makes copy of the given map for the newly created graph.
1091 /// The new map's key type is the to graph's arc type,
1092 /// and the copied map's key type is the from graph's arc
1094 template <typename ToMap, typename FromMap>
1095 GraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
1096 _arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc,
1097 ArcRefMap, ToMap, FromMap>(tmap, map));
1101 /// \brief Make a copy of the given arc.
1103 /// Make a copy of the given arc.
1104 GraphCopy& arc(TArc& tarc, const Arc& sarc) {
1105 _arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc,
1106 ArcRefMap, TArc>(tarc, sarc));
1110 /// \brief Copies the edge references into the given map.
1112 /// Copies the edge references into the given map.
1113 template <typename EdgeRef>
1114 GraphCopy& edgeRef(EdgeRef& map) {
1115 _edge_maps.push_back(new _graph_utils_bits::RefCopy<From, Edge,
1116 EdgeRefMap, EdgeRef>(map));
1120 /// \brief Copies the edge cross references into the given map.
1122 /// Copies the edge cross references (reverse
1123 /// references) into the given map.
1124 template <typename EdgeCrossRef>
1125 GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1126 _edge_maps.push_back(new _graph_utils_bits::CrossRefCopy<From,
1127 Edge, EdgeRefMap, EdgeCrossRef>(map));
1131 /// \brief Make copy of the given map.
1133 /// Makes copy of the given map for the newly created graph.
1134 /// The new map's key type is the to graph's edge type,
1135 /// and the copied map's key type is the from graph's edge
1137 template <typename ToMap, typename FromMap>
1138 GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
1139 _edge_maps.push_back(new _graph_utils_bits::MapCopy<From, Edge,
1140 EdgeRefMap, ToMap, FromMap>(tmap, map));
1144 /// \brief Make a copy of the given edge.
1146 /// Make a copy of the given edge.
1147 GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
1148 _edge_maps.push_back(new _graph_utils_bits::ItemCopy<From, Edge,
1149 EdgeRefMap, TEdge>(tedge, sedge));
1153 /// \brief Executes the copies.
1155 /// Executes the copies.
1157 NodeRefMap nodeRefMap(_from);
1158 EdgeRefMap edgeRefMap(_from);
1159 ArcRefMap arcRefMap(_to, _from, edgeRefMap, nodeRefMap);
1160 _graph_utils_bits::GraphCopySelector<To>::
1161 copy(_to, _from, nodeRefMap, edgeRefMap);
1162 for (int i = 0; i < int(_node_maps.size()); ++i) {
1163 _node_maps[i]->copy(_from, nodeRefMap);
1165 for (int i = 0; i < int(_edge_maps.size()); ++i) {
1166 _edge_maps[i]->copy(_from, edgeRefMap);
1168 for (int i = 0; i < int(_arc_maps.size()); ++i) {
1169 _arc_maps[i]->copy(_from, arcRefMap);
1178 std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
1181 std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
1184 std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
1189 /// \brief Copy a graph to another graph.
1191 /// Copy a graph to another graph. The complete usage of the
1192 /// function is detailed in the GraphCopy class, but a short
1193 /// example shows a basic work:
1195 /// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
1198 /// After the copy the \c nr map will contain the mapping from the
1199 /// nodes of the \c from graph to the nodes of the \c to graph and
1200 /// \c ecr will contain the mapping from the arcs of the \c to graph
1201 /// to the arcs of the \c from graph.
1204 template <typename To, typename From>
1206 copyGraph(To& to, const From& from) {
1207 return GraphCopy<To, From>(to, from);
1212 /// \addtogroup graph_maps
1215 /// Provides an immutable and unique id for each item in the graph.
1217 /// The IdMap class provides a unique and immutable id for each item of the
1218 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
1219 /// different items (nodes) get different ids <li>\b immutable: the id of an
1220 /// item (node) does not change (even if you delete other nodes). </ul>
1221 /// Through this map you get access (i.e. can read) the inner id values of
1222 /// the items stored in the graph. This map can be inverted with its member
1223 /// class \c InverseMap or with the \c operator() member.
1225 template <typename _Graph, typename _Item>
1228 typedef _Graph Graph;
1233 /// \brief Constructor.
1235 /// Constructor of the map.
1236 explicit IdMap(const Graph& graph) : _graph(&graph) {}
1238 /// \brief Gives back the \e id of the item.
1240 /// Gives back the immutable and unique \e id of the item.
1241 int operator[](const Item& item) const { return _graph->id(item);}
1243 /// \brief Gives back the item by its id.
1245 /// Gives back the item by its id.
1246 Item operator()(int id) { return _graph->fromId(id, Item()); }
1249 const Graph* _graph;
1253 /// \brief The class represents the inverse of its owner (IdMap).
1255 /// The class represents the inverse of its owner (IdMap).
1260 /// \brief Constructor.
1262 /// Constructor for creating an id-to-item map.
1263 explicit InverseMap(const Graph& graph) : _graph(&graph) {}
1265 /// \brief Constructor.
1267 /// Constructor for creating an id-to-item map.
1268 explicit InverseMap(const IdMap& map) : _graph(map._graph) {}
1270 /// \brief Gives back the given item from its id.
1272 /// Gives back the given item from its id.
1274 Item operator[](int id) const { return _graph->fromId(id, Item());}
1277 const Graph* _graph;
1280 /// \brief Gives back the inverse of the map.
1282 /// Gives back the inverse of the IdMap.
1283 InverseMap inverse() const { return InverseMap(*_graph);}
1288 /// \brief General invertable graph-map type.
1290 /// This type provides simple invertable graph-maps.
1291 /// The InvertableMap wraps an arbitrary ReadWriteMap
1292 /// and if a key is set to a new value then store it
1293 /// in the inverse map.
1295 /// The values of the map can be accessed
1296 /// with stl compatible forward iterator.
1298 /// \param _Graph The graph type.
1299 /// \param _Item The item type of the graph.
1300 /// \param _Value The value type of the map.
1302 /// \see IterableValueMap
1303 template <typename _Graph, typename _Item, typename _Value>
1304 class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> {
1307 typedef DefaultMap<_Graph, _Item, _Value> Map;
1308 typedef _Graph Graph;
1310 typedef std::map<_Value, _Item> Container;
1315 /// The key type of InvertableMap (Node, Arc, Edge).
1316 typedef typename Map::Key Key;
1317 /// The value type of the InvertableMap.
1318 typedef typename Map::Value Value;
1322 /// \brief Constructor.
1324 /// Construct a new InvertableMap for the graph.
1326 explicit InvertableMap(const Graph& graph) : Map(graph) {}
1328 /// \brief Forward iterator for values.
1330 /// This iterator is an stl compatible forward
1331 /// iterator on the values of the map. The values can
1332 /// be accessed in the [beginValue, endValue) range.
1335 : public std::iterator<std::forward_iterator_tag, Value> {
1336 friend class InvertableMap;
1338 ValueIterator(typename Container::const_iterator _it)
1344 ValueIterator& operator++() { ++it; return *this; }
1345 ValueIterator operator++(int) {
1346 ValueIterator tmp(*this);
1351 const Value& operator*() const { return it->first; }
1352 const Value* operator->() const { return &(it->first); }
1354 bool operator==(ValueIterator jt) const { return it == jt.it; }
1355 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1358 typename Container::const_iterator it;
1361 /// \brief Returns an iterator to the first value.
1363 /// Returns an stl compatible iterator to the
1364 /// first value of the map. The values of the
1365 /// map can be accessed in the [beginValue, endValue)
1367 ValueIterator beginValue() const {
1368 return ValueIterator(_inv_map.begin());
1371 /// \brief Returns an iterator after the last value.
1373 /// Returns an stl compatible iterator after the
1374 /// last value of the map. The values of the
1375 /// map can be accessed in the [beginValue, endValue)
1377 ValueIterator endValue() const {
1378 return ValueIterator(_inv_map.end());
1381 /// \brief The setter function of the map.
1383 /// Sets the mapped value.
1384 void set(const Key& key, const Value& val) {
1385 Value oldval = Map::operator[](key);
1386 typename Container::iterator it = _inv_map.find(oldval);
1387 if (it != _inv_map.end() && it->second == key) {
1390 _inv_map.insert(make_pair(val, key));
1394 /// \brief The getter function of the map.
1396 /// It gives back the value associated with the key.
1397 typename MapTraits<Map>::ConstReturnValue
1398 operator[](const Key& key) const {
1399 return Map::operator[](key);
1402 /// \brief Gives back the item by its value.
1404 /// Gives back the item by its value.
1405 Key operator()(const Value& key) const {
1406 typename Container::const_iterator it = _inv_map.find(key);
1407 return it != _inv_map.end() ? it->second : INVALID;
1412 /// \brief Erase the key from the map.
1414 /// Erase the key to the map. It is called by the
1415 /// \c AlterationNotifier.
1416 virtual void erase(const Key& key) {
1417 Value val = Map::operator[](key);
1418 typename Container::iterator it = _inv_map.find(val);
1419 if (it != _inv_map.end() && it->second == key) {
1425 /// \brief Erase more keys from the map.
1427 /// Erase more keys from the map. It is called by the
1428 /// \c AlterationNotifier.
1429 virtual void erase(const std::vector<Key>& keys) {
1430 for (int i = 0; i < int(keys.size()); ++i) {
1431 Value val = Map::operator[](keys[i]);
1432 typename Container::iterator it = _inv_map.find(val);
1433 if (it != _inv_map.end() && it->second == keys[i]) {
1440 /// \brief Clear the keys from the map and inverse map.
1442 /// Clear the keys from the map and inverse map. It is called by the
1443 /// \c AlterationNotifier.
1444 virtual void clear() {
1451 /// \brief The inverse map type.
1453 /// The inverse of this map. The subscript operator of the map
1454 /// gives back always the item what was last assigned to the value.
1457 /// \brief Constructor of the InverseMap.
1459 /// Constructor of the InverseMap.
1460 explicit InverseMap(const InvertableMap& inverted)
1461 : _inverted(inverted) {}
1463 /// The value type of the InverseMap.
1464 typedef typename InvertableMap::Key Value;
1465 /// The key type of the InverseMap.
1466 typedef typename InvertableMap::Value Key;
1468 /// \brief Subscript operator.
1470 /// Subscript operator. It gives back always the item
1471 /// what was last assigned to the value.
1472 Value operator[](const Key& key) const {
1473 return _inverted(key);
1477 const InvertableMap& _inverted;
1480 /// \brief It gives back the just readable inverse map.
1482 /// It gives back the just readable inverse map.
1483 InverseMap inverse() const {
1484 return InverseMap(*this);
1491 /// \brief Provides a mutable, continuous and unique descriptor for each
1492 /// item in the graph.
1494 /// The DescriptorMap class provides a unique and continuous (but mutable)
1495 /// descriptor (id) for each item of the same type (e.g. node) in the
1496 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1497 /// different ids <li>\b continuous: the range of the ids is the set of
1498 /// integers between 0 and \c n-1, where \c n is the number of the items of
1499 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1500 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1501 /// with its member class \c InverseMap, or with the \c operator() member.
1503 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1504 /// \param _Item The Item is the Key of the Map. It may be Node, Arc or
1506 template <typename _Graph, typename _Item>
1507 class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
1510 typedef DefaultMap<_Graph, _Item, int> Map;
1513 /// The graph class of DescriptorMap.
1514 typedef _Graph Graph;
1516 /// The key type of DescriptorMap (Node, Arc, Edge).
1517 typedef typename Map::Key Key;
1518 /// The value type of DescriptorMap.
1519 typedef typename Map::Value Value;
1521 /// \brief Constructor.
1523 /// Constructor for descriptor map.
1524 explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
1526 const typename Map::Notifier* nf = Map::notifier();
1527 for (nf->first(it); it != INVALID; nf->next(it)) {
1528 Map::set(it, _inv_map.size());
1529 _inv_map.push_back(it);
1535 /// \brief Add a new key to the map.
1537 /// Add a new key to the map. It is called by the
1538 /// \c AlterationNotifier.
1539 virtual void add(const Item& item) {
1541 Map::set(item, _inv_map.size());
1542 _inv_map.push_back(item);
1545 /// \brief Add more new keys to the map.
1547 /// Add more new keys to the map. It is called by the
1548 /// \c AlterationNotifier.
1549 virtual void add(const std::vector<Item>& items) {
1551 for (int i = 0; i < int(items.size()); ++i) {
1552 Map::set(items[i], _inv_map.size());
1553 _inv_map.push_back(items[i]);
1557 /// \brief Erase the key from the map.
1559 /// Erase the key from the map. It is called by the
1560 /// \c AlterationNotifier.
1561 virtual void erase(const Item& item) {
1562 Map::set(_inv_map.back(), Map::operator[](item));
1563 _inv_map[Map::operator[](item)] = _inv_map.back();
1564 _inv_map.pop_back();
1568 /// \brief Erase more keys from the map.
1570 /// Erase more keys from the map. It is called by the
1571 /// \c AlterationNotifier.
1572 virtual void erase(const std::vector<Item>& items) {
1573 for (int i = 0; i < int(items.size()); ++i) {
1574 Map::set(_inv_map.back(), Map::operator[](items[i]));
1575 _inv_map[Map::operator[](items[i])] = _inv_map.back();
1576 _inv_map.pop_back();
1581 /// \brief Build the unique map.
1583 /// Build the unique map. It is called by the
1584 /// \c AlterationNotifier.
1585 virtual void build() {
1588 const typename Map::Notifier* nf = Map::notifier();
1589 for (nf->first(it); it != INVALID; nf->next(it)) {
1590 Map::set(it, _inv_map.size());
1591 _inv_map.push_back(it);
1595 /// \brief Clear the keys from the map.
1597 /// Clear the keys from the map. It is called by the
1598 /// \c AlterationNotifier.
1599 virtual void clear() {
1606 /// \brief Returns the maximal value plus one.
1608 /// Returns the maximal value plus one in the map.
1609 unsigned int size() const {
1610 return _inv_map.size();
1613 /// \brief Swaps the position of the two items in the map.
1615 /// Swaps the position of the two items in the map.
1616 void swap(const Item& p, const Item& q) {
1617 int pi = Map::operator[](p);
1618 int qi = Map::operator[](q);
1625 /// \brief Gives back the \e descriptor of the item.
1627 /// Gives back the mutable and unique \e descriptor of the map.
1628 int operator[](const Item& item) const {
1629 return Map::operator[](item);
1632 /// \brief Gives back the item by its descriptor.
1634 /// Gives back th item by its descriptor.
1635 Item operator()(int id) const {
1636 return _inv_map[id];
1641 typedef std::vector<Item> Container;
1645 /// \brief The inverse map type of DescriptorMap.
1647 /// The inverse map type of DescriptorMap.
1650 /// \brief Constructor of the InverseMap.
1652 /// Constructor of the InverseMap.
1653 explicit InverseMap(const DescriptorMap& inverted)
1654 : _inverted(inverted) {}
1657 /// The value type of the InverseMap.
1658 typedef typename DescriptorMap::Key Value;
1659 /// The key type of the InverseMap.
1660 typedef typename DescriptorMap::Value Key;
1662 /// \brief Subscript operator.
1664 /// Subscript operator. It gives back the item
1665 /// that the descriptor belongs to currently.
1666 Value operator[](const Key& key) const {
1667 return _inverted(key);
1670 /// \brief Size of the map.
1672 /// Returns the size of the map.
1673 unsigned int size() const {
1674 return _inverted.size();
1678 const DescriptorMap& _inverted;
1681 /// \brief Gives back the inverse of the map.
1683 /// Gives back the inverse of the map.
1684 const InverseMap inverse() const {
1685 return InverseMap(*this);
1689 /// \brief Returns the source of the given arc.
1691 /// The SourceMap gives back the source Node of the given arc.
1693 /// \author Balazs Dezso
1694 template <typename Digraph>
1698 typedef typename Digraph::Node Value;
1699 typedef typename Digraph::Arc Key;
1701 /// \brief Constructor
1704 /// \param _digraph The digraph that the map belongs to.
1705 explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {}
1707 /// \brief The subscript operator.
1709 /// The subscript operator.
1710 /// \param arc The arc
1711 /// \return The source of the arc
1712 Value operator[](const Key& arc) const {
1713 return _digraph.source(arc);
1717 const Digraph& _digraph;
1720 /// \brief Returns a \ref SourceMap class.
1722 /// This function just returns an \ref SourceMap class.
1723 /// \relates SourceMap
1724 template <typename Digraph>
1725 inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
1726 return SourceMap<Digraph>(digraph);
1729 /// \brief Returns the target of the given arc.
1731 /// The TargetMap gives back the target Node of the given arc.
1733 /// \author Balazs Dezso
1734 template <typename Digraph>
1738 typedef typename Digraph::Node Value;
1739 typedef typename Digraph::Arc Key;
1741 /// \brief Constructor
1744 /// \param _digraph The digraph that the map belongs to.
1745 explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {}
1747 /// \brief The subscript operator.
1749 /// The subscript operator.
1750 /// \param e The arc
1751 /// \return The target of the arc
1752 Value operator[](const Key& e) const {
1753 return _digraph.target(e);
1757 const Digraph& _digraph;
1760 /// \brief Returns a \ref TargetMap class.
1762 /// This function just returns a \ref TargetMap class.
1763 /// \relates TargetMap
1764 template <typename Digraph>
1765 inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
1766 return TargetMap<Digraph>(digraph);
1769 /// \brief Returns the "forward" directed arc view of an edge.
1771 /// Returns the "forward" directed arc view of an edge.
1772 /// \see BackwardMap
1773 /// \author Balazs Dezso
1774 template <typename Graph>
1778 typedef typename Graph::Arc Value;
1779 typedef typename Graph::Edge Key;
1781 /// \brief Constructor
1784 /// \param _graph The graph that the map belongs to.
1785 explicit ForwardMap(const Graph& graph) : _graph(graph) {}
1787 /// \brief The subscript operator.
1789 /// The subscript operator.
1790 /// \param key An edge
1791 /// \return The "forward" directed arc view of edge
1792 Value operator[](const Key& key) const {
1793 return _graph.direct(key, true);
1797 const Graph& _graph;
1800 /// \brief Returns a \ref ForwardMap class.
1802 /// This function just returns an \ref ForwardMap class.
1803 /// \relates ForwardMap
1804 template <typename Graph>
1805 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
1806 return ForwardMap<Graph>(graph);
1809 /// \brief Returns the "backward" directed arc view of an edge.
1811 /// Returns the "backward" directed arc view of an edge.
1813 /// \author Balazs Dezso
1814 template <typename Graph>
1818 typedef typename Graph::Arc Value;
1819 typedef typename Graph::Edge Key;
1821 /// \brief Constructor
1824 /// \param _graph The graph that the map belongs to.
1825 explicit BackwardMap(const Graph& graph) : _graph(graph) {}
1827 /// \brief The subscript operator.
1829 /// The subscript operator.
1830 /// \param key An edge
1831 /// \return The "backward" directed arc view of edge
1832 Value operator[](const Key& key) const {
1833 return _graph.direct(key, false);
1837 const Graph& _graph;
1840 /// \brief Returns a \ref BackwardMap class
1842 /// This function just returns a \ref BackwardMap class.
1843 /// \relates BackwardMap
1844 template <typename Graph>
1845 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
1846 return BackwardMap<Graph>(graph);
1849 /// \brief Potential difference map
1851 /// If there is an potential map on the nodes then we
1852 /// can get an arc map as we get the substraction of the
1853 /// values of the target and source.
1854 template <typename Digraph, typename NodeMap>
1855 class PotentialDifferenceMap {
1857 typedef typename Digraph::Arc Key;
1858 typedef typename NodeMap::Value Value;
1860 /// \brief Constructor
1862 /// Contructor of the map
1863 explicit PotentialDifferenceMap(const Digraph& digraph,
1864 const NodeMap& potential)
1865 : _digraph(digraph), _potential(potential) {}
1867 /// \brief Const subscription operator
1869 /// Const subscription operator
1870 Value operator[](const Key& arc) const {
1871 return _potential[_digraph.target(arc)] -
1872 _potential[_digraph.source(arc)];
1876 const Digraph& _digraph;
1877 const NodeMap& _potential;
1880 /// \brief Returns a PotentialDifferenceMap.
1882 /// This function just returns a PotentialDifferenceMap.
1883 /// \relates PotentialDifferenceMap
1884 template <typename Digraph, typename NodeMap>
1885 PotentialDifferenceMap<Digraph, NodeMap>
1886 potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
1887 return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential);
1890 /// \brief Map of the node in-degrees.
1892 /// This map returns the in-degree of a node. Once it is constructed,
1893 /// the degrees are stored in a standard NodeMap, so each query is done
1894 /// in constant time. On the other hand, the values are updated automatically
1895 /// whenever the digraph changes.
1897 /// \warning Besides addNode() and addArc(), a digraph structure may provide
1898 /// alternative ways to modify the digraph. The correct behavior of InDegMap
1899 /// is not guarantied if these additional features are used. For example
1900 /// the functions \ref ListDigraph::changeSource() "changeSource()",
1901 /// \ref ListDigraph::changeTarget() "changeTarget()" and
1902 /// \ref ListDigraph::reverseArc() "reverseArc()"
1903 /// of \ref ListDigraph will \e not update the degree values correctly.
1907 template <typename _Digraph>
1909 : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
1910 ::ItemNotifier::ObserverBase {
1914 typedef _Digraph Digraph;
1916 typedef typename Digraph::Node Key;
1918 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
1919 ::ItemNotifier::ObserverBase Parent;
1923 class AutoNodeMap : public DefaultMap<Digraph, Key, int> {
1926 typedef DefaultMap<Digraph, Key, int> Parent;
1928 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
1930 virtual void add(const Key& key) {
1932 Parent::set(key, 0);
1935 virtual void add(const std::vector<Key>& keys) {
1937 for (int i = 0; i < int(keys.size()); ++i) {
1938 Parent::set(keys[i], 0);
1942 virtual void build() {
1945 typename Parent::Notifier* nf = Parent::notifier();
1946 for (nf->first(it); it != INVALID; nf->next(it)) {
1954 /// \brief Constructor.
1956 /// Constructor for creating in-degree map.
1957 explicit InDegMap(const Digraph& digraph)
1958 : _digraph(digraph), _deg(digraph) {
1959 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
1961 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
1962 _deg[it] = countInArcs(_digraph, it);
1966 /// Gives back the in-degree of a Node.
1967 int operator[](const Key& key) const {
1973 typedef typename Digraph::Arc Arc;
1975 virtual void add(const Arc& arc) {
1976 ++_deg[_digraph.target(arc)];
1979 virtual void add(const std::vector<Arc>& arcs) {
1980 for (int i = 0; i < int(arcs.size()); ++i) {
1981 ++_deg[_digraph.target(arcs[i])];
1985 virtual void erase(const Arc& arc) {
1986 --_deg[_digraph.target(arc)];
1989 virtual void erase(const std::vector<Arc>& arcs) {
1990 for (int i = 0; i < int(arcs.size()); ++i) {
1991 --_deg[_digraph.target(arcs[i])];
1995 virtual void build() {
1996 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
1997 _deg[it] = countInArcs(_digraph, it);
2001 virtual void clear() {
2002 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2008 const Digraph& _digraph;
2012 /// \brief Map of the node out-degrees.
2014 /// This map returns the out-degree of a node. Once it is constructed,
2015 /// the degrees are stored in a standard NodeMap, so each query is done
2016 /// in constant time. On the other hand, the values are updated automatically
2017 /// whenever the digraph changes.
2019 /// \warning Besides addNode() and addArc(), a digraph structure may provide
2020 /// alternative ways to modify the digraph. The correct behavior of OutDegMap
2021 /// is not guarantied if these additional features are used. For example
2022 /// the functions \ref ListDigraph::changeSource() "changeSource()",
2023 /// \ref ListDigraph::changeTarget() "changeTarget()" and
2024 /// \ref ListDigraph::reverseArc() "reverseArc()"
2025 /// of \ref ListDigraph will \e not update the degree values correctly.
2029 template <typename _Digraph>
2031 : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
2032 ::ItemNotifier::ObserverBase {
2036 typedef _Digraph Digraph;
2038 typedef typename Digraph::Node Key;
2040 typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
2041 ::ItemNotifier::ObserverBase Parent;
2045 class AutoNodeMap : public DefaultMap<Digraph, Key, int> {
2048 typedef DefaultMap<Digraph, Key, int> Parent;
2050 AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
2052 virtual void add(const Key& key) {
2054 Parent::set(key, 0);
2056 virtual void add(const std::vector<Key>& keys) {
2058 for (int i = 0; i < int(keys.size()); ++i) {
2059 Parent::set(keys[i], 0);
2062 virtual void build() {
2065 typename Parent::Notifier* nf = Parent::notifier();
2066 for (nf->first(it); it != INVALID; nf->next(it)) {
2074 /// \brief Constructor.
2076 /// Constructor for creating out-degree map.
2077 explicit OutDegMap(const Digraph& digraph)
2078 : _digraph(digraph), _deg(digraph) {
2079 Parent::attach(_digraph.notifier(typename Digraph::Arc()));
2081 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2082 _deg[it] = countOutArcs(_digraph, it);
2086 /// Gives back the out-degree of a Node.
2087 int operator[](const Key& key) const {
2093 typedef typename Digraph::Arc Arc;
2095 virtual void add(const Arc& arc) {
2096 ++_deg[_digraph.source(arc)];
2099 virtual void add(const std::vector<Arc>& arcs) {
2100 for (int i = 0; i < int(arcs.size()); ++i) {
2101 ++_deg[_digraph.source(arcs[i])];
2105 virtual void erase(const Arc& arc) {
2106 --_deg[_digraph.source(arc)];
2109 virtual void erase(const std::vector<Arc>& arcs) {
2110 for (int i = 0; i < int(arcs.size()); ++i) {
2111 --_deg[_digraph.source(arcs[i])];
2115 virtual void build() {
2116 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2117 _deg[it] = countOutArcs(_digraph, it);
2121 virtual void clear() {
2122 for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2128 const Digraph& _digraph;
2133 ///Dynamic arc look up between given endpoints.
2136 ///Using this class, you can find an arc in a digraph from a given
2137 ///source to a given target in amortized time <em>O(log d)</em>,
2138 ///where <em>d</em> is the out-degree of the source node.
2140 ///It is possible to find \e all parallel arcs between two nodes with
2141 ///the \c findFirst() and \c findNext() members.
2143 ///See the \ref ArcLookUp and \ref AllArcLookUp classes if your
2144 ///digraph is not changed so frequently.
2146 ///This class uses a self-adjusting binary search tree, Sleator's
2147 ///and Tarjan's Splay tree for guarantee the logarithmic amortized
2148 ///time bound for arc lookups. This class also guarantees the
2149 ///optimal time bound in a constant factor for any distribution of
2152 ///\param G The type of the underlying digraph.
2158 : protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
2161 typedef typename ItemSetTraits<G, typename G::Arc>
2162 ::ItemNotifier::ObserverBase Parent;
2164 DIGRAPH_TYPEDEFS(G);
2169 class AutoNodeMap : public DefaultMap<G, Node, Arc> {
2172 typedef DefaultMap<G, Node, Arc> Parent;
2174 AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
2176 virtual void add(const Node& node) {
2178 Parent::set(node, INVALID);
2181 virtual void add(const std::vector<Node>& nodes) {
2183 for (int i = 0; i < int(nodes.size()); ++i) {
2184 Parent::set(nodes[i], INVALID);
2188 virtual void build() {
2191 typename Parent::Notifier* nf = Parent::notifier();
2192 for (nf->first(it); it != INVALID; nf->next(it)) {
2193 Parent::set(it, INVALID);
2200 typename Digraph::template ArcMap<Arc> _parent;
2201 typename Digraph::template ArcMap<Arc> _left;
2202 typename Digraph::template ArcMap<Arc> _right;
2207 ArcLess(const Digraph &_g) : g(_g) {}
2208 bool operator()(Arc a,Arc b) const
2210 return g.target(a)<g.target(b);
2220 ///It builds up the search database.
2221 DynArcLookUp(const Digraph &g)
2222 : _g(g),_head(g),_parent(g),_left(g),_right(g)
2224 Parent::attach(_g.notifier(typename Digraph::Arc()));
2230 virtual void add(const Arc& arc) {
2234 virtual void add(const std::vector<Arc>& arcs) {
2235 for (int i = 0; i < int(arcs.size()); ++i) {
2240 virtual void erase(const Arc& arc) {
2244 virtual void erase(const std::vector<Arc>& arcs) {
2245 for (int i = 0; i < int(arcs.size()); ++i) {
2250 virtual void build() {
2254 virtual void clear() {
2255 for(NodeIt n(_g);n!=INVALID;++n) {
2256 _head.set(n, INVALID);
2260 void insert(Arc arc) {
2261 Node s = _g.source(arc);
2262 Node t = _g.target(arc);
2263 _left.set(arc, INVALID);
2264 _right.set(arc, INVALID);
2269 _parent.set(arc, INVALID);
2273 if (t < _g.target(e)) {
2274 if (_left[e] == INVALID) {
2276 _parent.set(arc, e);
2283 if (_right[e] == INVALID) {
2285 _parent.set(arc, e);
2295 void remove(Arc arc) {
2296 if (_left[arc] == INVALID) {
2297 if (_right[arc] != INVALID) {
2298 _parent.set(_right[arc], _parent[arc]);
2300 if (_parent[arc] != INVALID) {
2301 if (_left[_parent[arc]] == arc) {
2302 _left.set(_parent[arc], _right[arc]);
2304 _right.set(_parent[arc], _right[arc]);
2307 _head.set(_g.source(arc), _right[arc]);
2309 } else if (_right[arc] == INVALID) {
2310 _parent.set(_left[arc], _parent[arc]);
2311 if (_parent[arc] != INVALID) {
2312 if (_left[_parent[arc]] == arc) {
2313 _left.set(_parent[arc], _left[arc]);
2315 _right.set(_parent[arc], _left[arc]);
2318 _head.set(_g.source(arc), _left[arc]);
2322 if (_right[e] != INVALID) {
2324 while (_right[e] != INVALID) {
2328 _right.set(_parent[e], _left[e]);
2329 if (_left[e] != INVALID) {
2330 _parent.set(_left[e], _parent[e]);
2333 _left.set(e, _left[arc]);
2334 _parent.set(_left[arc], e);
2335 _right.set(e, _right[arc]);
2336 _parent.set(_right[arc], e);
2338 _parent.set(e, _parent[arc]);
2339 if (_parent[arc] != INVALID) {
2340 if (_left[_parent[arc]] == arc) {
2341 _left.set(_parent[arc], e);
2343 _right.set(_parent[arc], e);
2348 _right.set(e, _right[arc]);
2349 _parent.set(_right[arc], e);
2351 if (_parent[arc] != INVALID) {
2352 if (_left[_parent[arc]] == arc) {
2353 _left.set(_parent[arc], e);
2355 _right.set(_parent[arc], e);
2358 _head.set(_g.source(arc), e);
2364 Arc refreshRec(std::vector<Arc> &v,int a,int b)
2369 Arc left = refreshRec(v,a,m-1);
2370 _left.set(me, left);
2371 _parent.set(left, me);
2373 _left.set(me, INVALID);
2376 Arc right = refreshRec(v,m+1,b);
2377 _right.set(me, right);
2378 _parent.set(right, me);
2380 _right.set(me, INVALID);
2386 for(NodeIt n(_g);n!=INVALID;++n) {
2388 for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
2390 std::sort(v.begin(),v.end(),ArcLess(_g));
2391 Arc head = refreshRec(v,0,v.size()-1);
2393 _parent.set(head, INVALID);
2395 else _head.set(n, INVALID);
2401 _parent.set(v, _parent[w]);
2403 _left.set(w, _right[v]);
2405 if (_parent[v] != INVALID) {
2406 if (_right[_parent[v]] == w) {
2407 _right.set(_parent[v], v);
2409 _left.set(_parent[v], v);
2412 if (_left[w] != INVALID){
2413 _parent.set(_left[w], w);
2419 _parent.set(v, _parent[w]);
2421 _right.set(w, _left[v]);
2423 if (_parent[v] != INVALID){
2424 if (_left[_parent[v]] == w) {
2425 _left.set(_parent[v], v);
2427 _right.set(_parent[v], v);
2430 if (_right[w] != INVALID){
2431 _parent.set(_right[w], w);
2436 while (_parent[v] != INVALID) {
2437 if (v == _left[_parent[v]]) {
2438 if (_parent[_parent[v]] == INVALID) {
2441 if (_parent[v] == _left[_parent[_parent[v]]]) {
2450 if (_parent[_parent[v]] == INVALID) {
2453 if (_parent[v] == _left[_parent[_parent[v]]]) {
2463 _head[_g.source(v)] = v;
2469 ///Find an arc between two nodes.
2471 ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
2472 /// <em>d</em> is the number of outgoing arcs of \c s.
2473 ///\param s The source node
2474 ///\param t The target node
2475 ///\return An arc from \c s to \c t if there exists,
2476 ///\ref INVALID otherwise.
2477 Arc operator()(Node s, Node t) const
2481 if (_g.target(a) == t) {
2482 const_cast<DynArcLookUp&>(*this).splay(a);
2484 } else if (t < _g.target(a)) {
2485 if (_left[a] == INVALID) {
2486 const_cast<DynArcLookUp&>(*this).splay(a);
2492 if (_right[a] == INVALID) {
2493 const_cast<DynArcLookUp&>(*this).splay(a);
2502 ///Find the first arc between two nodes.
2504 ///Find the first arc between two nodes in time
2505 /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
2506 /// outgoing arcs of \c s.
2507 ///\param s The source node
2508 ///\param t The target node
2509 ///\return An arc from \c s to \c t if there exists, \ref INVALID
2511 Arc findFirst(Node s, Node t) const
2516 if (_g.target(a) < t) {
2517 if (_right[a] == INVALID) {
2518 const_cast<DynArcLookUp&>(*this).splay(a);
2524 if (_g.target(a) == t) {
2527 if (_left[a] == INVALID) {
2528 const_cast<DynArcLookUp&>(*this).splay(a);
2537 ///Find the next arc between two nodes.
2539 ///Find the next arc between two nodes in time
2540 /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
2541 /// outgoing arcs of \c s.
2542 ///\param s The source node
2543 ///\param t The target node
2544 ///\return An arc from \c s to \c t if there exists, \ref INVALID
2547 ///\note If \c e is not the result of the previous \c findFirst()
2548 ///operation then the amorized time bound can not be guaranteed.
2550 Arc findNext(Node s, Node t, Arc a) const
2552 Arc findNext(Node, Node t, Arc a) const
2555 if (_right[a] != INVALID) {
2557 while (_left[a] != INVALID) {
2560 const_cast<DynArcLookUp&>(*this).splay(a);
2562 while (_parent[a] != INVALID && _right[_parent[a]] == a) {
2565 if (_parent[a] == INVALID) {
2569 const_cast<DynArcLookUp&>(*this).splay(a);
2572 if (_g.target(a) == t) return a;
2573 else return INVALID;
2578 ///Fast arc look up between given endpoints.
2581 ///Using this class, you can find an arc in a digraph from a given
2582 ///source to a given target in time <em>O(log d)</em>,
2583 ///where <em>d</em> is the out-degree of the source node.
2585 ///It is not possible to find \e all parallel arcs between two nodes.
2586 ///Use \ref AllArcLookUp for this purpose.
2588 ///\warning This class is static, so you should refresh() (or at least
2589 ///refresh(Node)) this data structure
2590 ///whenever the digraph changes. This is a time consuming (superlinearly
2591 ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
2593 ///\param G The type of the underlying digraph.
2601 DIGRAPH_TYPEDEFS(G);
2606 typename Digraph::template NodeMap<Arc> _head;
2607 typename Digraph::template ArcMap<Arc> _left;
2608 typename Digraph::template ArcMap<Arc> _right;
2613 ArcLess(const Digraph &_g) : g(_g) {}
2614 bool operator()(Arc a,Arc b) const
2616 return g.target(a)<g.target(b);
2626 ///It builds up the search database, which remains valid until the digraph
2628 ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
2631 Arc refreshRec(std::vector<Arc> &v,int a,int b)
2635 _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
2636 _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
2640 ///Refresh the data structure at a node.
2642 ///Build up the search database of node \c n.
2644 ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2645 ///the number of the outgoing arcs of \c n.
2646 void refresh(Node n)
2649 for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
2651 std::sort(v.begin(),v.end(),ArcLess(_g));
2652 _head[n]=refreshRec(v,0,v.size()-1);
2654 else _head[n]=INVALID;
2656 ///Refresh the full data structure.
2658 ///Build up the full search database. In fact, it simply calls
2659 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2661 ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2662 ///the number of the arcs of \c n and <em>D</em> is the maximum
2663 ///out-degree of the digraph.
2667 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
2670 ///Find an arc between two nodes.
2672 ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
2673 /// <em>d</em> is the number of outgoing arcs of \c s.
2674 ///\param s The source node
2675 ///\param t The target node
2676 ///\return An arc from \c s to \c t if there exists,
2677 ///\ref INVALID otherwise.
2679 ///\warning If you change the digraph, refresh() must be called before using
2680 ///this operator. If you change the outgoing arcs of
2681 ///a single node \c n, then
2682 ///\ref refresh(Node) "refresh(n)" is enough.
2684 Arc operator()(Node s, Node t) const
2688 e!=INVALID&&_g.target(e)!=t;
2689 e = t < _g.target(e)?_left[e]:_right[e]) ;
2695 ///Fast look up of all arcs between given endpoints.
2698 ///This class is the same as \ref ArcLookUp, with the addition
2699 ///that it makes it possible to find all arcs between given endpoints.
2701 ///\warning This class is static, so you should refresh() (or at least
2702 ///refresh(Node)) this data structure
2703 ///whenever the digraph changes. This is a time consuming (superlinearly
2704 ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
2706 ///\param G The type of the underlying digraph.
2711 class AllArcLookUp : public ArcLookUp<G>
2713 using ArcLookUp<G>::_g;
2714 using ArcLookUp<G>::_right;
2715 using ArcLookUp<G>::_left;
2716 using ArcLookUp<G>::_head;
2718 DIGRAPH_TYPEDEFS(G);
2721 typename Digraph::template ArcMap<Arc> _next;
2723 Arc refreshNext(Arc head,Arc next=INVALID)
2725 if(head==INVALID) return next;
2727 next=refreshNext(_right[head],next);
2728 // _next[head]=next;
2729 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
2731 return refreshNext(_left[head],head);
2737 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
2745 ///It builds up the search database, which remains valid until the digraph
2747 AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
2749 ///Refresh the data structure at a node.
2751 ///Build up the search database of node \c n.
2753 ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2754 ///the number of the outgoing arcs of \c n.
2756 void refresh(Node n)
2758 ArcLookUp<G>::refresh(n);
2759 refreshNext(_head[n]);
2762 ///Refresh the full data structure.
2764 ///Build up the full search database. In fact, it simply calls
2765 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2767 ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2768 ///the number of the arcs of \c n and <em>D</em> is the maximum
2769 ///out-degree of the digraph.
2773 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
2776 ///Find an arc between two nodes.
2778 ///Find an arc between two nodes.
2779 ///\param s The source node
2780 ///\param t The target node
2781 ///\param prev The previous arc between \c s and \c t. It it is INVALID or
2782 ///not given, the operator finds the first appropriate arc.
2783 ///\return An arc from \c s to \c t after \c prev or
2784 ///\ref INVALID if there is no more.
2786 ///For example, you can count the number of arcs from \c u to \c v in the
2789 ///AllArcLookUp<ListDigraph> ae(g);
2792 ///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
2795 ///Finding the first arc take <em>O(</em>log<em>d)</em> time, where
2796 /// <em>d</em> is the number of outgoing arcs of \c s. Then, the
2797 ///consecutive arcs are found in constant time.
2799 ///\warning If you change the digraph, refresh() must be called before using
2800 ///this operator. If you change the outgoing arcs of
2801 ///a single node \c n, then
2802 ///\ref refresh(Node) "refresh(n)" is enough.
2805 Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
2807 using ArcLookUp<G>::operator() ;
2808 Arc operator()(Node s, Node t, Arc prev) const
2810 return prev==INVALID?(*this)(s,t):_next[prev];
2818 } //END OF NAMESPACE LEMON