1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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
25 #include <lemon/config.h>
26 #include <lemon/bits/enable_if.h>
27 #include <lemon/bits/traits.h>
28 #include <lemon/assert.h>
30 // Disable the following warnings when compiling with MSVC:
31 // C4250: 'class1' : inherits 'class2::member' via dominance
32 // C4355: 'this' : used in base member initializer list
33 // C4503: 'function' : decorated name length exceeded, name was truncated
34 // C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
35 // C4996: 'function': was declared deprecated
37 #pragma warning( disable : 4250 4355 4503 4800 4996 )
41 ///\brief LEMON core utilities.
43 ///This header file contains core utilities for LEMON.
44 ///It is automatically included by all graph types, therefore it usually
45 ///do not have to be included directly.
49 /// \brief Dummy type to make it easier to create invalid iterators.
51 /// Dummy type to make it easier to create invalid iterators.
52 /// See \ref INVALID for the usage.
55 bool operator==(Invalid) { return true; }
56 bool operator!=(Invalid) { return false; }
57 bool operator< (Invalid) { return false; }
60 /// \brief Invalid iterators.
62 /// \ref Invalid is a global type that converts to each iterator
63 /// in such a way that the value of the target iterator will be invalid.
64 #ifdef LEMON_ONLY_TEMPLATES
65 const Invalid INVALID = Invalid();
67 extern const Invalid INVALID;
70 /// \addtogroup gutils
73 ///Create convenience typedefs for the digraph types and iterators
75 ///This \c \#define creates convenient type definitions for the following
76 ///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
77 ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
78 ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
80 ///\note If the graph type is a dependent type, ie. the graph type depend
81 ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
83 #define DIGRAPH_TYPEDEFS(Digraph) \
84 typedef Digraph::Node Node; \
85 typedef Digraph::NodeIt NodeIt; \
86 typedef Digraph::Arc Arc; \
87 typedef Digraph::ArcIt ArcIt; \
88 typedef Digraph::InArcIt InArcIt; \
89 typedef Digraph::OutArcIt OutArcIt; \
90 typedef Digraph::NodeMap<bool> BoolNodeMap; \
91 typedef Digraph::NodeMap<int> IntNodeMap; \
92 typedef Digraph::NodeMap<double> DoubleNodeMap; \
93 typedef Digraph::ArcMap<bool> BoolArcMap; \
94 typedef Digraph::ArcMap<int> IntArcMap; \
95 typedef Digraph::ArcMap<double> DoubleArcMap
97 ///Create convenience typedefs for the digraph types and iterators
99 ///\see DIGRAPH_TYPEDEFS
101 ///\note Use this macro, if the graph type is a dependent type,
102 ///ie. the graph type depend on a template parameter.
103 #define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \
104 typedef typename Digraph::Node Node; \
105 typedef typename Digraph::NodeIt NodeIt; \
106 typedef typename Digraph::Arc Arc; \
107 typedef typename Digraph::ArcIt ArcIt; \
108 typedef typename Digraph::InArcIt InArcIt; \
109 typedef typename Digraph::OutArcIt OutArcIt; \
110 typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \
111 typedef typename Digraph::template NodeMap<int> IntNodeMap; \
112 typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \
113 typedef typename Digraph::template ArcMap<bool> BoolArcMap; \
114 typedef typename Digraph::template ArcMap<int> IntArcMap; \
115 typedef typename Digraph::template ArcMap<double> DoubleArcMap
117 ///Create convenience typedefs for the graph types and iterators
119 ///This \c \#define creates the same convenient type definitions as defined
120 ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
121 ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
124 ///\note If the graph type is a dependent type, ie. the graph type depend
125 ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
127 #define GRAPH_TYPEDEFS(Graph) \
128 DIGRAPH_TYPEDEFS(Graph); \
129 typedef Graph::Edge Edge; \
130 typedef Graph::EdgeIt EdgeIt; \
131 typedef Graph::IncEdgeIt IncEdgeIt; \
132 typedef Graph::EdgeMap<bool> BoolEdgeMap; \
133 typedef Graph::EdgeMap<int> IntEdgeMap; \
134 typedef Graph::EdgeMap<double> DoubleEdgeMap
136 ///Create convenience typedefs for the graph types and iterators
138 ///\see GRAPH_TYPEDEFS
140 ///\note Use this macro, if the graph type is a dependent type,
141 ///ie. the graph type depend on a template parameter.
142 #define TEMPLATE_GRAPH_TYPEDEFS(Graph) \
143 TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \
144 typedef typename Graph::Edge Edge; \
145 typedef typename Graph::EdgeIt EdgeIt; \
146 typedef typename Graph::IncEdgeIt IncEdgeIt; \
147 typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \
148 typedef typename Graph::template EdgeMap<int> IntEdgeMap; \
149 typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
151 ///Create convenience typedefs for the bipartite graph types and iterators
153 ///This \c \#define creates the same convenient type definitions as defined
154 ///by \ref GRAPH_TYPEDEFS(BpGraph) and ten more, namely it creates
155 ///\c RedNode, \c RedIt, \c BoolRedMap, \c IntRedMap, \c DoubleRedMap,
156 ///\c BlueNode, \c BlueIt, \c BoolBlueMap, \c IntBlueMap, \c DoubleBlueMap.
158 ///\note If the graph type is a dependent type, ie. the graph type depend
159 ///on a template parameter, then use \c TEMPLATE_BPGRAPH_TYPEDEFS()
161 #define BPGRAPH_TYPEDEFS(BpGraph) \
162 GRAPH_TYPEDEFS(BpGraph); \
163 typedef BpGraph::RedNode RedNode; \
164 typedef BpGraph::RedIt RedIt; \
165 typedef BpGraph::RedMap<bool> BoolRedMap; \
166 typedef BpGraph::RedMap<int> IntRedMap; \
167 typedef BpGraph::RedMap<double> DoubleRedMap; \
168 typedef BpGraph::BlueNode BlueNode; \
169 typedef BpGraph::BlueIt BlueIt; \
170 typedef BpGraph::BlueMap<bool> BoolBlueMap; \
171 typedef BpGraph::BlueMap<int> IntBlueMap; \
172 typedef BpGraph::BlueMap<double> DoubleBlueMap
174 ///Create convenience typedefs for the bipartite graph types and iterators
176 ///\see BPGRAPH_TYPEDEFS
178 ///\note Use this macro, if the graph type is a dependent type,
179 ///ie. the graph type depend on a template parameter.
180 #define TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph) \
181 TEMPLATE_GRAPH_TYPEDEFS(BpGraph); \
182 typedef typename BpGraph::RedNode RedNode; \
183 typedef typename BpGraph::RedIt RedIt; \
184 typedef typename BpGraph::template RedMap<bool> BoolRedMap; \
185 typedef typename BpGraph::template RedMap<int> IntRedMap; \
186 typedef typename BpGraph::template RedMap<double> DoubleRedMap; \
187 typedef typename BpGraph::BlueNode BlueNode; \
188 typedef typename BpGraph::BlueIt BlueIt; \
189 typedef typename BpGraph::template BlueMap<bool> BoolBlueMap; \
190 typedef typename BpGraph::template BlueMap<int> IntBlueMap; \
191 typedef typename BpGraph::template BlueMap<double> DoubleBlueMap
193 /// \brief Function to count the items in a graph.
195 /// This function counts the items (nodes, arcs etc.) in a graph.
196 /// The complexity of the function is linear because
197 /// it iterates on all of the items.
198 template <typename Graph, typename Item>
199 inline int countItems(const Graph& g) {
200 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
202 for (ItemIt it(g); it != INVALID; ++it) {
210 namespace _core_bits {
212 template <typename Graph, typename Enable = void>
213 struct CountNodesSelector {
214 static int count(const Graph &g) {
215 return countItems<Graph, typename Graph::Node>(g);
219 template <typename Graph>
220 struct CountNodesSelector<
222 enable_if<typename Graph::NodeNumTag, void>::type>
224 static int count(const Graph &g) {
230 /// \brief Function to count the nodes in the graph.
232 /// This function counts the nodes in the graph.
233 /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
234 /// graph structures it is specialized to run in <em>O</em>(1).
236 /// \note If the graph contains a \c nodeNum() member function and a
237 /// \c NodeNumTag tag then this function calls directly the member
238 /// function to query the cardinality of the node set.
239 template <typename Graph>
240 inline int countNodes(const Graph& g) {
241 return _core_bits::CountNodesSelector<Graph>::count(g);
244 namespace _graph_utils_bits {
246 template <typename Graph, typename Enable = void>
247 struct CountRedNodesSelector {
248 static int count(const Graph &g) {
249 return countItems<Graph, typename Graph::RedNode>(g);
253 template <typename Graph>
254 struct CountRedNodesSelector<
256 enable_if<typename Graph::NodeNumTag, void>::type>
258 static int count(const Graph &g) {
264 /// \brief Function to count the red nodes in the graph.
266 /// This function counts the red nodes in the graph.
267 /// The complexity of the function is O(n) but for some
268 /// graph structures it is specialized to run in O(1).
270 /// If the graph contains a \e redNum() member function and a
271 /// \e NodeNumTag tag then this function calls directly the member
272 /// function to query the cardinality of the node set.
273 template <typename Graph>
274 inline int countRedNodes(const Graph& g) {
275 return _graph_utils_bits::CountRedNodesSelector<Graph>::count(g);
278 namespace _graph_utils_bits {
280 template <typename Graph, typename Enable = void>
281 struct CountBlueNodesSelector {
282 static int count(const Graph &g) {
283 return countItems<Graph, typename Graph::BlueNode>(g);
287 template <typename Graph>
288 struct CountBlueNodesSelector<
290 enable_if<typename Graph::NodeNumTag, void>::type>
292 static int count(const Graph &g) {
298 /// \brief Function to count the blue nodes in the graph.
300 /// This function counts the blue nodes in the graph.
301 /// The complexity of the function is O(n) but for some
302 /// graph structures it is specialized to run in O(1).
304 /// If the graph contains a \e blueNum() member function and a
305 /// \e NodeNumTag tag then this function calls directly the member
306 /// function to query the cardinality of the node set.
307 template <typename Graph>
308 inline int countBlueNodes(const Graph& g) {
309 return _graph_utils_bits::CountBlueNodesSelector<Graph>::count(g);
314 namespace _core_bits {
316 template <typename Graph, typename Enable = void>
317 struct CountArcsSelector {
318 static int count(const Graph &g) {
319 return countItems<Graph, typename Graph::Arc>(g);
323 template <typename Graph>
324 struct CountArcsSelector<
326 typename enable_if<typename Graph::ArcNumTag, void>::type>
328 static int count(const Graph &g) {
334 /// \brief Function to count the arcs in the graph.
336 /// This function counts the arcs in the graph.
337 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
338 /// graph structures it is specialized to run in <em>O</em>(1).
340 /// \note If the graph contains a \c arcNum() member function and a
341 /// \c ArcNumTag tag then this function calls directly the member
342 /// function to query the cardinality of the arc set.
343 template <typename Graph>
344 inline int countArcs(const Graph& g) {
345 return _core_bits::CountArcsSelector<Graph>::count(g);
350 namespace _core_bits {
352 template <typename Graph, typename Enable = void>
353 struct CountEdgesSelector {
354 static int count(const Graph &g) {
355 return countItems<Graph, typename Graph::Edge>(g);
359 template <typename Graph>
360 struct CountEdgesSelector<
362 typename enable_if<typename Graph::EdgeNumTag, void>::type>
364 static int count(const Graph &g) {
370 /// \brief Function to count the edges in the graph.
372 /// This function counts the edges in the graph.
373 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
374 /// graph structures it is specialized to run in <em>O</em>(1).
376 /// \note If the graph contains a \c edgeNum() member function and a
377 /// \c EdgeNumTag tag then this function calls directly the member
378 /// function to query the cardinality of the edge set.
379 template <typename Graph>
380 inline int countEdges(const Graph& g) {
381 return _core_bits::CountEdgesSelector<Graph>::count(g);
386 template <typename Graph, typename DegIt>
387 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
389 for (DegIt it(_g, _n); it != INVALID; ++it) {
395 /// \brief Function to count the number of the out-arcs from node \c n.
397 /// This function counts the number of the out-arcs from node \c n
398 /// in the graph \c g.
399 template <typename Graph>
400 inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
401 return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
404 /// \brief Function to count the number of the in-arcs to node \c n.
406 /// This function counts the number of the in-arcs to node \c n
407 /// in the graph \c g.
408 template <typename Graph>
409 inline int countInArcs(const Graph& g, const typename Graph::Node& n) {
410 return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
413 /// \brief Function to count the number of the inc-edges to node \c n.
415 /// This function counts the number of the inc-edges to node \c n
416 /// in the undirected graph \c g.
417 template <typename Graph>
418 inline int countIncEdges(const Graph& g, const typename Graph::Node& n) {
419 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
422 namespace _core_bits {
424 template <typename Digraph, typename Item, typename RefMap>
427 virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
429 virtual ~MapCopyBase() {}
432 template <typename Digraph, typename Item, typename RefMap,
433 typename FromMap, typename ToMap>
434 class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
437 MapCopy(const FromMap& map, ToMap& tmap)
438 : _map(map), _tmap(tmap) {}
440 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
441 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
442 for (ItemIt it(digraph); it != INVALID; ++it) {
443 _tmap.set(refMap[it], _map[it]);
452 template <typename Digraph, typename Item, typename RefMap, typename It>
453 class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
456 ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
458 virtual void copy(const Digraph&, const RefMap& refMap) {
467 template <typename Digraph, typename Item, typename RefMap, typename Ref>
468 class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
471 RefCopy(Ref& map) : _map(map) {}
473 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
474 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
475 for (ItemIt it(digraph); it != INVALID; ++it) {
476 _map.set(it, refMap[it]);
484 template <typename Digraph, typename Item, typename RefMap,
486 class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
489 CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
491 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
492 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
493 for (ItemIt it(digraph); it != INVALID; ++it) {
494 _cmap.set(refMap[it], it);
502 template <typename Digraph, typename Enable = void>
503 struct DigraphCopySelector {
504 template <typename From, typename NodeRefMap, typename ArcRefMap>
505 static void copy(const From& from, Digraph &to,
506 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
508 for (typename From::NodeIt it(from); it != INVALID; ++it) {
509 nodeRefMap[it] = to.addNode();
511 for (typename From::ArcIt it(from); it != INVALID; ++it) {
512 arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
513 nodeRefMap[from.target(it)]);
518 template <typename Digraph>
519 struct DigraphCopySelector<
521 typename enable_if<typename Digraph::BuildTag, void>::type>
523 template <typename From, typename NodeRefMap, typename ArcRefMap>
524 static void copy(const From& from, Digraph &to,
525 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
526 to.build(from, nodeRefMap, arcRefMap);
530 template <typename Graph, typename Enable = void>
531 struct GraphCopySelector {
532 template <typename From, typename NodeRefMap, typename EdgeRefMap>
533 static void copy(const From& from, Graph &to,
534 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
536 for (typename From::NodeIt it(from); it != INVALID; ++it) {
537 nodeRefMap[it] = to.addNode();
539 for (typename From::EdgeIt it(from); it != INVALID; ++it) {
540 edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
541 nodeRefMap[from.v(it)]);
546 template <typename Graph>
547 struct GraphCopySelector<
549 typename enable_if<typename Graph::BuildTag, void>::type>
551 template <typename From, typename NodeRefMap, typename EdgeRefMap>
552 static void copy(const From& from, Graph &to,
553 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
554 to.build(from, nodeRefMap, edgeRefMap);
560 /// \brief Check whether a graph is undirected.
562 /// This function returns \c true if the given graph is undirected.
564 template <typename GR>
565 bool undirected(const GR& g) { return false; }
567 template <typename GR>
568 typename enable_if<UndirectedTagIndicator<GR>, bool>::type
569 undirected(const GR&) {
572 template <typename GR>
573 typename disable_if<UndirectedTagIndicator<GR>, bool>::type
574 undirected(const GR&) {
579 /// \brief Class to copy a digraph.
581 /// Class to copy a digraph to another digraph (duplicate a digraph). The
582 /// simplest way of using it is through the \c digraphCopy() function.
584 /// This class not only make a copy of a digraph, but it can create
585 /// references and cross references between the nodes and arcs of
586 /// the two digraphs, and it can copy maps to use with the newly created
589 /// To make a copy from a digraph, first an instance of DigraphCopy
590 /// should be created, then the data belongs to the digraph should
591 /// assigned to copy. In the end, the \c run() member should be
594 /// The next code copies a digraph with several data:
596 /// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
597 /// // Create references for the nodes
598 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
600 /// // Create cross references (inverse) for the arcs
601 /// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
602 /// cg.arcCrossRef(acr);
603 /// // Copy an arc map
604 /// OrigGraph::ArcMap<double> oamap(orig_graph);
605 /// NewGraph::ArcMap<double> namap(new_graph);
606 /// cg.arcMap(oamap, namap);
608 /// OrigGraph::Node on;
609 /// NewGraph::Node nn;
611 /// // Execute copying
614 template <typename From, typename To>
618 typedef typename From::Node Node;
619 typedef typename From::NodeIt NodeIt;
620 typedef typename From::Arc Arc;
621 typedef typename From::ArcIt ArcIt;
623 typedef typename To::Node TNode;
624 typedef typename To::Arc TArc;
626 typedef typename From::template NodeMap<TNode> NodeRefMap;
627 typedef typename From::template ArcMap<TArc> ArcRefMap;
631 /// \brief Constructor of DigraphCopy.
633 /// Constructor of DigraphCopy for copying the content of the
634 /// \c from digraph into the \c to digraph.
635 DigraphCopy(const From& from, To& to)
636 : _from(from), _to(to) {}
638 /// \brief Destructor of DigraphCopy
640 /// Destructor of DigraphCopy.
642 for (int i = 0; i < int(_node_maps.size()); ++i) {
643 delete _node_maps[i];
645 for (int i = 0; i < int(_arc_maps.size()); ++i) {
651 /// \brief Copy the node references into the given map.
653 /// This function copies the node references into the given map.
654 /// The parameter should be a map, whose key type is the Node type of
655 /// the source digraph, while the value type is the Node type of the
656 /// destination digraph.
657 template <typename NodeRef>
658 DigraphCopy& nodeRef(NodeRef& map) {
659 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
660 NodeRefMap, NodeRef>(map));
664 /// \brief Copy the node cross references into the given map.
666 /// This function copies the node cross references (reverse references)
667 /// into the given map. The parameter should be a map, whose key type
668 /// is the Node type of the destination digraph, while the value type is
669 /// the Node type of the source digraph.
670 template <typename NodeCrossRef>
671 DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
672 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
673 NodeRefMap, NodeCrossRef>(map));
677 /// \brief Make a copy of the given node map.
679 /// This function makes a copy of the given node map for the newly
681 /// The key type of the new map \c tmap should be the Node type of the
682 /// destination digraph, and the key type of the original map \c map
683 /// should be the Node type of the source digraph.
684 template <typename FromMap, typename ToMap>
685 DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
686 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
687 NodeRefMap, FromMap, ToMap>(map, tmap));
691 /// \brief Make a copy of the given node.
693 /// This function makes a copy of the given node.
694 DigraphCopy& node(const Node& node, TNode& tnode) {
695 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
696 NodeRefMap, TNode>(node, tnode));
700 /// \brief Copy the arc references into the given map.
702 /// This function copies the arc references into the given map.
703 /// The parameter should be a map, whose key type is the Arc type of
704 /// the source digraph, while the value type is the Arc type of the
705 /// destination digraph.
706 template <typename ArcRef>
707 DigraphCopy& arcRef(ArcRef& map) {
708 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
709 ArcRefMap, ArcRef>(map));
713 /// \brief Copy the arc cross references into the given map.
715 /// This function copies the arc cross references (reverse references)
716 /// into the given map. The parameter should be a map, whose key type
717 /// is the Arc type of the destination digraph, while the value type is
718 /// the Arc type of the source digraph.
719 template <typename ArcCrossRef>
720 DigraphCopy& arcCrossRef(ArcCrossRef& map) {
721 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
722 ArcRefMap, ArcCrossRef>(map));
726 /// \brief Make a copy of the given arc map.
728 /// This function makes a copy of the given arc map for the newly
730 /// The key type of the new map \c tmap should be the Arc type of the
731 /// destination digraph, and the key type of the original map \c map
732 /// should be the Arc type of the source digraph.
733 template <typename FromMap, typename ToMap>
734 DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
735 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
736 ArcRefMap, FromMap, ToMap>(map, tmap));
740 /// \brief Make a copy of the given arc.
742 /// This function makes a copy of the given arc.
743 DigraphCopy& arc(const Arc& arc, TArc& tarc) {
744 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
745 ArcRefMap, TArc>(arc, tarc));
749 /// \brief Execute copying.
751 /// This function executes the copying of the digraph along with the
752 /// copying of the assigned data.
754 NodeRefMap nodeRefMap(_from);
755 ArcRefMap arcRefMap(_from);
756 _core_bits::DigraphCopySelector<To>::
757 copy(_from, _to, nodeRefMap, arcRefMap);
758 for (int i = 0; i < int(_node_maps.size()); ++i) {
759 _node_maps[i]->copy(_from, nodeRefMap);
761 for (int i = 0; i < int(_arc_maps.size()); ++i) {
762 _arc_maps[i]->copy(_from, arcRefMap);
771 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
774 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
779 /// \brief Copy a digraph to another digraph.
781 /// This function copies a digraph to another digraph.
782 /// The complete usage of it is detailed in the DigraphCopy class, but
783 /// a short example shows a basic work:
785 /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
788 /// After the copy the \c nr map will contain the mapping from the
789 /// nodes of the \c from digraph to the nodes of the \c to digraph and
790 /// \c acr will contain the mapping from the arcs of the \c to digraph
791 /// to the arcs of the \c from digraph.
794 template <typename From, typename To>
795 DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
796 return DigraphCopy<From, To>(from, to);
799 /// \brief Class to copy a graph.
801 /// Class to copy a graph to another graph (duplicate a graph). The
802 /// simplest way of using it is through the \c graphCopy() function.
804 /// This class not only make a copy of a graph, but it can create
805 /// references and cross references between the nodes, edges and arcs of
806 /// the two graphs, and it can copy maps for using with the newly created
809 /// To make a copy from a graph, first an instance of GraphCopy
810 /// should be created, then the data belongs to the graph should
811 /// assigned to copy. In the end, the \c run() member should be
814 /// The next code copies a graph with several data:
816 /// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
817 /// // Create references for the nodes
818 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
820 /// // Create cross references (inverse) for the edges
821 /// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
822 /// cg.edgeCrossRef(ecr);
823 /// // Copy an edge map
824 /// OrigGraph::EdgeMap<double> oemap(orig_graph);
825 /// NewGraph::EdgeMap<double> nemap(new_graph);
826 /// cg.edgeMap(oemap, nemap);
828 /// OrigGraph::Node on;
829 /// NewGraph::Node nn;
831 /// // Execute copying
834 template <typename From, typename To>
838 typedef typename From::Node Node;
839 typedef typename From::NodeIt NodeIt;
840 typedef typename From::Arc Arc;
841 typedef typename From::ArcIt ArcIt;
842 typedef typename From::Edge Edge;
843 typedef typename From::EdgeIt EdgeIt;
845 typedef typename To::Node TNode;
846 typedef typename To::Arc TArc;
847 typedef typename To::Edge TEdge;
849 typedef typename From::template NodeMap<TNode> NodeRefMap;
850 typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
853 ArcRefMap(const From& from, const To& to,
854 const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
855 : _from(from), _to(to),
856 _edge_ref(edge_ref), _node_ref(node_ref) {}
858 typedef typename From::Arc Key;
859 typedef typename To::Arc Value;
861 Value operator[](const Key& key) const {
862 bool forward = _from.u(key) != _from.v(key) ?
863 _node_ref[_from.source(key)] ==
864 _to.source(_to.direct(_edge_ref[key], true)) :
865 _from.direction(key);
866 return _to.direct(_edge_ref[key], forward);
871 const EdgeRefMap& _edge_ref;
872 const NodeRefMap& _node_ref;
877 /// \brief Constructor of GraphCopy.
879 /// Constructor of GraphCopy for copying the content of the
880 /// \c from graph into the \c to graph.
881 GraphCopy(const From& from, To& to)
882 : _from(from), _to(to) {}
884 /// \brief Destructor of GraphCopy
886 /// Destructor of GraphCopy.
888 for (int i = 0; i < int(_node_maps.size()); ++i) {
889 delete _node_maps[i];
891 for (int i = 0; i < int(_arc_maps.size()); ++i) {
894 for (int i = 0; i < int(_edge_maps.size()); ++i) {
895 delete _edge_maps[i];
899 /// \brief Copy the node references into the given map.
901 /// This function copies the node references into the given map.
902 /// The parameter should be a map, whose key type is the Node type of
903 /// the source graph, while the value type is the Node type of the
904 /// destination graph.
905 template <typename NodeRef>
906 GraphCopy& nodeRef(NodeRef& map) {
907 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
908 NodeRefMap, NodeRef>(map));
912 /// \brief Copy the node cross references into the given map.
914 /// This function copies the node cross references (reverse references)
915 /// into the given map. The parameter should be a map, whose key type
916 /// is the Node type of the destination graph, while the value type is
917 /// the Node type of the source graph.
918 template <typename NodeCrossRef>
919 GraphCopy& nodeCrossRef(NodeCrossRef& map) {
920 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
921 NodeRefMap, NodeCrossRef>(map));
925 /// \brief Make a copy of the given node map.
927 /// This function makes a copy of the given node map for the newly
929 /// The key type of the new map \c tmap should be the Node type of the
930 /// destination graph, and the key type of the original map \c map
931 /// should be the Node type of the source graph.
932 template <typename FromMap, typename ToMap>
933 GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
934 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
935 NodeRefMap, FromMap, ToMap>(map, tmap));
939 /// \brief Make a copy of the given node.
941 /// This function makes a copy of the given node.
942 GraphCopy& node(const Node& node, TNode& tnode) {
943 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
944 NodeRefMap, TNode>(node, tnode));
948 /// \brief Copy the arc references into the given map.
950 /// This function copies the arc references into the given map.
951 /// The parameter should be a map, whose key type is the Arc type of
952 /// the source graph, while the value type is the Arc type of the
953 /// destination graph.
954 template <typename ArcRef>
955 GraphCopy& arcRef(ArcRef& map) {
956 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
957 ArcRefMap, ArcRef>(map));
961 /// \brief Copy the arc cross references into the given map.
963 /// This function copies the arc cross references (reverse references)
964 /// into the given map. The parameter should be a map, whose key type
965 /// is the Arc type of the destination graph, while the value type is
966 /// the Arc type of the source graph.
967 template <typename ArcCrossRef>
968 GraphCopy& arcCrossRef(ArcCrossRef& map) {
969 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
970 ArcRefMap, ArcCrossRef>(map));
974 /// \brief Make a copy of the given arc map.
976 /// This function makes a copy of the given arc map for the newly
978 /// The key type of the new map \c tmap should be the Arc type of the
979 /// destination graph, and the key type of the original map \c map
980 /// should be the Arc type of the source graph.
981 template <typename FromMap, typename ToMap>
982 GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
983 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
984 ArcRefMap, FromMap, ToMap>(map, tmap));
988 /// \brief Make a copy of the given arc.
990 /// This function makes a copy of the given arc.
991 GraphCopy& arc(const Arc& arc, TArc& tarc) {
992 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
993 ArcRefMap, TArc>(arc, tarc));
997 /// \brief Copy the edge references into the given map.
999 /// This function copies the edge references into the given map.
1000 /// The parameter should be a map, whose key type is the Edge type of
1001 /// the source graph, while the value type is the Edge type of the
1002 /// destination graph.
1003 template <typename EdgeRef>
1004 GraphCopy& edgeRef(EdgeRef& map) {
1005 _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
1006 EdgeRefMap, EdgeRef>(map));
1010 /// \brief Copy the edge cross references into the given map.
1012 /// This function copies the edge cross references (reverse references)
1013 /// into the given map. The parameter should be a map, whose key type
1014 /// is the Edge type of the destination graph, while the value type is
1015 /// the Edge type of the source graph.
1016 template <typename EdgeCrossRef>
1017 GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1018 _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
1019 Edge, EdgeRefMap, EdgeCrossRef>(map));
1023 /// \brief Make a copy of the given edge map.
1025 /// This function makes a copy of the given edge map for the newly
1027 /// The key type of the new map \c tmap should be the Edge type of the
1028 /// destination graph, and the key type of the original map \c map
1029 /// should be the Edge type of the source graph.
1030 template <typename FromMap, typename ToMap>
1031 GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
1032 _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
1033 EdgeRefMap, FromMap, ToMap>(map, tmap));
1037 /// \brief Make a copy of the given edge.
1039 /// This function makes a copy of the given edge.
1040 GraphCopy& edge(const Edge& edge, TEdge& tedge) {
1041 _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
1042 EdgeRefMap, TEdge>(edge, tedge));
1046 /// \brief Execute copying.
1048 /// This function executes the copying of the graph along with the
1049 /// copying of the assigned data.
1051 NodeRefMap nodeRefMap(_from);
1052 EdgeRefMap edgeRefMap(_from);
1053 ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
1054 _core_bits::GraphCopySelector<To>::
1055 copy(_from, _to, nodeRefMap, edgeRefMap);
1056 for (int i = 0; i < int(_node_maps.size()); ++i) {
1057 _node_maps[i]->copy(_from, nodeRefMap);
1059 for (int i = 0; i < int(_edge_maps.size()); ++i) {
1060 _edge_maps[i]->copy(_from, edgeRefMap);
1062 for (int i = 0; i < int(_arc_maps.size()); ++i) {
1063 _arc_maps[i]->copy(_from, arcRefMap);
1072 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
1075 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
1078 std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
1083 /// \brief Copy a graph to another graph.
1085 /// This function copies a graph to another graph.
1086 /// The complete usage of it is detailed in the GraphCopy class,
1087 /// but a short example shows a basic work:
1089 /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
1092 /// After the copy the \c nr map will contain the mapping from the
1093 /// nodes of the \c from graph to the nodes of the \c to graph and
1094 /// \c ecr will contain the mapping from the edges of the \c to graph
1095 /// to the edges of the \c from graph.
1098 template <typename From, typename To>
1100 graphCopy(const From& from, To& to) {
1101 return GraphCopy<From, To>(from, to);
1104 namespace _core_bits {
1106 template <typename Graph, typename Enable = void>
1107 struct FindArcSelector {
1108 typedef typename Graph::Node Node;
1109 typedef typename Graph::Arc Arc;
1110 static Arc find(const Graph &g, Node u, Node v, Arc e) {
1116 while (e != INVALID && g.target(e) != v) {
1123 template <typename Graph>
1124 struct FindArcSelector<
1126 typename enable_if<typename Graph::FindArcTag, void>::type>
1128 typedef typename Graph::Node Node;
1129 typedef typename Graph::Arc Arc;
1130 static Arc find(const Graph &g, Node u, Node v, Arc prev) {
1131 return g.findArc(u, v, prev);
1136 /// \brief Find an arc between two nodes of a digraph.
1138 /// This function finds an arc from node \c u to node \c v in the
1141 /// If \c prev is \ref INVALID (this is the default value), then
1142 /// it finds the first arc from \c u to \c v. Otherwise it looks for
1143 /// the next arc from \c u to \c v after \c prev.
1144 /// \return The found arc or \ref INVALID if there is no such an arc.
1146 /// Thus you can iterate through each arc from \c u to \c v as it follows.
1148 /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
1153 /// \note \ref ConArcIt provides iterator interface for the same
1157 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1158 template <typename Graph>
1159 inline typename Graph::Arc
1160 findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1161 typename Graph::Arc prev = INVALID) {
1162 return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
1165 /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
1167 /// Iterator for iterating on parallel arcs connecting the same nodes. It is
1168 /// a higher level interface for the \ref findArc() function. You can
1169 /// use it the following way:
1171 /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
1177 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1178 template <typename GR>
1179 class ConArcIt : public GR::Arc {
1180 typedef typename GR::Arc Parent;
1184 typedef typename GR::Arc Arc;
1185 typedef typename GR::Node Node;
1187 /// \brief Constructor.
1189 /// Construct a new ConArcIt iterating on the arcs that
1190 /// connects nodes \c u and \c v.
1191 ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
1192 Parent::operator=(findArc(_graph, u, v));
1195 /// \brief Constructor.
1197 /// Construct a new ConArcIt that continues the iterating from arc \c a.
1198 ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
1200 /// \brief Increment operator.
1202 /// It increments the iterator and gives back the next arc.
1203 ConArcIt& operator++() {
1204 Parent::operator=(findArc(_graph, _graph.source(*this),
1205 _graph.target(*this), *this));
1212 namespace _core_bits {
1214 template <typename Graph, typename Enable = void>
1215 struct FindEdgeSelector {
1216 typedef typename Graph::Node Node;
1217 typedef typename Graph::Edge Edge;
1218 static Edge find(const Graph &g, Node u, Node v, Edge e) {
1222 g.firstInc(e, b, u);
1227 while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
1232 g.firstInc(e, b, u);
1237 while (e != INVALID && (!b || g.v(e) != v)) {
1245 template <typename Graph>
1246 struct FindEdgeSelector<
1248 typename enable_if<typename Graph::FindEdgeTag, void>::type>
1250 typedef typename Graph::Node Node;
1251 typedef typename Graph::Edge Edge;
1252 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
1253 return g.findEdge(u, v, prev);
1258 /// \brief Find an edge between two nodes of a graph.
1260 /// This function finds an edge from node \c u to node \c v in graph \c g.
1261 /// If node \c u and node \c v is equal then each loop edge
1262 /// will be enumerated once.
1264 /// If \c prev is \ref INVALID (this is the default value), then
1265 /// it finds the first edge from \c u to \c v. Otherwise it looks for
1266 /// the next edge from \c u to \c v after \c prev.
1267 /// \return The found edge or \ref INVALID if there is no such an edge.
1269 /// Thus you can iterate through each edge between \c u and \c v
1272 /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
1277 /// \note \ref ConEdgeIt provides iterator interface for the same
1281 template <typename Graph>
1282 inline typename Graph::Edge
1283 findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1284 typename Graph::Edge p = INVALID) {
1285 return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
1288 /// \brief Iterator for iterating on parallel edges connecting the same nodes.
1290 /// Iterator for iterating on parallel edges connecting the same nodes.
1291 /// It is a higher level interface for the findEdge() function. You can
1292 /// use it the following way:
1294 /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
1300 template <typename GR>
1301 class ConEdgeIt : public GR::Edge {
1302 typedef typename GR::Edge Parent;
1306 typedef typename GR::Edge Edge;
1307 typedef typename GR::Node Node;
1309 /// \brief Constructor.
1311 /// Construct a new ConEdgeIt iterating on the edges that
1312 /// connects nodes \c u and \c v.
1313 ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1314 Parent::operator=(findEdge(_graph, _u, _v));
1317 /// \brief Constructor.
1319 /// Construct a new ConEdgeIt that continues iterating from edge \c e.
1320 ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
1322 /// \brief Increment operator.
1324 /// It increments the iterator and gives back the next edge.
1325 ConEdgeIt& operator++() {
1326 Parent::operator=(findEdge(_graph, _u, _v, *this));
1335 ///Dynamic arc look-up between given endpoints.
1337 ///Using this class, you can find an arc in a digraph from a given
1338 ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
1339 ///where <em>d</em> is the out-degree of the source node.
1341 ///It is possible to find \e all parallel arcs between two nodes with
1342 ///the \c operator() member.
1344 ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
1345 ///\ref AllArcLookUp if your digraph is not changed so frequently.
1347 ///This class uses a self-adjusting binary search tree, the Splay tree
1348 ///of Sleator and Tarjan to guarantee the logarithmic amortized
1349 ///time bound for arc look-ups. This class also guarantees the
1350 ///optimal time bound in a constant factor for any distribution of
1353 ///\tparam GR The type of the underlying digraph.
1357 template <typename GR>
1359 : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
1361 typedef typename ItemSetTraits<GR, typename GR::Arc>
1362 ::ItemNotifier::ObserverBase Parent;
1364 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1368 /// The Digraph type
1373 class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type
1375 typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
1379 AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
1381 virtual void add(const Node& node) {
1383 Parent::set(node, INVALID);
1386 virtual void add(const std::vector<Node>& nodes) {
1388 for (int i = 0; i < int(nodes.size()); ++i) {
1389 Parent::set(nodes[i], INVALID);
1393 virtual void build() {
1396 typename Parent::Notifier* nf = Parent::notifier();
1397 for (nf->first(it); it != INVALID; nf->next(it)) {
1398 Parent::set(it, INVALID);
1406 ArcLess(const Digraph &_g) : g(_g) {}
1407 bool operator()(Arc a,Arc b) const
1409 return g.target(a)<g.target(b);
1417 typename Digraph::template ArcMap<Arc> _parent;
1418 typename Digraph::template ArcMap<Arc> _left;
1419 typename Digraph::template ArcMap<Arc> _right;
1427 ///It builds up the search database.
1428 DynArcLookUp(const Digraph &g)
1429 : _g(g),_head(g),_parent(g),_left(g),_right(g)
1431 Parent::attach(_g.notifier(typename Digraph::Arc()));
1437 virtual void add(const Arc& arc) {
1441 virtual void add(const std::vector<Arc>& arcs) {
1442 for (int i = 0; i < int(arcs.size()); ++i) {
1447 virtual void erase(const Arc& arc) {
1451 virtual void erase(const std::vector<Arc>& arcs) {
1452 for (int i = 0; i < int(arcs.size()); ++i) {
1457 virtual void build() {
1461 virtual void clear() {
1462 for(NodeIt n(_g);n!=INVALID;++n) {
1467 void insert(Arc arc) {
1468 Node s = _g.source(arc);
1469 Node t = _g.target(arc);
1470 _left[arc] = INVALID;
1471 _right[arc] = INVALID;
1476 _parent[arc] = INVALID;
1480 if (t < _g.target(e)) {
1481 if (_left[e] == INVALID) {
1490 if (_right[e] == INVALID) {
1502 void remove(Arc arc) {
1503 if (_left[arc] == INVALID) {
1504 if (_right[arc] != INVALID) {
1505 _parent[_right[arc]] = _parent[arc];
1507 if (_parent[arc] != INVALID) {
1508 if (_left[_parent[arc]] == arc) {
1509 _left[_parent[arc]] = _right[arc];
1511 _right[_parent[arc]] = _right[arc];
1514 _head[_g.source(arc)] = _right[arc];
1516 } else if (_right[arc] == INVALID) {
1517 _parent[_left[arc]] = _parent[arc];
1518 if (_parent[arc] != INVALID) {
1519 if (_left[_parent[arc]] == arc) {
1520 _left[_parent[arc]] = _left[arc];
1522 _right[_parent[arc]] = _left[arc];
1525 _head[_g.source(arc)] = _left[arc];
1529 if (_right[e] != INVALID) {
1531 while (_right[e] != INVALID) {
1535 _right[_parent[e]] = _left[e];
1536 if (_left[e] != INVALID) {
1537 _parent[_left[e]] = _parent[e];
1540 _left[e] = _left[arc];
1541 _parent[_left[arc]] = e;
1542 _right[e] = _right[arc];
1543 _parent[_right[arc]] = e;
1545 _parent[e] = _parent[arc];
1546 if (_parent[arc] != INVALID) {
1547 if (_left[_parent[arc]] == arc) {
1548 _left[_parent[arc]] = e;
1550 _right[_parent[arc]] = e;
1555 _right[e] = _right[arc];
1556 _parent[_right[arc]] = e;
1557 _parent[e] = _parent[arc];
1559 if (_parent[arc] != INVALID) {
1560 if (_left[_parent[arc]] == arc) {
1561 _left[_parent[arc]] = e;
1563 _right[_parent[arc]] = e;
1566 _head[_g.source(arc)] = e;
1572 Arc refreshRec(std::vector<Arc> &v,int a,int b)
1577 Arc left = refreshRec(v,a,m-1);
1581 _left[me] = INVALID;
1584 Arc right = refreshRec(v,m+1,b);
1586 _parent[right] = me;
1588 _right[me] = INVALID;
1594 for(NodeIt n(_g);n!=INVALID;++n) {
1596 for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
1598 std::sort(v.begin(),v.end(),ArcLess(_g));
1599 Arc head = refreshRec(v,0,v.size()-1);
1601 _parent[head] = INVALID;
1603 else _head[n] = INVALID;
1609 _parent[v] = _parent[w];
1611 _left[w] = _right[v];
1613 if (_parent[v] != INVALID) {
1614 if (_right[_parent[v]] == w) {
1615 _right[_parent[v]] = v;
1617 _left[_parent[v]] = v;
1620 if (_left[w] != INVALID){
1621 _parent[_left[w]] = w;
1627 _parent[v] = _parent[w];
1629 _right[w] = _left[v];
1631 if (_parent[v] != INVALID){
1632 if (_left[_parent[v]] == w) {
1633 _left[_parent[v]] = v;
1635 _right[_parent[v]] = v;
1638 if (_right[w] != INVALID){
1639 _parent[_right[w]] = w;
1644 while (_parent[v] != INVALID) {
1645 if (v == _left[_parent[v]]) {
1646 if (_parent[_parent[v]] == INVALID) {
1649 if (_parent[v] == _left[_parent[_parent[v]]]) {
1658 if (_parent[_parent[v]] == INVALID) {
1661 if (_parent[v] == _left[_parent[_parent[v]]]) {
1671 _head[_g.source(v)] = v;
1677 ///Find an arc between two nodes.
1679 ///Find an arc between two nodes.
1680 ///\param s The source node.
1681 ///\param t The target node.
1682 ///\param p The previous arc between \c s and \c t. It it is INVALID or
1683 ///not given, the operator finds the first appropriate arc.
1684 ///\return An arc from \c s to \c t after \c p or
1685 ///\ref INVALID if there is no more.
1687 ///For example, you can count the number of arcs from \c u to \c v in the
1690 ///DynArcLookUp<ListDigraph> ae(g);
1693 ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
1696 ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
1697 ///amortized time, specifically, the time complexity of the lookups
1698 ///is equal to the optimal search tree implementation for the
1699 ///current query distribution in a constant factor.
1701 ///\note This is a dynamic data structure, therefore the data
1702 ///structure is updated after each graph alteration. Thus although
1703 ///this data structure is theoretically faster than \ref ArcLookUp
1704 ///and \ref AllArcLookUp, it often provides worse performance than
1706 Arc operator()(Node s, Node t, Arc p = INVALID) const {
1709 if (a == INVALID) return INVALID;
1712 if (_g.target(a) < t) {
1713 if (_right[a] == INVALID) {
1714 const_cast<DynArcLookUp&>(*this).splay(a);
1720 if (_g.target(a) == t) {
1723 if (_left[a] == INVALID) {
1724 const_cast<DynArcLookUp&>(*this).splay(a);
1733 if (_right[a] != INVALID) {
1735 while (_left[a] != INVALID) {
1738 const_cast<DynArcLookUp&>(*this).splay(a);
1740 while (_parent[a] != INVALID && _right[_parent[a]] == a) {
1743 if (_parent[a] == INVALID) {
1747 const_cast<DynArcLookUp&>(*this).splay(a);
1750 if (_g.target(a) == t) return a;
1751 else return INVALID;
1757 ///Fast arc look-up between given endpoints.
1759 ///Using this class, you can find an arc in a digraph from a given
1760 ///source to a given target in time <em>O</em>(log<em>d</em>),
1761 ///where <em>d</em> is the out-degree of the source node.
1763 ///It is not possible to find \e all parallel arcs between two nodes.
1764 ///Use \ref AllArcLookUp for this purpose.
1766 ///\warning This class is static, so you should call refresh() (or at
1767 ///least refresh(Node)) to refresh this data structure whenever the
1768 ///digraph changes. This is a time consuming (superlinearly proportional
1769 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1771 ///\tparam GR The type of the underlying digraph.
1778 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1782 /// The Digraph type
1787 typename Digraph::template NodeMap<Arc> _head;
1788 typename Digraph::template ArcMap<Arc> _left;
1789 typename Digraph::template ArcMap<Arc> _right;
1794 ArcLess(const Digraph &_g) : g(_g) {}
1795 bool operator()(Arc a,Arc b) const
1797 return g.target(a)<g.target(b);
1807 ///It builds up the search database, which remains valid until the digraph
1809 ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1812 Arc refreshRec(std::vector<Arc> &v,int a,int b)
1816 _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
1817 _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
1821 ///Refresh the search data structure at a node.
1823 ///Build up the search database of node \c n.
1825 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
1826 ///is the number of the outgoing arcs of \c n.
1827 void refresh(Node n)
1830 for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1832 std::sort(v.begin(),v.end(),ArcLess(_g));
1833 _head[n]=refreshRec(v,0,v.size()-1);
1835 else _head[n]=INVALID;
1837 ///Refresh the full data structure.
1839 ///Build up the full search database. In fact, it simply calls
1840 ///\ref refresh(Node) "refresh(n)" for each node \c n.
1842 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1843 ///the number of the arcs in the digraph and <em>D</em> is the maximum
1844 ///out-degree of the digraph.
1847 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1850 ///Find an arc between two nodes.
1852 ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
1853 ///where <em>d</em> is the number of outgoing arcs of \c s.
1854 ///\param s The source node.
1855 ///\param t The target node.
1856 ///\return An arc from \c s to \c t if there exists,
1857 ///\ref INVALID otherwise.
1859 ///\warning If you change the digraph, refresh() must be called before using
1860 ///this operator. If you change the outgoing arcs of
1861 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1862 Arc operator()(Node s, Node t) const
1866 e!=INVALID&&_g.target(e)!=t;
1867 e = t < _g.target(e)?_left[e]:_right[e]) ;
1873 ///Fast look-up of all arcs between given endpoints.
1875 ///This class is the same as \ref ArcLookUp, with the addition
1876 ///that it makes it possible to find all parallel arcs between given
1879 ///\warning This class is static, so you should call refresh() (or at
1880 ///least refresh(Node)) to refresh this data structure whenever the
1881 ///digraph changes. This is a time consuming (superlinearly proportional
1882 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1884 ///\tparam GR The type of the underlying digraph.
1889 class AllArcLookUp : public ArcLookUp<GR>
1891 using ArcLookUp<GR>::_g;
1892 using ArcLookUp<GR>::_right;
1893 using ArcLookUp<GR>::_left;
1894 using ArcLookUp<GR>::_head;
1896 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1898 typename GR::template ArcMap<Arc> _next;
1900 Arc refreshNext(Arc head,Arc next=INVALID)
1902 if(head==INVALID) return next;
1904 next=refreshNext(_right[head],next);
1905 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1907 return refreshNext(_left[head],head);
1913 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
1918 /// The Digraph type
1925 ///It builds up the search database, which remains valid until the digraph
1927 AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
1929 ///Refresh the data structure at a node.
1931 ///Build up the search database of node \c n.
1933 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
1934 ///the number of the outgoing arcs of \c n.
1935 void refresh(Node n)
1937 ArcLookUp<GR>::refresh(n);
1938 refreshNext(_head[n]);
1941 ///Refresh the full data structure.
1943 ///Build up the full search database. In fact, it simply calls
1944 ///\ref refresh(Node) "refresh(n)" for each node \c n.
1946 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1947 ///the number of the arcs in the digraph and <em>D</em> is the maximum
1948 ///out-degree of the digraph.
1951 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
1954 ///Find an arc between two nodes.
1956 ///Find an arc between two nodes.
1957 ///\param s The source node.
1958 ///\param t The target node.
1959 ///\param prev The previous arc between \c s and \c t. It it is INVALID or
1960 ///not given, the operator finds the first appropriate arc.
1961 ///\return An arc from \c s to \c t after \c prev or
1962 ///\ref INVALID if there is no more.
1964 ///For example, you can count the number of arcs from \c u to \c v in the
1967 ///AllArcLookUp<ListDigraph> ae(g);
1970 ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
1973 ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
1974 ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
1975 ///consecutive arcs are found in constant time.
1977 ///\warning If you change the digraph, refresh() must be called before using
1978 ///this operator. If you change the outgoing arcs of
1979 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1981 Arc operator()(Node s, Node t, Arc prev=INVALID) const
1988 e!=INVALID&&_g.target(e)!=t;
1989 e = t < _g.target(e)?_left[e]:_right[e]) ;
1999 else return _next[prev];