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_LIST_GRAPH_H
20 #define LEMON_LIST_GRAPH_H
24 ///\brief ListDigraph, ListGraph classes.
26 #include <lemon/core.h>
27 #include <lemon/error.h>
28 #include <lemon/bits/graph_extender.h>
35 class ListDigraphBase {
39 int first_in, first_out;
45 int prev_in, prev_out;
46 int next_in, next_out;
49 std::vector<NodeT> nodes;
55 std::vector<ArcT> arcs;
61 typedef ListDigraphBase Digraph;
64 friend class ListDigraphBase;
68 explicit Node(int pid) { id = pid;}
72 Node (Invalid) { id = -1; }
73 bool operator==(const Node& node) const {return id == node.id;}
74 bool operator!=(const Node& node) const {return id != node.id;}
75 bool operator<(const Node& node) const {return id < node.id;}
79 friend class ListDigraphBase;
83 explicit Arc(int pid) { id = pid;}
87 Arc (Invalid) { id = -1; }
88 bool operator==(const Arc& arc) const {return id == arc.id;}
89 bool operator!=(const Arc& arc) const {return id != arc.id;}
90 bool operator<(const Arc& arc) const {return id < arc.id;}
96 : nodes(), first_node(-1),
97 first_free_node(-1), arcs(), first_free_arc(-1) {}
100 int maxNodeId() const { return nodes.size()-1; }
101 int maxArcId() const { return arcs.size()-1; }
103 Node source(Arc e) const { return Node(arcs[e.id].source); }
104 Node target(Arc e) const { return Node(arcs[e.id].target); }
107 void first(Node& node) const {
108 node.id = first_node;
111 void next(Node& node) const {
112 node.id = nodes[node.id].next;
116 void first(Arc& arc) const {
119 n!=-1 && nodes[n].first_in == -1;
120 n = nodes[n].next) {}
121 arc.id = (n == -1) ? -1 : nodes[n].first_in;
124 void next(Arc& arc) const {
125 if (arcs[arc.id].next_in != -1) {
126 arc.id = arcs[arc.id].next_in;
129 for(n = nodes[arcs[arc.id].target].next;
130 n!=-1 && nodes[n].first_in == -1;
131 n = nodes[n].next) {}
132 arc.id = (n == -1) ? -1 : nodes[n].first_in;
136 void firstOut(Arc &e, const Node& v) const {
137 e.id = nodes[v.id].first_out;
139 void nextOut(Arc &e) const {
140 e.id=arcs[e.id].next_out;
143 void firstIn(Arc &e, const Node& v) const {
144 e.id = nodes[v.id].first_in;
146 void nextIn(Arc &e) const {
147 e.id=arcs[e.id].next_in;
151 static int id(Node v) { return v.id; }
152 static int id(Arc e) { return e.id; }
154 static Node nodeFromId(int id) { return Node(id);}
155 static Arc arcFromId(int id) { return Arc(id);}
157 bool valid(Node n) const {
158 return n.id >= 0 && n.id < static_cast<int>(nodes.size()) &&
159 nodes[n.id].prev != -2;
162 bool valid(Arc a) const {
163 return a.id >= 0 && a.id < static_cast<int>(arcs.size()) &&
164 arcs[a.id].prev_in != -2;
170 if(first_free_node==-1) {
172 nodes.push_back(NodeT());
175 first_free_node = nodes[n].next;
178 nodes[n].next = first_node;
179 if(first_node != -1) nodes[first_node].prev = n;
183 nodes[n].first_in = nodes[n].first_out = -1;
188 Arc addArc(Node u, Node v) {
191 if (first_free_arc == -1) {
193 arcs.push_back(ArcT());
196 first_free_arc = arcs[n].next_in;
199 arcs[n].source = u.id;
200 arcs[n].target = v.id;
202 arcs[n].next_out = nodes[u.id].first_out;
203 if(nodes[u.id].first_out != -1) {
204 arcs[nodes[u.id].first_out].prev_out = n;
207 arcs[n].next_in = nodes[v.id].first_in;
208 if(nodes[v.id].first_in != -1) {
209 arcs[nodes[v.id].first_in].prev_in = n;
212 arcs[n].prev_in = arcs[n].prev_out = -1;
214 nodes[u.id].first_out = nodes[v.id].first_in = n;
219 void erase(const Node& node) {
222 if(nodes[n].next != -1) {
223 nodes[nodes[n].next].prev = nodes[n].prev;
226 if(nodes[n].prev != -1) {
227 nodes[nodes[n].prev].next = nodes[n].next;
229 first_node = nodes[n].next;
232 nodes[n].next = first_free_node;
238 void erase(const Arc& arc) {
241 if(arcs[n].next_in!=-1) {
242 arcs[arcs[n].next_in].prev_in = arcs[n].prev_in;
245 if(arcs[n].prev_in!=-1) {
246 arcs[arcs[n].prev_in].next_in = arcs[n].next_in;
248 nodes[arcs[n].target].first_in = arcs[n].next_in;
252 if(arcs[n].next_out!=-1) {
253 arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
256 if(arcs[n].prev_out!=-1) {
257 arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
259 nodes[arcs[n].source].first_out = arcs[n].next_out;
262 arcs[n].next_in = first_free_arc;
264 arcs[n].prev_in = -2;
270 first_node = first_free_node = first_free_arc = -1;
274 void changeTarget(Arc e, Node n)
276 if(arcs[e.id].next_in != -1)
277 arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in;
278 if(arcs[e.id].prev_in != -1)
279 arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in;
280 else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in;
281 if (nodes[n.id].first_in != -1) {
282 arcs[nodes[n.id].first_in].prev_in = e.id;
284 arcs[e.id].target = n.id;
285 arcs[e.id].prev_in = -1;
286 arcs[e.id].next_in = nodes[n.id].first_in;
287 nodes[n.id].first_in = e.id;
289 void changeSource(Arc e, Node n)
291 if(arcs[e.id].next_out != -1)
292 arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out;
293 if(arcs[e.id].prev_out != -1)
294 arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out;
295 else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out;
296 if (nodes[n.id].first_out != -1) {
297 arcs[nodes[n.id].first_out].prev_out = e.id;
299 arcs[e.id].source = n.id;
300 arcs[e.id].prev_out = -1;
301 arcs[e.id].next_out = nodes[n.id].first_out;
302 nodes[n.id].first_out = e.id;
307 typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase;
309 /// \addtogroup graphs
312 ///A general directed graph structure.
314 ///\ref ListDigraph is a simple and fast <em>directed graph</em>
315 ///implementation based on static linked lists that are stored in
316 ///\c std::vector structures.
318 ///It conforms to the \ref concepts::Digraph "Digraph concept" and it
319 ///also provides several useful additional functionalities.
320 ///Most of the member functions and nested classes are documented
321 ///only in the concept class.
323 ///An important extra feature of this digraph implementation is that
324 ///its maps are real \ref concepts::ReferenceMap "reference map"s.
326 ///\sa concepts::Digraph
328 class ListDigraph : public ExtendedListDigraphBase {
330 ///ListDigraph is \e not copy constructible. Use copyDigraph() instead.
332 ///ListDigraph is \e not copy constructible. Use copyDigraph() instead.
334 ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {};
335 ///\brief Assignment of ListDigraph to another one is \e not allowed.
336 ///Use copyDigraph() instead.
338 ///Assignment of ListDigraph to another one is \e not allowed.
339 ///Use copyDigraph() instead.
340 void operator=(const ListDigraph &) {}
343 typedef ExtendedListDigraphBase Parent;
351 ///Add a new node to the digraph.
353 ///Add a new node to the digraph.
354 ///\return the new node.
355 Node addNode() { return Parent::addNode(); }
357 ///Add a new arc to the digraph.
359 ///Add a new arc to the digraph with source node \c s
360 ///and target node \c t.
361 ///\return the new arc.
362 Arc addArc(const Node& s, const Node& t) {
363 return Parent::addArc(s, t);
366 /// Node validity check
368 /// This function gives back true if the given node is valid,
369 /// ie. it is a real node of the graph.
371 /// \warning A Node pointing to a removed item
372 /// could become valid again later if new nodes are
373 /// added to the graph.
374 bool valid(Node n) const { return Parent::valid(n); }
376 /// Arc validity check
378 /// This function gives back true if the given arc is valid,
379 /// ie. it is a real arc of the graph.
381 /// \warning An Arc pointing to a removed item
382 /// could become valid again later if new nodes are
383 /// added to the graph.
384 bool valid(Arc a) const { return Parent::valid(a); }
386 /// Change the target of \c e to \c n
388 /// Change the target of \c e to \c n
390 ///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing
391 ///the changed arc remain valid. However <tt>InArcIt</tt>s are
394 ///\warning This functionality cannot be used together with the Snapshot
396 void changeTarget(Arc e, Node n) {
397 Parent::changeTarget(e,n);
399 /// Change the source of \c e to \c n
401 /// Change the source of \c e to \c n
403 ///\note The <tt>ArcIt</tt>s and <tt>InArcIt</tt>s referencing
404 ///the changed arc remain valid. However <tt>OutArcIt</tt>s are
407 ///\warning This functionality cannot be used together with the Snapshot
409 void changeSource(Arc e, Node n) {
410 Parent::changeSource(e,n);
413 /// Invert the direction of an arc.
415 ///\note The <tt>ArcIt</tt>s referencing the changed arc remain
416 ///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are
419 ///\warning This functionality cannot be used together with the Snapshot
421 void reverseArc(Arc e) {
423 changeTarget(e,source(e));
427 /// Reserve memory for nodes.
429 /// Using this function it is possible to avoid the superfluous memory
430 /// allocation: if you know that the digraph you want to build will
431 /// be very large (e.g. it will contain millions of nodes and/or arcs)
432 /// then it is worth reserving space for this amount before starting
433 /// to build the digraph.
435 void reserveNode(int n) { nodes.reserve(n); };
437 /// Reserve memory for arcs.
439 /// Using this function it is possible to avoid the superfluous memory
440 /// allocation: if you know that the digraph you want to build will
441 /// be very large (e.g. it will contain millions of nodes and/or arcs)
442 /// then it is worth reserving space for this amount before starting
443 /// to build the digraph.
445 void reserveArc(int m) { arcs.reserve(m); };
447 ///Contract two nodes.
449 ///This function contracts two nodes.
450 ///Node \p b will be removed but instead of deleting
451 ///incident arcs, they will be joined to \p a.
452 ///The last parameter \p r controls whether to remove loops. \c true
453 ///means that loops will be removed.
455 ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
456 ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s
457 ///may be invalidated.
459 ///\warning This functionality cannot be used together with the Snapshot
461 void contract(Node a, Node b, bool r = true)
463 for(OutArcIt e(*this,b);e!=INVALID;) {
466 if(r && target(e)==a) erase(e);
467 else changeSource(e,a);
470 for(InArcIt e(*this,b);e!=INVALID;) {
473 if(r && source(e)==a) erase(e);
474 else changeTarget(e,a);
482 ///This function splits a node. First a new node is added to the digraph,
483 ///then the source of each outgoing arc of \c n is moved to this new node.
484 ///If \c connect is \c true (this is the default value), then a new arc
485 ///from \c n to the newly created node is also added.
486 ///\return The newly created node.
488 ///\note The <tt>ArcIt</tt>s referencing a moved arc remain
489 ///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may
492 ///\warning This functionality cannot be used together with the
495 ///\todo It could be implemented in a bit faster way.
496 Node split(Node n, bool connect = true) {
498 for(OutArcIt e(*this,n);e!=INVALID;) {
504 if (connect) addArc(n,b);
510 ///This function splits an arc. First a new node \c b is added to
511 ///the digraph, then the original arc is re-targeted to \c
512 ///b. Finally an arc from \c b to the original target is added.
514 ///\return The newly created node.
516 ///\warning This functionality cannot be used together with the
525 /// \brief Class to make a snapshot of the digraph and restore
528 /// Class to make a snapshot of the digraph and restore it later.
530 /// The newly added nodes and arcs can be removed using the
531 /// restore() function.
533 /// \warning Arc and node deletions and other modifications (e.g.
534 /// contracting, splitting, reversing arcs or nodes) cannot be
535 /// restored. These events invalidate the snapshot.
539 typedef Parent::NodeNotifier NodeNotifier;
541 class NodeObserverProxy : public NodeNotifier::ObserverBase {
544 NodeObserverProxy(Snapshot& _snapshot)
545 : snapshot(_snapshot) {}
547 using NodeNotifier::ObserverBase::attach;
548 using NodeNotifier::ObserverBase::detach;
549 using NodeNotifier::ObserverBase::attached;
553 virtual void add(const Node& node) {
554 snapshot.addNode(node);
556 virtual void add(const std::vector<Node>& nodes) {
557 for (int i = nodes.size() - 1; i >= 0; ++i) {
558 snapshot.addNode(nodes[i]);
561 virtual void erase(const Node& node) {
562 snapshot.eraseNode(node);
564 virtual void erase(const std::vector<Node>& nodes) {
565 for (int i = 0; i < int(nodes.size()); ++i) {
566 snapshot.eraseNode(nodes[i]);
569 virtual void build() {
571 std::vector<Node> nodes;
572 for (notifier()->first(node); node != INVALID;
573 notifier()->next(node)) {
574 nodes.push_back(node);
576 for (int i = nodes.size() - 1; i >= 0; --i) {
577 snapshot.addNode(nodes[i]);
580 virtual void clear() {
582 for (notifier()->first(node); node != INVALID;
583 notifier()->next(node)) {
584 snapshot.eraseNode(node);
591 class ArcObserverProxy : public ArcNotifier::ObserverBase {
594 ArcObserverProxy(Snapshot& _snapshot)
595 : snapshot(_snapshot) {}
597 using ArcNotifier::ObserverBase::attach;
598 using ArcNotifier::ObserverBase::detach;
599 using ArcNotifier::ObserverBase::attached;
603 virtual void add(const Arc& arc) {
604 snapshot.addArc(arc);
606 virtual void add(const std::vector<Arc>& arcs) {
607 for (int i = arcs.size() - 1; i >= 0; ++i) {
608 snapshot.addArc(arcs[i]);
611 virtual void erase(const Arc& arc) {
612 snapshot.eraseArc(arc);
614 virtual void erase(const std::vector<Arc>& arcs) {
615 for (int i = 0; i < int(arcs.size()); ++i) {
616 snapshot.eraseArc(arcs[i]);
619 virtual void build() {
621 std::vector<Arc> arcs;
622 for (notifier()->first(arc); arc != INVALID;
623 notifier()->next(arc)) {
626 for (int i = arcs.size() - 1; i >= 0; --i) {
627 snapshot.addArc(arcs[i]);
630 virtual void clear() {
632 for (notifier()->first(arc); arc != INVALID;
633 notifier()->next(arc)) {
634 snapshot.eraseArc(arc);
641 ListDigraph *digraph;
643 NodeObserverProxy node_observer_proxy;
644 ArcObserverProxy arc_observer_proxy;
646 std::list<Node> added_nodes;
647 std::list<Arc> added_arcs;
650 void addNode(const Node& node) {
651 added_nodes.push_front(node);
653 void eraseNode(const Node& node) {
654 std::list<Node>::iterator it =
655 std::find(added_nodes.begin(), added_nodes.end(), node);
656 if (it == added_nodes.end()) {
658 arc_observer_proxy.detach();
659 throw NodeNotifier::ImmediateDetach();
661 added_nodes.erase(it);
665 void addArc(const Arc& arc) {
666 added_arcs.push_front(arc);
668 void eraseArc(const Arc& arc) {
669 std::list<Arc>::iterator it =
670 std::find(added_arcs.begin(), added_arcs.end(), arc);
671 if (it == added_arcs.end()) {
673 node_observer_proxy.detach();
674 throw ArcNotifier::ImmediateDetach();
676 added_arcs.erase(it);
680 void attach(ListDigraph &_digraph) {
682 node_observer_proxy.attach(digraph->notifier(Node()));
683 arc_observer_proxy.attach(digraph->notifier(Arc()));
687 node_observer_proxy.detach();
688 arc_observer_proxy.detach();
691 bool attached() const {
692 return node_observer_proxy.attached();
702 /// \brief Default constructor.
704 /// Default constructor.
705 /// To actually make a snapshot you must call save().
707 : digraph(0), node_observer_proxy(*this),
708 arc_observer_proxy(*this) {}
710 /// \brief Constructor that immediately makes a snapshot.
712 /// This constructor immediately makes a snapshot of the digraph.
713 /// \param _digraph The digraph we make a snapshot of.
714 Snapshot(ListDigraph &_digraph)
715 : node_observer_proxy(*this),
716 arc_observer_proxy(*this) {
720 /// \brief Make a snapshot.
722 /// Make a snapshot of the digraph.
724 /// This function can be called more than once. In case of a repeated
725 /// call, the previous snapshot gets lost.
726 /// \param _digraph The digraph we make the snapshot of.
727 void save(ListDigraph &_digraph) {
735 /// \brief Undo the changes until the last snapshot.
737 /// Undo the changes until the last snapshot created by save().
740 for(std::list<Arc>::iterator it = added_arcs.begin();
741 it != added_arcs.end(); ++it) {
744 for(std::list<Node>::iterator it = added_nodes.begin();
745 it != added_nodes.end(); ++it) {
751 /// \brief Gives back true when the snapshot is valid.
753 /// Gives back true when the snapshot is valid.
763 class ListGraphBase {
774 int prev_out, next_out;
777 std::vector<NodeT> nodes;
783 std::vector<ArcT> arcs;
789 typedef ListGraphBase Digraph;
796 friend class ListGraphBase;
800 explicit Node(int pid) { id = pid;}
804 Node (Invalid) { id = -1; }
805 bool operator==(const Node& node) const {return id == node.id;}
806 bool operator!=(const Node& node) const {return id != node.id;}
807 bool operator<(const Node& node) const {return id < node.id;}
811 friend class ListGraphBase;
815 explicit Edge(int pid) { id = pid;}
819 Edge (Invalid) { id = -1; }
820 bool operator==(const Edge& edge) const {return id == edge.id;}
821 bool operator!=(const Edge& edge) const {return id != edge.id;}
822 bool operator<(const Edge& edge) const {return id < edge.id;}
826 friend class ListGraphBase;
830 explicit Arc(int pid) { id = pid;}
833 operator Edge() const { return edgeFromId(id / 2); }
836 Arc (Invalid) { id = -1; }
837 bool operator==(const Arc& arc) const {return id == arc.id;}
838 bool operator!=(const Arc& arc) const {return id != arc.id;}
839 bool operator<(const Arc& arc) const {return id < arc.id;}
845 : nodes(), first_node(-1),
846 first_free_node(-1), arcs(), first_free_arc(-1) {}
849 int maxNodeId() const { return nodes.size()-1; }
850 int maxEdgeId() const { return arcs.size() / 2 - 1; }
851 int maxArcId() const { return arcs.size()-1; }
853 Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); }
854 Node target(Arc e) const { return Node(arcs[e.id].target); }
856 Node u(Edge e) const { return Node(arcs[2 * e.id].target); }
857 Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); }
859 static bool direction(Arc e) {
860 return (e.id & 1) == 1;
863 static Arc direct(Edge e, bool d) {
864 return Arc(e.id * 2 + (d ? 1 : 0));
867 void first(Node& node) const {
868 node.id = first_node;
871 void next(Node& node) const {
872 node.id = nodes[node.id].next;
875 void first(Arc& e) const {
877 while (n != -1 && nodes[n].first_out == -1) {
880 e.id = (n == -1) ? -1 : nodes[n].first_out;
883 void next(Arc& e) const {
884 if (arcs[e.id].next_out != -1) {
885 e.id = arcs[e.id].next_out;
887 int n = nodes[arcs[e.id ^ 1].target].next;
888 while(n != -1 && nodes[n].first_out == -1) {
891 e.id = (n == -1) ? -1 : nodes[n].first_out;
895 void first(Edge& e) const {
898 e.id = nodes[n].first_out;
899 while ((e.id & 1) != 1) {
900 e.id = arcs[e.id].next_out;
911 void next(Edge& e) const {
912 int n = arcs[e.id * 2].target;
913 e.id = arcs[(e.id * 2) | 1].next_out;
914 while ((e.id & 1) != 1) {
915 e.id = arcs[e.id].next_out;
923 e.id = nodes[n].first_out;
924 while ((e.id & 1) != 1) {
925 e.id = arcs[e.id].next_out;
936 void firstOut(Arc &e, const Node& v) const {
937 e.id = nodes[v.id].first_out;
939 void nextOut(Arc &e) const {
940 e.id = arcs[e.id].next_out;
943 void firstIn(Arc &e, const Node& v) const {
944 e.id = ((nodes[v.id].first_out) ^ 1);
945 if (e.id == -2) e.id = -1;
947 void nextIn(Arc &e) const {
948 e.id = ((arcs[e.id ^ 1].next_out) ^ 1);
949 if (e.id == -2) e.id = -1;
952 void firstInc(Edge &e, bool& d, const Node& v) const {
953 int a = nodes[v.id].first_out;
962 void nextInc(Edge &e, bool& d) const {
963 int a = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out);
973 static int id(Node v) { return v.id; }
974 static int id(Arc e) { return e.id; }
975 static int id(Edge e) { return e.id; }
977 static Node nodeFromId(int id) { return Node(id);}
978 static Arc arcFromId(int id) { return Arc(id);}
979 static Edge edgeFromId(int id) { return Edge(id);}
981 bool valid(Node n) const {
982 return n.id >= 0 && n.id < static_cast<int>(nodes.size()) &&
983 nodes[n.id].prev != -2;
986 bool valid(Arc a) const {
987 return a.id >= 0 && a.id < static_cast<int>(arcs.size()) &&
988 arcs[a.id].prev_out != -2;
991 bool valid(Edge e) const {
992 return e.id >= 0 && 2 * e.id < static_cast<int>(arcs.size()) &&
993 arcs[2 * e.id].prev_out != -2;
999 if(first_free_node==-1) {
1001 nodes.push_back(NodeT());
1003 n = first_free_node;
1004 first_free_node = nodes[n].next;
1007 nodes[n].next = first_node;
1008 if (first_node != -1) nodes[first_node].prev = n;
1012 nodes[n].first_out = -1;
1017 Edge addEdge(Node u, Node v) {
1020 if (first_free_arc == -1) {
1022 arcs.push_back(ArcT());
1023 arcs.push_back(ArcT());
1026 first_free_arc = arcs[n].next_out;
1029 arcs[n].target = u.id;
1030 arcs[n | 1].target = v.id;
1032 arcs[n].next_out = nodes[v.id].first_out;
1033 if (nodes[v.id].first_out != -1) {
1034 arcs[nodes[v.id].first_out].prev_out = n;
1036 arcs[n].prev_out = -1;
1037 nodes[v.id].first_out = n;
1039 arcs[n | 1].next_out = nodes[u.id].first_out;
1040 if (nodes[u.id].first_out != -1) {
1041 arcs[nodes[u.id].first_out].prev_out = (n | 1);
1043 arcs[n | 1].prev_out = -1;
1044 nodes[u.id].first_out = (n | 1);
1049 void erase(const Node& node) {
1052 if(nodes[n].next != -1) {
1053 nodes[nodes[n].next].prev = nodes[n].prev;
1056 if(nodes[n].prev != -1) {
1057 nodes[nodes[n].prev].next = nodes[n].next;
1059 first_node = nodes[n].next;
1062 nodes[n].next = first_free_node;
1063 first_free_node = n;
1067 void erase(const Edge& edge) {
1068 int n = edge.id * 2;
1070 if (arcs[n].next_out != -1) {
1071 arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
1074 if (arcs[n].prev_out != -1) {
1075 arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
1077 nodes[arcs[n | 1].target].first_out = arcs[n].next_out;
1080 if (arcs[n | 1].next_out != -1) {
1081 arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
1084 if (arcs[n | 1].prev_out != -1) {
1085 arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
1087 nodes[arcs[n].target].first_out = arcs[n | 1].next_out;
1090 arcs[n].next_out = first_free_arc;
1092 arcs[n].prev_out = -2;
1093 arcs[n | 1].prev_out = -2;
1100 first_node = first_free_node = first_free_arc = -1;
1105 void changeTarget(Edge e, Node n) {
1106 if(arcs[2 * e.id].next_out != -1) {
1107 arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out;
1109 if(arcs[2 * e.id].prev_out != -1) {
1110 arcs[arcs[2 * e.id].prev_out].next_out =
1111 arcs[2 * e.id].next_out;
1113 nodes[arcs[(2 * e.id) | 1].target].first_out =
1114 arcs[2 * e.id].next_out;
1117 if (nodes[n.id].first_out != -1) {
1118 arcs[nodes[n.id].first_out].prev_out = 2 * e.id;
1120 arcs[(2 * e.id) | 1].target = n.id;
1121 arcs[2 * e.id].prev_out = -1;
1122 arcs[2 * e.id].next_out = nodes[n.id].first_out;
1123 nodes[n.id].first_out = 2 * e.id;
1126 void changeSource(Edge e, Node n) {
1127 if(arcs[(2 * e.id) | 1].next_out != -1) {
1128 arcs[arcs[(2 * e.id) | 1].next_out].prev_out =
1129 arcs[(2 * e.id) | 1].prev_out;
1131 if(arcs[(2 * e.id) | 1].prev_out != -1) {
1132 arcs[arcs[(2 * e.id) | 1].prev_out].next_out =
1133 arcs[(2 * e.id) | 1].next_out;
1135 nodes[arcs[2 * e.id].target].first_out =
1136 arcs[(2 * e.id) | 1].next_out;
1139 if (nodes[n.id].first_out != -1) {
1140 arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1);
1142 arcs[2 * e.id].target = n.id;
1143 arcs[(2 * e.id) | 1].prev_out = -1;
1144 arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out;
1145 nodes[n.id].first_out = ((2 * e.id) | 1);
1150 typedef GraphExtender<ListGraphBase> ExtendedListGraphBase;
1153 /// \addtogroup graphs
1156 ///A general undirected graph structure.
1158 ///\ref ListGraph is a simple and fast <em>undirected graph</em>
1159 ///implementation based on static linked lists that are stored in
1160 ///\c std::vector structures.
1162 ///It conforms to the \ref concepts::Graph "Graph concept" and it
1163 ///also provides several useful additional functionalities.
1164 ///Most of the member functions and nested classes are documented
1165 ///only in the concept class.
1167 ///An important extra feature of this graph implementation is that
1168 ///its maps are real \ref concepts::ReferenceMap "reference map"s.
1170 ///\sa concepts::Graph
1172 class ListGraph : public ExtendedListGraphBase {
1174 ///ListGraph is \e not copy constructible. Use copyGraph() instead.
1176 ///ListGraph is \e not copy constructible. Use copyGraph() instead.
1178 ListGraph(const ListGraph &) :ExtendedListGraphBase() {};
1179 ///\brief Assignment of ListGraph to another one is \e not allowed.
1180 ///Use copyGraph() instead.
1182 ///Assignment of ListGraph to another one is \e not allowed.
1183 ///Use copyGraph() instead.
1184 void operator=(const ListGraph &) {}
1192 typedef ExtendedListGraphBase Parent;
1194 typedef Parent::OutArcIt IncEdgeIt;
1196 /// \brief Add a new node to the graph.
1198 /// Add a new node to the graph.
1199 /// \return the new node.
1200 Node addNode() { return Parent::addNode(); }
1202 /// \brief Add a new edge to the graph.
1204 /// Add a new edge to the graph with source node \c s
1205 /// and target node \c t.
1206 /// \return the new edge.
1207 Edge addEdge(const Node& s, const Node& t) {
1208 return Parent::addEdge(s, t);
1210 /// Node validity check
1212 /// This function gives back true if the given node is valid,
1213 /// ie. it is a real node of the graph.
1215 /// \warning A Node pointing to a removed item
1216 /// could become valid again later if new nodes are
1217 /// added to the graph.
1218 bool valid(Node n) const { return Parent::valid(n); }
1219 /// Arc validity check
1221 /// This function gives back true if the given arc is valid,
1222 /// ie. it is a real arc of the graph.
1224 /// \warning An Arc pointing to a removed item
1225 /// could become valid again later if new edges are
1226 /// added to the graph.
1227 bool valid(Arc a) const { return Parent::valid(a); }
1228 /// Edge validity check
1230 /// This function gives back true if the given edge is valid,
1231 /// ie. it is a real arc of the graph.
1233 /// \warning A Edge pointing to a removed item
1234 /// could become valid again later if new edges are
1235 /// added to the graph.
1236 bool valid(Edge e) const { return Parent::valid(e); }
1237 /// \brief Change the source of \c e to \c n
1239 /// This function changes the source of \c e to \c n.
1241 ///\note The <tt>ArcIt</tt>s and <tt>InArcIt</tt>s
1242 ///referencing the changed arc remain
1243 ///valid. However <tt>OutArcIt</tt>s are invalidated.
1245 ///\warning This functionality cannot be used together with the
1246 ///Snapshot feature.
1247 void changeSource(Edge e, Node n) {
1248 Parent::changeSource(e,n);
1250 /// \brief Change the target of \c e to \c n
1252 /// This function changes the target of \c e to \c n.
1254 /// \note The <tt>ArcIt</tt>s referencing the changed arc remain
1255 /// valid. However the other iterators may be invalidated.
1257 ///\warning This functionality cannot be used together with the
1258 ///Snapshot feature.
1259 void changeTarget(Edge e, Node n) {
1260 Parent::changeTarget(e,n);
1262 /// \brief Change the source of \c e to \c n
1264 /// This function changes the source of \c e to \c n.
1265 /// It also changes the proper node of the represented edge.
1267 ///\note The <tt>ArcIt</tt>s and <tt>InArcIt</tt>s
1268 ///referencing the changed arc remain
1269 ///valid. However <tt>OutArcIt</tt>s are invalidated.
1271 ///\warning This functionality cannot be used together with the
1272 ///Snapshot feature.
1273 void changeSource(Arc e, Node n) {
1274 if (Parent::direction(e)) {
1275 Parent::changeSource(e,n);
1277 Parent::changeTarget(e,n);
1280 /// \brief Change the target of \c e to \c n
1282 /// This function changes the target of \c e to \c n.
1283 /// It also changes the proper node of the represented edge.
1285 ///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s
1286 ///referencing the changed arc remain
1287 ///valid. However <tt>InArcIt</tt>s are invalidated.
1289 ///\warning This functionality cannot be used together with the
1290 ///Snapshot feature.
1291 void changeTarget(Arc e, Node n) {
1292 if (Parent::direction(e)) {
1293 Parent::changeTarget(e,n);
1295 Parent::changeSource(e,n);
1298 /// \brief Contract two nodes.
1300 /// This function contracts two nodes.
1301 /// Node \p b will be removed but instead of deleting
1302 /// its neighboring arcs, they will be joined to \p a.
1303 /// The last parameter \p r controls whether to remove loops. \c true
1304 /// means that loops will be removed.
1306 /// \note The <tt>ArcIt</tt>s referencing a moved arc remain
1309 ///\warning This functionality cannot be used together with the
1310 ///Snapshot feature.
1311 void contract(Node a, Node b, bool r = true) {
1312 for(IncEdgeIt e(*this, b); e!=INVALID;) {
1313 IncEdgeIt f = e; ++f;
1314 if (r && runningNode(e) == a) {
1316 } else if (source(e) == b) {
1327 /// \brief Class to make a snapshot of the graph and restore
1330 /// Class to make a snapshot of the graph and restore it later.
1332 /// The newly added nodes and edges can be removed
1333 /// using the restore() function.
1335 /// \warning Edge and node deletions and other modifications
1336 /// (e.g. changing nodes of edges, contracting nodes) cannot be
1337 /// restored. These events invalidate the snapshot.
1341 typedef Parent::NodeNotifier NodeNotifier;
1343 class NodeObserverProxy : public NodeNotifier::ObserverBase {
1346 NodeObserverProxy(Snapshot& _snapshot)
1347 : snapshot(_snapshot) {}
1349 using NodeNotifier::ObserverBase::attach;
1350 using NodeNotifier::ObserverBase::detach;
1351 using NodeNotifier::ObserverBase::attached;
1355 virtual void add(const Node& node) {
1356 snapshot.addNode(node);
1358 virtual void add(const std::vector<Node>& nodes) {
1359 for (int i = nodes.size() - 1; i >= 0; ++i) {
1360 snapshot.addNode(nodes[i]);
1363 virtual void erase(const Node& node) {
1364 snapshot.eraseNode(node);
1366 virtual void erase(const std::vector<Node>& nodes) {
1367 for (int i = 0; i < int(nodes.size()); ++i) {
1368 snapshot.eraseNode(nodes[i]);
1371 virtual void build() {
1373 std::vector<Node> nodes;
1374 for (notifier()->first(node); node != INVALID;
1375 notifier()->next(node)) {
1376 nodes.push_back(node);
1378 for (int i = nodes.size() - 1; i >= 0; --i) {
1379 snapshot.addNode(nodes[i]);
1382 virtual void clear() {
1384 for (notifier()->first(node); node != INVALID;
1385 notifier()->next(node)) {
1386 snapshot.eraseNode(node);
1393 class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
1396 EdgeObserverProxy(Snapshot& _snapshot)
1397 : snapshot(_snapshot) {}
1399 using EdgeNotifier::ObserverBase::attach;
1400 using EdgeNotifier::ObserverBase::detach;
1401 using EdgeNotifier::ObserverBase::attached;
1405 virtual void add(const Edge& edge) {
1406 snapshot.addEdge(edge);
1408 virtual void add(const std::vector<Edge>& edges) {
1409 for (int i = edges.size() - 1; i >= 0; ++i) {
1410 snapshot.addEdge(edges[i]);
1413 virtual void erase(const Edge& edge) {
1414 snapshot.eraseEdge(edge);
1416 virtual void erase(const std::vector<Edge>& edges) {
1417 for (int i = 0; i < int(edges.size()); ++i) {
1418 snapshot.eraseEdge(edges[i]);
1421 virtual void build() {
1423 std::vector<Edge> edges;
1424 for (notifier()->first(edge); edge != INVALID;
1425 notifier()->next(edge)) {
1426 edges.push_back(edge);
1428 for (int i = edges.size() - 1; i >= 0; --i) {
1429 snapshot.addEdge(edges[i]);
1432 virtual void clear() {
1434 for (notifier()->first(edge); edge != INVALID;
1435 notifier()->next(edge)) {
1436 snapshot.eraseEdge(edge);
1445 NodeObserverProxy node_observer_proxy;
1446 EdgeObserverProxy edge_observer_proxy;
1448 std::list<Node> added_nodes;
1449 std::list<Edge> added_edges;
1452 void addNode(const Node& node) {
1453 added_nodes.push_front(node);
1455 void eraseNode(const Node& node) {
1456 std::list<Node>::iterator it =
1457 std::find(added_nodes.begin(), added_nodes.end(), node);
1458 if (it == added_nodes.end()) {
1460 edge_observer_proxy.detach();
1461 throw NodeNotifier::ImmediateDetach();
1463 added_nodes.erase(it);
1467 void addEdge(const Edge& edge) {
1468 added_edges.push_front(edge);
1470 void eraseEdge(const Edge& edge) {
1471 std::list<Edge>::iterator it =
1472 std::find(added_edges.begin(), added_edges.end(), edge);
1473 if (it == added_edges.end()) {
1475 node_observer_proxy.detach();
1476 throw EdgeNotifier::ImmediateDetach();
1478 added_edges.erase(it);
1482 void attach(ListGraph &_graph) {
1484 node_observer_proxy.attach(graph->notifier(Node()));
1485 edge_observer_proxy.attach(graph->notifier(Edge()));
1489 node_observer_proxy.detach();
1490 edge_observer_proxy.detach();
1493 bool attached() const {
1494 return node_observer_proxy.attached();
1498 added_nodes.clear();
1499 added_edges.clear();
1504 /// \brief Default constructor.
1506 /// Default constructor.
1507 /// To actually make a snapshot you must call save().
1509 : graph(0), node_observer_proxy(*this),
1510 edge_observer_proxy(*this) {}
1512 /// \brief Constructor that immediately makes a snapshot.
1514 /// This constructor immediately makes a snapshot of the graph.
1515 /// \param _graph The graph we make a snapshot of.
1516 Snapshot(ListGraph &_graph)
1517 : node_observer_proxy(*this),
1518 edge_observer_proxy(*this) {
1522 /// \brief Make a snapshot.
1524 /// Make a snapshot of the graph.
1526 /// This function can be called more than once. In case of a repeated
1527 /// call, the previous snapshot gets lost.
1528 /// \param _graph The graph we make the snapshot of.
1529 void save(ListGraph &_graph) {
1537 /// \brief Undo the changes until the last snapshot.
1539 /// Undo the changes until the last snapshot created by save().
1542 for(std::list<Edge>::iterator it = added_edges.begin();
1543 it != added_edges.end(); ++it) {
1546 for(std::list<Node>::iterator it = added_nodes.begin();
1547 it != added_nodes.end(); ++it) {
1553 /// \brief Gives back true when the snapshot is valid.
1555 /// Gives back true when the snapshot is valid.
1556 bool valid() const {