bezier.h is no longer in the repository.
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 {
435 virtual const char* what() const throw() {
436 return "lemon::SmartBpUGraph::NodeSetError";
445 NodeT(int _first) : first(_first) {}
453 std::vector<NodeT> aNodes;
454 std::vector<NodeT> bNodes;
456 std::vector<UEdgeT> edges;
461 friend class SmartBpUGraphBase;
465 Node(int _id) : id(_id) {}
468 Node(Invalid) { id = -1; }
469 bool operator==(const Node i) const {return id==i.id;}
470 bool operator!=(const Node i) const {return id!=i.id;}
471 bool operator<(const Node i) const {return id<i.id;}
475 friend class SmartBpUGraphBase;
479 UEdge(int _id) { id = _id;}
482 UEdge (Invalid) { id = -1; }
483 bool operator==(const UEdge i) const {return id==i.id;}
484 bool operator!=(const UEdge i) const {return id!=i.id;}
485 bool operator<(const UEdge i) const {return id<i.id;}
488 void firstANode(Node& node) const {
489 node.id = 2 * aNodes.size() - 2;
490 if (node.id < 0) node.id = -1;
492 void nextANode(Node& node) const {
494 if (node.id < 0) node.id = -1;
497 void firstBNode(Node& node) const {
498 node.id = 2 * bNodes.size() - 1;
500 void nextBNode(Node& node) const {
504 void first(Node& node) const {
505 if (aNodes.size() > 0) {
506 node.id = 2 * aNodes.size() - 2;
508 node.id = 2 * bNodes.size() - 1;
511 void next(Node& node) const {
514 node.id = 2 * bNodes.size() - 1;
518 void first(UEdge& edge) const {
519 edge.id = edges.size() - 1;
521 void next(UEdge& edge) const {
525 void firstFromANode(UEdge& edge, const Node& node) const {
526 LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
527 edge.id = aNodes[node.id >> 1].first;
529 void nextFromANode(UEdge& edge) const {
530 edge.id = edges[edge.id].next_out;
533 void firstFromBNode(UEdge& edge, const Node& node) const {
534 LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
535 edge.id = bNodes[node.id >> 1].first;
537 void nextFromBNode(UEdge& edge) const {
538 edge.id = edges[edge.id].next_in;
541 static int id(const Node& node) {
544 static Node nodeFromId(int id) {
547 int maxNodeId() const {
548 return aNodes.size() > bNodes.size() ?
549 aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
552 static int id(const UEdge& edge) {
555 static UEdge uEdgeFromId(int id) {
558 int maxUEdgeId() const {
562 static int aNodeId(const Node& node) {
565 static Node fromANodeId(int id) {
566 return Node(id << 1);
568 int maxANodeId() const {
569 return aNodes.size();
572 static int bNodeId(const Node& node) {
575 static Node fromBNodeId(int id) {
576 return Node((id << 1) + 1);
578 int maxBNodeId() const {
579 return bNodes.size();
582 Node aNode(const UEdge& edge) const {
583 return Node(edges[edge.id].aNode);
585 Node bNode(const UEdge& edge) const {
586 return Node(edges[edge.id].bNode);
589 static bool aNode(const Node& node) {
590 return (node.id & 1) == 0;
593 static bool bNode(const Node& node) {
594 return (node.id & 1) == 1;
600 aNodes.push_back(nodeT);
601 return Node(aNodes.size() * 2 - 2);
607 bNodes.push_back(nodeT);
608 return Node(bNodes.size() * 2 - 1);
611 UEdge addEdge(const Node& source, const Node& target) {
612 LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
614 if ((source.id & 1) == 0) {
615 edgeT.aNode = source.id;
616 edgeT.bNode = target.id;
618 edgeT.aNode = target.id;
619 edgeT.bNode = source.id;
621 edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
622 aNodes[edgeT.aNode >> 1].first = edges.size();
623 edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
624 bNodes[edgeT.bNode >> 1].first = edges.size();
625 edges.push_back(edgeT);
626 return UEdge(edges.size() - 1);
635 typedef True NodeNumTag;
636 int nodeNum() const { return aNodes.size() + bNodes.size(); }
637 int aNodeNum() const { return aNodes.size(); }
638 int bNodeNum() const { return bNodes.size(); }
640 typedef True EdgeNumTag;
641 int uEdgeNum() const { return edges.size(); }
646 typedef BpUGraphExtender<SmartBpUGraphBase> ExtendedSmartBpUGraphBase;
650 /// \brief A smart bipartite undirected graph class.
652 /// This is a simple and fast bipartite undirected graph implementation.
653 /// It is also quite memory efficient, but at the price
654 /// that <b> it does not support node and edge deletions</b>.
655 /// Except from this it conforms to
656 /// the \ref concept::BpUGraph "BpUGraph concept".
657 /// \sa concept::BpUGraph.
659 class SmartBpUGraph : public ExtendedSmartBpUGraphBase {};
666 #endif //LEMON_SMART_GRAPH_H