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/invalid.h>
30 #include <lemon/bits/graph_extender.h>
32 #include <lemon/utility.h>
33 #include <lemon/error.h>
35 #include <lemon/bits/graph_extender.h>
44 class SmartGraphBase {
46 friend class SmatGraph;
51 int first_in,first_out;
52 NodeT() : first_in(-1), first_out(-1) {}
56 int target, source, next_in, next_out;
57 //FIXME: is this necessary?
58 EdgeT() : next_in(-1), next_out(-1) {}
61 std::vector<NodeT> nodes;
63 std::vector<EdgeT> edges;
68 typedef SmartGraphBase Graph;
76 SmartGraphBase() : nodes(), edges() { }
77 SmartGraphBase(const SmartGraphBase &_g)
78 : nodes(_g.nodes), edges(_g.edges) { }
80 typedef True NodeNumTag;
81 typedef True EdgeNumTag;
84 int nodeNum() const { return nodes.size(); }
86 int edgeNum() const { return edges.size(); }
92 int maxNodeId() const { return nodes.size()-1; }
97 int maxEdgeId() const { return edges.size()-1; }
99 Node source(Edge e) const { return edges[e.n].source; }
100 Node target(Edge e) const { return edges[e.n].target; }
104 /// The ID of a valid Node is a nonnegative integer not greater than
105 /// \ref maxNodeId(). The range of the ID's is not surely continuous
106 /// and the greatest node ID can be actually less then \ref maxNodeId().
108 /// The ID of the \ref INVALID node is -1.
109 ///\return The ID of the node \c v.
110 static int id(Node v) { return v.n; }
113 /// The ID of a valid Edge is a nonnegative integer not greater than
114 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
115 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
117 /// The ID of the \ref INVALID edge is -1.
118 ///\return The ID of the edge \c e.
119 static int id(Edge e) { return e.n; }
121 static Node nodeFromId(int id) { return Node(id);}
123 static Edge edgeFromId(int id) { return Edge(id);}
126 Node n; n.n=nodes.size();
127 nodes.push_back(NodeT()); //FIXME: Hmmm...
131 Edge addEdge(Node u, Node v) {
132 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
133 edges[e.n].source=u.n; edges[e.n].target=v.n;
134 edges[e.n].next_out=nodes[u.n].first_out;
135 edges[e.n].next_in=nodes[v.n].first_in;
136 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
148 friend class SmartGraphBase;
149 friend class SmartGraph;
156 Node (Invalid) { n=-1; }
157 bool operator==(const Node i) const {return n==i.n;}
158 bool operator!=(const Node i) const {return n!=i.n;}
159 bool operator<(const Node i) const {return n<i.n;}
164 friend class SmartGraphBase;
165 friend class SmartGraph;
172 Edge (Invalid) { n=-1; }
173 bool operator==(const Edge i) const {return n==i.n;}
174 bool operator!=(const Edge i) const {return n!=i.n;}
175 bool operator<(const Edge i) const {return n<i.n;}
178 void first(Node& node) const {
179 node.n = nodes.size() - 1;
182 static void next(Node& node) {
186 void first(Edge& edge) const {
187 edge.n = edges.size() - 1;
190 static void next(Edge& edge) {
194 void firstOut(Edge& edge, const Node& node) const {
195 edge.n = nodes[node.n].first_out;
198 void nextOut(Edge& edge) const {
199 edge.n = edges[edge.n].next_out;
202 void firstIn(Edge& edge, const Node& node) const {
203 edge.n = nodes[node.n].first_in;
206 void nextIn(Edge& edge) const {
207 edge.n = edges[edge.n].next_in;
210 Node _split(Node n, bool connect = true)
213 nodes[b.n].first_out=nodes[n.n].first_out;
214 nodes[n.n].first_out=-1;
215 for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
216 if(connect) addEdge(n,b);
222 typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
226 ///A smart graph class.
228 ///This is a simple and fast graph implementation.
229 ///It is also quite memory efficient, but at the price
230 ///that <b> it does support only limited (only stack-like)
231 ///node and edge deletions</b>.
233 ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
234 ///\sa concept::ExtendableGraph.
236 ///\author Alpar Juttner
237 class SmartGraph : public ExtendedSmartGraphBase {
240 typedef ExtendedSmartGraphBase Parent;
243 friend class Snapshot;
246 void restoreSnapshot(const Snapshot &s)
248 while(s.edge_num<edges.size()) {
249 Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
250 nodes[edges.back().target].first_in=edges.back().next_in;
251 nodes[edges.back().source].first_out=edges.back().next_out;
254 //nodes.resize(s.nodes_num);
255 while(s.node_num<nodes.size()) {
256 Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
265 ///This function splits a node. First a new node is added to the graph,
266 ///then the source of each outgoing edge of \c n is moved to this new node.
267 ///If \c connect is \c true (this is the default value), then a new edge
268 ///from \c n to the newly created node is also added.
269 ///\return The newly created node.
271 ///\note The <tt>Edge</tt>s
272 ///referencing a moved edge remain
273 ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
274 ///may be invalidated.
275 ///\warning This functionality cannot be used together with the Snapshot
277 ///\todo It could be implemented in a bit faster way.
278 Node split(Node n, bool connect = true)
280 Node b = _split(n,connect);
285 ///Class to make a snapshot of the graph and to restrore to it later.
287 ///Class to make a snapshot of the graph and to restrore to it later.
289 ///The newly added nodes and edges can be removed using the
290 ///restore() function.
291 ///\note After you restore a state, you cannot restore
292 ///a later state, in other word you cannot add again the edges deleted
293 ///by restore() using another Snapshot instance.
299 friend class SmartGraph;
300 unsigned int node_num;
301 unsigned int edge_num;
303 ///Default constructor.
305 ///Default constructor.
306 ///To actually make a snapshot you must call save().
309 ///Constructor that immediately makes a snapshot
311 ///This constructor immediately makes a snapshot of the graph.
312 ///\param _g The graph we make a snapshot of.
313 Snapshot(SmartGraph &_g) :g(&_g) {
314 node_num=g->nodes.size();
315 edge_num=g->edges.size();
320 ///Make a snapshot of the graph.
322 ///This function can be called more than once. In case of a repeated
323 ///call, the previous snapshot gets lost.
324 ///\param _g The graph we make the snapshot of.
325 void save(SmartGraph &_g)
328 node_num=g->nodes.size();
329 edge_num=g->edges.size();
332 ///Undo the changes until a snapshot.
334 ///Undo the changes until a snapshot created by save().
336 ///\note After you restored a state, you cannot restore
337 ///a later state, in other word you cannot add again the edges deleted
340 ///\todo This function might be called undo().
344 g->restoreSnapshot(*this);
350 /**************** Undirected List Graph ****************/
352 typedef UGraphExtender<UGraphBaseExtender<SmartGraphBase> >
353 ExtendedSmartUGraphBase;
357 /// \brief A smart undirected graph class.
359 /// This is a simple and fast undirected graph implementation.
360 /// It is also quite memory efficient, but at the price
361 /// that <b> it does support only limited (only stack-like)
362 /// node and edge deletions</b>.
363 /// Except from this it conforms to
364 /// the \ref concept::UGraph "UGraph" concept.
365 /// \sa concept::UGraph.
367 /// \todo Snapshot hasn't been implemented yet.
369 class SmartUGraph : public ExtendedSmartUGraphBase {
373 class SmartBpUGraphBase {
376 class NodeSetError : public LogicError {
377 virtual const char* exceptionName() const {
378 return "lemon::SmartBpUGraph::NodeSetError";
387 NodeT(int _first) : first(_first) {}
395 std::vector<NodeT> aNodes;
396 std::vector<NodeT> bNodes;
398 std::vector<EdgeT> edges;
403 friend class SmartBpUGraphBase;
407 Node(int _id) : id(_id) {}
410 Node(Invalid) { id = -1; }
411 bool operator==(const Node i) const {return id==i.id;}
412 bool operator!=(const Node i) const {return id!=i.id;}
413 bool operator<(const Node i) const {return id<i.id;}
417 friend class SmartBpUGraphBase;
421 Edge(int _id) { id = _id;}
424 Edge (Invalid) { id = -1; }
425 bool operator==(const Edge i) const {return id==i.id;}
426 bool operator!=(const Edge i) const {return id!=i.id;}
427 bool operator<(const Edge i) const {return id<i.id;}
430 void firstANode(Node& node) const {
431 node.id = 2 * aNodes.size() - 2;
432 if (node.id < 0) node.id = -1;
434 void nextANode(Node& node) const {
436 if (node.id < 0) node.id = -1;
439 void firstBNode(Node& node) const {
440 node.id = 2 * bNodes.size() - 1;
442 void nextBNode(Node& node) const {
446 void first(Node& node) const {
447 if (aNodes.size() > 0) {
448 node.id = 2 * aNodes.size() - 2;
450 node.id = 2 * bNodes.size() - 1;
453 void next(Node& node) const {
456 node.id = 2 * bNodes.size() - 1;
460 void first(Edge& edge) const {
461 edge.id = edges.size() - 1;
463 void next(Edge& edge) const {
467 void firstOut(Edge& edge, const Node& node) const {
468 LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
469 edge.id = aNodes[node.id >> 1].first;
471 void nextOut(Edge& edge) const {
472 edge.id = edges[edge.id].next_out;
475 void firstIn(Edge& edge, const Node& node) const {
476 LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
477 edge.id = bNodes[node.id >> 1].first;
479 void nextIn(Edge& edge) const {
480 edge.id = edges[edge.id].next_in;
483 static int id(const Node& node) {
486 static Node nodeFromId(int id) {
489 int maxNodeId() const {
490 return aNodes.size() > bNodes.size() ?
491 aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
494 static int id(const Edge& edge) {
497 static Edge edgeFromId(int id) {
500 int maxEdgeId() const {
504 static int aNodeId(const Node& node) {
507 static Node fromANodeId(int id, Node) {
508 return Node(id << 1);
510 int maxANodeId() const {
511 return aNodes.size();
514 static int bNodeId(const Node& node) {
517 static Node fromBNodeId(int id) {
518 return Node((id << 1) + 1);
520 int maxBNodeId() const {
521 return bNodes.size();
524 Node aNode(const Edge& edge) const {
525 return Node(edges[edge.id].aNode);
527 Node bNode(const Edge& edge) const {
528 return Node(edges[edge.id].bNode);
531 static bool aNode(const Node& node) {
532 return (node.id & 1) == 0;
535 static bool bNode(const Node& node) {
536 return (node.id & 1) == 1;
542 aNodes.push_back(nodeT);
543 return Node(aNodes.size() * 2 - 2);
549 bNodes.push_back(nodeT);
550 return Node(bNodes.size() * 2 - 1);
553 Edge addEdge(const Node& source, const Node& target) {
554 LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
556 if ((source.id & 1) == 0) {
557 edgeT.aNode = source.id;
558 edgeT.bNode = target.id;
560 edgeT.aNode = target.id;
561 edgeT.bNode = source.id;
563 edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
564 aNodes[edgeT.aNode >> 1].first = edges.size();
565 edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
566 bNodes[edgeT.bNode >> 1].first = edges.size();
567 edges.push_back(edgeT);
568 return Edge(edges.size() - 1);
580 typedef BpUGraphExtender< BpUGraphBaseExtender<
581 SmartBpUGraphBase> > ExtendedSmartBpUGraphBase;
585 /// \brief A smart bipartite undirected graph class.
587 /// This is a simple and fast bipartite undirected graph implementation.
588 /// It is also quite memory efficient, but at the price
589 /// that <b> it does not support node and edge deletions</b>.
590 /// Except from this it conforms to
591 /// the \ref concept::BpUGraph "BpUGraph" concept.
592 /// \sa concept::BpUGraph.
594 class SmartBpUGraph : public ExtendedSmartBpUGraphBase {};
601 #endif //LEMON_SMART_GRAPH_H