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_SMART_GRAPH_H
20 #define LEMON_SMART_GRAPH_H
24 ///\brief SmartDigraph and SmartGraph classes.
28 #include <lemon/bits/invalid.h>
30 #include <lemon/bits/base_extender.h>
31 #include <lemon/bits/graph_extender.h>
33 #include <lemon/bits/utility.h>
34 #include <lemon/error.h>
36 #include <lemon/bits/graph_extender.h>
41 ///Base of SmartDigraph
43 ///Base of SmartDigraph
45 class SmartDigraphBase {
50 int first_in, first_out;
55 int target, source, next_in, next_out;
59 std::vector<NodeT> nodes;
60 std::vector<ArcT> arcs;
64 typedef SmartDigraphBase Graph;
71 SmartDigraphBase() : nodes(), arcs() { }
72 SmartDigraphBase(const SmartDigraphBase &_g)
73 : nodes(_g.nodes), arcs(_g.arcs) { }
75 typedef True NodeNumTag;
76 typedef True ArcNumTag;
78 int nodeNum() const { return nodes.size(); }
79 int arcNum() const { return arcs.size(); }
81 int maxNodeId() const { return nodes.size()-1; }
82 int maxArcId() const { return arcs.size()-1; }
86 nodes.push_back(NodeT());
87 nodes[n].first_in = -1;
88 nodes[n].first_out = -1;
92 Arc addArc(Node u, Node v) {
94 arcs.push_back(ArcT());
95 arcs[n].source = u._id;
96 arcs[n].target = v._id;
97 arcs[n].next_out = nodes[u._id].first_out;
98 arcs[n].next_in = nodes[v._id].first_in;
99 nodes[u._id].first_out = nodes[v._id].first_in = n;
109 Node source(Arc a) const { return Node(arcs[a._id].source); }
110 Node target(Arc a) const { return Node(arcs[a._id].target); }
112 static int id(Node v) { return v._id; }
113 static int id(Arc a) { return a._id; }
115 static Node nodeFromId(int id) { return Node(id);}
116 static Arc arcFromId(int id) { return Arc(id);}
119 friend class SmartDigraphBase;
120 friend class SmartDigraph;
124 explicit Node(int id) : _id(id) {}
127 Node (Invalid) : _id(-1) {}
128 bool operator==(const Node i) const {return _id == i._id;}
129 bool operator!=(const Node i) const {return _id != i._id;}
130 bool operator<(const Node i) const {return _id < i._id;}
135 friend class SmartDigraphBase;
136 friend class SmartDigraph;
140 explicit Arc(int id) : _id(id) {}
143 Arc (Invalid) : _id(-1) {}
144 bool operator==(const Arc i) const {return _id == i._id;}
145 bool operator!=(const Arc i) const {return _id != i._id;}
146 bool operator<(const Arc i) const {return _id < i._id;}
149 void first(Node& node) const {
150 node._id = nodes.size() - 1;
153 static void next(Node& node) {
157 void first(Arc& arc) const {
158 arc._id = arcs.size() - 1;
161 static void next(Arc& arc) {
165 void firstOut(Arc& arc, const Node& node) const {
166 arc._id = nodes[node._id].first_out;
169 void nextOut(Arc& arc) const {
170 arc._id = arcs[arc._id].next_out;
173 void firstIn(Arc& arc, const Node& node) const {
174 arc._id = nodes[node._id].first_in;
177 void nextIn(Arc& arc) const {
178 arc._id = arcs[arc._id].next_in;
183 typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase;
187 ///\brief A smart directed graph class.
189 ///This is a simple and fast digraph implementation.
190 ///It is also quite memory efficient, but at the price
191 ///that <b> it does support only limited (only stack-like)
192 ///node and arc deletions</b>.
193 ///It conforms to the \ref concepts::Digraph "Digraph concept" with
194 ///an important extra feature that its maps are real \ref
195 ///concepts::ReferenceMap "reference map"s.
197 ///\sa concepts::Digraph.
199 ///\author Alpar Juttner
200 class SmartDigraph : public ExtendedSmartDigraphBase {
203 typedef ExtendedSmartDigraphBase Parent;
207 ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
209 ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
211 SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
212 ///\brief Assignment of SmartDigraph to another one is \e not allowed.
213 ///Use DigraphCopy() instead.
215 ///Assignment of SmartDigraph to another one is \e not allowed.
216 ///Use DigraphCopy() instead.
217 void operator=(const SmartDigraph &) {}
227 ///Add a new node to the digraph.
229 /// \return the new node.
231 Node addNode() { return Parent::addNode(); }
233 ///Add a new arc to the digraph.
235 ///Add a new arc to the digraph with source node \c s
236 ///and target node \c t.
237 ///\return the new arc.
238 Arc addArc(const Node& s, const Node& t) {
239 return Parent::addArc(s, t);
242 /// \brief Using this it is possible to avoid the superfluous memory
245 /// Using this it is possible to avoid the superfluous memory
246 /// allocation: if you know that the digraph you want to build will
247 /// be very large (e.g. it will contain millions of nodes and/or arcs)
248 /// then it is worth reserving space for this amount before starting
249 /// to build the digraph.
251 void reserveNode(int n) { nodes.reserve(n); };
253 /// \brief Using this it is possible to avoid the superfluous memory
256 /// Using this it is possible to avoid the superfluous memory
257 /// allocation: if you know that the digraph you want to build will
258 /// be very large (e.g. it will contain millions of nodes and/or arcs)
259 /// then it is worth reserving space for this amount before starting
260 /// to build the digraph.
262 void reserveArc(int m) { arcs.reserve(m); };
264 ///Clear the digraph.
266 ///Erase all the nodes and arcs from the digraph.
274 ///This function splits a node. First a new node is added to the digraph,
275 ///then the source of each outgoing arc of \c n is moved to this new node.
276 ///If \c connect is \c true (this is the default value), then a new arc
277 ///from \c n to the newly created node is also added.
278 ///\return The newly created node.
280 ///\note The <tt>Arc</tt>s
281 ///referencing a moved arc remain
282 ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
283 ///may be invalidated.
284 ///\warning This functionality cannot be used together with the Snapshot
286 ///\todo It could be implemented in a bit faster way.
287 Node split(Node n, bool connect = true)
290 nodes[b._id].first_out=nodes[n._id].first_out;
291 nodes[n._id].first_out=-1;
292 for(int i=nodes[b._id].first_out;i!=-1;i++) arcs[i].source=b._id;
293 if(connect) addArc(n,b);
303 void restoreSnapshot(const Snapshot &s)
305 while(s.arc_num<arcs.size()) {
306 Arc arc = arcFromId(arcs.size()-1);
307 Parent::notifier(Arc()).erase(arc);
308 nodes[arcs.back().source].first_out=arcs.back().next_out;
309 nodes[arcs.back().target].first_in=arcs.back().next_in;
312 while(s.node_num<nodes.size()) {
313 Node node = nodeFromId(nodes.size()-1);
314 Parent::notifier(Node()).erase(node);
321 ///Class to make a snapshot of the digraph and to restrore to it later.
323 ///Class to make a snapshot of the digraph and to restrore to it later.
325 ///The newly added nodes and arcs can be removed using the
326 ///restore() function.
327 ///\note After you restore a state, you cannot restore
328 ///a later state, in other word you cannot add again the arcs deleted
329 ///by restore() using another one Snapshot instance.
331 ///\warning If you do not use correctly the snapshot that can cause
332 ///either broken program, invalid state of the digraph, valid but
333 ///not the restored digraph or no change. Because the runtime performance
334 ///the validity of the snapshot is not stored.
337 SmartDigraph *_graph;
339 friend class SmartDigraph;
340 unsigned int node_num;
341 unsigned int arc_num;
343 ///Default constructor.
345 ///Default constructor.
346 ///To actually make a snapshot you must call save().
348 Snapshot() : _graph(0) {}
349 ///Constructor that immediately makes a snapshot
351 ///This constructor immediately makes a snapshot of the digraph.
352 ///\param _g The digraph we make a snapshot of.
353 Snapshot(SmartDigraph &graph) : _graph(&graph) {
354 node_num=_graph->nodes.size();
355 arc_num=_graph->arcs.size();
360 ///Make a snapshot of the digraph.
362 ///This function can be called more than once. In case of a repeated
363 ///call, the previous snapshot gets lost.
364 ///\param _g The digraph we make the snapshot of.
365 void save(SmartDigraph &graph)
368 node_num=_graph->nodes.size();
369 arc_num=_graph->arcs.size();
372 ///Undo the changes until a snapshot.
374 ///Undo the changes until a snapshot created by save().
376 ///\note After you restored a state, you cannot restore
377 ///a later state, in other word you cannot add again the arcs deleted
381 _graph->restoreSnapshot(*this);
387 class SmartGraphBase {
400 std::vector<NodeT> nodes;
401 std::vector<ArcT> arcs;
407 typedef SmartGraphBase Digraph;
414 friend class SmartGraphBase;
418 explicit Node(int id) { _id = id;}
422 Node (Invalid) { _id = -1; }
423 bool operator==(const Node& node) const {return _id == node._id;}
424 bool operator!=(const Node& node) const {return _id != node._id;}
425 bool operator<(const Node& node) const {return _id < node._id;}
429 friend class SmartGraphBase;
433 explicit Edge(int id) { _id = id;}
437 Edge (Invalid) { _id = -1; }
438 bool operator==(const Edge& arc) const {return _id == arc._id;}
439 bool operator!=(const Edge& arc) const {return _id != arc._id;}
440 bool operator<(const Edge& arc) const {return _id < arc._id;}
444 friend class SmartGraphBase;
448 explicit Arc(int id) { _id = id;}
451 operator Edge() const { return edgeFromId(_id / 2); }
454 Arc (Invalid) { _id = -1; }
455 bool operator==(const Arc& arc) const {return _id == arc._id;}
456 bool operator!=(const Arc& arc) const {return _id != arc._id;}
457 bool operator<(const Arc& arc) const {return _id < arc._id;}
466 int maxNodeId() const { return nodes.size()-1; }
467 int maxEdgeId() const { return arcs.size() / 2 - 1; }
468 int maxArcId() const { return arcs.size()-1; }
470 Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
471 Node target(Arc e) const { return Node(arcs[e._id].target); }
473 Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
474 Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
476 static bool direction(Arc e) {
477 return (e._id & 1) == 1;
480 static Arc direct(Edge e, bool d) {
481 return Arc(e._id * 2 + (d ? 1 : 0));
484 void first(Node& node) const {
485 node._id = nodes.size() - 1;
488 void next(Node& node) const {
492 void first(Arc& arc) const {
493 arc._id = arcs.size() - 1;
496 void next(Arc& arc) const {
500 void first(Edge& arc) const {
501 arc._id = arcs.size() / 2 - 1;
504 void next(Edge& arc) const {
508 void firstOut(Arc &arc, const Node& v) const {
509 arc._id = nodes[v._id].first_out;
511 void nextOut(Arc &arc) const {
512 arc._id = arcs[arc._id].next_out;
515 void firstIn(Arc &arc, const Node& v) const {
516 arc._id = ((nodes[v._id].first_out) ^ 1);
517 if (arc._id == -2) arc._id = -1;
519 void nextIn(Arc &arc) const {
520 arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
521 if (arc._id == -2) arc._id = -1;
524 void firstInc(Edge &arc, bool& d, const Node& v) const {
525 int de = nodes[v._id].first_out;
534 void nextInc(Edge &arc, bool& d) const {
535 int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
545 static int id(Node v) { return v._id; }
546 static int id(Arc e) { return e._id; }
547 static int id(Edge e) { return e._id; }
549 static Node nodeFromId(int id) { return Node(id);}
550 static Arc arcFromId(int id) { return Arc(id);}
551 static Edge edgeFromId(int id) { return Edge(id);}
554 int n = nodes.size();
555 nodes.push_back(NodeT());
556 nodes[n].first_out = -1;
561 Edge addArc(Node u, Node v) {
563 arcs.push_back(ArcT());
564 arcs.push_back(ArcT());
566 arcs[n].target = u._id;
567 arcs[n | 1].target = v._id;
569 arcs[n].next_out = nodes[v._id].first_out;
570 nodes[v._id].first_out = n;
572 arcs[n | 1].next_out = nodes[u._id].first_out;
573 nodes[u._id].first_out = (n | 1);
585 typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
589 /// \brief A smart undirected graph class.
591 /// This is a simple and fast graph implementation.
592 /// It is also quite memory efficient, but at the price
593 /// that <b> it does support only limited (only stack-like)
594 /// node and arc deletions</b>.
595 /// Except from this it conforms to
596 /// the \ref concepts::Graph "Graph concept".
599 /// important extra feature that
600 /// its maps are real \ref concepts::ReferenceMap "reference map"s.
602 /// \sa concepts::Graph.
604 class SmartGraph : public ExtendedSmartGraphBase {
607 ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
609 ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
611 SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
613 ///\brief Assignment of SmartGraph to another one is \e not allowed.
614 ///Use GraphCopy() instead.
616 ///Assignment of SmartGraph to another one is \e not allowed.
617 ///Use GraphCopy() instead.
618 void operator=(const SmartGraph &) {}
622 typedef ExtendedSmartGraphBase Parent;
630 ///Add a new node to the graph.
632 /// \return the new node.
634 Node addNode() { return Parent::addNode(); }
636 ///Add a new edge to the graph.
638 ///Add a new edge to the graph with node \c s
640 ///\return the new edge.
641 Edge addEdge(const Node& s, const Node& t) {
642 return Parent::addArc(s, t);
647 ///Erase all the nodes and edges from the graph.
659 void saveSnapshot(Snapshot &s)
662 s.node_num = nodes.size();
663 s.arc_num = arcs.size();
666 void restoreSnapshot(const Snapshot &s)
668 while(s.arc_num<arcs.size()) {
670 Edge arc=edgeFromId(n/2);
671 Parent::notifier(Edge()).erase(arc);
672 std::vector<Arc> dir;
673 dir.push_back(arcFromId(n));
674 dir.push_back(arcFromId(n-1));
675 Parent::notifier(Arc()).erase(dir);
676 nodes[arcs[n].target].first_out=arcs[n].next_out;
677 nodes[arcs[n-1].target].first_out=arcs[n-1].next_out;
681 while(s.node_num<nodes.size()) {
682 int n=nodes.size()-1;
683 Node node = nodeFromId(n);
684 Parent::notifier(Node()).erase(node);
691 ///Class to make a snapshot of the digraph and to restrore to it later.
693 ///Class to make a snapshot of the digraph and to restrore to it later.
695 ///The newly added nodes and arcs can be removed using the
696 ///restore() function.
698 ///\note After you restore a state, you cannot restore
699 ///a later state, in other word you cannot add again the arcs deleted
700 ///by restore() using another one Snapshot instance.
702 ///\warning If you do not use correctly the snapshot that can cause
703 ///either broken program, invalid state of the digraph, valid but
704 ///not the restored digraph or no change. Because the runtime performance
705 ///the validity of the snapshot is not stored.
710 friend class SmartGraph;
711 unsigned int node_num;
712 unsigned int arc_num;
714 ///Default constructor.
716 ///Default constructor.
717 ///To actually make a snapshot you must call save().
719 Snapshot() : _graph(0) {}
720 ///Constructor that immediately makes a snapshot
722 ///This constructor immediately makes a snapshot of the digraph.
723 ///\param g The digraph we make a snapshot of.
724 Snapshot(SmartGraph &graph) {
725 graph.saveSnapshot(*this);
730 ///Make a snapshot of the graph.
732 ///This function can be called more than once. In case of a repeated
733 ///call, the previous snapshot gets lost.
734 ///\param g The digraph we make the snapshot of.
735 void save(SmartGraph &graph)
737 graph.saveSnapshot(*this);
740 ///Undo the changes until a snapshot.
742 ///Undo the changes until a snapshot created by save().
744 ///\note After you restored a state, you cannot restore
745 ///a later state, in other word you cannot add again the arcs deleted
749 _graph->restoreSnapshot(*this);
757 #endif //LEMON_SMART_GRAPH_H