Coding style.
2 * src/lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_SMART_GRAPH_H
18 #define LEMON_SMART_GRAPH_H
22 ///\brief SmartGraph and SymSmartGraph classes.
27 #include <lemon/invalid.h>
30 #include <lemon/array_map.h>
32 #include <lemon/map_registry.h>
34 #include <lemon/map_defines.h>
38 /// \addtogroup graphs
40 // class SymSmartGraph;
42 ///A smart graph class.
44 ///This is a simple and fast graph implementation.
45 ///It is also quite memory efficient, but at the price
46 ///that <b> it does not support node and edge deletion</b>.
48 ///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept.
49 ///\sa skeleton::ExtendableGraph.
51 ///\todo Some member functions could be \c static.
53 ///\todo A possibly useful functionality: a function saveState() would
54 ///give back a data sturcture X and then the function restoreState(X)
55 ///would remove the nodes and edges added after the call of saveState().
56 ///Of course it should be used as a stack. (Maybe X is not necessary.)
58 ///\author Alpar Juttner
63 int first_in,first_out;
64 NodeT() : first_in(-1), first_out(-1) {}
68 int head, tail, next_in, next_out;
69 //FIXME: is this necessary?
70 EdgeT() : next_in(-1), next_out(-1) {}
73 std::vector<NodeT> nodes;
75 std::vector<EdgeT> edges;
80 typedef SmartGraph Graph;
90 // Create map registries.
91 CREATE_MAP_REGISTRIES;
92 // Create node and edge maps.
93 CREATE_MAPS(ArrayMap);
97 SmartGraph() : nodes(), edges() { }
98 SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
101 int nodeNum() const { return nodes.size(); }
103 int edgeNum() const { return edges.size(); }
109 int maxNodeId() const { return nodes.size()-1; }
114 int maxEdgeId() const { return edges.size()-1; }
116 Node tail(Edge e) const { return edges[e.n].tail; }
117 Node head(Edge e) const { return edges[e.n].head; }
119 NodeIt& first(NodeIt& v) const {
120 v=NodeIt(*this); return v; }
121 EdgeIt& first(EdgeIt& e) const {
122 e=EdgeIt(*this); return e; }
123 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
124 e=OutEdgeIt(*this,v); return e; }
125 InEdgeIt& first(InEdgeIt& e, const Node v) const {
126 e=InEdgeIt(*this,v); return e; }
130 /// The ID of a valid Node is a nonnegative integer not greater than
131 /// \ref maxNodeId(). The range of the ID's is not surely continuous
132 /// and the greatest node ID can be actually less then \ref maxNodeId().
134 /// The ID of the \ref INVALID node is -1.
135 ///\return The ID of the node \c v.
136 static int id(Node v) { return v.n; }
139 /// The ID of a valid Edge is a nonnegative integer not greater than
140 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
141 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
143 /// The ID of the \ref INVALID edge is -1.
144 ///\return The ID of the edge \c e.
145 static int id(Edge e) { return e.n; }
148 Node n; n.n=nodes.size();
149 nodes.push_back(NodeT()); //FIXME: Hmmm...
156 Edge addEdge(Node u, Node v) {
157 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
158 edges[e.n].tail=u.n; edges[e.n].head=v.n;
159 edges[e.n].next_out=nodes[u.n].first_out;
160 edges[e.n].next_in=nodes[v.n].first_in;
161 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
168 /// Finds an edge between two nodes.
170 /// Finds an edge from node \c u to node \c v.
172 /// If \c prev is \ref INVALID (this is the default value), then
173 /// It finds the first edge from \c u to \c v. Otherwise it looks for
174 /// the next edge from \c u to \c v after \c prev.
175 /// \return The found edge or INVALID if there is no such an edge.
176 Edge findEdge(Node u,Node v, Edge prev = INVALID)
178 int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
179 while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
192 friend class SmartGraph;
193 template <typename T> friend class NodeMap;
196 friend class OutEdgeIt;
197 friend class InEdgeIt;
198 friend class SymEdge;
202 friend int SmartGraph::id(Node v);
206 Node (Invalid) { n=-1; }
207 bool operator==(const Node i) const {return n==i.n;}
208 bool operator!=(const Node i) const {return n!=i.n;}
209 bool operator<(const Node i) const {return n<i.n;}
211 // operator bool() { return n!=-1; }
214 class NodeIt : public Node {
216 friend class SmartGraph;
218 NodeIt() : Node() { }
219 NodeIt(const SmartGraph& _G,Node n) : Node(n), G(&_G) { }
220 NodeIt(Invalid i) : Node(i) { }
221 NodeIt(const SmartGraph& _G) : Node(_G.nodes.size()?0:-1), G(&_G) { }
222 NodeIt &operator++() {
223 n=(n+2)%(G->nodes.size()+1)-1;
227 // operator bool() { return Node::operator bool(); }
231 friend class SmartGraph;
232 template <typename T> friend class EdgeMap;
234 friend class SymSmartGraph;
240 friend int SmartGraph::id(Edge e);
243 /// An Edge with id \c n.
246 Edge (Invalid) { n=-1; }
247 bool operator==(const Edge i) const {return n==i.n;}
248 bool operator!=(const Edge i) const {return n!=i.n;}
249 bool operator<(const Edge i) const {return n<i.n;}
251 // operator bool() { return n!=-1; }
253 ///Set the edge to that have ID \c ID.
254 void setToId(int id) { n=id; }
257 class EdgeIt : public Edge {
259 friend class SmartGraph;
261 EdgeIt(const SmartGraph& _G) : Edge(_G.edges.size()-1), G(&_G) { }
262 EdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
263 EdgeIt (Invalid i) : Edge(i) { }
264 EdgeIt() : Edge() { }
265 EdgeIt &operator++() { --n; return *this; }
267 // operator bool() { return Edge::operator bool(); }
270 class OutEdgeIt : public Edge {
272 friend class SmartGraph;
274 OutEdgeIt() : Edge() { }
275 OutEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
276 OutEdgeIt (Invalid i) : Edge(i) { }
278 OutEdgeIt(const SmartGraph& _G,const Node v)
279 : Edge(_G.nodes[v.n].first_out), G(&_G) {}
280 OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
282 // operator bool() { return Edge::operator bool(); }
285 class InEdgeIt : public Edge {
287 friend class SmartGraph;
289 InEdgeIt() : Edge() { }
290 InEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
291 InEdgeIt (Invalid i) : Edge(i) { }
292 InEdgeIt(const SmartGraph& _G,Node v)
293 : Edge(_G.nodes[v.n].first_in), G(&_G) { }
294 InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
296 // operator bool() { return Edge::operator bool(); }
303 class SymSmartGraph : public SmartGraph {
304 typedef SmartGraph Parent;
307 typedef SymSmartGraph Graph;
309 typedef SmartGraph::Node Node;
310 typedef SmartGraph::NodeIt NodeIt;
320 template <typename Value>
321 class NodeMap : public Parent::NodeMap<Value> {
323 NodeMap(const SymSmartGraph& g)
324 : SymSmartGraph::Parent::NodeMap<Value>(g) {}
325 NodeMap(const SymSmartGraph& g, Value v)
326 : SymSmartGraph::Parent::NodeMap<Value>(g, v) {}
327 template<typename TT>
328 NodeMap(const NodeMap<TT>& copy)
329 : SymSmartGraph::Parent::NodeMap<Value>(copy) { }
332 template <typename Value>
333 class SymEdgeMap : public Parent::EdgeMap<Value> {
335 typedef SymEdge KeyType;
337 SymEdgeMap(const SymSmartGraph& g)
338 : SymSmartGraph::Parent::EdgeMap<Value>(g) {}
339 SymEdgeMap(const SymSmartGraph& g, Value v)
340 : SymSmartGraph::Parent::EdgeMap<Value>(g, v) {}
341 template<typename TT>
342 SymEdgeMap(const SymEdgeMap<TT>& copy)
343 : SymSmartGraph::Parent::EdgeMap<Value>(copy) { }
347 // Create edge map registry.
348 CREATE_EDGE_MAP_REGISTRY;
350 CREATE_EDGE_MAP(ArrayMap);
353 friend class SymSmartGraph;
354 friend class SymSmartGraph::EdgeIt;
355 friend class SymSmartGraph::OutEdgeIt;
356 friend class SymSmartGraph::InEdgeIt;
361 Edge(int pid) { id = pid; }
364 /// An Edge with id \c n.
367 Edge (Invalid) { id = -1; }
369 operator SymEdge(){ return SymEdge(id >> 1);}
371 bool operator==(const Edge i) const {return id == i.id;}
372 bool operator!=(const Edge i) const {return id != i.id;}
373 bool operator<(const Edge i) const {return id < i.id;}
375 // operator bool() { return n!=-1; }
378 class SymEdge : public SmartGraph::Edge {
379 friend class SymSmartGraph;
380 friend class SymSmartGraph::Edge;
381 typedef SmartGraph::Edge Parent;
384 SymEdge(int pid) : Parent(pid) {}
388 SymEdge(const SmartGraph::Edge& i) : Parent(i) {}
389 SymEdge (Invalid) : Parent(INVALID) {}
394 Parent::OutEdgeIt out;
398 OutEdgeIt(const SymSmartGraph& g, Edge e) {
399 if ((e.id & 1) == 0) {
400 out = Parent::OutEdgeIt(g, SymEdge(e));
401 in = Parent::InEdgeIt(g, g.tail(e));
403 out = Parent::OutEdgeIt(INVALID);
404 in = Parent::InEdgeIt(g, SymEdge(e));
407 OutEdgeIt (Invalid i) : out(INVALID), in(INVALID) { }
409 OutEdgeIt(const SymSmartGraph& g, const Node v)
410 : out(g, v), in(g, v) {}
411 OutEdgeIt &operator++() {
412 if (out != INVALID) {
420 operator Edge() const {
421 if (out == INVALID && in == INVALID) return INVALID;
422 return out != INVALID ? forward(out) : backward(in);
425 bool operator==(const Edge i) const {return Edge(*this) == i;}
426 bool operator!=(const Edge i) const {return Edge(*this) != i;}
427 bool operator<(const Edge i) const {return Edge(*this) < i;}
431 Parent::OutEdgeIt out;
435 InEdgeIt(const SymSmartGraph& g, Edge e) {
436 if ((e.id & 1) == 0) {
437 out = Parent::OutEdgeIt(g, SymEdge(e));
438 in = Parent::InEdgeIt(g, g.tail(e));
440 out = Parent::OutEdgeIt(INVALID);
441 in = Parent::InEdgeIt(g, SymEdge(e));
444 InEdgeIt (Invalid i) : out(INVALID), in(INVALID) { }
446 InEdgeIt(const SymSmartGraph& g, const Node v)
447 : out(g, v), in(g, v) {}
449 InEdgeIt &operator++() {
450 if (out != INVALID) {
458 operator Edge() const {
459 if (out == INVALID && in == INVALID) return INVALID;
460 return out != INVALID ? backward(out) : forward(in);
463 bool operator==(const Edge i) const {return Edge(*this) == i;}
464 bool operator!=(const Edge i) const {return Edge(*this) != i;}
465 bool operator<(const Edge i) const {return Edge(*this) < i;}
468 class SymEdgeIt : public Parent::EdgeIt {
473 SymEdgeIt(const SymSmartGraph& g)
474 : SymSmartGraph::Parent::EdgeIt(g) {}
476 SymEdgeIt(const SymSmartGraph& g, SymEdge e)
477 : SymSmartGraph::Parent::EdgeIt(g, e) {}
480 : SymSmartGraph::Parent::EdgeIt(INVALID) {}
482 SymEdgeIt& operator++() {
483 SymSmartGraph::Parent::EdgeIt::operator++();
487 operator SymEdge() const {
489 (static_cast<const SymSmartGraph::Parent::EdgeIt&>(*this));
491 bool operator==(const SymEdge i) const {return SymEdge(*this) == i;}
492 bool operator!=(const SymEdge i) const {return SymEdge(*this) != i;}
493 bool operator<(const SymEdge i) const {return SymEdge(*this) < i;}
500 EdgeIt(const SymSmartGraph& g) : it(g), fw(true) {}
501 EdgeIt (Invalid i) : it(i) { }
502 EdgeIt(const SymSmartGraph& g, Edge e)
503 : it(g, SymEdge(e)), fw(id(e) & 1 == 0) { }
505 EdgeIt& operator++() {
510 operator Edge() const {
511 if (it == INVALID) return INVALID;
512 return fw ? forward(it) : backward(it);
514 bool operator==(const Edge i) const {return Edge(*this) == i;}
515 bool operator!=(const Edge i) const {return Edge(*this) != i;}
516 bool operator<(const Edge i) const {return Edge(*this) < i;}
521 int nodeNum() const { return Parent::nodeNum(); }
523 int edgeNum() const { return 2*Parent::edgeNum(); }
524 ///Number of symmetric edges.
525 int symEdgeNum() const { return Parent::edgeNum(); }
531 int maxNodeId() const { return Parent::maxNodeId(); }
536 int maxEdgeId() const { return 2*Parent::maxEdgeId(); }
537 /// Maximum symmetric edge ID.
539 /// Maximum symmetric edge ID.
541 int maxSymEdgeId() const { return Parent::maxEdgeId(); }
544 Node tail(Edge e) const {
545 return (e.id & 1) == 0 ?
546 Parent::tail(SymEdge(e)) : Parent::head(SymEdge(e));
549 Node head(Edge e) const {
550 return (e.id & 1) == 0 ?
551 Parent::head(SymEdge(e)) : Parent::tail(SymEdge(e));
554 Node tail(SymEdge e) const {
555 return Parent::tail(e);
558 Node head(SymEdge e) const {
559 return Parent::head(e);
562 NodeIt& first(NodeIt& v) const {
563 v=NodeIt(*this); return v; }
564 EdgeIt& first(EdgeIt& e) const {
565 e=EdgeIt(*this); return e; }
566 SymEdgeIt& first(SymEdgeIt& e) const {
567 e=SymEdgeIt(*this); return e; }
568 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
569 e=OutEdgeIt(*this,v); return e; }
570 InEdgeIt& first(InEdgeIt& e, const Node v) const {
571 e=InEdgeIt(*this,v); return e; }
575 /// The ID of a valid Node is a nonnegative integer not greater than
576 /// \ref maxNodeId(). The range of the ID's is not surely continuous
577 /// and the greatest node ID can be actually less then \ref maxNodeId().
579 /// The ID of the \ref INVALID node is -1.
580 ///\return The ID of the node \c v.
581 static int id(Node v) { return Parent::id(v); }
584 /// The ID of a valid Edge is a nonnegative integer not greater than
585 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
586 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
588 /// The ID of the \ref INVALID edge is -1.
589 ///\return The ID of the edge \c e.
590 static int id(Edge e) { return e.id; }
592 /// The ID of a valid SymEdge is a nonnegative integer not greater than
593 /// \ref maxSymEdgeId(). The range of the ID's is not surely continuous
594 /// and the greatest edge ID can be actually less then \ref maxSymEdgeId().
596 /// The ID of the \ref INVALID symmetric edge is -1.
597 ///\return The ID of the edge \c e.
598 static int id(SymEdge e) { return Parent::id(e); }
600 /// Adds a new node to the graph.
602 /// \warning It adds the new node to the front of the list.
603 /// (i.e. the lastly added node becomes the first.)
605 return Parent::addNode();
608 SymEdge addEdge(Node u, Node v) {
609 SymEdge se = Parent::addEdge(u, v);
610 edge_maps.add(forward(se));
611 edge_maps.add(backward(se));
615 /// Finds an edge between two nodes.
617 /// Finds an edge from node \c u to node \c v.
619 /// If \c prev is \ref INVALID (this is the default value), then
620 /// It finds the first edge from \c u to \c v. Otherwise it looks for
621 /// the next edge from \c u to \c v after \c prev.
622 /// \return The found edge or INVALID if there is no such an edge.
623 Edge findEdge(Node u, Node v, Edge prev = INVALID)
625 if (prev == INVALID || id(prev) & 1 == 0) {
626 SymEdge se = Parent::findEdge(u, v, SymEdge(prev));
627 if (se != INVALID) return forward(se);
629 SymEdge se = Parent::findEdge(v, u, SymEdge(prev));
630 if (se != INVALID) return backward(se);
635 // /// Finds an symmetric edge between two nodes.
637 // /// Finds an symmetric edge from node \c u to node \c v.
639 // /// If \c prev is \ref INVALID (this is the default value), then
640 // /// It finds the first edge from \c u to \c v. Otherwise it looks for
641 // /// the next edge from \c u to \c v after \c prev.
642 // /// \return The found edge or INVALID if there is no such an edge.
644 // SymEdge findEdge(Node u, Node v, SymEdge prev = INVALID)
646 // if (prev == INVALID || id(prev) & 1 == 0) {
647 // SymEdge se = Parent::findEdge(u, v, SymEdge(prev));
648 // if (se != INVALID) return se;
650 // SymEdge se = Parent::findEdge(v, u, SymEdge(prev));
651 // if (se != INVALID) return se;
663 static Edge opposite(Edge e) {
664 return Edge(id(e) ^ 1);
667 static Edge forward(SymEdge e) {
668 return Edge(id(e) << 1);
671 static Edge backward(SymEdge e) {
672 return Edge((id(e) << 1) | 1);
676 ///Graph for bidirectional edges.
678 ///The purpose of this graph structure is to handle graphs
679 ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
680 ///of oppositely directed edges.
681 ///There is a new edge map type called
682 ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
683 ///that complements this
685 ///storing shared values for the edge pairs. The usual
686 ///\ref Graph::EdgeMap "EdgeMap"
690 ///The oppositely directed edge can also be obtained easily
691 ///using \ref opposite.
692 ///\warning It shares the similarity with \ref SmartGraph that
693 ///it is not possible to delete edges or nodes from the graph.
696 /* class SymSmartGraph : public SmartGraph
699 typedef SymSmartGraph Graph;
701 // Create symmetric map registry.
702 CREATE_SYM_EDGE_MAP_REGISTRY;
703 // Create symmetric edge map.
704 CREATE_SYM_EDGE_MAP(ArrayMap);
707 SymSmartGraph() : SmartGraph() { }
708 SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
709 ///Adds a pair of oppositely directed edges to the graph.
710 Edge addEdge(Node u, Node v)
712 Edge e = SmartGraph::addEdge(u,v);
713 Edge f = SmartGraph::addEdge(v,u);
714 sym_edge_maps.add(e);
715 sym_edge_maps.add(f);
719 ///The oppositely directed edge.
721 ///Returns the oppositely directed
722 ///pair of the edge \c e.
723 static Edge opposite(Edge e)
726 f.n = e.n - 2*(e.n%2) + 1;
739 #endif //LEMON_SMART_GRAPH_H