-
Notifications
You must be signed in to change notification settings - Fork 0
/
relation.cpp
371 lines (317 loc) · 9.57 KB
/
relation.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "relation.h"
//---------------------------------------------------------------------------------------------------
Relation::Relation(int d) {
// Construct an empty relation of dimension d
dimension = d;
matrix = new int[dimension * dimension];
memset(matrix, 0, sizeof(int) * dimension * dimension);
}
Relation::Relation(int d, const int* m) {
// Construct a relation from the dimension of its relational tempMatrix and the array of the relational tempMatrix
dimension = d;
matrix = new int[dimension * dimension];
memcpy(matrix, m, sizeof(int) * dimension * dimension);
}
Relation::Relation(const string filePath) {
// Read the tempMatrix of a relation from a file
ifstream readStream(filePath);
if (!readStream) {
cout << "Fail to open." << endl;
exit(1);
}
else {
int row, column;
readStream >> dimension;
matrix = new int[dimension * dimension];
for (row = 0; row < dimension; ++row)
for (column = 0; column < dimension; ++column)
readStream >> matrix[row * dimension + column];
}
readStream.close();
}
Relation::~Relation() {
// Destructor
delete[] matrix;
matrix = nullptr;
}
Relation::Relation(const Relation & r) {
dimension = r.dimension;
matrix = new int[dimension * dimension];
memcpy(matrix, r.matrix, sizeof(int) * dimension * dimension);
}
Relation& Relation::operator =(const Relation & r) {
dimension = r.dimension;
matrix = new int[dimension * dimension];
memcpy(matrix, r.matrix, sizeof(int) * dimension * dimension);
return *this;
}
//---------------------------------------------------------------------------------------------------
int Relation::getDimension() const {
// Get the dimension of relational tempMatrix
return dimension;
}
int Relation::getAtPosition(int row, int column) const {
// Get the value at the cross position in row-th row and column-th column of the relational tempMatrix, and this function returns -1 if it is over the boundary.
if (row >= 0 && row < dimension && column >= 0 && column < dimension)
return matrix[row * dimension + column];
else
return -1;
}
int Relation::operator()(int row, int column) const {
// You can use R(row, column) to get the value at the cross position in row-th row and column-th column of the relational tempMatrix, and this function returns - 1 if it is over the boundary.
if (row >= 0 && row < dimension && column >= 0 && column < dimension)
return matrix[row * dimension + column];
else
return -1;
}
int* Relation::getMatrix() const {
// Get the relational tempMatrix
return matrix;
}
//---------------------------------------------------------------------------------------------------
void Relation::output() const {
// Display the tempMatrix of the relation on the screen
int row, column;
cout << "The dimension is: " << dimension << endl;
for (row = 0; row < dimension; ++row)
{
for (column = 0; column < dimension; ++column)
{
cout << (*this)(row, column) << " ";
}
cout << endl;
}
}
bool Relation::outputToFile(const string filePath) const {
// Write the relation tempMatrix to a file in the same format as the read file
ofstream writeStream(filePath);
if (!writeStream) {
cout << "Fail to open." << endl;
return false;
}
else {
int row, column;
writeStream << dimension << endl;
for (row = 0; row < dimension; ++row) {
for (column = 0; column < dimension; ++column)
writeStream << matrix[row * dimension + column] << " ";
writeStream << endl;
}
}
writeStream.flush();
writeStream.close();
return true;
}
//---------------------------------------------------------------------------------------------------
bool Relation::isReflexive() const {
int row;
// Traverse all elements on the main diagonal
for (row = 0; row < getDimension(); ++row) {
if (getAtPosition(row, row) == 0)
return false;
}
return true;
}
bool Relation::isIrreflexive() const {
int row;
// Traverse all elements on the main diagonal
for (row = 0; row < getDimension(); ++row) {
if (getAtPosition(row, row) == 1)
return false;
}
return true;
}
bool Relation::isSymmetric() const {
int row, column;
// Traverse all elements in the martrix
for (row = 0; row < getDimension(); ++row) {
for (column = 0; column < getDimension(); ++column) {
if (getAtPosition(row, column) != getAtPosition(column, row))
return false;
}
}
return true;
}
bool Relation::isAsymmetric() const {
int row, column;
// Traverse all elements in the martrix
for (row = 0; row < getDimension(); ++row) {
for (column = 0; column < getDimension(); ++column) {
if (getAtPosition(row, column) == 1 && getAtPosition(column, row) == 1)
return false;
}
}
return true;
}
bool Relation::isAntisymmetric() const {
int row, column;
// Traverse all elements in the martrix
for (row = 0; row < getDimension(); ++row) {
for (column = 0; column < getDimension(); ++column) {
if (getAtPosition(row, column) == 1 && getAtPosition(column, row) == 1 && row != column)
return false;
}
}
return true;
}
bool Relation::isTransitive() const {
// Traverse all elements in the martrix, and check whether the third element is in relation R
for (int i = 0; i < getDimension(); ++i) {
for (int j = 0; j < getDimension(); ++j) {
if (getAtPosition(i, j) == 1) {
for (int k = 0; k < getDimension(); ++k) {
if (getAtPosition(j, k) == 1 && getAtPosition(i, k) != 1)
return false;
}
}
}
}
return true;
}
bool Relation::isEquivalence() const {
return isReflexive() && isSymmetric() && isTransitive();
}
bool Relation::isPartial() const {
return isReflexive() && isAntisymmetric() && isTransitive();
}
//---------------------------------------------------------------------------------------------------
Relation Relation::getReflexiveClosure() const {
int* m = new int[dimension * dimension];
m = getMatrix();
int row;
// Traverse all elements on the main diagonal
for (row = 0; row < dimension; ++row) {
if (m[row * dimension + row] == 0)
m[row * dimension + row] = 1;
}
Relation r(dimension, m);
return r;
}
Relation Relation::getSymmetricClosure() const {
int* m = new int[dimension * dimension];
m = getMatrix();
int* transpose = new int[dimension * dimension];
int row, column;
// Get the transposed tempMatrix
for (row = 0; row < dimension; ++row) {
for (column = 0; column < dimension; ++column) {
transpose[row * dimension + column] = m[column * dimension + row];
}
}
// Traverse all elements in the martrix
for (row = 0; row < dimension; ++row) {
for (column = 0; column < dimension; ++column) {
if (m[row * dimension + column] == 1 || transpose[row * dimension + column] == 1) {
m[row * dimension + column] = 1;
}
}
}
Relation r(dimension, m);
return r;
}
Relation Relation::getTransitiveClosure() const {
int* m = new int[dimension * dimension];
m = getMatrix();
for (int times = 0; times < dimension; ++times) {
for (int row = 0; row < dimension; ++row) {
for (int column = 0; column < dimension; ++column) {
if (row != times || column != times) {
if (m[times * dimension + column] == 1 && m[row * dimension + times] == 1)
m[row * dimension + column] = 1;
}
}
}
}
Relation r(dimension, m);
return r;
}
Relation Relation::getEquivalenceClosure() const {
return this->getReflexiveClosure().getSymmetricClosure().getTransitiveClosure();
}
//---------------------------------------------------------------------------------------------------
int* Relation::getEquivalenceClasses() const {
int row, column, number = 0;
int* result = (int*)malloc(sizeof(int) * dimension);
//if (isEquivalence()) {
for (row = 0; row < dimension; ++row) {
result[row] = number;
++number;
}
for (row = 0; row < dimension; ++row) {
for (column = 0; column < dimension; ++column) {
if (getAtPosition(row, column) == 1) {
result[column] = result[row];
}
}
}
return result;
//}
//return 0;
}
Relation Relation::hasse() const {
int* result = getMatrix();
for (int i = 0; i < dimension; ++i)
result[i * dimension + i] = 0;
for (int i = 0; i < dimension; ++i)
for (int j = 0; j < dimension; ++j)
for (int k = 0; k < dimension; ++k)
if (1 == result[i * dimension + j] && 1 == result[j * dimension + k])
result[i * dimension + k] = 0;
Relation r(dimension, result);
return r;
}
int* Relation::topoSorting() const {
bool zeroIndegree;
int* tempMatrix = new int[dimension * dimension];
int* result = new int[dimension];
bool* isAdded = new bool[dimension];
queue<int> queue;
int count = 0;
for (int i = 0; i < dimension; ++i)
isAdded[i] = false;
for (int row = 0; row < dimension; ++row)
for (int column = 0; column < dimension; ++column)
tempMatrix[row * dimension + column] = (*this)(row, column);
while (count < dimension) {
for (int column = 0; column < dimension; ++column) {
zeroIndegree = true;
for (int row = 0; row < dimension; ++row) {
if (1 == tempMatrix[row * dimension + column]) {
zeroIndegree = false;
break;
}
}
if (zeroIndegree) {
// If the indegree is 0, then push column-th node into queue
if (false == isAdded[column])
queue.push(column);
isAdded[column] = true;
}
}
if (!queue.empty()) {
int temp = queue.front();
result[count] = temp;
queue.pop();
// Delete all edges starting from No.temp node
for (int i = 0; i < dimension; ++i)
tempMatrix[temp * dimension + i] = 0;
}
++count;
}
for (int i = 0; i < dimension; ++i) {
if (false == isAdded[i]) {
delete[] isAdded;
isAdded = nullptr;
delete[] tempMatrix;
tempMatrix = nullptr;
delete[] result;
result = nullptr;
return nullptr;
}
}
delete[] isAdded;
isAdded = nullptr;
delete[] tempMatrix;
tempMatrix = nullptr;
return result;
}