-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
318 lines (264 loc) · 6.48 KB
/
Graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Graph.cpp
*
* Created on: Sep 11, 2011
* Author: correa
*/
#include "Graph.h"
#define GSTERR_NEGATIVE_SURPLUS 1 //codigo de erro de tamanho do vetor (acho)
template <class T> class Complement;
class Homomorphism : public Graph {
friend class Graph;
friend class Complement<Homomorphism>;
friend class Complement<Graph>;
int r;
int * R;
long long re;
int lastnechanges;
bool alloc;
Homomorphism(Homomorphism * bg);
Homomorphism(const int ar, const int * aR, Graph * bg);
Homomorphism(const int ar, int * aR, Homomorphism * const bg);
int nverts();
virtual long long int nedges();
virtual bool addEdge(const int i, const int j);
virtual bool delEdge(const int i, const int j);
void addAllEdges();
void delAllEdges();
virtual bool hasEdge(const int i, const int j);
int vertex(const int i);
virtual Graph * clone();
virtual Graph * homomorphism(const int ar, const int * aR);
virtual Graph * complement();
public:
virtual ~Homomorphism();
};
template <class T>
class Complement : public T {
friend class Graph;
friend class Homomorphism;
T * baseg;
long long cne;
int lastnechanges;
Graph * homom;
Complement() {};
Complement(T * bg);
long long int nedges();
bool hasEdge(const int i, const int j);
bool addEdge(const int i, const int j);
bool delEdge(const int i, const int j);
Graph * clone();
Graph * complement();
Graph * homomorphism(const int ar, const int * aR);
public:
~Complement();
};
// AdjMatrix
int AdjMatrix::adjToArray(const int i, const int asz, int * const ng, int * const surplus_p) {
int ia = 0, j;
*surplus_p = asz;
if (hasEdge(i, i)) {
(*surplus_p)--;
if (*surplus_p >= 0)
ng[ia++] = i;
}
for (j = 0; j < nverts(); j++)
if (i != j && hasEdge(i, j)) {
(*surplus_p)--;
if (*surplus_p >= 0)
ng[ia++] = j;
}
return (*surplus_p < 0) ? GSTERR_NEGATIVE_SURPLUS : 0;
}
int AdjMatrix::adjToArrays(const int asz, int * const ad, int * const ang, int * const surplus_p) {
int ia = 0, aa = 0, j;
int i;
*surplus_p = asz;
for (i = 0; i < nverts(); i++) {
ad[i] = 0;
if (hasEdge(i, i)) {
(*surplus_p)--;
if (*surplus_p >= 0)
ang[aa++] = i;
ad[i]++;
}
for (j = 0; j < nverts(); j++)
if (i != j && hasEdge(i, j)) {
(*surplus_p)--;
if (*surplus_p >= 0)
ang[aa++] = j;
ad[i]++;
}
}
return (*surplus_p < 0) ? GSTERR_NEGATIVE_SURPLUS : 0;
}
int AdjMatrix::adjSize(const int i, int * const deg) {
int j;
(*deg) = 0;
for (j = 0; j < nverts(); j++)
if (hasEdge(i, j))
(*deg)++;
return 0;
}
int AdjMatrix::adjSizes(int * const deg) {
memset(deg, 0, nverts()*sizeof(int));
int iu, iv;
for (iu = 0; iu < nverts(); iu++) {
for (iv = iu+1; iv < nverts(); iv++)
if (hasEdge(iu, iv)) {
deg[iu]++;
deg[iv]++;
}
}
return 0;
}
// Graph
Graph::Graph(int n) : AdjMatrix(n) {
AdjMatrix::nechanges = 0;
}
Graph::Graph(Graph * bg) {
linkTo(bg);
}
Graph * Graph::clone() {
Graph * dest = new Graph(nverts());
dest->copyTo(this);
return dest;
}
Graph * Graph::homomorphism(const int r, const int * R) {
return new Homomorphism(r, R, this);
}
Graph * Graph::complement() {
return new Complement<Graph>(this);
}
// Homomorphism
inline Homomorphism::Homomorphism(const int ar, int * aR, Homomorphism * const bg) : Graph(bg), R(aR) {
r = ar;
re = -1;
alloc = true;
}
inline Homomorphism::Homomorphism(const int ar, const int * aR, Graph * const bg) : Graph(bg) {
r = ar;
R = new int[r];
memcpy(R, aR, r*sizeof(int));
re = -1;
alloc = true;
}
inline Homomorphism::Homomorphism(Homomorphism * const bg) : Graph(bg), R(bg->R) {
re = bg->re;
r = bg->r;
alloc = false;
}
inline Homomorphism::~Homomorphism() {
if (alloc) {
delete R;
}
}
inline int Homomorphism::nverts() {
return r;
}
inline long long int Homomorphism::nedges() {
if (re < 0 || lastnechanges != AdjMatrix::nechanges) {
int i, j;
re = 0;
for (i = 0; i < nverts(); i++)
for (j = 0; j < nverts(); j++)
if (hasEdge(i, j))
re++;
lastnechanges = AdjMatrix::nechanges;
}
return re;
}
inline bool Homomorphism::addEdge(const int i, const int j) {
bool ret = Graph::addEdge(R[i], R[j]);
if (re >= 0 && ret && lastnechanges == AdjMatrix::nechanges - 1) {
re++;
lastnechanges = AdjMatrix::nechanges;
}
return ret;
}
inline bool Homomorphism::delEdge(const int i, const int j) {
bool ret = Graph::delEdge(R[i], R[j]);
if (re >= 0 && ret && lastnechanges == AdjMatrix::nechanges - 1) {
re--;
lastnechanges = AdjMatrix::nechanges;
}
return ret;
}
inline void Homomorphism::addAllEdges() {
int i, j;
for (i = 0; i < nverts(); i++)
for (j = 0; j < nverts(); j++)
addEdge(i, j);
}
inline void Homomorphism::delAllEdges() {
int i, j;
for (i = 0; i < nverts(); i++)
for (j = 0; j < nverts(); j++)
delEdge(i, j);
}
inline bool Homomorphism::hasEdge(const int i, const int j) {
return Graph::hasEdge(R[i], R[j]);
}
inline int Homomorphism::vertex(const int i) {
return Graph::vertex(R[i]);
}
inline Graph * Homomorphism::clone() {
int * RR = new int[nverts()];
memcpy(RR, R, nverts()*sizeof(int));
Homomorphism * ret = (Homomorphism *) Graph::clone()->homomorphism(r, RR);
ret->re = re;
ret->lastnechanges = lastnechanges;
return ret;
}
inline Graph * Homomorphism::homomorphism(const int ar, const int * aR) {
int i;
int * Raux = new int[ar];
for (i = 0; i < ar; i++)
Raux[i] = R[aR[i]];
return new Homomorphism(ar, Raux, this);
}
inline Graph * Homomorphism::complement() {
return new Complement<Homomorphism>(this);
}
// Complement
template <class T>
inline Complement<T>::Complement(T * bg) : T(bg), homom(NULL) {
baseg = bg;
}
template <class T>
inline Complement<T>::~Complement() {
if (homom != NULL)
delete homom;
}
template <class T>
inline long long int Complement<T>::nedges() {
int nv = T::nverts();
cne = nv*nv-T::nedges();
return cne;
}
template <class T>
inline bool Complement<T>::hasEdge(const int i, const int j) {
return !T::hasEdge(i, j);
}
template <class T>
inline bool Complement<T>::addEdge(const int i, const int j) {
return T::delEdge(i, j);
}
template <class T>
inline bool Complement<T>::delEdge(const int i, const int j) {
return T::addEdge(i, j);
}
template <class T>
inline Graph * Complement<T>::clone() {
return T::clone()->complement();
}
template <class T>
inline Graph * Complement<T>::complement() {
return baseg;
}
template <class T>
inline Graph * Complement<T>::homomorphism(const int ar, const int * aR) {
if (homom == NULL)
homom = T::homomorphism(ar, aR);
return homom->complement();
}