2 * lemon/full_graph.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, 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_FULL_GRAPH_H
18 #define LEMON_FULL_GRAPH_H
23 #include <lemon/bits/iterable_graph_extender.h>
24 #include <lemon/bits/alteration_notifier.h>
25 #include <lemon/bits/static_map.h>
27 #include <lemon/bits/undir_graph_extender.h>
29 #include <lemon/invalid.h>
30 #include <lemon/utility.h>
35 ///\brief FullGraph and UndirFullGraph classes.
45 typedef FullGraphBase Graph;
55 ///Creates a full graph with \c n nodes.
56 void construct(int n) { _nodeNum = n; _edgeNum = n * n; }
58 // FullGraphBase(const FullGraphBase &_g)
59 // : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
61 typedef True NodeNumTag;
62 typedef True EdgeNumTag;
65 int nodeNum() const { return _nodeNum; }
67 int edgeNum() const { return _edgeNum; }
73 int maxId(Node = INVALID) const { return _nodeNum-1; }
78 int maxId(Edge = INVALID) const { return _edgeNum-1; }
80 Node source(Edge e) const { return e.id % _nodeNum; }
81 Node target(Edge e) const { return e.id / _nodeNum; }
86 /// The ID of a valid Node is a nonnegative integer not greater than
87 /// \ref maxNodeId(). The range of the ID's is not surely continuous
88 /// and the greatest node ID can be actually less then \ref maxNodeId().
90 /// The ID of the \ref INVALID node is -1.
91 ///\return The ID of the node \c v.
93 static int id(Node v) { return v.id; }
96 /// The ID of a valid Edge is a nonnegative integer not greater than
97 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
98 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
100 /// The ID of the \ref INVALID edge is -1.
101 ///\return The ID of the edge \c e.
102 static int id(Edge e) { return e.id; }
104 static Node fromId(int id, Node) { return Node(id);}
106 static Edge fromId(int id, Edge) { return Edge(id);}
108 typedef True FindEdgeTag;
110 /// Finds an edge between two nodes.
112 /// Finds an edge from node \c u to node \c v.
114 /// If \c prev is \ref INVALID (this is the default value), then
115 /// It finds the first edge from \c u to \c v. Otherwise it looks for
116 /// the next edge from \c u to \c v after \c prev.
117 /// \return The found edge or INVALID if there is no such an edge.
118 Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
119 return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
124 friend class FullGraphBase;
128 Node(int _id) : id(_id) {}
131 Node (Invalid) : id(-1) {}
132 bool operator==(const Node node) const {return id == node.id;}
133 bool operator!=(const Node node) const {return id != node.id;}
134 bool operator<(const Node node) const {return id < node.id;}
140 friend class FullGraphBase;
143 int id; // _nodeNum * target + source;
145 Edge(int _id) : id(_id) {}
147 Edge(const FullGraphBase& _graph, int source, int target)
148 : id(_graph._nodeNum * target+source) {}
151 Edge (Invalid) { id = -1; }
152 bool operator==(const Edge edge) const {return id == edge.id;}
153 bool operator!=(const Edge edge) const {return id != edge.id;}
154 bool operator<(const Edge edge) const {return id < edge.id;}
157 void first(Node& node) const {
158 node.id = _nodeNum-1;
161 static void next(Node& node) {
165 void first(Edge& edge) const {
166 edge.id = _edgeNum-1;
169 static void next(Edge& edge) {
173 void firstOut(Edge& edge, const Node& node) const {
174 edge.id = _edgeNum + node.id - _nodeNum;
177 void nextOut(Edge& edge) const {
179 if (edge.id < 0) edge.id = -1;
182 void firstIn(Edge& edge, const Node& node) const {
183 edge.id = node.id * _nodeNum;
186 void nextIn(Edge& edge) const {
188 if (edge.id % _nodeNum == 0) edge.id = -1;
194 typedef AlterableGraphExtender<FullGraphBase>
195 AlterableFullGraphBase;
196 typedef IterableGraphExtender<AlterableFullGraphBase>
197 IterableFullGraphBase;
198 typedef StaticMappableGraphExtender<
199 IterableGraphExtender<
200 AlterableGraphExtender<FullGraphBase> > > ExtendedFullGraphBase;
204 /// \brief A full graph class.
206 /// This is a simple and fast directed full graph implementation.
207 /// It is completely static, so you can neither add nor delete either
209 /// Thus it conforms to
210 /// the \ref concept::StaticGraph "StaticGraph" concept
211 /// \sa concept::StaticGraph.
213 /// \author Alpar Juttner
214 class FullGraph : public ExtendedFullGraphBase {
217 FullGraph(int n) { construct(n); }
222 class UndirFullGraphBase {
227 typedef UndirFullGraphBase Graph;
234 UndirFullGraphBase() {}
237 ///Creates a full graph with \c n nodes.
238 void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
240 // FullGraphBase(const FullGraphBase &_g)
241 // : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
243 typedef True NodeNumTag;
244 typedef True EdgeNumTag;
247 int nodeNum() const { return _nodeNum; }
249 int edgeNum() const { return _edgeNum; }
255 int maxId(Node = INVALID) const { return _nodeNum-1; }
260 int maxId(Edge = INVALID) const { return _edgeNum-1; }
262 Node source(Edge e) const {
263 /// \todo we may do it faster
264 return Node(((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2);
267 Node target(Edge e) const {
268 int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
269 return Node(e.id - (source) * (source - 1) / 2);
275 /// The ID of a valid Node is a nonnegative integer not greater than
276 /// \ref maxNodeId(). The range of the ID's is not surely continuous
277 /// and the greatest node ID can be actually less then \ref maxNodeId().
279 /// The ID of the \ref INVALID node is -1.
280 ///\return The ID of the node \c v.
282 static int id(Node v) { return v.id; }
285 /// The ID of a valid Edge is a nonnegative integer not greater than
286 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
287 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
289 /// The ID of the \ref INVALID edge is -1.
290 ///\return The ID of the edge \c e.
291 static int id(Edge e) { return e.id; }
293 /// Finds an edge between two nodes.
295 /// Finds an edge from node \c u to node \c v.
297 /// If \c prev is \ref INVALID (this is the default value), then
298 /// It finds the first edge from \c u to \c v. Otherwise it looks for
299 /// the next edge from \c u to \c v after \c prev.
300 /// \return The found edge or INVALID if there is no such an edge.
301 Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
302 if (prev.id != -1 || u.id <= v.id) return -1;
303 return Edge(u.id * (u.id - 1) / 2 + v.id);
306 typedef True FindEdgeTag;
310 friend class UndirFullGraphBase;
314 Node(int _id) { id = _id;}
317 Node (Invalid) { id = -1; }
318 bool operator==(const Node node) const {return id == node.id;}
319 bool operator!=(const Node node) const {return id != node.id;}
320 bool operator<(const Node node) const {return id < node.id;}
326 friend class UndirFullGraphBase;
329 int id; // _nodeNum * target + source;
331 Edge(int _id) : id(_id) {}
335 Edge (Invalid) { id = -1; }
336 bool operator==(const Edge edge) const {return id == edge.id;}
337 bool operator!=(const Edge edge) const {return id != edge.id;}
338 bool operator<(const Edge edge) const {return id < edge.id;}
341 void first(Node& node) const {
342 node.id = _nodeNum - 1;
345 static void next(Node& node) {
349 void first(Edge& edge) const {
350 edge.id = _edgeNum - 1;
353 static void next(Edge& edge) {
357 void firstOut(Edge& edge, const Node& node) const {
360 edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
363 /// \todo with specialized iterators we can make faster iterating
364 void nextOut(Edge& edge) const {
365 int src = source(edge).id;
366 int trg = target(edge).id;
368 edge.id = (trg < src ? src * (src - 1) / 2 + trg : -1);
371 void firstIn(Edge& edge, const Node& node) const {
372 int src = node.id + 1;
374 edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
377 void nextIn(Edge& edge) const {
378 int src = source(edge).id;
379 int trg = target(edge).id;
381 edge.id = (src < _nodeNum ? src * (src - 1) / 2 + trg : -1);
386 typedef StaticMappableUndirGraphExtender<
387 IterableUndirGraphExtender<
388 AlterableUndirGraphExtender<
389 UndirGraphExtender<UndirFullGraphBase> > > > ExtendedUndirFullGraphBase;
393 /// \brief An undirected full graph class.
395 /// This is a simple and fast directed full graph implementation.
396 /// It is completely static, so you can neither add nor delete either
399 /// The main difference beetween the \e FullGraph and \e UndirFullGraph class
400 /// is that this class conforms to the undirected graph concept and
401 /// it does not contain the hook edges.
405 /// \author Balazs Dezso
406 class UndirFullGraph : public ExtendedUndirFullGraphBase {
408 UndirFullGraph(int n) { construct(n); }
414 #endif //LEMON_FULL_GRAPH_H