Some modification in the documentation.
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 {
47 friend class SmatGraph;
52 int first_in,first_out;
53 NodeT() : first_in(-1), first_out(-1) {}
57 int target, source, next_in, next_out;
58 //FIXME: is this necessary?
59 EdgeT() : next_in(-1), next_out(-1) {}
62 std::vector<NodeT> nodes;
64 std::vector<EdgeT> edges;
69 typedef SmartGraphBase Graph;
77 SmartGraphBase() : nodes(), edges() { }
78 SmartGraphBase(const SmartGraphBase &_g)
79 : nodes(_g.nodes), edges(_g.edges) { }
81 typedef True NodeNumTag;
82 typedef True EdgeNumTag;
85 int nodeNum() const { return nodes.size(); }
87 int edgeNum() const { return edges.size(); }
93 int maxNodeId() const { return nodes.size()-1; }
98 int maxEdgeId() const { return edges.size()-1; }
100 Node source(Edge e) const { return edges[e.n].source; }
101 Node target(Edge e) const { return edges[e.n].target; }
105 /// The ID of a valid Node is a nonnegative integer not greater than
106 /// \ref maxNodeId(). The range of the ID's is not surely continuous
107 /// and the greatest node ID can be actually less then \ref maxNodeId().
109 /// The ID of the \ref INVALID node is -1.
110 ///\return The ID of the node \c v.
111 static int id(Node v) { return v.n; }
114 /// The ID of a valid Edge is a nonnegative integer not greater than
115 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
116 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
118 /// The ID of the \ref INVALID edge is -1.
119 ///\return The ID of the edge \c e.
120 static int id(Edge e) { return e.n; }
122 /// \brief Returns the node from its \c id.
124 /// Returns the node from its \c id. If there is not node
125 /// with the given id the effect of the function is undefinied.
126 static Node nodeFromId(int id) { return Node(id);}
128 /// \brief Returns the edge from its \c id.
130 /// Returns the edge from its \c id. If there is not edge
131 /// with the given id the effect of the function is undefinied.
132 static Edge edgeFromId(int id) { return Edge(id);}
135 Node n; n.n=nodes.size();
136 nodes.push_back(NodeT()); //FIXME: Hmmm...
140 Edge addEdge(Node u, Node v) {
141 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
142 edges[e.n].source=u.n; edges[e.n].target=v.n;
143 edges[e.n].next_out=nodes[u.n].first_out;
144 edges[e.n].next_in=nodes[v.n].first_in;
145 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
157 friend class SmartGraphBase;
158 friend class SmartGraph;
165 Node (Invalid) { n=-1; }
166 bool operator==(const Node i) const {return n==i.n;}
167 bool operator!=(const Node i) const {return n!=i.n;}
168 bool operator<(const Node i) const {return n<i.n;}
173 friend class SmartGraphBase;
174 friend class SmartGraph;
181 Edge (Invalid) { n=-1; }
182 bool operator==(const Edge i) const {return n==i.n;}
183 bool operator!=(const Edge i) const {return n!=i.n;}
184 bool operator<(const Edge i) const {return n<i.n;}
187 void first(Node& node) const {
188 node.n = nodes.size() - 1;
191 static void next(Node& node) {
195 void first(Edge& edge) const {
196 edge.n = edges.size() - 1;
199 static void next(Edge& edge) {
203 void firstOut(Edge& edge, const Node& node) const {
204 edge.n = nodes[node.n].first_out;
207 void nextOut(Edge& edge) const {
208 edge.n = edges[edge.n].next_out;
211 void firstIn(Edge& edge, const Node& node) const {
212 edge.n = nodes[node.n].first_in;
215 void nextIn(Edge& edge) const {
216 edge.n = edges[edge.n].next_in;
219 Node _split(Node n, bool connect = true)
222 nodes[b.n].first_out=nodes[n.n].first_out;
223 nodes[n.n].first_out=-1;
224 for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
225 if(connect) addEdge(n,b);
231 typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
235 ///A smart graph class.
237 ///This is a simple and fast graph implementation.
238 ///It is also quite memory efficient, but at the price
239 ///that <b> it does support only limited (only stack-like)
240 ///node and edge deletions</b>.
242 ///the \ref concept::Graph "Graph" concept.
243 ///\sa concept::Graph.
245 ///\author Alpar Juttner
246 class SmartGraph : public ExtendedSmartGraphBase {
249 typedef ExtendedSmartGraphBase Parent;
252 friend class Snapshot;
255 void restoreSnapshot(const Snapshot &s)
257 while(s.edge_num<edges.size()) {
258 Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
259 nodes[edges.back().target].first_in=edges.back().next_in;
260 nodes[edges.back().source].first_out=edges.back().next_out;
263 //nodes.resize(s.nodes_num);
264 while(s.node_num<nodes.size()) {
265 Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
274 ///This function splits a node. First a new node is added to the graph,
275 ///then the source of each outgoing edge of \c n is moved to this new node.
276 ///If \c connect is \c true (this is the default value), then a new edge
277 ///from \c n to the newly created node is also added.
278 ///\return The newly created node.
280 ///\note The <tt>Edge</tt>s
281 ///referencing a moved edge remain
282 ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</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)
289 Node b = _split(n,connect);
294 ///Class to make a snapshot of the graph and to restrore to it later.
296 ///Class to make a snapshot of the graph and to restrore to it later.
298 ///The newly added nodes and edges can be removed using the
299 ///restore() function.
300 ///\note After you restore a state, you cannot restore
301 ///a later state, in other word you cannot add again the edges deleted
302 ///by restore() using another Snapshot instance.
308 friend class SmartGraph;
309 unsigned int node_num;
310 unsigned int edge_num;
312 ///Default constructor.
314 ///Default constructor.
315 ///To actually make a snapshot you must call save().
318 ///Constructor that immediately makes a snapshot
320 ///This constructor immediately makes a snapshot of the graph.
321 ///\param _g The graph we make a snapshot of.
322 Snapshot(SmartGraph &_g) :g(&_g) {
323 node_num=g->nodes.size();
324 edge_num=g->edges.size();
329 ///Make a snapshot of the graph.
331 ///This function can be called more than once. In case of a repeated
332 ///call, the previous snapshot gets lost.
333 ///\param _g The graph we make the snapshot of.
334 void save(SmartGraph &_g)
337 node_num=g->nodes.size();
338 edge_num=g->edges.size();
341 ///Undo the changes until a snapshot.
343 ///Undo the changes until a snapshot created by save().
345 ///\note After you restored a state, you cannot restore
346 ///a later state, in other word you cannot add again the edges deleted
349 ///\todo This function might be called undo().
353 g->restoreSnapshot(*this);
359 /**************** Undirected List Graph ****************/
361 typedef UGraphExtender<UndirGraphExtender<SmartGraphBase> >
362 ExtendedSmartUGraphBase;
366 /// \brief A smart undirected graph class.
368 /// This is a simple and fast undirected graph implementation.
369 /// It is also quite memory efficient, but at the price
370 /// that <b> it does support only limited (only stack-like)
371 /// node and edge deletions</b>.
372 /// Except from this it conforms to
373 /// the \ref concept::UGraph "UGraph" concept.
374 /// \sa concept::UGraph.
376 /// \todo Snapshot hasn't been implemented yet.
378 class SmartUGraph : public ExtendedSmartUGraphBase {
382 class SmartBpUGraphBase {
385 class NodeSetError : public LogicError {
386 virtual const char* exceptionName() const {
387 return "lemon::SmartBpUGraph::NodeSetError";
396 NodeT(int _first) : first(_first) {}
404 std::vector<NodeT> aNodes;
405 std::vector<NodeT> bNodes;
407 std::vector<UEdgeT> edges;
412 friend class SmartBpUGraphBase;
416 Node(int _id) : id(_id) {}
419 Node(Invalid) { id = -1; }
420 bool operator==(const Node i) const {return id==i.id;}
421 bool operator!=(const Node i) const {return id!=i.id;}
422 bool operator<(const Node i) const {return id<i.id;}
426 friend class SmartBpUGraphBase;
430 UEdge(int _id) { id = _id;}
433 UEdge (Invalid) { id = -1; }
434 bool operator==(const UEdge i) const {return id==i.id;}
435 bool operator!=(const UEdge i) const {return id!=i.id;}
436 bool operator<(const UEdge i) const {return id<i.id;}
439 void firstANode(Node& node) const {
440 node.id = 2 * aNodes.size() - 2;
441 if (node.id < 0) node.id = -1;
443 void nextANode(Node& node) const {
445 if (node.id < 0) node.id = -1;
448 void firstBNode(Node& node) const {
449 node.id = 2 * bNodes.size() - 1;
451 void nextBNode(Node& node) const {
455 void first(Node& node) const {
456 if (aNodes.size() > 0) {
457 node.id = 2 * aNodes.size() - 2;
459 node.id = 2 * bNodes.size() - 1;
462 void next(Node& node) const {
465 node.id = 2 * bNodes.size() - 1;
469 void first(UEdge& edge) const {
470 edge.id = edges.size() - 1;
472 void next(UEdge& edge) const {
476 void firstFromANode(UEdge& edge, const Node& node) const {
477 LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
478 edge.id = aNodes[node.id >> 1].first;
480 void nextFromANode(UEdge& edge) const {
481 edge.id = edges[edge.id].next_out;
484 void firstFromBNode(UEdge& edge, const Node& node) const {
485 LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
486 edge.id = bNodes[node.id >> 1].first;
488 void nextFromBNode(UEdge& edge) const {
489 edge.id = edges[edge.id].next_in;
492 static int id(const Node& node) {
495 static Node nodeFromId(int id) {
498 int maxNodeId() const {
499 return aNodes.size() > bNodes.size() ?
500 aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
503 static int id(const UEdge& edge) {
506 static UEdge uEdgeFromId(int id) {
509 int maxUEdgeId() const {
513 static int aNodeId(const Node& node) {
516 static Node fromANodeId(int id) {
517 return Node(id << 1);
519 int maxANodeId() const {
520 return aNodes.size();
523 static int bNodeId(const Node& node) {
526 static Node fromBNodeId(int id) {
527 return Node((id << 1) + 1);
529 int maxBNodeId() const {
530 return bNodes.size();
533 Node aNode(const UEdge& edge) const {
534 return Node(edges[edge.id].aNode);
536 Node bNode(const UEdge& edge) const {
537 return Node(edges[edge.id].bNode);
540 static bool aNode(const Node& node) {
541 return (node.id & 1) == 0;
544 static bool bNode(const Node& node) {
545 return (node.id & 1) == 1;
551 aNodes.push_back(nodeT);
552 return Node(aNodes.size() * 2 - 2);
558 bNodes.push_back(nodeT);
559 return Node(bNodes.size() * 2 - 1);
562 UEdge addEdge(const Node& source, const Node& target) {
563 LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
565 if ((source.id & 1) == 0) {
566 edgeT.aNode = source.id;
567 edgeT.bNode = target.id;
569 edgeT.aNode = target.id;
570 edgeT.bNode = source.id;
572 edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
573 aNodes[edgeT.aNode >> 1].first = edges.size();
574 edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
575 bNodes[edgeT.bNode >> 1].first = edges.size();
576 edges.push_back(edgeT);
577 return UEdge(edges.size() - 1);
586 typedef True NodeNumTag;
587 int nodeNum() const { return aNodes.size() + bNodes.size(); }
588 int aNodeNum() const { return aNodes.size(); }
589 int bNodeNum() const { return bNodes.size(); }
591 typedef True EdgeNumTag;
592 int uEdgeNum() const { return edges.size(); }
597 typedef BpUGraphExtender<SmartBpUGraphBase> ExtendedSmartBpUGraphBase;
601 /// \brief A smart bipartite undirected graph class.
603 /// This is a simple and fast bipartite undirected graph implementation.
604 /// It is also quite memory efficient, but at the price
605 /// that <b> it does not support node and edge deletions</b>.
606 /// Except from this it conforms to
607 /// the \ref concept::BpUGraph "BpUGraph" concept.
608 /// \sa concept::BpUGraph.
610 class SmartBpUGraph : public ExtendedSmartBpUGraphBase {};
617 #endif //LEMON_SMART_GRAPH_H