Bugfix. (unionfind segfaulted when compiled with icc)
2 * src/hugo/smart_graph.h - Part of HUGOlib, 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 HUGO_SMART_GRAPH_H
18 #define HUGO_SMART_GRAPH_H
22 ///\brief SmartGraph and SymSmartGraph classes.
27 #include <hugo/invalid.h>
29 #include <hugo/array_map.h>
30 #include <hugo/map_registry.h>
32 #include <hugo/map_defines.h>
36 /// \addtogroup graphs
38 // class SymSmartGraph;
40 ///A smart graph class.
42 ///This is a simple and fast graph implementation.
43 ///It is also quite memory efficient, but at the price
44 ///that <b> it does not support node and edge deletion</b>.
46 ///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept.
47 ///\sa skeleton::ExtendableGraph.
49 ///\todo Some member functions could be \c static.
51 ///\todo A possibly useful functionality: a function saveState() would
52 ///give back a data sturcture X and then the function restoreState(X)
53 ///would remove the nodes and edges added after the call of saveState().
54 ///Of course it should be used as a stack. (Maybe X is not necessary.)
56 ///\author Alpar Juttner
61 int first_in,first_out;
62 NodeT() : first_in(-1), first_out(-1) {}
66 int head, tail, next_in, next_out;
67 //FIXME: is this necessary?
68 EdgeT() : next_in(-1), next_out(-1) {}
71 std::vector<NodeT> nodes;
73 std::vector<EdgeT> edges;
78 typedef SmartGraph Graph;
88 // Create map registries.
89 CREATE_MAP_REGISTRIES;
90 // Create node and edge maps.
91 CREATE_MAPS(ArrayMap);
95 SmartGraph() : nodes(), edges() { }
96 SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
99 int nodeNum() const { return nodes.size(); }
101 int edgeNum() const { return edges.size(); }
107 int maxNodeId() const { return nodes.size()-1; }
112 int maxEdgeId() const { return edges.size()-1; }
114 Node tail(Edge e) const { return edges[e.n].tail; }
115 Node head(Edge e) const { return edges[e.n].head; }
117 NodeIt& first(NodeIt& v) const {
118 v=NodeIt(*this); return v; }
119 EdgeIt& first(EdgeIt& e) const {
120 e=EdgeIt(*this); return e; }
121 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
122 e=OutEdgeIt(*this,v); return e; }
123 InEdgeIt& first(InEdgeIt& e, const Node v) const {
124 e=InEdgeIt(*this,v); return e; }
128 /// The ID of a valid Node is a nonnegative integer not greater than
129 /// \ref maxNodeId(). The range of the ID's is not surely continuous
130 /// and the greatest node ID can be actually less then \ref maxNodeId().
132 /// The ID of the \ref INVALID node is -1.
133 ///\return The ID of the node \c v.
134 static int id(Node v) { return v.n; }
137 /// The ID of a valid Edge is a nonnegative integer not greater than
138 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
139 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
141 /// The ID of the \ref INVALID edge is -1.
142 ///\return The ID of the edge \c e.
143 static int id(Edge e) { return e.n; }
146 Node n; n.n=nodes.size();
147 nodes.push_back(NodeT()); //FIXME: Hmmm...
154 Edge addEdge(Node u, Node v) {
155 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
156 edges[e.n].tail=u.n; edges[e.n].head=v.n;
157 edges[e.n].next_out=nodes[u.n].first_out;
158 edges[e.n].next_in=nodes[v.n].first_in;
159 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
166 /// Finds an edge between two nodes.
168 /// Finds an edge from node \c u to node \c v.
170 /// If \c prev is \ref INVALID (this is the default value), then
171 /// It finds the first edge from \c u to \c v. Otherwise it looks for
172 /// the next edge from \c u to \c v after \c prev.
173 /// \return The found edge or INVALID if there is no such an edge.
174 Edge findEdge(Node u,Node v, Edge prev = INVALID)
176 int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
177 while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
190 friend class SmartGraph;
191 template <typename T> friend class NodeMap;
194 friend class OutEdgeIt;
195 friend class InEdgeIt;
196 friend class SymEdge;
200 friend int SmartGraph::id(Node v);
204 Node (Invalid) { n=-1; }
205 bool operator==(const Node i) const {return n==i.n;}
206 bool operator!=(const Node i) const {return n!=i.n;}
207 bool operator<(const Node i) const {return n<i.n;}
209 // operator bool() { return n!=-1; }
212 class NodeIt : public Node {
214 friend class SmartGraph;
216 NodeIt() : Node() { }
217 NodeIt(const SmartGraph& _G,Node n) : Node(n), G(&_G) { }
218 NodeIt(Invalid i) : Node(i) { }
219 NodeIt(const SmartGraph& _G) : Node(_G.nodes.size()?0:-1), G(&_G) { }
220 NodeIt &operator++() {
221 n=(n+2)%(G->nodes.size()+1)-1;
225 // operator bool() { return Node::operator bool(); }
229 friend class SmartGraph;
230 template <typename T> friend class EdgeMap;
232 friend class SymSmartGraph;
238 friend int SmartGraph::id(Edge e);
241 /// An Edge with id \c n.
244 Edge (Invalid) { n=-1; }
245 bool operator==(const Edge i) const {return n==i.n;}
246 bool operator!=(const Edge i) const {return n!=i.n;}
247 bool operator<(const Edge i) const {return n<i.n;}
249 // operator bool() { return n!=-1; }
251 ///Set the edge to that have ID \c ID.
252 void setToId(int id) { n=id; }
255 class EdgeIt : public Edge {
257 friend class SmartGraph;
259 EdgeIt(const SmartGraph& _G) : Edge(_G.edges.size()-1), G(&_G) { }
260 EdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
261 EdgeIt (Invalid i) : Edge(i) { }
262 EdgeIt() : Edge() { }
263 EdgeIt &operator++() { --n; return *this; }
265 // operator bool() { return Edge::operator bool(); }
268 class OutEdgeIt : public Edge {
270 friend class SmartGraph;
272 OutEdgeIt() : Edge() { }
273 OutEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
274 OutEdgeIt (Invalid i) : Edge(i) { }
276 OutEdgeIt(const SmartGraph& _G,const Node v)
277 : Edge(_G.nodes[v.n].first_out), G(&_G) {}
278 OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
280 // operator bool() { return Edge::operator bool(); }
283 class InEdgeIt : public Edge {
285 friend class SmartGraph;
287 InEdgeIt() : Edge() { }
288 InEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
289 InEdgeIt (Invalid i) : Edge(i) { }
290 InEdgeIt(const SmartGraph& _G,Node v)
291 : Edge(_G.nodes[v.n].first_in), G(&_G) { }
292 InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
294 // operator bool() { return Edge::operator bool(); }
301 class SymSmartGraph : public SmartGraph {
302 typedef SmartGraph Parent;
305 typedef SymSmartGraph Graph;
307 typedef SmartGraph::Node Node;
308 typedef SmartGraph::NodeIt NodeIt;
318 template <typename Value>
319 class NodeMap : public Parent::NodeMap<Value> {
321 NodeMap(const SymSmartGraph& g)
322 : SymSmartGraph::Parent::NodeMap<Value>(g) {}
323 NodeMap(const SymSmartGraph& g, Value v)
324 : SymSmartGraph::Parent::NodeMap<Value>(g, v) {}
325 template<typename TT>
326 NodeMap(const NodeMap<TT>& copy)
327 : SymSmartGraph::Parent::NodeMap<Value>(copy) { }
330 template <typename Value>
331 class SymEdgeMap : public Parent::EdgeMap<Value> {
333 typedef SymEdge KeyType;
335 SymEdgeMap(const SymSmartGraph& g)
336 : SymSmartGraph::Parent::EdgeMap<Value>(g) {}
337 SymEdgeMap(const SymSmartGraph& g, Value v)
338 : SymSmartGraph::Parent::EdgeMap<Value>(g, v) {}
339 template<typename TT>
340 SymEdgeMap(const SymEdgeMap<TT>& copy)
341 : SymSmartGraph::Parent::EdgeMap<Value>(copy) { }
345 // Create edge map registry.
346 CREATE_EDGE_MAP_REGISTRY;
348 CREATE_EDGE_MAP(ArrayMap);
351 friend class SymSmartGraph;
352 friend class SymSmartGraph::EdgeIt;
353 friend class SymSmartGraph::OutEdgeIt;
354 friend class SymSmartGraph::InEdgeIt;
359 Edge(int pid) { id = pid; }
362 /// An Edge with id \c n.
365 Edge (Invalid) { id = -1; }
367 operator SymEdge(){ return SymEdge(id >> 1);}
369 bool operator==(const Edge i) const {return id == i.id;}
370 bool operator!=(const Edge i) const {return id != i.id;}
371 bool operator<(const Edge i) const {return id < i.id;}
373 // operator bool() { return n!=-1; }
376 class SymEdge : public SmartGraph::Edge {
377 friend class SymSmartGraph;
378 friend class SymSmartGraph::Edge;
379 typedef SmartGraph::Edge Parent;
382 SymEdge(int pid) : Parent(pid) {}
386 SymEdge(const SmartGraph::Edge& i) : Parent(i) {}
387 SymEdge (Invalid) : Parent(INVALID) {}
392 Parent::OutEdgeIt out;
396 OutEdgeIt(const SymSmartGraph& g, Edge e) {
398 out = Parent::OutEdgeIt(g, SymEdge(e));
399 in = Parent::InEdgeIt(g, g.tail(e));
401 out = Parent::OutEdgeIt(INVALID);
402 in = Parent::InEdgeIt(g, SymEdge(e));
405 OutEdgeIt (Invalid i) : out(INVALID), in(INVALID) { }
407 OutEdgeIt(const SymSmartGraph& g, const Node v)
408 : out(g, v), in(g, v) {}
409 OutEdgeIt &operator++() {
410 if (out != INVALID) {
418 operator Edge() const {
419 if (out == INVALID && in == INVALID) return INVALID;
420 return out != INVALID ? forward(out) : backward(in);
423 bool operator==(const Edge i) const {return Edge(*this) == i;}
424 bool operator!=(const Edge i) const {return Edge(*this) != i;}
425 bool operator<(const Edge i) const {return Edge(*this) < i;}
429 Parent::OutEdgeIt out;
433 InEdgeIt(const SymSmartGraph& g, Edge e) {
435 out = Parent::OutEdgeIt(g, SymEdge(e));
436 in = Parent::InEdgeIt(g, g.tail(e));
438 out = Parent::OutEdgeIt(INVALID);
439 in = Parent::InEdgeIt(g, SymEdge(e));
442 InEdgeIt (Invalid i) : out(INVALID), in(INVALID) { }
444 InEdgeIt(const SymSmartGraph& g, const Node v)
445 : out(g, v), in(g, v) {}
447 InEdgeIt &operator++() {
448 if (out != INVALID) {
456 operator Edge() const {
457 if (out == INVALID && in == INVALID) return INVALID;
458 return out != INVALID ? backward(out) : forward(in);
461 bool operator==(const Edge i) const {return Edge(*this) == i;}
462 bool operator!=(const Edge i) const {return Edge(*this) != i;}
463 bool operator<(const Edge i) const {return Edge(*this) < i;}
466 class SymEdgeIt : public Parent::EdgeIt {
471 SymEdgeIt(const SymSmartGraph& g)
472 : SymSmartGraph::Parent::EdgeIt(g) {}
474 SymEdgeIt(const SymSmartGraph& g, SymEdge e)
475 : SymSmartGraph::Parent::EdgeIt(g, e) {}
478 : SymSmartGraph::Parent::EdgeIt(INVALID) {}
480 SymEdgeIt& operator++() {
481 SymSmartGraph::Parent::EdgeIt::operator++();
485 operator SymEdge() const {
487 (static_cast<const SymSmartGraph::Parent::EdgeIt&>(*this));
489 bool operator==(const SymEdge i) const {return SymEdge(*this) == i;}
490 bool operator!=(const SymEdge i) const {return SymEdge(*this) != i;}
491 bool operator<(const SymEdge i) const {return SymEdge(*this) < i;}
498 EdgeIt(const SymSmartGraph& g) : it(g), fw(true) {}
499 EdgeIt (Invalid i) : it(i) { }
500 EdgeIt(const SymSmartGraph& g, Edge e)
501 : it(g, SymEdge(e)), fw(id(e) & 1 == 0) { }
503 EdgeIt& operator++() {
508 operator Edge() const {
509 if (it == INVALID) return INVALID;
510 return fw ? forward(it) : backward(it);
512 bool operator==(const Edge i) const {return Edge(*this) == i;}
513 bool operator!=(const Edge i) const {return Edge(*this) != i;}
514 bool operator<(const Edge i) const {return Edge(*this) < i;}
519 int nodeNum() const { return Parent::nodeNum(); }
521 int edgeNum() const { return 2*Parent::edgeNum(); }
522 ///Number of symmetric edges.
523 int symEdgeNum() const { return Parent::edgeNum(); }
529 int maxNodeId() const { return Parent::maxNodeId(); }
534 int maxEdgeId() const { return 2*Parent::maxEdgeId(); }
535 /// Maximum symmetric edge ID.
537 /// Maximum symmetric edge ID.
539 int maxSymEdgeId() const { return Parent::maxEdgeId(); }
542 Node tail(Edge e) const {
543 return e.id & 1 == 0 ?
544 Parent::tail(SymEdge(e)) : Parent::head(SymEdge(e));
547 Node head(Edge e) const {
548 return e.id & 1 == 0 ?
549 Parent::head(SymEdge(e)) : Parent::tail(SymEdge(e));
552 Node tail(SymEdge e) const {
553 return Parent::tail(e);
556 Node head(SymEdge e) const {
557 return Parent::head(e);
560 NodeIt& first(NodeIt& v) const {
561 v=NodeIt(*this); return v; }
562 EdgeIt& first(EdgeIt& e) const {
563 e=EdgeIt(*this); return e; }
564 SymEdgeIt& first(SymEdgeIt& e) const {
565 e=SymEdgeIt(*this); return e; }
566 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
567 e=OutEdgeIt(*this,v); return e; }
568 InEdgeIt& first(InEdgeIt& e, const Node v) const {
569 e=InEdgeIt(*this,v); return e; }
573 /// The ID of a valid Node is a nonnegative integer not greater than
574 /// \ref maxNodeId(). The range of the ID's is not surely continuous
575 /// and the greatest node ID can be actually less then \ref maxNodeId().
577 /// The ID of the \ref INVALID node is -1.
578 ///\return The ID of the node \c v.
579 static int id(Node v) { return Parent::id(v); }
582 /// The ID of a valid Edge is a nonnegative integer not greater than
583 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
584 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
586 /// The ID of the \ref INVALID edge is -1.
587 ///\return The ID of the edge \c e.
588 static int id(Edge e) { return e.id; }
590 /// The ID of a valid SymEdge is a nonnegative integer not greater than
591 /// \ref maxSymEdgeId(). The range of the ID's is not surely continuous
592 /// and the greatest edge ID can be actually less then \ref maxSymEdgeId().
594 /// The ID of the \ref INVALID symmetric edge is -1.
595 ///\return The ID of the edge \c e.
596 static int id(SymEdge e) { return Parent::id(e); }
598 /// Adds a new node to the graph.
600 /// \warning It adds the new node to the front of the list.
601 /// (i.e. the lastly added node becomes the first.)
603 return Parent::addNode();
606 SymEdge addEdge(Node u, Node v) {
607 SymEdge se = Parent::addEdge(u, v);
608 edge_maps.add(forward(se));
609 edge_maps.add(backward(se));
613 /// Finds an edge between two nodes.
615 /// Finds an edge from node \c u to node \c v.
617 /// If \c prev is \ref INVALID (this is the default value), then
618 /// It finds the first edge from \c u to \c v. Otherwise it looks for
619 /// the next edge from \c u to \c v after \c prev.
620 /// \return The found edge or INVALID if there is no such an edge.
621 Edge findEdge(Node u, Node v, Edge prev = INVALID)
623 if (prev == INVALID || id(prev) & 1 == 0) {
624 SymEdge se = Parent::findEdge(u, v, SymEdge(prev));
625 if (se != INVALID) return forward(se);
627 SymEdge se = Parent::findEdge(v, u, SymEdge(prev));
628 if (se != INVALID) return backward(se);
633 /// Finds an symmetric edge between two nodes.
635 /// Finds an symmetric edge from node \c u to node \c v.
637 /// If \c prev is \ref INVALID (this is the default value), then
638 /// It finds the first edge from \c u to \c v. Otherwise it looks for
639 /// the next edge from \c u to \c v after \c prev.
640 /// \return The found edge or INVALID if there is no such an edge.
642 // SymEdge findEdge(Node u, Node v, SymEdge prev = INVALID)
644 // if (prev == INVALID || id(prev) & 1 == 0) {
645 // SymEdge se = Parent::findEdge(u, v, SymEdge(prev));
646 // if (se != INVALID) return se;
648 // SymEdge se = Parent::findEdge(v, u, SymEdge(prev));
649 // if (se != INVALID) return se;
661 static Edge opposite(Edge e) {
662 return Edge(id(e) ^ 1);
665 static Edge forward(SymEdge e) {
666 return Edge(id(e) << 1);
669 static Edge backward(SymEdge e) {
670 return Edge((id(e) << 1) & 1);
674 ///Graph for bidirectional edges.
676 ///The purpose of this graph structure is to handle graphs
677 ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
678 ///of oppositely directed edges.
679 ///There is a new edge map type called
680 ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
681 ///that complements this
683 ///storing shared values for the edge pairs. The usual
684 ///\ref Graph::EdgeMap "EdgeMap"
688 ///The oppositely directed edge can also be obtained easily
689 ///using \ref opposite.
690 ///\warning It shares the similarity with \ref SmartGraph that
691 ///it is not possible to delete edges or nodes from the graph.
694 /* class SymSmartGraph : public SmartGraph
697 typedef SymSmartGraph Graph;
699 // Create symmetric map registry.
700 CREATE_SYM_EDGE_MAP_REGISTRY;
701 // Create symmetric edge map.
702 CREATE_SYM_EDGE_MAP(ArrayMap);
705 SymSmartGraph() : SmartGraph() { }
706 SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
707 ///Adds a pair of oppositely directed edges to the graph.
708 Edge addEdge(Node u, Node v)
710 Edge e = SmartGraph::addEdge(u,v);
711 Edge f = SmartGraph::addEdge(v,u);
712 sym_edge_maps.add(e);
713 sym_edge_maps.add(f);
717 ///The oppositely directed edge.
719 ///Returns the oppositely directed
720 ///pair of the edge \c e.
721 static Edge opposite(Edge e)
724 f.n = e.n - 2*(e.n%2) + 1;
737 #endif //HUGO_SMART_GRAPH_H