1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
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_EDGE_SET_H
20 #define LEMON_EDGE_SET_H
22 #include <lemon/core.h>
23 #include <lemon/bits/edge_set_extender.h>
27 /// \brief ArcSet and EdgeSet classes.
29 /// Graphs which use another graph's node-set as own.
32 template <typename GR>
33 class ListArcSetBase {
36 typedef typename GR::Node Node;
37 typedef typename GR::NodeIt NodeIt;
42 int first_out, first_in;
43 NodeT() : first_out(-1), first_in(-1) {}
46 typedef typename ItemSetTraits<GR, Node>::
47 template Map<NodeT>::Type NodesImplBase;
49 NodesImplBase* _nodes;
53 int next_out, next_in;
54 int prev_out, prev_in;
55 ArcT() : prev_out(-1), prev_in(-1) {}
58 std::vector<ArcT> arcs;
65 void initalize(const GR& graph, NodesImplBase& nodes) {
73 friend class ListArcSetBase<GR>;
75 Arc(int _id) : id(_id) {}
79 Arc(Invalid) : id(-1) {}
80 bool operator==(const Arc& arc) const { return id == arc.id; }
81 bool operator!=(const Arc& arc) const { return id != arc.id; }
82 bool operator<(const Arc& arc) const { return id < arc.id; }
85 ListArcSetBase() : first_arc(-1), first_free_arc(-1) {}
89 "This graph structure does not support node insertion");
90 return INVALID; // avoid warning
93 Arc addArc(const Node& u, const Node& v) {
95 if (first_free_arc == -1) {
97 arcs.push_back(ArcT());
100 first_free_arc = arcs[first_free_arc].next_in;
102 arcs[n].next_in = (*_nodes)[v].first_in;
103 if ((*_nodes)[v].first_in != -1) {
104 arcs[(*_nodes)[v].first_in].prev_in = n;
106 (*_nodes)[v].first_in = n;
107 arcs[n].next_out = (*_nodes)[u].first_out;
108 if ((*_nodes)[u].first_out != -1) {
109 arcs[(*_nodes)[u].first_out].prev_out = n;
111 (*_nodes)[u].first_out = n;
117 void erase(const Arc& arc) {
119 if (arcs[n].prev_in != -1) {
120 arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
122 (*_nodes)[arcs[n].target].first_in = arcs[n].next_in;
124 if (arcs[n].next_in != -1) {
125 arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
128 if (arcs[n].prev_out != -1) {
129 arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
131 (*_nodes)[arcs[n].source].first_out = arcs[n].next_out;
133 if (arcs[n].next_out != -1) {
134 arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
141 for (first(node); node != INVALID; next(node)) {
142 (*_nodes)[node].first_in = -1;
143 (*_nodes)[node].first_out = -1;
150 void first(Node& node) const {
154 void next(Node& node) const {
158 void first(Arc& arc) const {
161 while (node != INVALID && (*_nodes)[node].first_in == -1) {
164 arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
167 void next(Arc& arc) const {
168 if (arcs[arc.id].next_in != -1) {
169 arc.id = arcs[arc.id].next_in;
171 Node node = arcs[arc.id].target;
173 while (node != INVALID && (*_nodes)[node].first_in == -1) {
176 arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in;
180 void firstOut(Arc& arc, const Node& node) const {
181 arc.id = (*_nodes)[node].first_out;
184 void nextOut(Arc& arc) const {
185 arc.id = arcs[arc.id].next_out;
188 void firstIn(Arc& arc, const Node& node) const {
189 arc.id = (*_nodes)[node].first_in;
192 void nextIn(Arc& arc) const {
193 arc.id = arcs[arc.id].next_in;
196 int id(const Node& node) const { return _graph->id(node); }
197 int id(const Arc& arc) const { return arc.id; }
199 Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
200 Arc arcFromId(int ix) const { return Arc(ix); }
202 int maxNodeId() const { return _graph->maxNodeId(); };
203 int maxArcId() const { return arcs.size() - 1; }
205 Node source(const Arc& arc) const { return arcs[arc.id].source;}
206 Node target(const Arc& arc) const { return arcs[arc.id].target;}
208 typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
210 NodeNotifier& notifier(Node) const {
211 return _graph->notifier(Node());
214 template <typename V>
215 class NodeMap : public GR::template NodeMap<V> {
216 typedef typename GR::template NodeMap<V> Parent;
220 explicit NodeMap(const ListArcSetBase<GR>& arcset)
221 : Parent(*arcset._graph) {}
223 NodeMap(const ListArcSetBase<GR>& arcset, const V& value)
224 : Parent(*arcset._graph, value) {}
226 NodeMap& operator=(const NodeMap& cmap) {
227 return operator=<NodeMap>(cmap);
230 template <typename CMap>
231 NodeMap& operator=(const CMap& cmap) {
232 Parent::operator=(cmap);
241 /// \brief Digraph using a node set of another digraph or graph and
244 /// This structure can be used to establish another directed graph
245 /// over a node set of an existing one. This class uses the same
246 /// Node type as the underlying graph, and each valid node of the
247 /// original graph is valid in this arc set, therefore the node
248 /// objects of the original graph can be used directly with this
249 /// class. The node handling functions (id handling, observing, and
250 /// iterators) works equivalently as in the original graph.
252 /// This implementation is based on doubly-linked lists, from each
253 /// node the outgoing and the incoming arcs make up lists, therefore
254 /// one arc can be erased in constant time. It also makes possible,
255 /// that node can be removed from the underlying graph, in this case
256 /// all arcs incident to the given node is erased from the arc set.
258 /// \param GR The type of the graph which shares its node set with
259 /// this class. Its interface must conform to the
260 /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
263 /// This class fully conforms to the \ref concepts::Digraph
264 /// "Digraph" concept.
265 template <typename GR>
266 class ListArcSet : public ArcSetExtender<ListArcSetBase<GR> > {
267 typedef ArcSetExtender<ListArcSetBase<GR> > Parent;
271 typedef typename Parent::Node Node;
272 typedef typename Parent::Arc Arc;
274 typedef typename Parent::NodesImplBase NodesImplBase;
276 void eraseNode(const Node& node) {
278 Parent::firstOut(arc, node);
279 while (arc != INVALID ) {
281 Parent::firstOut(arc, node);
284 Parent::firstIn(arc, node);
285 while (arc != INVALID ) {
287 Parent::firstIn(arc, node);
295 class NodesImpl : public NodesImplBase {
296 typedef NodesImplBase Parent;
299 NodesImpl(const GR& graph, ListArcSet& arcset)
300 : Parent(graph), _arcset(arcset) {}
302 virtual ~NodesImpl() {}
306 virtual void erase(const Node& node) {
307 _arcset.eraseNode(node);
310 virtual void erase(const std::vector<Node>& nodes) {
311 for (int i = 0; i < int(nodes.size()); ++i) {
312 _arcset.eraseNode(nodes[i]);
314 Parent::erase(nodes);
316 virtual void clear() {
317 _arcset.clearNodes();
329 /// \brief Constructor of the ArcSet.
331 /// Constructor of the ArcSet.
332 ListArcSet(const GR& graph) : _nodes(graph, *this) {
333 Parent::initalize(graph, _nodes);
336 /// \brief Add a new arc to the digraph.
338 /// Add a new arc to the digraph with source node \c s
339 /// and target node \c t.
340 /// \return The new arc.
341 Arc addArc(const Node& s, const Node& t) {
342 return Parent::addArc(s, t);
345 /// \brief Erase an arc from the digraph.
347 /// Erase an arc \c a from the digraph.
348 void erase(const Arc& a) {
349 return Parent::erase(a);
354 template <typename GR>
355 class ListEdgeSetBase {
358 typedef typename GR::Node Node;
359 typedef typename GR::NodeIt NodeIt;
365 NodeT() : first_out(-1) {}
368 typedef typename ItemSetTraits<GR, Node>::
369 template Map<NodeT>::Type NodesImplBase;
371 NodesImplBase* _nodes;
375 int prev_out, next_out;
376 ArcT() : prev_out(-1), next_out(-1) {}
379 std::vector<ArcT> arcs;
386 void initalize(const GR& graph, NodesImplBase& nodes) {
394 friend class ListEdgeSetBase;
398 explicit Edge(int _id) { id = _id;}
402 Edge (Invalid) { id = -1; }
403 bool operator==(const Edge& arc) const {return id == arc.id;}
404 bool operator!=(const Edge& arc) const {return id != arc.id;}
405 bool operator<(const Edge& arc) const {return id < arc.id;}
409 friend class ListEdgeSetBase;
411 Arc(int _id) : id(_id) {}
414 operator Edge() const { return edgeFromId(id / 2); }
417 Arc(Invalid) : id(-1) {}
418 bool operator==(const Arc& arc) const { return id == arc.id; }
419 bool operator!=(const Arc& arc) const { return id != arc.id; }
420 bool operator<(const Arc& arc) const { return id < arc.id; }
423 ListEdgeSetBase() : first_arc(-1), first_free_arc(-1) {}
427 "This graph structure does not support node insertion");
428 return INVALID; // avoid warning
431 Edge addEdge(const Node& u, const Node& v) {
434 if (first_free_arc == -1) {
436 arcs.push_back(ArcT());
437 arcs.push_back(ArcT());
440 first_free_arc = arcs[n].next_out;
444 arcs[n | 1].target = v;
446 arcs[n].next_out = (*_nodes)[v].first_out;
447 if ((*_nodes)[v].first_out != -1) {
448 arcs[(*_nodes)[v].first_out].prev_out = n;
450 (*_nodes)[v].first_out = n;
451 arcs[n].prev_out = -1;
453 if ((*_nodes)[u].first_out != -1) {
454 arcs[(*_nodes)[u].first_out].prev_out = (n | 1);
456 arcs[n | 1].next_out = (*_nodes)[u].first_out;
457 (*_nodes)[u].first_out = (n | 1);
458 arcs[n | 1].prev_out = -1;
463 void erase(const Edge& arc) {
466 if (arcs[n].next_out != -1) {
467 arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
470 if (arcs[n].prev_out != -1) {
471 arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
473 (*_nodes)[arcs[n | 1].target].first_out = arcs[n].next_out;
476 if (arcs[n | 1].next_out != -1) {
477 arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
480 if (arcs[n | 1].prev_out != -1) {
481 arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
483 (*_nodes)[arcs[n].target].first_out = arcs[n | 1].next_out;
486 arcs[n].next_out = first_free_arc;
493 for (first(node); node != INVALID; next(node)) {
494 (*_nodes)[node].first_out = -1;
501 void first(Node& node) const {
505 void next(Node& node) const {
509 void first(Arc& arc) const {
512 while (node != INVALID && (*_nodes)[node].first_out == -1) {
515 arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
518 void next(Arc& arc) const {
519 if (arcs[arc.id].next_out != -1) {
520 arc.id = arcs[arc.id].next_out;
522 Node node = arcs[arc.id ^ 1].target;
524 while(node != INVALID && (*_nodes)[node].first_out == -1) {
527 arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out;
531 void first(Edge& edge) const {
534 while (node != INVALID) {
535 edge.id = (*_nodes)[node].first_out;
536 while ((edge.id & 1) != 1) {
537 edge.id = arcs[edge.id].next_out;
548 void next(Edge& edge) const {
549 Node node = arcs[edge.id * 2].target;
550 edge.id = arcs[(edge.id * 2) | 1].next_out;
551 while ((edge.id & 1) != 1) {
552 edge.id = arcs[edge.id].next_out;
559 while (node != INVALID) {
560 edge.id = (*_nodes)[node].first_out;
561 while ((edge.id & 1) != 1) {
562 edge.id = arcs[edge.id].next_out;
573 void firstOut(Arc& arc, const Node& node) const {
574 arc.id = (*_nodes)[node].first_out;
577 void nextOut(Arc& arc) const {
578 arc.id = arcs[arc.id].next_out;
581 void firstIn(Arc& arc, const Node& node) const {
582 arc.id = (((*_nodes)[node].first_out) ^ 1);
583 if (arc.id == -2) arc.id = -1;
586 void nextIn(Arc& arc) const {
587 arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
588 if (arc.id == -2) arc.id = -1;
591 void firstInc(Edge &arc, bool& dir, const Node& node) const {
592 int de = (*_nodes)[node].first_out;
595 dir = ((de & 1) == 1);
601 void nextInc(Edge &arc, bool& dir) const {
602 int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
605 dir = ((de & 1) == 1);
612 static bool direction(Arc arc) {
613 return (arc.id & 1) == 1;
616 static Arc direct(Edge edge, bool dir) {
617 return Arc(edge.id * 2 + (dir ? 1 : 0));
620 int id(const Node& node) const { return _graph->id(node); }
621 static int id(Arc e) { return e.id; }
622 static int id(Edge e) { return e.id; }
624 Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
625 static Arc arcFromId(int id) { return Arc(id);}
626 static Edge edgeFromId(int id) { return Edge(id);}
628 int maxNodeId() const { return _graph->maxNodeId(); };
629 int maxEdgeId() const { return arcs.size() / 2 - 1; }
630 int maxArcId() const { return arcs.size()-1; }
632 Node source(Arc e) const { return arcs[e.id ^ 1].target; }
633 Node target(Arc e) const { return arcs[e.id].target; }
635 Node u(Edge e) const { return arcs[2 * e.id].target; }
636 Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
638 typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
640 NodeNotifier& notifier(Node) const {
641 return _graph->notifier(Node());
644 template <typename V>
645 class NodeMap : public GR::template NodeMap<V> {
646 typedef typename GR::template NodeMap<V> Parent;
650 explicit NodeMap(const ListEdgeSetBase<GR>& arcset)
651 : Parent(*arcset._graph) {}
653 NodeMap(const ListEdgeSetBase<GR>& arcset, const V& value)
654 : Parent(*arcset._graph, value) {}
656 NodeMap& operator=(const NodeMap& cmap) {
657 return operator=<NodeMap>(cmap);
660 template <typename CMap>
661 NodeMap& operator=(const CMap& cmap) {
662 Parent::operator=(cmap);
671 /// \brief Graph using a node set of another digraph or graph and an
674 /// This structure can be used to establish another graph over a
675 /// node set of an existing one. This class uses the same Node type
676 /// as the underlying graph, and each valid node of the original
677 /// graph is valid in this arc set, therefore the node objects of
678 /// the original graph can be used directly with this class. The
679 /// node handling functions (id handling, observing, and iterators)
680 /// works equivalently as in the original graph.
682 /// This implementation is based on doubly-linked lists, from each
683 /// node the incident edges make up lists, therefore one edge can be
684 /// erased in constant time. It also makes possible, that node can
685 /// be removed from the underlying graph, in this case all edges
686 /// incident to the given node is erased from the arc set.
688 /// \param GR The type of the graph which shares its node set
689 /// with this class. Its interface must conform to the
690 /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
693 /// This class fully conforms to the \ref concepts::Graph "Graph"
695 template <typename GR>
696 class ListEdgeSet : public EdgeSetExtender<ListEdgeSetBase<GR> > {
697 typedef EdgeSetExtender<ListEdgeSetBase<GR> > Parent;
701 typedef typename Parent::Node Node;
702 typedef typename Parent::Arc Arc;
703 typedef typename Parent::Edge Edge;
705 typedef typename Parent::NodesImplBase NodesImplBase;
707 void eraseNode(const Node& node) {
709 Parent::firstOut(arc, node);
710 while (arc != INVALID ) {
712 Parent::firstOut(arc, node);
721 class NodesImpl : public NodesImplBase {
722 typedef NodesImplBase Parent;
725 NodesImpl(const GR& graph, ListEdgeSet& arcset)
726 : Parent(graph), _arcset(arcset) {}
728 virtual ~NodesImpl() {}
732 virtual void erase(const Node& node) {
733 _arcset.eraseNode(node);
736 virtual void erase(const std::vector<Node>& nodes) {
737 for (int i = 0; i < int(nodes.size()); ++i) {
738 _arcset.eraseNode(nodes[i]);
740 Parent::erase(nodes);
742 virtual void clear() {
743 _arcset.clearNodes();
748 ListEdgeSet& _arcset;
755 /// \brief Constructor of the EdgeSet.
757 /// Constructor of the EdgeSet.
758 ListEdgeSet(const GR& graph) : _nodes(graph, *this) {
759 Parent::initalize(graph, _nodes);
762 /// \brief Add a new edge to the graph.
764 /// Add a new edge to the graph with node \c u
765 /// and node \c v endpoints.
766 /// \return The new edge.
767 Edge addEdge(const Node& u, const Node& v) {
768 return Parent::addEdge(u, v);
771 /// \brief Erase an edge from the graph.
773 /// Erase the edge \c e from the graph.
774 void erase(const Edge& e) {
775 return Parent::erase(e);
780 template <typename GR>
781 class SmartArcSetBase {
784 typedef typename GR::Node Node;
785 typedef typename GR::NodeIt NodeIt;
790 int first_out, first_in;
791 NodeT() : first_out(-1), first_in(-1) {}
794 typedef typename ItemSetTraits<GR, Node>::
795 template Map<NodeT>::Type NodesImplBase;
797 NodesImplBase* _nodes;
801 int next_out, next_in;
805 std::vector<ArcT> arcs;
809 void initalize(const GR& graph, NodesImplBase& nodes) {
817 friend class SmartArcSetBase<GR>;
819 Arc(int _id) : id(_id) {}
823 Arc(Invalid) : id(-1) {}
824 bool operator==(const Arc& arc) const { return id == arc.id; }
825 bool operator!=(const Arc& arc) const { return id != arc.id; }
826 bool operator<(const Arc& arc) const { return id < arc.id; }
833 "This graph structure does not support node insertion");
834 return INVALID; // avoid warning
837 Arc addArc(const Node& u, const Node& v) {
839 arcs.push_back(ArcT());
840 arcs[n].next_in = (*_nodes)[v].first_in;
841 (*_nodes)[v].first_in = n;
842 arcs[n].next_out = (*_nodes)[u].first_out;
843 (*_nodes)[u].first_out = n;
851 for (first(node); node != INVALID; next(node)) {
852 (*_nodes)[node].first_in = -1;
853 (*_nodes)[node].first_out = -1;
858 void first(Node& node) const {
862 void next(Node& node) const {
866 void first(Arc& arc) const {
867 arc.id = arcs.size() - 1;
870 static void next(Arc& arc) {
874 void firstOut(Arc& arc, const Node& node) const {
875 arc.id = (*_nodes)[node].first_out;
878 void nextOut(Arc& arc) const {
879 arc.id = arcs[arc.id].next_out;
882 void firstIn(Arc& arc, const Node& node) const {
883 arc.id = (*_nodes)[node].first_in;
886 void nextIn(Arc& arc) const {
887 arc.id = arcs[arc.id].next_in;
890 int id(const Node& node) const { return _graph->id(node); }
891 int id(const Arc& arc) const { return arc.id; }
893 Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
894 Arc arcFromId(int ix) const { return Arc(ix); }
896 int maxNodeId() const { return _graph->maxNodeId(); };
897 int maxArcId() const { return arcs.size() - 1; }
899 Node source(const Arc& arc) const { return arcs[arc.id].source;}
900 Node target(const Arc& arc) const { return arcs[arc.id].target;}
902 typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
904 NodeNotifier& notifier(Node) const {
905 return _graph->notifier(Node());
908 template <typename V>
909 class NodeMap : public GR::template NodeMap<V> {
910 typedef typename GR::template NodeMap<V> Parent;
914 explicit NodeMap(const SmartArcSetBase<GR>& arcset)
915 : Parent(*arcset._graph) { }
917 NodeMap(const SmartArcSetBase<GR>& arcset, const V& value)
918 : Parent(*arcset._graph, value) { }
920 NodeMap& operator=(const NodeMap& cmap) {
921 return operator=<NodeMap>(cmap);
924 template <typename CMap>
925 NodeMap& operator=(const CMap& cmap) {
926 Parent::operator=(cmap);
936 /// \brief Digraph using a node set of another digraph or graph and
939 /// This structure can be used to establish another directed graph
940 /// over a node set of an existing one. This class uses the same
941 /// Node type as the underlying graph, and each valid node of the
942 /// original graph is valid in this arc set, therefore the node
943 /// objects of the original graph can be used directly with this
944 /// class. The node handling functions (id handling, observing, and
945 /// iterators) works equivalently as in the original graph.
947 /// \param GR The type of the graph which shares its node set with
948 /// this class. Its interface must conform to the
949 /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
952 /// This implementation is slightly faster than the \c ListArcSet,
953 /// because it uses continuous storage for arcs and it uses just
954 /// single-linked lists for enumerate outgoing and incoming
955 /// arcs. Therefore the arcs cannot be erased from the arc sets.
957 /// \warning If a node is erased from the underlying graph and this
958 /// node is the source or target of one arc in the arc set, then
959 /// the arc set is invalidated, and it cannot be used anymore. The
960 /// validity can be checked with the \c valid() member function.
962 /// This class fully conforms to the \ref concepts::Digraph
963 /// "Digraph" concept.
964 template <typename GR>
965 class SmartArcSet : public ArcSetExtender<SmartArcSetBase<GR> > {
966 typedef ArcSetExtender<SmartArcSetBase<GR> > Parent;
970 typedef typename Parent::Node Node;
971 typedef typename Parent::Arc Arc;
975 typedef typename Parent::NodesImplBase NodesImplBase;
977 void eraseNode(const Node& node) {
978 if (typename Parent::InArcIt(*this, node) == INVALID &&
979 typename Parent::OutArcIt(*this, node) == INVALID) {
982 throw typename NodesImplBase::Notifier::ImmediateDetach();
989 class NodesImpl : public NodesImplBase {
990 typedef NodesImplBase Parent;
993 NodesImpl(const GR& graph, SmartArcSet& arcset)
994 : Parent(graph), _arcset(arcset) {}
996 virtual ~NodesImpl() {}
998 bool attached() const {
999 return Parent::attached();
1004 virtual void erase(const Node& node) {
1006 _arcset.eraseNode(node);
1007 Parent::erase(node);
1008 } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1013 virtual void erase(const std::vector<Node>& nodes) {
1015 for (int i = 0; i < int(nodes.size()); ++i) {
1016 _arcset.eraseNode(nodes[i]);
1018 Parent::erase(nodes);
1019 } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1024 virtual void clear() {
1025 _arcset.clearNodes();
1030 SmartArcSet& _arcset;
1037 /// \brief Constructor of the ArcSet.
1039 /// Constructor of the ArcSet.
1040 SmartArcSet(const GR& graph) : _nodes(graph, *this) {
1041 Parent::initalize(graph, _nodes);
1044 /// \brief Add a new arc to the digraph.
1046 /// Add a new arc to the digraph with source node \c s
1047 /// and target node \c t.
1048 /// \return The new arc.
1049 Arc addArc(const Node& s, const Node& t) {
1050 return Parent::addArc(s, t);
1053 /// \brief Validity check
1055 /// This functions gives back false if the ArcSet is
1056 /// invalidated. It occurs when a node in the underlying graph is
1057 /// erased and it is not isolated in the ArcSet.
1058 bool valid() const {
1059 return _nodes.attached();
1065 template <typename GR>
1066 class SmartEdgeSetBase {
1069 typedef typename GR::Node Node;
1070 typedef typename GR::NodeIt NodeIt;
1076 NodeT() : first_out(-1) {}
1079 typedef typename ItemSetTraits<GR, Node>::
1080 template Map<NodeT>::Type NodesImplBase;
1082 NodesImplBase* _nodes;
1090 std::vector<ArcT> arcs;
1094 void initalize(const GR& graph, NodesImplBase& nodes) {
1102 friend class SmartEdgeSetBase;
1106 explicit Edge(int _id) { id = _id;}
1110 Edge (Invalid) { id = -1; }
1111 bool operator==(const Edge& arc) const {return id == arc.id;}
1112 bool operator!=(const Edge& arc) const {return id != arc.id;}
1113 bool operator<(const Edge& arc) const {return id < arc.id;}
1117 friend class SmartEdgeSetBase;
1119 Arc(int _id) : id(_id) {}
1122 operator Edge() const { return edgeFromId(id / 2); }
1125 Arc(Invalid) : id(-1) {}
1126 bool operator==(const Arc& arc) const { return id == arc.id; }
1127 bool operator!=(const Arc& arc) const { return id != arc.id; }
1128 bool operator<(const Arc& arc) const { return id < arc.id; }
1131 SmartEdgeSetBase() {}
1135 "This graph structure does not support node insertion");
1136 return INVALID; // avoid warning
1139 Edge addEdge(const Node& u, const Node& v) {
1140 int n = arcs.size();
1141 arcs.push_back(ArcT());
1142 arcs.push_back(ArcT());
1145 arcs[n | 1].target = v;
1147 arcs[n].next_out = (*_nodes)[v].first_out;
1148 (*_nodes)[v].first_out = n;
1150 arcs[n | 1].next_out = (*_nodes)[u].first_out;
1151 (*_nodes)[u].first_out = (n | 1);
1158 for (first(node); node != INVALID; next(node)) {
1159 (*_nodes)[node].first_out = -1;
1164 void first(Node& node) const {
1165 _graph->first(node);
1168 void next(Node& node) const {
1172 void first(Arc& arc) const {
1173 arc.id = arcs.size() - 1;
1176 static void next(Arc& arc) {
1180 void first(Edge& arc) const {
1181 arc.id = arcs.size() / 2 - 1;
1184 static void next(Edge& arc) {
1188 void firstOut(Arc& arc, const Node& node) const {
1189 arc.id = (*_nodes)[node].first_out;
1192 void nextOut(Arc& arc) const {
1193 arc.id = arcs[arc.id].next_out;
1196 void firstIn(Arc& arc, const Node& node) const {
1197 arc.id = (((*_nodes)[node].first_out) ^ 1);
1198 if (arc.id == -2) arc.id = -1;
1201 void nextIn(Arc& arc) const {
1202 arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1);
1203 if (arc.id == -2) arc.id = -1;
1206 void firstInc(Edge &arc, bool& dir, const Node& node) const {
1207 int de = (*_nodes)[node].first_out;
1210 dir = ((de & 1) == 1);
1216 void nextInc(Edge &arc, bool& dir) const {
1217 int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out);
1220 dir = ((de & 1) == 1);
1227 static bool direction(Arc arc) {
1228 return (arc.id & 1) == 1;
1231 static Arc direct(Edge edge, bool dir) {
1232 return Arc(edge.id * 2 + (dir ? 1 : 0));
1235 int id(Node node) const { return _graph->id(node); }
1236 static int id(Arc arc) { return arc.id; }
1237 static int id(Edge arc) { return arc.id; }
1239 Node nodeFromId(int id) const { return _graph->nodeFromId(id); }
1240 static Arc arcFromId(int id) { return Arc(id); }
1241 static Edge edgeFromId(int id) { return Edge(id);}
1243 int maxNodeId() const { return _graph->maxNodeId(); };
1244 int maxArcId() const { return arcs.size() - 1; }
1245 int maxEdgeId() const { return arcs.size() / 2 - 1; }
1247 Node source(Arc e) const { return arcs[e.id ^ 1].target; }
1248 Node target(Arc e) const { return arcs[e.id].target; }
1250 Node u(Edge e) const { return arcs[2 * e.id].target; }
1251 Node v(Edge e) const { return arcs[2 * e.id + 1].target; }
1253 typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier;
1255 NodeNotifier& notifier(Node) const {
1256 return _graph->notifier(Node());
1259 template <typename V>
1260 class NodeMap : public GR::template NodeMap<V> {
1261 typedef typename GR::template NodeMap<V> Parent;
1265 explicit NodeMap(const SmartEdgeSetBase<GR>& arcset)
1266 : Parent(*arcset._graph) { }
1268 NodeMap(const SmartEdgeSetBase<GR>& arcset, const V& value)
1269 : Parent(*arcset._graph, value) { }
1271 NodeMap& operator=(const NodeMap& cmap) {
1272 return operator=<NodeMap>(cmap);
1275 template <typename CMap>
1276 NodeMap& operator=(const CMap& cmap) {
1277 Parent::operator=(cmap);
1286 /// \brief Graph using a node set of another digraph or graph and an
1289 /// This structure can be used to establish another graph over a
1290 /// node set of an existing one. This class uses the same Node type
1291 /// as the underlying graph, and each valid node of the original
1292 /// graph is valid in this arc set, therefore the node objects of
1293 /// the original graph can be used directly with this class. The
1294 /// node handling functions (id handling, observing, and iterators)
1295 /// works equivalently as in the original graph.
1297 /// \param GR The type of the graph which shares its node set
1298 /// with this class. Its interface must conform to the
1299 /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph"
1302 /// This implementation is slightly faster than the \c ListEdgeSet,
1303 /// because it uses continuous storage for edges and it uses just
1304 /// single-linked lists for enumerate incident edges. Therefore the
1305 /// edges cannot be erased from the edge sets.
1307 /// \warning If a node is erased from the underlying graph and this
1308 /// node is incident to one edge in the edge set, then the edge set
1309 /// is invalidated, and it cannot be used anymore. The validity can
1310 /// be checked with the \c valid() member function.
1312 /// This class fully conforms to the \ref concepts::Graph
1313 /// "Graph" concept.
1314 template <typename GR>
1315 class SmartEdgeSet : public EdgeSetExtender<SmartEdgeSetBase<GR> > {
1316 typedef EdgeSetExtender<SmartEdgeSetBase<GR> > Parent;
1320 typedef typename Parent::Node Node;
1321 typedef typename Parent::Arc Arc;
1322 typedef typename Parent::Edge Edge;
1326 typedef typename Parent::NodesImplBase NodesImplBase;
1328 void eraseNode(const Node& node) {
1329 if (typename Parent::IncEdgeIt(*this, node) == INVALID) {
1332 throw typename NodesImplBase::Notifier::ImmediateDetach();
1339 class NodesImpl : public NodesImplBase {
1340 typedef NodesImplBase Parent;
1343 NodesImpl(const GR& graph, SmartEdgeSet& arcset)
1344 : Parent(graph), _arcset(arcset) {}
1346 virtual ~NodesImpl() {}
1348 bool attached() const {
1349 return Parent::attached();
1354 virtual void erase(const Node& node) {
1356 _arcset.eraseNode(node);
1357 Parent::erase(node);
1358 } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1363 virtual void erase(const std::vector<Node>& nodes) {
1365 for (int i = 0; i < int(nodes.size()); ++i) {
1366 _arcset.eraseNode(nodes[i]);
1368 Parent::erase(nodes);
1369 } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) {
1374 virtual void clear() {
1375 _arcset.clearNodes();
1380 SmartEdgeSet& _arcset;
1387 /// \brief Constructor of the EdgeSet.
1389 /// Constructor of the EdgeSet.
1390 SmartEdgeSet(const GR& graph) : _nodes(graph, *this) {
1391 Parent::initalize(graph, _nodes);
1394 /// \brief Add a new edge to the graph.
1396 /// Add a new edge to the graph with node \c u
1397 /// and node \c v endpoints.
1398 /// \return The new edge.
1399 Edge addEdge(const Node& u, const Node& v) {
1400 return Parent::addEdge(u, v);
1403 /// \brief Validity check
1405 /// This functions gives back false if the EdgeSet is
1406 /// invalidated. It occurs when a node in the underlying graph is
1407 /// erased and it is not isolated in the EdgeSet.
1408 bool valid() const {
1409 return _nodes.attached();