2 * src/lemon/full_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_FULL_GRAPH_H
18 #define LEMON_FULL_GRAPH_H
23 #include <lemon/iterable_graph_extender.h>
24 #include <lemon/alteration_observer_registry.h>
25 #include <lemon/default_map.h>
27 #include <lemon/invalid.h>
28 #include <lemon/utility.h>
33 ///\brief FullGraph and SymFullGraph classes.
38 /// \addtogroup graphs
46 typedef FullGraphBase Graph;
56 ///Creates a full graph with \c n nodes.
57 void construct(int n) { NodeNum = n; EdgeNum = n * n; }
59 // FullGraphBase(const FullGraphBase &_g)
60 // : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
62 typedef True NodeNumTag;
63 typedef True EdgeNumTag;
66 int nodeNum() const { return NodeNum; }
68 int edgeNum() const { return EdgeNum; }
74 int maxId(Node = INVALID) const { return NodeNum-1; }
79 int maxId(Edge = INVALID) const { return EdgeNum-1; }
81 Node tail(Edge e) const { return e.id % NodeNum; }
82 Node head(Edge e) const { return e.id / NodeNum; }
87 /// The ID of a valid Node is a nonnegative integer not greater than
88 /// \ref maxNodeId(). The range of the ID's is not surely continuous
89 /// and the greatest node ID can be actually less then \ref maxNodeId().
91 /// The ID of the \ref INVALID node is -1.
92 ///\return The ID of the node \c v.
94 static int id(Node v) { return v.id; }
97 /// The ID of a valid Edge is a nonnegative integer not greater than
98 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
99 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
101 /// The ID of the \ref INVALID edge is -1.
102 ///\return The ID of the edge \c e.
103 static int id(Edge e) { return e.id; }
105 /// Finds an edge between two nodes.
107 /// Finds an edge from node \c u to node \c v.
109 /// If \c prev is \ref INVALID (this is the default value), then
110 /// It finds the first edge from \c u to \c v. Otherwise it looks for
111 /// the next edge from \c u to \c v after \c prev.
112 /// \return The found edge or INVALID if there is no such an edge.
113 Edge findEdge(Node u,Node v, Edge prev = INVALID)
115 return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
120 friend class FullGraphBase;
124 Node(int _id) { id = _id;}
127 Node (Invalid) { id = -1; }
128 bool operator==(const Node node) const {return id == node.id;}
129 bool operator!=(const Node node) const {return id != node.id;}
130 bool operator<(const Node node) const {return id < node.id;}
136 friend class FullGraphBase;
139 int id; // NodeNum * head + tail;
141 Edge(int _id) : id(_id) {}
143 Edge(const FullGraphBase& _graph, int tail, int head)
144 : id(_graph.NodeNum * head+tail) {}
147 Edge (Invalid) { id = -1; }
148 bool operator==(const Edge edge) const {return id == edge.id;}
149 bool operator!=(const Edge edge) const {return id != edge.id;}
150 bool operator<(const Edge edge) const {return id < edge.id;}
153 void first(Node& node) const {
157 static void next(Node& node) {
161 void first(Edge& edge) const {
165 static void next(Edge& edge) {
169 void firstOut(Edge& edge, const Node& node) const {
170 edge.id = EdgeNum + node.id - NodeNum;
173 void nextOut(Edge& edge) const {
175 if (edge.id < 0) edge.id = -1;
178 void firstIn(Edge& edge, const Node& node) const {
179 edge.id = node.id * NodeNum;
182 void nextIn(Edge& edge) const {
184 if (edge.id % NodeNum == 0) edge.id = -1;
190 typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase;
191 typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase;
192 typedef DefaultMappableGraphExtender<IterableFullGraphBase> MappableFullGraphBase;
194 ///A full graph class.
196 ///This is a simple and fast directed full graph implementation.
197 ///It is completely static, so you can neither add nor delete either
199 ///Thus it conforms to
200 ///the \ref concept::StaticGraph "StaticGraph" concept
201 ///\sa concept::StaticGraph.
203 ///\author Alpar Juttner
204 class FullGraph : public MappableFullGraphBase {
207 FullGraph(int n) { construct(n); }
211 /// Base graph class for UndirFullGraph.
213 class UndirFullGraphBase {
218 typedef UndirFullGraphBase Graph;
225 UndirFullGraphBase() {}
228 ///Creates a full graph with \c n nodes.
229 void construct(int n) { NodeNum = n; EdgeNum = n * (n - 1) / 2; }
231 // FullGraphBase(const FullGraphBase &_g)
232 // : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
234 typedef True NodeNumTag;
235 typedef True EdgeNumTag;
238 int nodeNum() const { return NodeNum; }
240 int edgeNum() const { return EdgeNum; }
246 int maxId(Node = INVALID) const { return NodeNum-1; }
251 int maxId(Edge = INVALID) const { return EdgeNum-1; }
253 Node tail(Edge e) const {
254 /// \todo we may do it faster
255 return ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;
258 Node head(Edge e) const {
259 int tail = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
260 return e.id - (tail) * (tail - 1) / 2;
266 /// The ID of a valid Node is a nonnegative integer not greater than
267 /// \ref maxNodeId(). The range of the ID's is not surely continuous
268 /// and the greatest node ID can be actually less then \ref maxNodeId().
270 /// The ID of the \ref INVALID node is -1.
271 ///\return The ID of the node \c v.
273 static int id(Node v) { return v.id; }
276 /// The ID of a valid Edge is a nonnegative integer not greater than
277 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
278 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
280 /// The ID of the \ref INVALID edge is -1.
281 ///\return The ID of the edge \c e.
282 static int id(Edge e) { return e.id; }
284 /// Finds an edge between two nodes.
286 /// Finds an edge from node \c u to node \c v.
288 /// If \c prev is \ref INVALID (this is the default value), then
289 /// It finds the first edge from \c u to \c v. Otherwise it looks for
290 /// the next edge from \c u to \c v after \c prev.
291 /// \return The found edge or INVALID if there is no such an edge.
292 Edge findEdge(Node u,Node v, Edge prev = INVALID)
294 return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
299 friend class UndirFullGraphBase;
303 Node(int _id) { id = _id;}
306 Node (Invalid) { id = -1; }
307 bool operator==(const Node node) const {return id == node.id;}
308 bool operator!=(const Node node) const {return id != node.id;}
309 bool operator<(const Node node) const {return id < node.id;}
315 friend class UndirFullGraphBase;
318 int id; // NodeNum * head + tail;
320 Edge(int _id) : id(_id) {}
322 Edge(const UndirFullGraphBase& _graph, int tail, int head)
323 : id(_graph.NodeNum * head+tail) {}
326 Edge (Invalid) { id = -1; }
327 bool operator==(const Edge edge) const {return id == edge.id;}
328 bool operator!=(const Edge edge) const {return id != edge.id;}
329 bool operator<(const Edge edge) const {return id < edge.id;}
332 void first(Node& node) const {
336 static void next(Node& node) {
340 void first(Edge& edge) const {
344 static void next(Edge& edge) {
348 void firstOut(Edge& edge, const Node& node) const {
349 edge.id = node.id != 0 ? node.id * (node.id - 1) / 2 : -1;
352 /// \todo with specialized iterators we can make faster iterating
353 void nextOut(Edge& e) const {
354 int tail = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
355 int head = e.id - (tail) * (tail - 1) / 2;
357 e.id = head < tail ? tail * (tail - 1) / 2 + head : -1;
360 void firstIn(Edge& edge, const Node& node) const {
361 edge.id = node.id * (node.id + 1) / 2 - 1;
364 void nextIn(Edge& e) const {
365 int tail = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
366 int head = e.id - (tail) * (tail - 1) / 2; ++head;
368 e.id = tail < NodeNum ? tail * (tail - 1) / 2 + head : -1;
373 /// \todo UndirFullGraph from the UndirFullGraphBase
382 #endif //LEMON_FULL_GRAPH_H