A sample file.
2 * src/lemon/full_graph.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 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/bits/iterable_graph_extender.h>
24 #include <lemon/bits/alteration_notifier.h>
25 #include <lemon/bits/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 source(Edge e) const { return e.id % NodeNum; }
82 Node target(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 static Node fromId(int id, Node) { return Node(id);}
107 static Edge fromId(int id, Edge) { return Edge(id);}
109 /// Finds an edge between two nodes.
111 /// Finds an edge from node \c u to node \c v.
113 /// If \c prev is \ref INVALID (this is the default value), then
114 /// It finds the first edge from \c u to \c v. Otherwise it looks for
115 /// the next edge from \c u to \c v after \c prev.
116 /// \return The found edge or INVALID if there is no such an edge.
117 Edge findEdge(Node u,Node v, Edge prev = INVALID)
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 {
161 static void next(Node& node) {
165 void first(Edge& edge) const {
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> AlterableFullGraphBase;
195 typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase;
196 typedef DefaultMappableGraphExtender<IterableFullGraphBase> MappableFullGraphBase;
198 ///A full graph class.
200 ///This is a simple and fast directed full graph implementation.
201 ///It is completely static, so you can neither add nor delete either
203 ///Thus it conforms to
204 ///the \ref concept::StaticGraph "StaticGraph" concept
205 ///\sa concept::StaticGraph.
207 ///\author Alpar Juttner
208 class FullGraph : public MappableFullGraphBase {
211 FullGraph(int n) { construct(n); }
215 // Base graph class for UndirFullGraph.
216 class UndirFullGraphBase {
221 typedef UndirFullGraphBase Graph;
228 UndirFullGraphBase() {}
231 ///Creates a full graph with \c n nodes.
232 void construct(int n) { NodeNum = n; EdgeNum = n * (n - 1) / 2; }
234 // FullGraphBase(const FullGraphBase &_g)
235 // : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
237 typedef True NodeNumTag;
238 typedef True EdgeNumTag;
241 int nodeNum() const { return NodeNum; }
243 int edgeNum() const { return EdgeNum; }
249 int maxId(Node = INVALID) const { return NodeNum-1; }
254 int maxId(Edge = INVALID) const { return EdgeNum-1; }
256 Node source(Edge e) const {
257 /// \todo we may do it faster
258 return ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;
261 Node target(Edge e) const {
262 int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
263 return e.id - (source) * (source - 1) / 2;
269 /// The ID of a valid Node is a nonnegative integer not greater than
270 /// \ref maxNodeId(). The range of the ID's is not surely continuous
271 /// and the greatest node ID can be actually less then \ref maxNodeId().
273 /// The ID of the \ref INVALID node is -1.
274 ///\return The ID of the node \c v.
276 static int id(Node v) { return v.id; }
279 /// The ID of a valid Edge is a nonnegative integer not greater than
280 /// \ref maxEdgeId(). The range of the ID's is not surely continuous
281 /// and the greatest edge ID can be actually less then \ref maxEdgeId().
283 /// The ID of the \ref INVALID edge is -1.
284 ///\return The ID of the edge \c e.
285 static int id(Edge e) { return e.id; }
287 /// Finds an edge between two nodes.
289 /// Finds an edge from node \c u to node \c v.
291 /// If \c prev is \ref INVALID (this is the default value), then
292 /// It finds the first edge from \c u to \c v. Otherwise it looks for
293 /// the next edge from \c u to \c v after \c prev.
294 /// \return The found edge or INVALID if there is no such an edge.
295 Edge findEdge(Node u,Node v, Edge prev = INVALID)
297 return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
302 friend class UndirFullGraphBase;
306 Node(int _id) { id = _id;}
309 Node (Invalid) { id = -1; }
310 bool operator==(const Node node) const {return id == node.id;}
311 bool operator!=(const Node node) const {return id != node.id;}
312 bool operator<(const Node node) const {return id < node.id;}
318 friend class UndirFullGraphBase;
321 int id; // NodeNum * target + source;
323 Edge(int _id) : id(_id) {}
325 Edge(const UndirFullGraphBase& _graph, int source, int target)
326 : id(_graph.NodeNum * target+source) {}
329 Edge (Invalid) { id = -1; }
330 bool operator==(const Edge edge) const {return id == edge.id;}
331 bool operator!=(const Edge edge) const {return id != edge.id;}
332 bool operator<(const Edge edge) const {return id < edge.id;}
335 void first(Node& node) const {
339 static void next(Node& node) {
343 void first(Edge& edge) const {
347 static void next(Edge& edge) {
351 void firstOut(Edge& edge, const Node& node) const {
352 edge.id = node.id != 0 ? node.id * (node.id - 1) / 2 : -1;
355 /// \todo with specialized iterators we can make faster iterating
356 void nextOut(Edge& e) const {
357 int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
358 int target = e.id - (source) * (source - 1) / 2;
360 e.id = target < source ? source * (source - 1) / 2 + target : -1;
363 void firstIn(Edge& edge, const Node& node) const {
364 edge.id = node.id * (node.id + 1) / 2 - 1;
367 void nextIn(Edge& e) const {
368 int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
369 int target = e.id - (source) * (source - 1) / 2; ++target;
371 e.id = source < NodeNum ? source * (source - 1) / 2 + target : -1;
376 /// \todo UndirFullGraph from the UndirFullGraphBase
385 #endif //LEMON_FULL_GRAPH_H