3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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 SmartGraph and SmartUGraph 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>
45 class SmartGraphBase {
50 int first_in, first_out;
55 int target, source, next_in, next_out;
59 std::vector<NodeT> nodes;
61 std::vector<EdgeT> edges;
66 typedef SmartGraphBase Graph;
74 SmartGraphBase() : nodes(), edges() { }
75 SmartGraphBase(const SmartGraphBase &_g)
76 : nodes(_g.nodes), edges(_g.edges) { }
78 typedef True NodeNumTag;
79 typedef True EdgeNumTag;
81 int nodeNum() const { return nodes.size(); }
82 int edgeNum() const { return edges.size(); }
84 int maxNodeId() const { return nodes.size()-1; }
85 int maxEdgeId() const { return edges.size()-1; }
89 nodes.push_back(NodeT());
90 nodes[n].first_in = -1;
91 nodes[n].first_out = -1;
95 Edge addEdge(Node u, Node v) {
97 edges.push_back(EdgeT());
98 edges[n].source = u.id;
99 edges[n].target = v.id;
100 edges[n].next_out = nodes[u.id].first_out;
101 edges[n].next_in = nodes[v.id].first_in;
102 nodes[u.id].first_out = nodes[v.id].first_in = n;
112 Node source(Edge e) const { return Node(edges[e.id].source); }
113 Node target(Edge e) const { return Node(edges[e.id].target); }
115 static int id(Node v) { return v.id; }
116 static int id(Edge e) { return e.id; }
118 static Node nodeFromId(int id) { return Node(id);}
119 static Edge edgeFromId(int id) { return Edge(id);}
122 friend class SmartGraphBase;
123 friend class SmartGraph;
127 explicit Node(int _id) : id(_id) {}
130 Node (Invalid) : id(-1) {}
131 bool operator==(const Node i) const {return id == i.id;}
132 bool operator!=(const Node i) const {return id != i.id;}
133 bool operator<(const Node i) const {return id < i.id;}
138 friend class SmartGraphBase;
139 friend class SmartGraph;
143 explicit Edge(int _id) : id(_id) {}
146 Edge (Invalid) : id(-1) {}
147 bool operator==(const Edge i) const {return id == i.id;}
148 bool operator!=(const Edge i) const {return id != i.id;}
149 bool operator<(const Edge i) const {return id < i.id;}
152 void first(Node& node) const {
153 node.id = nodes.size() - 1;
156 static void next(Node& node) {
160 void first(Edge& edge) const {
161 edge.id = edges.size() - 1;
164 static void next(Edge& edge) {
168 void firstOut(Edge& edge, const Node& node) const {
169 edge.id = nodes[node.id].first_out;
172 void nextOut(Edge& edge) const {
173 edge.id = edges[edge.id].next_out;
176 void firstIn(Edge& edge, const Node& node) const {
177 edge.id = nodes[node.id].first_in;
180 void nextIn(Edge& edge) const {
181 edge.id = edges[edge.id].next_in;
186 typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
190 ///A smart graph class.
192 ///This is a simple and fast graph implementation.
193 ///It is also quite memory efficient, but at the price
194 ///that <b> it does support only limited (only stack-like)
195 ///node and edge deletions</b>.
197 ///the \ref concept::Graph "Graph concept".
198 ///\sa concept::Graph.
200 ///\author Alpar Juttner
201 class SmartGraph : public ExtendedSmartGraphBase {
204 typedef ExtendedSmartGraphBase Parent;
208 ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
210 ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
212 SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
213 ///\brief Assignment of SmartGraph to another one is \e not allowed.
214 ///Use GraphCopy() instead.
216 ///Assignment of SmartGraph to another one is \e not allowed.
217 ///Use GraphCopy() instead.
218 void operator=(const SmartGraph &) {}
228 ///Add a new node to the graph.
230 /// \return the new node.
232 Node addNode() { return Parent::addNode(); }
234 ///Add a new edge to the graph.
236 ///Add a new edge to the graph with source node \c s
237 ///and target node \c t.
238 ///\return the new edge.
239 Edge addEdge(const Node& s, const Node& t) {
240 return Parent::addEdge(s, t);
245 ///Erase all the nodes and edges from the graph.
253 ///This function splits a node. First a new node is added to the graph,
254 ///then the source of each outgoing edge of \c n is moved to this new node.
255 ///If \c connect is \c true (this is the default value), then a new edge
256 ///from \c n to the newly created node is also added.
257 ///\return The newly created node.
259 ///\note The <tt>Edge</tt>s
260 ///referencing a moved edge remain
261 ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
262 ///may be invalidated.
263 ///\warning This functionality cannot be used together with the Snapshot
265 ///\todo It could be implemented in a bit faster way.
266 Node split(Node n, bool connect = true)
269 nodes[b.id].first_out=nodes[n.id].first_out;
270 nodes[n.id].first_out=-1;
271 for(int i=nodes[b.id].first_out;i!=-1;i++) edges[i].source=b.id;
272 if(connect) addEdge(n,b);
282 void restoreSnapshot(const Snapshot &s)
284 while(s.edge_num<edges.size()) {
285 Edge edge = edgeFromId(edges.size()-1);
286 Parent::getNotifier(Edge()).erase(edge);
287 nodes[edges.back().source].first_out=edges.back().next_out;
288 nodes[edges.back().target].first_in=edges.back().next_in;
291 while(s.node_num<nodes.size()) {
292 Node node = nodeFromId(nodes.size()-1);
293 Parent::getNotifier(Node()).erase(node);
300 ///Class to make a snapshot of the graph and to restrore to it later.
302 ///Class to make a snapshot of the graph and to restrore to it later.
304 ///The newly added nodes and edges can be removed using the
305 ///restore() function.
306 ///\note After you restore a state, you cannot restore
307 ///a later state, in other word you cannot add again the edges deleted
308 ///by restore() using another one Snapshot instance.
310 ///\warning If you do not use correctly the snapshot that can cause
311 ///either broken program, invalid state of the graph, valid but
312 ///not the restored graph or no change. Because the runtime performance
313 ///the validity of the snapshot is not stored.
318 friend class SmartGraph;
319 unsigned int node_num;
320 unsigned int edge_num;
322 ///Default constructor.
324 ///Default constructor.
325 ///To actually make a snapshot you must call save().
328 ///Constructor that immediately makes a snapshot
330 ///This constructor immediately makes a snapshot of the graph.
331 ///\param _g The graph we make a snapshot of.
332 Snapshot(SmartGraph &_g) :g(&_g) {
333 node_num=g->nodes.size();
334 edge_num=g->edges.size();
339 ///Make a snapshot of the graph.
341 ///This function can be called more than once. In case of a repeated
342 ///call, the previous snapshot gets lost.
343 ///\param _g The graph we make the snapshot of.
344 void save(SmartGraph &_g)
347 node_num=g->nodes.size();
348 edge_num=g->edges.size();
351 ///Undo the changes until a snapshot.
353 ///Undo the changes until a snapshot created by save().
355 ///\note After you restored a state, you cannot restore
356 ///a later state, in other word you cannot add again the edges deleted
360 g->restoreSnapshot(*this);
366 /**************** Undirected List Graph ****************/
368 typedef UGraphExtender<UndirGraphExtender<SmartGraphBase> >
369 ExtendedSmartUGraphBase;
373 /// \brief A smart undirected graph class.
375 /// This is a simple and fast undirected graph implementation.
376 /// It is also quite memory efficient, but at the price
377 /// that <b> it does support only limited (only stack-like)
378 /// node and edge deletions</b>.
379 /// Except from this it conforms to
380 /// the \ref concept::UGraph "UGraph concept".
381 /// \sa concept::UGraph.
383 /// \todo Snapshot hasn't been implemented yet.
385 class SmartUGraph : public ExtendedSmartUGraphBase {
388 ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead.
390 ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead.
392 SmartUGraph(const SmartUGraph &) : ExtendedSmartUGraphBase() {};
394 ///\brief Assignment of SmartUGraph to another one is \e not allowed.
395 ///Use UGraphCopy() instead.
397 ///Assignment of SmartUGraph to another one is \e not allowed.
398 ///Use UGraphCopy() instead.
399 void operator=(const SmartUGraph &) {}
403 typedef ExtendedSmartUGraphBase Parent;
411 ///Add a new node to the graph.
413 /// \return the new node.
415 Node addNode() { return Parent::addNode(); }
417 ///Add a new undirected edge to the graph.
419 ///Add a new undirected edge to the graph with node \c s
421 ///\return the new undirected edge.
422 UEdge addEdge(const Node& s, const Node& t) {
423 return Parent::addEdge(s, t);
428 ///Erase all the nodes and edges from the graph.
441 void restoreSnapshot(const Snapshot &s)
443 while(s.edge_num<edges.size()) {
444 UEdge edge = uEdgeFromId(edges.size()-1);
445 Parent::getNotifier(UEdge()).erase(edge);
446 std::vector<Edge> dir;
447 dir.push_back(Parent::direct(edge, true));
448 dir.push_back(Parent::direct(edge, false));
449 Parent::getNotifier(Edge()).erase(dir);
450 nodes[edges.back().source].first_out=edges.back().next_out;
451 nodes[edges.back().target].first_in=edges.back().next_in;
454 while(s.node_num<nodes.size()) {
455 Node node = nodeFromId(nodes.size()-1);
456 Parent::getNotifier(Node()).erase(node);
463 ///Class to make a snapshot of the graph and to restrore to it later.
465 ///Class to make a snapshot of the graph and to restrore to it later.
467 ///The newly added nodes and edges can be removed using the
468 ///restore() function.
470 ///\note After you restore a state, you cannot restore
471 ///a later state, in other word you cannot add again the edges deleted
472 ///by restore() using another one Snapshot instance.
474 ///\warning If you do not use correctly the snapshot that can cause
475 ///either broken program, invalid state of the graph, valid but
476 ///not the restored graph or no change. Because the runtime performance
477 ///the validity of the snapshot is not stored.
482 friend class SmartUGraph;
483 unsigned int node_num;
484 unsigned int edge_num;
486 ///Default constructor.
488 ///Default constructor.
489 ///To actually make a snapshot you must call save().
492 ///Constructor that immediately makes a snapshot
494 ///This constructor immediately makes a snapshot of the graph.
495 ///\param _g The graph we make a snapshot of.
496 Snapshot(SmartUGraph &_g) :g(&_g) {
497 node_num=g->nodes.size();
498 edge_num=g->edges.size();
503 ///Make a snapshot of the graph.
505 ///This function can be called more than once. In case of a repeated
506 ///call, the previous snapshot gets lost.
507 ///\param _g The graph we make the snapshot of.
508 void save(SmartUGraph &_g)
511 node_num=g->nodes.size();
512 edge_num=g->edges.size();
515 ///Undo the changes until a snapshot.
517 ///Undo the changes until a snapshot created by save().
519 ///\note After you restored a state, you cannot restore
520 ///a later state, in other word you cannot add again the edges deleted
524 g->restoreSnapshot(*this);
530 class SmartBpUGraphBase {
533 class NodeSetError : public LogicError {
535 virtual const char* what() const throw() {
536 return "lemon::SmartBpUGraph::NodeSetError";
545 NodeT(int _first) : first(_first) {}
553 std::vector<NodeT> aNodes;
554 std::vector<NodeT> bNodes;
556 std::vector<UEdgeT> edges;
561 friend class SmartBpUGraphBase;
565 explicit Node(int _id) : id(_id) {}
568 Node(Invalid) : id(-1) {}
569 bool operator==(const Node i) const {return id==i.id;}
570 bool operator!=(const Node i) const {return id!=i.id;}
571 bool operator<(const Node i) const {return id<i.id;}
575 friend class SmartBpUGraphBase;
579 UEdge(int _id) : id(_id) {}
582 UEdge(Invalid) : id(-1) {}
583 bool operator==(const UEdge i) const {return id==i.id;}
584 bool operator!=(const UEdge i) const {return id!=i.id;}
585 bool operator<(const UEdge i) const {return id<i.id;}
588 void firstANode(Node& node) const {
589 node.id = 2 * aNodes.size() - 2;
590 if (node.id < 0) node.id = -1;
592 void nextANode(Node& node) const {
594 if (node.id < 0) node.id = -1;
597 void firstBNode(Node& node) const {
598 node.id = 2 * bNodes.size() - 1;
600 void nextBNode(Node& node) const {
604 void first(Node& node) const {
605 if (aNodes.size() > 0) {
606 node.id = 2 * aNodes.size() - 2;
608 node.id = 2 * bNodes.size() - 1;
611 void next(Node& node) const {
614 node.id = 2 * bNodes.size() - 1;
618 void first(UEdge& edge) const {
619 edge.id = edges.size() - 1;
621 void next(UEdge& edge) const {
625 void firstFromANode(UEdge& edge, const Node& node) const {
626 LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
627 edge.id = aNodes[node.id >> 1].first;
629 void nextFromANode(UEdge& edge) const {
630 edge.id = edges[edge.id].next_out;
633 void firstFromBNode(UEdge& edge, const Node& node) const {
634 LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
635 edge.id = bNodes[node.id >> 1].first;
637 void nextFromBNode(UEdge& edge) const {
638 edge.id = edges[edge.id].next_in;
641 static int id(const Node& node) {
644 static Node nodeFromId(int id) {
647 int maxNodeId() const {
648 return aNodes.size() > bNodes.size() ?
649 aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
652 static int id(const UEdge& edge) {
655 static UEdge uEdgeFromId(int id) {
658 int maxUEdgeId() const {
662 static int aNodeId(const Node& node) {
665 static Node fromANodeId(int id) {
666 return Node(id << 1);
668 int maxANodeId() const {
669 return aNodes.size();
672 static int bNodeId(const Node& node) {
675 static Node fromBNodeId(int id) {
676 return Node((id << 1) + 1);
678 int maxBNodeId() const {
679 return bNodes.size();
682 Node aNode(const UEdge& edge) const {
683 return Node(edges[edge.id].aNode);
685 Node bNode(const UEdge& edge) const {
686 return Node(edges[edge.id].bNode);
689 static bool aNode(const Node& node) {
690 return (node.id & 1) == 0;
693 static bool bNode(const Node& node) {
694 return (node.id & 1) == 1;
700 aNodes.push_back(nodeT);
701 return Node(aNodes.size() * 2 - 2);
707 bNodes.push_back(nodeT);
708 return Node(bNodes.size() * 2 - 1);
711 UEdge addEdge(const Node& source, const Node& target) {
712 LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
714 if ((source.id & 1) == 0) {
715 edgeT.aNode = source.id;
716 edgeT.bNode = target.id;
718 edgeT.aNode = target.id;
719 edgeT.bNode = source.id;
721 edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
722 aNodes[edgeT.aNode >> 1].first = edges.size();
723 edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
724 bNodes[edgeT.bNode >> 1].first = edges.size();
725 edges.push_back(edgeT);
726 return UEdge(edges.size() - 1);
735 typedef True NodeNumTag;
736 int nodeNum() const { return aNodes.size() + bNodes.size(); }
737 int aNodeNum() const { return aNodes.size(); }
738 int bNodeNum() const { return bNodes.size(); }
740 typedef True EdgeNumTag;
741 int uEdgeNum() const { return edges.size(); }
746 typedef BpUGraphExtender<SmartBpUGraphBase> ExtendedSmartBpUGraphBase;
750 /// \brief A smart bipartite undirected graph class.
752 /// This is a simple and fast bipartite undirected graph implementation.
753 /// It is also quite memory efficient, but at the price
754 /// that <b> it does not support node and edge deletions</b>.
755 /// Except from this it conforms to
756 /// the \ref concept::BpUGraph "BpUGraph concept".
757 /// \sa concept::BpUGraph.
759 class SmartBpUGraph : public ExtendedSmartBpUGraphBase {
762 /// \brief SmartBpUGraph is \e not copy constructible.
764 ///SmartBpUGraph is \e not copy constructible.
765 SmartBpUGraph(const SmartBpUGraph &) : ExtendedSmartBpUGraphBase() {};
767 /// \brief Assignment of SmartBpUGraph to another one is \e not
770 /// Assignment of SmartBpUGraph to another one is \e not allowed.
771 void operator=(const SmartBpUGraph &) {}
775 typedef ExtendedSmartBpUGraphBase Parent;
781 SmartBpUGraph() : ExtendedSmartBpUGraphBase() {}
783 ///Add a new ANode to the graph.
785 /// \return the new node.
787 Node addANode() { return Parent::addANode(); }
789 ///Add a new BNode to the graph.
791 /// \return the new node.
793 Node addBNode() { return Parent::addBNode(); }
795 ///Add a new undirected edge to the graph.
797 ///Add a new undirected edge to the graph with node \c s
799 ///\return the new undirected edge.
800 UEdge addEdge(const Node& s, const Node& t) {
801 return Parent::addEdge(s, t);
806 ///Erase all the nodes and edges from the graph.
818 void restoreSnapshot(const Snapshot &s)
820 while(s.edge_num<edges.size()) {
821 UEdge edge = uEdgeFromId(edges.size()-1);
822 Parent::getNotifier(UEdge()).erase(edge);
823 std::vector<Edge> dir;
824 dir.push_back(Parent::direct(edge, true));
825 dir.push_back(Parent::direct(edge, false));
826 Parent::getNotifier(Edge()).erase(dir);
827 aNodes[edges.back().aNode >> 1].first=edges.back().next_out;
828 bNodes[edges.back().bNode >> 1].first=edges.back().next_in;
831 while(s.anode_num<aNodes.size()) {
832 Node node = fromANodeId(aNodes.size() - 1);
833 Parent::getNotifier(ANode()).erase(node);
834 Parent::getNotifier(Node()).erase(node);
837 while(s.bnode_num<bNodes.size()) {
838 Node node = fromBNodeId(bNodes.size() - 1);
839 Parent::getNotifier(BNode()).erase(node);
840 Parent::getNotifier(Node()).erase(node);
847 ///Class to make a snapshot of the graph and to restrore to it later.
849 ///Class to make a snapshot of the graph and to restrore to it later.
851 ///The newly added nodes and edges can be removed using the
852 ///restore() function.
854 ///\note After you restore a state, you cannot restore
855 ///a later state, in other word you cannot add again the edges deleted
856 ///by restore() using another one Snapshot instance.
858 ///\warning If you do not use correctly the snapshot that can cause
859 ///either broken program, invalid state of the graph, valid but
860 ///not the restored graph or no change. Because the runtime performance
861 ///the validity of the snapshot is not stored.
866 friend class SmartBpUGraph;
867 unsigned int anode_num;
868 unsigned int bnode_num;
869 unsigned int edge_num;
871 ///Default constructor.
873 ///Default constructor.
874 ///To actually make a snapshot you must call save().
878 ///Constructor that immediately makes a snapshot
880 ///This constructor immediately makes a snapshot of the graph.
881 ///\param _g The graph we make a snapshot of.
882 Snapshot(SmartBpUGraph &_g) : g(&_g) {
883 anode_num=g->aNodes.size();
884 bnode_num=g->bNodes.size();
885 edge_num=g->edges.size();
890 ///Make a snapshot of the graph.
892 ///This function can be called more than once. In case of a repeated
893 ///call, the previous snapshot gets lost.
894 ///\param _g The graph we make the snapshot of.
895 void save(SmartBpUGraph &_g)
898 anode_num=g->aNodes.size();
899 bnode_num=g->bNodes.size();
900 edge_num=g->edges.size();
903 ///Undo the changes until a snapshot.
905 ///Undo the changes until a snapshot created by save().
907 ///\note After you restored a state, you cannot restore
908 ///a later state, in other word you cannot add again the edges deleted
912 g->restoreSnapshot(*this);
922 #endif //LEMON_SMART_GRAPH_H