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_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 EdgeNumTag;
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);}
118 bool valid(Node n) const {
119 return n._id >= 0 && n._id < static_cast<int>(nodes.size());
121 bool valid(Arc a) const {
122 return a._id >= 0 && a._id < static_cast<int>(arcs.size());
126 friend class SmartDigraphBase;
127 friend class SmartDigraph;
131 explicit Node(int id) : _id(id) {}
134 Node (Invalid) : _id(-1) {}
135 bool operator==(const Node i) const {return _id == i._id;}
136 bool operator!=(const Node i) const {return _id != i._id;}
137 bool operator<(const Node i) const {return _id < i._id;}
142 friend class SmartDigraphBase;
143 friend class SmartDigraph;
147 explicit Arc(int id) : _id(id) {}
150 Arc (Invalid) : _id(-1) {}
151 bool operator==(const Arc i) const {return _id == i._id;}
152 bool operator!=(const Arc i) const {return _id != i._id;}
153 bool operator<(const Arc i) const {return _id < i._id;}
156 void first(Node& node) const {
157 node._id = nodes.size() - 1;
160 static void next(Node& node) {
164 void first(Arc& arc) const {
165 arc._id = arcs.size() - 1;
168 static void next(Arc& arc) {
172 void firstOut(Arc& arc, const Node& node) const {
173 arc._id = nodes[node._id].first_out;
176 void nextOut(Arc& arc) const {
177 arc._id = arcs[arc._id].next_out;
180 void firstIn(Arc& arc, const Node& node) const {
181 arc._id = nodes[node._id].first_in;
184 void nextIn(Arc& arc) const {
185 arc._id = arcs[arc._id].next_in;
190 typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase;
194 ///\brief A smart directed graph class.
196 ///This is a simple and fast digraph implementation.
197 ///It is also quite memory efficient, but at the price
198 ///that <b> it does support only limited (only stack-like)
199 ///node and arc deletions</b>.
200 ///It conforms to the \ref concepts::Digraph "Digraph concept" with
201 ///an important extra feature that its maps are real \ref
202 ///concepts::ReferenceMap "reference map"s.
204 ///\sa concepts::Digraph.
205 class SmartDigraph : public ExtendedSmartDigraphBase {
208 typedef ExtendedSmartDigraphBase Parent;
212 ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
214 ///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead.
216 SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
217 ///\brief Assignment of SmartDigraph to another one is \e not allowed.
218 ///Use DigraphCopy() instead.
220 ///Assignment of SmartDigraph to another one is \e not allowed.
221 ///Use DigraphCopy() instead.
222 void operator=(const SmartDigraph &) {}
232 ///Add a new node to the digraph.
234 /// \return the new node.
236 Node addNode() { return Parent::addNode(); }
238 ///Add a new arc to the digraph.
240 ///Add a new arc to the digraph with source node \c s
241 ///and target node \c t.
242 ///\return the new arc.
243 Arc addArc(const Node& s, const Node& t) {
244 return Parent::addArc(s, t);
247 /// \brief Using this it is possible to avoid the superfluous memory
250 /// Using this it is possible to avoid the superfluous memory
251 /// allocation: if you know that the digraph you want to build will
252 /// be very large (e.g. it will contain millions of nodes and/or arcs)
253 /// then it is worth reserving space for this amount before starting
254 /// to build the digraph.
256 void reserveNode(int n) { nodes.reserve(n); };
258 /// \brief Using this it is possible to avoid the superfluous memory
261 /// Using this it is possible to avoid the superfluous memory
262 /// allocation: if you know that the digraph you want to build will
263 /// be very large (e.g. it will contain millions of nodes and/or arcs)
264 /// then it is worth reserving space for this amount before starting
265 /// to build the digraph.
267 void reserveArc(int m) { arcs.reserve(m); };
269 /// \brief Node validity check
271 /// This function gives back true if the given node is valid,
272 /// ie. it is a real node of the graph.
274 /// \warning A removed node (using Snapshot) could become valid again
275 /// when new nodes are added to the graph.
276 bool valid(Node n) const { return Parent::valid(n); }
278 /// \brief Arc validity check
280 /// This function gives back true if the given arc is valid,
281 /// ie. it is a real arc of the graph.
283 /// \warning A removed arc (using Snapshot) could become valid again
284 /// when new arcs are added to the graph.
285 bool valid(Arc a) const { return Parent::valid(a); }
287 ///Clear the digraph.
289 ///Erase all the nodes and arcs from the digraph.
297 ///This function splits a node. First a new node is added to the digraph,
298 ///then the source of each outgoing arc of \c n is moved to this new node.
299 ///If \c connect is \c true (this is the default value), then a new arc
300 ///from \c n to the newly created node is also added.
301 ///\return The newly created node.
303 ///\note The <tt>Arc</tt>s
304 ///referencing a moved arc remain
305 ///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s
306 ///may be invalidated.
307 ///\warning This functionality cannot be used together with the Snapshot
309 ///\todo It could be implemented in a bit faster way.
310 Node split(Node n, bool connect = true)
313 nodes[b._id].first_out=nodes[n._id].first_out;
314 nodes[n._id].first_out=-1;
315 for(int i=nodes[b._id].first_out;i!=-1;i++) arcs[i].source=b._id;
316 if(connect) addArc(n,b);
326 void restoreSnapshot(const Snapshot &s)
328 while(s.arc_num<arcs.size()) {
329 Arc arc = arcFromId(arcs.size()-1);
330 Parent::notifier(Arc()).erase(arc);
331 nodes[arcs.back().source].first_out=arcs.back().next_out;
332 nodes[arcs.back().target].first_in=arcs.back().next_in;
335 while(s.node_num<nodes.size()) {
336 Node node = nodeFromId(nodes.size()-1);
337 Parent::notifier(Node()).erase(node);
344 ///Class to make a snapshot of the digraph and to restrore to it later.
346 ///Class to make a snapshot of the digraph and to restrore to it later.
348 ///The newly added nodes and arcs can be removed using the
349 ///restore() function.
350 ///\note After you restore a state, you cannot restore
351 ///a later state, in other word you cannot add again the arcs deleted
352 ///by restore() using another one Snapshot instance.
354 ///\warning If you do not use correctly the snapshot that can cause
355 ///either broken program, invalid state of the digraph, valid but
356 ///not the restored digraph or no change. Because the runtime performance
357 ///the validity of the snapshot is not stored.
360 SmartDigraph *_graph;
362 friend class SmartDigraph;
363 unsigned int node_num;
364 unsigned int arc_num;
366 ///Default constructor.
368 ///Default constructor.
369 ///To actually make a snapshot you must call save().
371 Snapshot() : _graph(0) {}
372 ///Constructor that immediately makes a snapshot
374 ///This constructor immediately makes a snapshot of the digraph.
375 ///\param _g The digraph we make a snapshot of.
376 Snapshot(SmartDigraph &graph) : _graph(&graph) {
377 node_num=_graph->nodes.size();
378 arc_num=_graph->arcs.size();
383 ///Make a snapshot of the digraph.
385 ///This function can be called more than once. In case of a repeated
386 ///call, the previous snapshot gets lost.
387 ///\param _g The digraph we make the snapshot of.
388 void save(SmartDigraph &graph)
391 node_num=_graph->nodes.size();
392 arc_num=_graph->arcs.size();
395 ///Undo the changes until a snapshot.
397 ///Undo the changes until a snapshot created by save().
399 ///\note After you restored a state, you cannot restore
400 ///a later state, in other word you cannot add again the arcs deleted
404 _graph->restoreSnapshot(*this);
410 class SmartGraphBase {
423 std::vector<NodeT> nodes;
424 std::vector<ArcT> arcs;
430 typedef SmartGraphBase Digraph;
437 friend class SmartGraphBase;
441 explicit Node(int id) { _id = id;}
445 Node (Invalid) { _id = -1; }
446 bool operator==(const Node& node) const {return _id == node._id;}
447 bool operator!=(const Node& node) const {return _id != node._id;}
448 bool operator<(const Node& node) const {return _id < node._id;}
452 friend class SmartGraphBase;
456 explicit Edge(int id) { _id = id;}
460 Edge (Invalid) { _id = -1; }
461 bool operator==(const Edge& arc) const {return _id == arc._id;}
462 bool operator!=(const Edge& arc) const {return _id != arc._id;}
463 bool operator<(const Edge& arc) const {return _id < arc._id;}
467 friend class SmartGraphBase;
471 explicit Arc(int id) { _id = id;}
474 operator Edge() const { return edgeFromId(_id / 2); }
477 Arc (Invalid) { _id = -1; }
478 bool operator==(const Arc& arc) const {return _id == arc._id;}
479 bool operator!=(const Arc& arc) const {return _id != arc._id;}
480 bool operator<(const Arc& arc) const {return _id < arc._id;}
489 int maxNodeId() const { return nodes.size()-1; }
490 int maxEdgeId() const { return arcs.size() / 2 - 1; }
491 int maxArcId() const { return arcs.size()-1; }
493 Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
494 Node target(Arc e) const { return Node(arcs[e._id].target); }
496 Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
497 Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
499 static bool direction(Arc e) {
500 return (e._id & 1) == 1;
503 static Arc direct(Edge e, bool d) {
504 return Arc(e._id * 2 + (d ? 1 : 0));
507 void first(Node& node) const {
508 node._id = nodes.size() - 1;
511 void next(Node& node) const {
515 void first(Arc& arc) const {
516 arc._id = arcs.size() - 1;
519 void next(Arc& arc) const {
523 void first(Edge& arc) const {
524 arc._id = arcs.size() / 2 - 1;
527 void next(Edge& arc) const {
531 void firstOut(Arc &arc, const Node& v) const {
532 arc._id = nodes[v._id].first_out;
534 void nextOut(Arc &arc) const {
535 arc._id = arcs[arc._id].next_out;
538 void firstIn(Arc &arc, const Node& v) const {
539 arc._id = ((nodes[v._id].first_out) ^ 1);
540 if (arc._id == -2) arc._id = -1;
542 void nextIn(Arc &arc) const {
543 arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
544 if (arc._id == -2) arc._id = -1;
547 void firstInc(Edge &arc, bool& d, const Node& v) const {
548 int de = nodes[v._id].first_out;
557 void nextInc(Edge &arc, bool& d) const {
558 int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
568 static int id(Node v) { return v._id; }
569 static int id(Arc e) { return e._id; }
570 static int id(Edge e) { return e._id; }
572 static Node nodeFromId(int id) { return Node(id);}
573 static Arc arcFromId(int id) { return Arc(id);}
574 static Edge edgeFromId(int id) { return Edge(id);}
576 bool valid(Node n) const {
577 return n._id >= 0 && n._id < static_cast<int>(nodes.size());
579 bool valid(Arc a) const {
580 return a._id >= 0 && a._id < static_cast<int>(arcs.size());
582 bool valid(Edge e) const {
583 return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
587 int n = nodes.size();
588 nodes.push_back(NodeT());
589 nodes[n].first_out = -1;
594 Edge addEdge(Node u, Node v) {
596 arcs.push_back(ArcT());
597 arcs.push_back(ArcT());
599 arcs[n].target = u._id;
600 arcs[n | 1].target = v._id;
602 arcs[n].next_out = nodes[v._id].first_out;
603 nodes[v._id].first_out = n;
605 arcs[n | 1].next_out = nodes[u._id].first_out;
606 nodes[u._id].first_out = (n | 1);
618 typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
622 /// \brief A smart undirected graph class.
624 /// This is a simple and fast graph implementation.
625 /// It is also quite memory efficient, but at the price
626 /// that <b> it does support only limited (only stack-like)
627 /// node and arc deletions</b>.
628 /// Except from this it conforms to
629 /// the \ref concepts::Graph "Graph concept".
632 /// important extra feature that
633 /// its maps are real \ref concepts::ReferenceMap "reference map"s.
635 /// \sa concepts::Graph.
637 class SmartGraph : public ExtendedSmartGraphBase {
640 ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
642 ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
644 SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
646 ///\brief Assignment of SmartGraph to another one is \e not allowed.
647 ///Use GraphCopy() instead.
649 ///Assignment of SmartGraph to another one is \e not allowed.
650 ///Use GraphCopy() instead.
651 void operator=(const SmartGraph &) {}
655 typedef ExtendedSmartGraphBase Parent;
663 ///Add a new node to the graph.
665 /// \return the new node.
667 Node addNode() { return Parent::addNode(); }
669 ///Add a new edge to the graph.
671 ///Add a new edge to the graph with node \c s
673 ///\return the new edge.
674 Edge addEdge(const Node& s, const Node& t) {
675 return Parent::addEdge(s, t);
678 /// \brief Node validity check
680 /// This function gives back true if the given node is valid,
681 /// ie. it is a real node of the graph.
683 /// \warning A removed node (using Snapshot) could become valid again
684 /// when new nodes are added to the graph.
685 bool valid(Node n) const { return Parent::valid(n); }
687 /// \brief Arc validity check
689 /// This function gives back true if the given arc is valid,
690 /// ie. it is a real arc of the graph.
692 /// \warning A removed arc (using Snapshot) could become valid again
693 /// when new edges are added to the graph.
694 bool valid(Arc a) const { return Parent::valid(a); }
696 /// \brief Edge validity check
698 /// This function gives back true if the given edge is valid,
699 /// ie. it is a real edge of the graph.
701 /// \warning A removed edge (using Snapshot) could become valid again
702 /// when new edges are added to the graph.
703 bool valid(Edge e) const { return Parent::valid(e); }
707 ///Erase all the nodes and edges from the graph.
719 void saveSnapshot(Snapshot &s)
722 s.node_num = nodes.size();
723 s.arc_num = arcs.size();
726 void restoreSnapshot(const Snapshot &s)
728 while(s.arc_num<arcs.size()) {
730 Edge arc=edgeFromId(n/2);
731 Parent::notifier(Edge()).erase(arc);
732 std::vector<Arc> dir;
733 dir.push_back(arcFromId(n));
734 dir.push_back(arcFromId(n-1));
735 Parent::notifier(Arc()).erase(dir);
736 nodes[arcs[n].target].first_out=arcs[n].next_out;
737 nodes[arcs[n-1].target].first_out=arcs[n-1].next_out;
741 while(s.node_num<nodes.size()) {
742 int n=nodes.size()-1;
743 Node node = nodeFromId(n);
744 Parent::notifier(Node()).erase(node);
751 ///Class to make a snapshot of the digraph and to restrore to it later.
753 ///Class to make a snapshot of the digraph and to restrore to it later.
755 ///The newly added nodes and arcs can be removed using the
756 ///restore() function.
758 ///\note After you restore a state, you cannot restore
759 ///a later state, in other word you cannot add again the arcs deleted
760 ///by restore() using another one Snapshot instance.
762 ///\warning If you do not use correctly the snapshot that can cause
763 ///either broken program, invalid state of the digraph, valid but
764 ///not the restored digraph or no change. Because the runtime performance
765 ///the validity of the snapshot is not stored.
770 friend class SmartGraph;
771 unsigned int node_num;
772 unsigned int arc_num;
774 ///Default constructor.
776 ///Default constructor.
777 ///To actually make a snapshot you must call save().
779 Snapshot() : _graph(0) {}
780 ///Constructor that immediately makes a snapshot
782 ///This constructor immediately makes a snapshot of the digraph.
783 ///\param g The digraph we make a snapshot of.
784 Snapshot(SmartGraph &graph) {
785 graph.saveSnapshot(*this);
790 ///Make a snapshot of the graph.
792 ///This function can be called more than once. In case of a repeated
793 ///call, the previous snapshot gets lost.
794 ///\param g The digraph we make the snapshot of.
795 void save(SmartGraph &graph)
797 graph.saveSnapshot(*this);
800 ///Undo the changes until a snapshot.
802 ///Undo the changes until a snapshot created by save().
804 ///\note After you restored a state, you cannot restore
805 ///a later state, in other word you cannot add again the arcs deleted
809 _graph->restoreSnapshot(*this);
817 #endif //LEMON_SMART_GRAPH_H