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.
26 #include <lemon/invalid.h>
28 #include <lemon/erasable_graph_extender.h>
29 #include <lemon/clearable_graph_extender.h>
30 #include <lemon/extendable_graph_extender.h>
32 #include <lemon/idmappable_graph_extender.h>
34 #include <lemon/iterable_graph_extender.h>
36 #include <lemon/alteration_observer_registry.h>
37 #include <lemon/default_map.h>
40 #include <lemon/graph_utils.h>
45 /// \addtogroup graphs
52 class SmartGraphBase {
56 int first_in,first_out;
57 NodeT() : first_in(-1), first_out(-1) {}
61 int head, tail, next_in, next_out;
62 //FIXME: is this necessary?
63 EdgeT() : next_in(-1), next_out(-1) {}
66 std::vector<NodeT> nodes;
68 std::vector<EdgeT> edges;
73 typedef SmartGraphBase Graph;
81 SmartGraphBase() : nodes(), edges() { }
82 SmartGraphBase(const SmartGraphBase &_g) : nodes(_g.nodes), edges(_g.edges) { }
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; }
100 Node tail(Edge e) const { return edges[e.n].tail; }
101 Node head(Edge e) const { return edges[e.n].head; }
105 /// The ID of a valid Node is a nonnegative integer not greater than
106 /// \ref maxNodeId(). The range of the ID's is not surely continuous
107 /// and the greatest node ID can be actually less then \ref maxNodeId().
109 /// The ID of the \ref INVALID node is -1.
110 ///\return The ID of the node \c v.
111 static int id(Node v) { return v.n; }
114 /// The ID of a valid Edge is a nonnegative integer not greater than
115 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
116 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
118 /// The ID of the \ref INVALID edge is -1.
119 ///\return The ID of the edge \c e.
120 static int id(Edge e) { return e.n; }
123 Node n; n.n=nodes.size();
124 nodes.push_back(NodeT()); //FIXME: Hmmm...
128 Edge addEdge(Node u, Node v) {
129 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
130 edges[e.n].tail=u.n; edges[e.n].head=v.n;
131 edges[e.n].next_out=nodes[u.n].first_out;
132 edges[e.n].next_in=nodes[v.n].first_in;
133 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
145 friend class SmartGraphBase;
152 Node (Invalid) { n=-1; }
153 bool operator==(const Node i) const {return n==i.n;}
154 bool operator!=(const Node i) const {return n!=i.n;}
155 bool operator<(const Node i) const {return n<i.n;}
160 friend class SmartGraphBase;
167 Edge (Invalid) { n=-1; }
168 bool operator==(const Edge i) const {return n==i.n;}
169 bool operator!=(const Edge i) const {return n!=i.n;}
170 bool operator<(const Edge i) const {return n<i.n;}
173 void first(Node& node) const {
174 node.n = nodes.size() - 1;
177 static void next(Node& node) {
181 void first(Edge& edge) const {
182 edge.n = edges.size() - 1;
185 static void next(Edge& edge) {
189 void firstOut(Edge& edge, const Node& node) const {
190 edge.n = nodes[node.n].first_out;
193 void nextOut(Edge& edge) const {
194 edge.n = edges[edge.n].next_out;
197 void firstIn(Edge& edge, const Node& node) const {
198 edge.n = nodes[node.n].first_in;
201 void nextIn(Edge& edge) const {
202 edge.n = edges[edge.n].next_in;
205 Edge _findEdge(Node u,Node v, Edge prev = INVALID)
207 int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
208 while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
214 typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
215 typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase;
216 typedef IdMappableGraphExtender<IterableSmartGraphBase> IdMappableSmartGraphBase;
217 typedef DefaultMappableGraphExtender<IdMappableSmartGraphBase> MappableSmartGraphBase;
218 typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase;
219 typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase;
221 ///A smart graph class.
223 ///This is a simple and fast graph implementation.
224 ///It is also quite memory efficient, but at the price
225 ///that <b> it does not support node and edge deletion</b>.
227 ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
228 ///\sa concept::ExtendableGraph.
230 ///\todo Some member functions could be \c static.
232 ///\todo A possibly useful functionality: a function saveState()
233 ///(or snapshot() ) would
234 ///give back a data sturcture X and then the function restoreState(X)
236 ///would remove the nodes and edges added after the call of saveState().
237 ///Of course it should be used as a stack. (Maybe X is not necessary.)
239 ///\author Alpar Juttner
240 class SmartGraph :public ClearableSmartGraphBase {
242 /// Finds an edge between two nodes.
244 /// Finds an edge from node \c u to node \c v.
246 /// If \c prev is \ref INVALID (this is the default value), then
247 /// it finds the first edge from \c u to \c v. Otherwise it looks for
248 /// the next edge from \c u to \c v after \c prev.
249 /// \return The found edge or \ref INVALID if there is no such an edge.
251 /// Thus you can iterate through each edge from \c u to \c v as it follows.
253 /// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) {
257 /// \todo Possibly it should be a global function.
258 Edge findEdge(Node u,Node v, Edge prev = INVALID)
260 return _findEdge(u,v,prev);
265 int countNodes<SmartGraph>(const SmartGraph& graph) {
266 return graph.nodeNum();
270 int countEdges<SmartGraph>(const SmartGraph& graph) {
271 return graph.edgeNum();
280 #endif //LEMON_SMART_GRAPH_H