1 | /* -*- C++ -*- |
---|
2 | * src/lemon/full_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_FULL_GRAPH_H |
---|
18 | #define LEMON_FULL_GRAPH_H |
---|
19 | |
---|
20 | |
---|
21 | #include <lemon/idmappable_graph_extender.h> |
---|
22 | #include <lemon/iterable_graph_extender.h> |
---|
23 | #include <lemon/alteration_observer_registry.h> |
---|
24 | #include <lemon/default_map.h> |
---|
25 | |
---|
26 | #include <lemon/invalid.h> |
---|
27 | #include <lemon/utility.h> |
---|
28 | |
---|
29 | |
---|
30 | ///\ingroup graphs |
---|
31 | ///\file |
---|
32 | ///\brief FullGraph and SymFullGraph classes. |
---|
33 | |
---|
34 | |
---|
35 | namespace lemon { |
---|
36 | |
---|
37 | /// \addtogroup graphs |
---|
38 | /// @{ |
---|
39 | |
---|
40 | class FullGraphBase { |
---|
41 | int NodeNum; |
---|
42 | int EdgeNum; |
---|
43 | public: |
---|
44 | |
---|
45 | typedef FullGraphBase Graph; |
---|
46 | |
---|
47 | class Node; |
---|
48 | class Edge; |
---|
49 | |
---|
50 | public: |
---|
51 | |
---|
52 | FullGraphBase() {} |
---|
53 | |
---|
54 | |
---|
55 | ///Creates a full graph with \c n nodes. |
---|
56 | void construct(int n) { NodeNum = n; EdgeNum = n * n; } |
---|
57 | /// |
---|
58 | // FullGraphBase(const FullGraphBase &_g) |
---|
59 | // : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { } |
---|
60 | |
---|
61 | typedef True NodeNumTag; |
---|
62 | typedef True EdgeNumTag; |
---|
63 | |
---|
64 | ///Number of nodes. |
---|
65 | int nodeNum() const { return NodeNum; } |
---|
66 | ///Number of edges. |
---|
67 | int edgeNum() const { return EdgeNum; } |
---|
68 | |
---|
69 | /// Maximum node ID. |
---|
70 | |
---|
71 | /// Maximum node ID. |
---|
72 | ///\sa id(Node) |
---|
73 | int maxNodeId() const { return NodeNum-1; } |
---|
74 | /// Maximum edge ID. |
---|
75 | |
---|
76 | /// Maximum edge ID. |
---|
77 | ///\sa id(Edge) |
---|
78 | int maxEdgeId() const { return EdgeNum-1; } |
---|
79 | |
---|
80 | Node tail(Edge e) const { return e.id % NodeNum; } |
---|
81 | Node head(Edge e) const { return e.id / NodeNum; } |
---|
82 | |
---|
83 | |
---|
84 | /// Node ID. |
---|
85 | |
---|
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(). |
---|
89 | /// |
---|
90 | /// The ID of the \ref INVALID node is -1. |
---|
91 | ///\return The ID of the node \c v. |
---|
92 | |
---|
93 | static int id(Node v) { return v.id; } |
---|
94 | /// Edge ID. |
---|
95 | |
---|
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(). |
---|
99 | /// |
---|
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; } |
---|
103 | |
---|
104 | /// Finds an edge between two nodes. |
---|
105 | |
---|
106 | /// Finds an edge from node \c u to node \c v. |
---|
107 | /// |
---|
108 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
109 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
110 | /// the next edge from \c u to \c v after \c prev. |
---|
111 | /// \return The found edge or INVALID if there is no such an edge. |
---|
112 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
113 | { |
---|
114 | return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | class Node { |
---|
119 | friend class FullGraphBase; |
---|
120 | |
---|
121 | protected: |
---|
122 | int id; |
---|
123 | Node(int _id) { id = _id;} |
---|
124 | public: |
---|
125 | Node() {} |
---|
126 | Node (Invalid) { id = -1; } |
---|
127 | bool operator==(const Node node) const {return id == node.id;} |
---|
128 | bool operator!=(const Node node) const {return id != node.id;} |
---|
129 | bool operator<(const Node node) const {return id < node.id;} |
---|
130 | }; |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | class Edge { |
---|
135 | friend class FullGraphBase; |
---|
136 | |
---|
137 | protected: |
---|
138 | int id; // NodeNum * head + tail; |
---|
139 | |
---|
140 | Edge(int _id) : id(_id) {} |
---|
141 | |
---|
142 | Edge(const FullGraphBase& _graph, int tail, int head) |
---|
143 | : id(_graph.NodeNum * head+tail) {} |
---|
144 | public: |
---|
145 | Edge() { } |
---|
146 | Edge (Invalid) { id = -1; } |
---|
147 | bool operator==(const Edge edge) const {return id == edge.id;} |
---|
148 | bool operator!=(const Edge edge) const {return id != edge.id;} |
---|
149 | bool operator<(const Edge edge) const {return id < edge.id;} |
---|
150 | }; |
---|
151 | |
---|
152 | void first(Node& node) const { |
---|
153 | node.id = NodeNum-1; |
---|
154 | } |
---|
155 | |
---|
156 | static void next(Node& node) { |
---|
157 | --node.id; |
---|
158 | } |
---|
159 | |
---|
160 | void first(Edge& edge) const { |
---|
161 | edge.id = EdgeNum-1; |
---|
162 | } |
---|
163 | |
---|
164 | static void next(Edge& edge) { |
---|
165 | --edge.id; |
---|
166 | } |
---|
167 | |
---|
168 | void firstOut(Edge& edge, const Node& node) const { |
---|
169 | edge.id = EdgeNum + node.id - NodeNum; |
---|
170 | } |
---|
171 | |
---|
172 | void nextOut(Edge& edge) const { |
---|
173 | edge.id -= NodeNum; |
---|
174 | if (edge.id < 0) edge.id = -1; |
---|
175 | } |
---|
176 | |
---|
177 | void firstIn(Edge& edge, const Node& node) const { |
---|
178 | edge.id = node.id * NodeNum; |
---|
179 | } |
---|
180 | |
---|
181 | void nextIn(Edge& edge) const { |
---|
182 | ++edge.id; |
---|
183 | if (edge.id % NodeNum == 0) edge.id = -1; |
---|
184 | } |
---|
185 | |
---|
186 | }; |
---|
187 | |
---|
188 | |
---|
189 | typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase; |
---|
190 | typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase; |
---|
191 | typedef IdMappableGraphExtender<IterableFullGraphBase> IdMappableFullGraphBase; |
---|
192 | typedef DefaultMappableGraphExtender<IdMappableFullGraphBase> MappableFullGraphBase; |
---|
193 | |
---|
194 | ///A full graph class. |
---|
195 | |
---|
196 | ///This is a simple and fast directed full graph implementation. |
---|
197 | ///It is completely static, so you can neither add nor delete either |
---|
198 | ///edges or nodes. |
---|
199 | ///Thus it conforms to |
---|
200 | ///the \ref concept::StaticGraph "StaticGraph" concept |
---|
201 | ///\sa concept::StaticGraph. |
---|
202 | ///\todo What about loops? |
---|
203 | ///\todo Don't we need SymEdgeMap? |
---|
204 | /// |
---|
205 | ///\author Alpar Juttner |
---|
206 | class FullGraph : public MappableFullGraphBase { |
---|
207 | public: |
---|
208 | |
---|
209 | FullGraph(int n) { construct(n); } |
---|
210 | }; |
---|
211 | |
---|
212 | /// @} |
---|
213 | |
---|
214 | } //namespace lemon |
---|
215 | |
---|
216 | |
---|
217 | #endif //LEMON_FULL_GRAPH_H |
---|