1 | /* -*- C++ -*- |
---|
2 | * src/lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
6 | * |
---|
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. |
---|
10 | * |
---|
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 |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_SMART_GRAPH_H |
---|
18 | #define LEMON_SMART_GRAPH_H |
---|
19 | |
---|
20 | ///\ingroup graphs |
---|
21 | ///\file |
---|
22 | ///\brief SmartGraph and SymSmartGraph classes. |
---|
23 | |
---|
24 | #include <vector> |
---|
25 | |
---|
26 | #include <lemon/invalid.h> |
---|
27 | |
---|
28 | #include <lemon/erasable_graph_extender.h> |
---|
29 | #include <lemon/clearable_graph_extender.h> |
---|
30 | #include <lemon/extendable_graph_extender.h> |
---|
31 | |
---|
32 | #include <lemon/idmappable_graph_extender.h> |
---|
33 | |
---|
34 | #include <lemon/iterable_graph_extender.h> |
---|
35 | |
---|
36 | #include <lemon/alteration_observer_registry.h> |
---|
37 | #include <lemon/default_map.h> |
---|
38 | |
---|
39 | |
---|
40 | #include <lemon/graph_utils.h> |
---|
41 | |
---|
42 | |
---|
43 | namespace lemon { |
---|
44 | |
---|
45 | /// \addtogroup graphs |
---|
46 | /// @{ |
---|
47 | |
---|
48 | class SmartGraphBase { |
---|
49 | |
---|
50 | struct NodeT |
---|
51 | { |
---|
52 | int first_in,first_out; |
---|
53 | NodeT() : first_in(-1), first_out(-1) {} |
---|
54 | }; |
---|
55 | struct EdgeT |
---|
56 | { |
---|
57 | int head, tail, next_in, next_out; |
---|
58 | //FIXME: is this necessary? |
---|
59 | EdgeT() : next_in(-1), next_out(-1) {} |
---|
60 | }; |
---|
61 | |
---|
62 | std::vector<NodeT> nodes; |
---|
63 | |
---|
64 | std::vector<EdgeT> edges; |
---|
65 | |
---|
66 | |
---|
67 | public: |
---|
68 | |
---|
69 | typedef SmartGraphBase Graph; |
---|
70 | |
---|
71 | class Node; |
---|
72 | class Edge; |
---|
73 | |
---|
74 | |
---|
75 | public: |
---|
76 | |
---|
77 | SmartGraphBase() : nodes(), edges() { } |
---|
78 | SmartGraphBase(const SmartGraphBase &_g) : nodes(_g.nodes), edges(_g.edges) { } |
---|
79 | |
---|
80 | ///Number of nodes. |
---|
81 | int nodeNum() const { return nodes.size(); } |
---|
82 | ///Number of edges. |
---|
83 | int edgeNum() const { return edges.size(); } |
---|
84 | |
---|
85 | /// Maximum node ID. |
---|
86 | |
---|
87 | /// Maximum node ID. |
---|
88 | ///\sa id(Node) |
---|
89 | int maxNodeId() const { return nodes.size()-1; } |
---|
90 | /// Maximum edge ID. |
---|
91 | |
---|
92 | /// Maximum edge ID. |
---|
93 | ///\sa id(Edge) |
---|
94 | int maxEdgeId() const { return edges.size()-1; } |
---|
95 | |
---|
96 | Node tail(Edge e) const { return edges[e.n].tail; } |
---|
97 | Node head(Edge e) const { return edges[e.n].head; } |
---|
98 | |
---|
99 | /// Node ID. |
---|
100 | |
---|
101 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
102 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
103 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
104 | /// |
---|
105 | /// The ID of the \ref INVALID node is -1. |
---|
106 | ///\return The ID of the node \c v. |
---|
107 | static int id(Node v) { return v.n; } |
---|
108 | /// Edge ID. |
---|
109 | |
---|
110 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
111 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
112 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
113 | /// |
---|
114 | /// The ID of the \ref INVALID edge is -1. |
---|
115 | ///\return The ID of the edge \c e. |
---|
116 | static int id(Edge e) { return e.n; } |
---|
117 | |
---|
118 | Node addNode() { |
---|
119 | Node n; n.n=nodes.size(); |
---|
120 | nodes.push_back(NodeT()); //FIXME: Hmmm... |
---|
121 | return n; |
---|
122 | } |
---|
123 | |
---|
124 | Edge addEdge(Node u, Node v) { |
---|
125 | Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm... |
---|
126 | edges[e.n].tail=u.n; edges[e.n].head=v.n; |
---|
127 | edges[e.n].next_out=nodes[u.n].first_out; |
---|
128 | edges[e.n].next_in=nodes[v.n].first_in; |
---|
129 | nodes[u.n].first_out=nodes[v.n].first_in=e.n; |
---|
130 | |
---|
131 | return e; |
---|
132 | } |
---|
133 | |
---|
134 | /// Finds an edge between two nodes. |
---|
135 | |
---|
136 | /// Finds an edge from node \c u to node \c v. |
---|
137 | /// |
---|
138 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
139 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
140 | /// the next edge from \c u to \c v after \c prev. |
---|
141 | /// \return The found edge or INVALID if there is no such an edge. |
---|
142 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
143 | { |
---|
144 | int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out; |
---|
145 | while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out; |
---|
146 | prev.n=e; |
---|
147 | return prev; |
---|
148 | } |
---|
149 | |
---|
150 | void clear() { |
---|
151 | edges.clear(); |
---|
152 | nodes.clear(); |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | class Node { |
---|
157 | friend class SmartGraphBase; |
---|
158 | |
---|
159 | protected: |
---|
160 | int n; |
---|
161 | Node(int nn) {n=nn;} |
---|
162 | public: |
---|
163 | Node() {} |
---|
164 | Node (Invalid) { n=-1; } |
---|
165 | bool operator==(const Node i) const {return n==i.n;} |
---|
166 | bool operator!=(const Node i) const {return n!=i.n;} |
---|
167 | bool operator<(const Node i) const {return n<i.n;} |
---|
168 | }; |
---|
169 | |
---|
170 | |
---|
171 | class Edge { |
---|
172 | friend class SmartGraphBase; |
---|
173 | |
---|
174 | protected: |
---|
175 | int n; |
---|
176 | Edge(int nn) {n=nn;} |
---|
177 | public: |
---|
178 | Edge() { } |
---|
179 | Edge (Invalid) { n=-1; } |
---|
180 | bool operator==(const Edge i) const {return n==i.n;} |
---|
181 | bool operator!=(const Edge i) const {return n!=i.n;} |
---|
182 | bool operator<(const Edge i) const {return n<i.n;} |
---|
183 | }; |
---|
184 | |
---|
185 | void first(Node& node) const { |
---|
186 | node.n = nodes.size() - 1; |
---|
187 | } |
---|
188 | |
---|
189 | static void next(Node& node) { |
---|
190 | --node.n; |
---|
191 | } |
---|
192 | |
---|
193 | void first(Edge& edge) const { |
---|
194 | edge.n = edges.size() - 1; |
---|
195 | } |
---|
196 | |
---|
197 | static void next(Edge& edge) { |
---|
198 | --edge.n; |
---|
199 | } |
---|
200 | |
---|
201 | void firstOut(Edge& edge, const Node& node) const { |
---|
202 | edge.n = nodes[node.n].first_out; |
---|
203 | } |
---|
204 | |
---|
205 | void nextOut(Edge& edge) const { |
---|
206 | edge.n = edges[edge.n].next_out; |
---|
207 | } |
---|
208 | |
---|
209 | void firstIn(Edge& edge, const Node& node) const { |
---|
210 | edge.n = nodes[node.n].first_in; |
---|
211 | } |
---|
212 | |
---|
213 | void nextIn(Edge& edge) const { |
---|
214 | edge.n = edges[edge.n].next_in; |
---|
215 | } |
---|
216 | |
---|
217 | }; |
---|
218 | |
---|
219 | typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase; |
---|
220 | typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase; |
---|
221 | typedef IdMappableGraphExtender<IterableSmartGraphBase> IdMappableSmartGraphBase; |
---|
222 | typedef DefaultMappableGraphExtender<IdMappableSmartGraphBase> MappableSmartGraphBase; |
---|
223 | typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase; |
---|
224 | typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase; |
---|
225 | |
---|
226 | ///A smart graph class. |
---|
227 | |
---|
228 | ///This is a simple and fast graph implementation. |
---|
229 | ///It is also quite memory efficient, but at the price |
---|
230 | ///that <b> it does not support node and edge deletion</b>. |
---|
231 | ///It conforms to |
---|
232 | ///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept. |
---|
233 | ///\sa skeleton::ExtendableGraph. |
---|
234 | /// |
---|
235 | ///\todo Some member functions could be \c static. |
---|
236 | /// |
---|
237 | ///\todo A possibly useful functionality: a function saveState() |
---|
238 | ///(or snapshot() ) would |
---|
239 | ///give back a data sturcture X and then the function restoreState(X) |
---|
240 | ///(or rollBack() ) |
---|
241 | ///would remove the nodes and edges added after the call of saveState(). |
---|
242 | ///Of course it should be used as a stack. (Maybe X is not necessary.) |
---|
243 | /// |
---|
244 | ///\author Alpar Juttner |
---|
245 | class SmartGraph :public ClearableSmartGraphBase { }; |
---|
246 | |
---|
247 | template <> |
---|
248 | int countNodes<SmartGraph>(const SmartGraph& graph) { |
---|
249 | return graph.nodeNum(); |
---|
250 | } |
---|
251 | |
---|
252 | template <> |
---|
253 | int countEdges<SmartGraph>(const SmartGraph& graph) { |
---|
254 | return graph.edgeNum(); |
---|
255 | } |
---|
256 | |
---|
257 | /// @} |
---|
258 | } //namespace lemon |
---|
259 | |
---|
260 | |
---|
261 | |
---|
262 | |
---|
263 | #endif //LEMON_SMART_GRAPH_H |
---|