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; }
101 Node n; n.n=nodes.size();
102 nodes.push_back(NodeT()); //FIXME: Hmmm...
106 Edge addEdge(Node u, Node v) {
107 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
108 edges[e.n].source=u.n; edges[e.n].target=v.n;
109 edges[e.n].next_out=nodes[u.n].first_out;
110 edges[e.n].next_in=nodes[v.n].first_in;
111 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
117 Node source(Edge e) const { return edges[e.n].source; }
118 Node target(Edge e) const { return edges[e.n].target; }
122 /// The ID of a valid Node is a nonnegative integer not greater than
123 /// \ref maxNodeId(). The range of the ID's is not surely continuous
124 /// and the greatest node ID can be actually less then \ref maxNodeId().
126 /// The ID of the \ref INVALID node is -1.
127 ///\return The ID of the node \c v.
128 static int id(Node v) { return v.n; }
131 /// The ID of a valid Edge is a nonnegative integer not greater than
132 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
133 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
135 /// The ID of the \ref INVALID edge is -1.
136 ///\return The ID of the edge \c e.
137 static int id(Edge e) { return e.n; }
139 /// \brief Returns the node from its \c id.
141 /// Returns the node from its \c id. If there is not node
142 /// with the given id the effect of the function is undefinied.
143 static Node nodeFromId(int id) { return Node(id);}
145 /// \brief Returns the edge from its \c id.
147 /// Returns the edge from its \c id. If there is not edge
148 /// with the given id the effect of the function is undefinied.
149 static Edge edgeFromId(int id) { return Edge(id);}
152 friend class SmartGraphBase;
153 friend class SmartGraph;
160 Node (Invalid) { n=-1; }
161 bool operator==(const Node i) const {return n==i.n;}
162 bool operator!=(const Node i) const {return n!=i.n;}
163 bool operator<(const Node i) const {return n<i.n;}
168 friend class SmartGraphBase;
169 friend class SmartGraph;
176 Edge (Invalid) { n=-1; }
177 bool operator==(const Edge i) const {return n==i.n;}
178 bool operator!=(const Edge i) const {return n!=i.n;}
179 bool operator<(const Edge i) const {return n<i.n;}
182 void first(Node& node) const {
183 node.n = nodes.size() - 1;
186 static void next(Node& node) {
190 void first(Edge& edge) const {
191 edge.n = edges.size() - 1;
194 static void next(Edge& edge) {
198 void firstOut(Edge& edge, const Node& node) const {
199 edge.n = nodes[node.n].first_out;
202 void nextOut(Edge& edge) const {
203 edge.n = edges[edge.n].next_out;
206 void firstIn(Edge& edge, const Node& node) const {
207 edge.n = nodes[node.n].first_in;
210 void nextIn(Edge& edge) const {
211 edge.n = edges[edge.n].next_in;
216 typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
220 ///A smart graph class.
222 ///This is a simple and fast graph implementation.
223 ///It is also quite memory efficient, but at the price
224 ///that <b> it does support only limited (only stack-like)
225 ///node and edge deletions</b>.
227 ///the \ref concept::Graph "Graph concept".
228 ///\sa concept::Graph.
230 ///\author Alpar Juttner
231 class SmartGraph : public ExtendedSmartGraphBase {
234 typedef ExtendedSmartGraphBase Parent;
237 friend class Snapshot;
240 ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
242 ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
244 SmartGraph(const SmartGraph &) :ExtendedSmartGraphBase() {};
245 ///\brief Assignment of SmartGraph to another one is \e not allowed.
246 ///Use GraphCopy() instead.
248 ///Assignment of SmartGraph to another one is \e not allowed.
249 ///Use GraphCopy() instead.
250 void operator=(const SmartGraph &) {}
252 void restoreSnapshot(const Snapshot &s)
254 while(s.edge_num<edges.size()) {
255 Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
256 nodes[edges.back().target].first_in=edges.back().next_in;
257 nodes[edges.back().source].first_out=edges.back().next_out;
260 //nodes.resize(s.nodes_num);
261 while(s.node_num<nodes.size()) {
262 Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
275 ///Add a new node to the graph.
277 /// \return the new node.
279 Node addNode() { return Parent::addNode(); }
281 ///Add a new edge to the graph.
283 ///Add a new edge to the graph with source node \c s
284 ///and target node \c t.
285 ///\return the new edge.
286 Edge addEdge(const Node& s, const Node& t) {
287 return Parent::addEdge(s, t);
293 ///\bug Doesn't destruct the maps.
301 ///This function splits a node. First a new node is added to the graph,
302 ///then the source of each outgoing edge of \c n is moved to this new node.
303 ///If \c connect is \c true (this is the default value), then a new edge
304 ///from \c n to the newly created node is also added.
305 ///\return The newly created node.
307 ///\note The <tt>Edge</tt>s
308 ///referencing a moved edge remain
309 ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
310 ///may be invalidated.
311 ///\warning This functionality cannot be used together with the Snapshot
313 ///\todo It could be implemented in a bit faster way.
314 Node split(Node n, bool connect = true)
317 nodes[b.n].first_out=nodes[n.n].first_out;
318 nodes[n.n].first_out=-1;
319 for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
320 if(connect) addEdge(n,b);
324 ///Class to make a snapshot of the graph and to restrore to it later.
326 ///Class to make a snapshot of the graph and to restrore to it later.
328 ///The newly added nodes and edges can be removed using the
329 ///restore() function.
330 ///\note After you restore a state, you cannot restore
331 ///a later state, in other word you cannot add again the edges deleted
332 ///by restore() using another one Snapshot instance.
338 friend class SmartGraph;
339 unsigned int node_num;
340 unsigned int edge_num;
342 ///Default constructor.
344 ///Default constructor.
345 ///To actually make a snapshot you must call save().
348 ///Constructor that immediately makes a snapshot
350 ///This constructor immediately makes a snapshot of the graph.
351 ///\param _g The graph we make a snapshot of.
352 Snapshot(SmartGraph &_g) :g(&_g) {
353 node_num=g->nodes.size();
354 edge_num=g->edges.size();
359 ///Make a snapshot of the graph.
361 ///This function can be called more than once. In case of a repeated
362 ///call, the previous snapshot gets lost.
363 ///\param _g The graph we make the snapshot of.
364 void save(SmartGraph &_g)
367 node_num=g->nodes.size();
368 edge_num=g->edges.size();
371 ///Undo the changes until a snapshot.
373 ///Undo the changes until a snapshot created by save().
375 ///\note After you restored a state, you cannot restore
376 ///a later state, in other word you cannot add again the edges deleted
379 ///\todo This function might be called undo().
383 g->restoreSnapshot(*this);
389 /**************** Undirected List Graph ****************/
391 typedef UGraphExtender<UndirGraphExtender<SmartGraphBase> >
392 ExtendedSmartUGraphBase;
396 /// \brief A smart undirected graph class.
398 /// This is a simple and fast undirected graph implementation.
399 /// It is also quite memory efficient, but at the price
400 /// that <b> it does support only limited (only stack-like)
401 /// node and edge deletions</b>.
402 /// Except from this it conforms to
403 /// the \ref concept::UGraph "UGraph concept".
404 /// \sa concept::UGraph.
406 /// \todo Snapshot hasn't been implemented yet.
408 class SmartUGraph : public ExtendedSmartUGraphBase {
410 ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead.
412 ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead.
414 SmartUGraph(const SmartUGraph &) : ExtendedSmartUGraphBase() {};
415 ///\brief Assignment of SmartUGraph to another one is \e not allowed.
416 ///Use UGraphCopy() instead.
418 ///Assignment of SmartUGraph to another one is \e not allowed.
419 ///Use UGraphCopy() instead.
420 void operator=(const SmartUGraph &) {}
430 class SmartBpUGraphBase {
433 class NodeSetError : public LogicError {
434 virtual const char* what() const throw() {
435 return "lemon::SmartBpUGraph::NodeSetError";
444 NodeT(int _first) : first(_first) {}
452 std::vector<NodeT> aNodes;
453 std::vector<NodeT> bNodes;
455 std::vector<UEdgeT> edges;
460 friend class SmartBpUGraphBase;
464 Node(int _id) : id(_id) {}
467 Node(Invalid) { id = -1; }
468 bool operator==(const Node i) const {return id==i.id;}
469 bool operator!=(const Node i) const {return id!=i.id;}
470 bool operator<(const Node i) const {return id<i.id;}
474 friend class SmartBpUGraphBase;
478 UEdge(int _id) { id = _id;}
481 UEdge (Invalid) { id = -1; }
482 bool operator==(const UEdge i) const {return id==i.id;}
483 bool operator!=(const UEdge i) const {return id!=i.id;}
484 bool operator<(const UEdge i) const {return id<i.id;}
487 void firstANode(Node& node) const {
488 node.id = 2 * aNodes.size() - 2;
489 if (node.id < 0) node.id = -1;
491 void nextANode(Node& node) const {
493 if (node.id < 0) node.id = -1;
496 void firstBNode(Node& node) const {
497 node.id = 2 * bNodes.size() - 1;
499 void nextBNode(Node& node) const {
503 void first(Node& node) const {
504 if (aNodes.size() > 0) {
505 node.id = 2 * aNodes.size() - 2;
507 node.id = 2 * bNodes.size() - 1;
510 void next(Node& node) const {
513 node.id = 2 * bNodes.size() - 1;
517 void first(UEdge& edge) const {
518 edge.id = edges.size() - 1;
520 void next(UEdge& edge) const {
524 void firstFromANode(UEdge& edge, const Node& node) const {
525 LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
526 edge.id = aNodes[node.id >> 1].first;
528 void nextFromANode(UEdge& edge) const {
529 edge.id = edges[edge.id].next_out;
532 void firstFromBNode(UEdge& edge, const Node& node) const {
533 LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
534 edge.id = bNodes[node.id >> 1].first;
536 void nextFromBNode(UEdge& edge) const {
537 edge.id = edges[edge.id].next_in;
540 static int id(const Node& node) {
543 static Node nodeFromId(int id) {
546 int maxNodeId() const {
547 return aNodes.size() > bNodes.size() ?
548 aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
551 static int id(const UEdge& edge) {
554 static UEdge uEdgeFromId(int id) {
557 int maxUEdgeId() const {
561 static int aNodeId(const Node& node) {
564 static Node fromANodeId(int id) {
565 return Node(id << 1);
567 int maxANodeId() const {
568 return aNodes.size();
571 static int bNodeId(const Node& node) {
574 static Node fromBNodeId(int id) {
575 return Node((id << 1) + 1);
577 int maxBNodeId() const {
578 return bNodes.size();
581 Node aNode(const UEdge& edge) const {
582 return Node(edges[edge.id].aNode);
584 Node bNode(const UEdge& edge) const {
585 return Node(edges[edge.id].bNode);
588 static bool aNode(const Node& node) {
589 return (node.id & 1) == 0;
592 static bool bNode(const Node& node) {
593 return (node.id & 1) == 1;
599 aNodes.push_back(nodeT);
600 return Node(aNodes.size() * 2 - 2);
606 bNodes.push_back(nodeT);
607 return Node(bNodes.size() * 2 - 1);
610 UEdge addEdge(const Node& source, const Node& target) {
611 LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
613 if ((source.id & 1) == 0) {
614 edgeT.aNode = source.id;
615 edgeT.bNode = target.id;
617 edgeT.aNode = target.id;
618 edgeT.bNode = source.id;
620 edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
621 aNodes[edgeT.aNode >> 1].first = edges.size();
622 edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
623 bNodes[edgeT.bNode >> 1].first = edges.size();
624 edges.push_back(edgeT);
625 return UEdge(edges.size() - 1);
634 typedef True NodeNumTag;
635 int nodeNum() const { return aNodes.size() + bNodes.size(); }
636 int aNodeNum() const { return aNodes.size(); }
637 int bNodeNum() const { return bNodes.size(); }
639 typedef True EdgeNumTag;
640 int uEdgeNum() const { return edges.size(); }
645 typedef BpUGraphExtender<SmartBpUGraphBase> ExtendedSmartBpUGraphBase;
649 /// \brief A smart bipartite undirected graph class.
651 /// This is a simple and fast bipartite undirected graph implementation.
652 /// It is also quite memory efficient, but at the price
653 /// that <b> it does not support node and edge deletions</b>.
654 /// Except from this it conforms to
655 /// the \ref concept::BpUGraph "BpUGraph concept".
656 /// \sa concept::BpUGraph.
658 class SmartBpUGraph : public ExtendedSmartBpUGraphBase {};
665 #endif //LEMON_SMART_GRAPH_H