-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths21_graph.cpp
372 lines (346 loc) · 10.7 KB
/
s21_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
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
372
#include "s21_graph.hpp"
#include <cstring>
#include <fstream>
#include <set>
#include <vector>
Graph::Graph() : adjacencyMatrix_(nullptr) {}
Graph::Graph(const Graph &other) : adjacencyMatrix_(nullptr) { *this = other; }
Graph::Graph(Graph &&other) : adjacencyMatrix_(nullptr) {
*this = std::move(other);
}
Graph::~Graph() { delete adjacencyMatrix_; }
void Graph::operator=(const Graph &other) {
if (this != &other) {
if (IsGraphInizialize()) {
delete adjacencyMatrix_;
adjacencyMatrix_ = nullptr;
}
if (other.IsGraphInizialize()) {
int mx_size = other.adjacencyMatrix_->get_rows();
adjacencyMatrix_ = new Matrix<double>(mx_size, mx_size);
adjacencyMatrix_ = other.adjacencyMatrix_;
}
graphType_ = other.graphType_;
graphWeights_ = other.graphWeights_;
positiveWeights_ = other.positiveWeights_;
}
}
void Graph::operator=(Graph &&other) {
if (this == &other) {
throw std::out_of_range("Can't move yourself");
}
if (IsGraphInizialize()) {
delete adjacencyMatrix_;
adjacencyMatrix_ = nullptr;
}
std::swap(adjacencyMatrix_, other.adjacencyMatrix_);
std::swap(graphType_, other.graphType_);
std::swap(graphWeights_, other.graphWeights_);
std::swap(positiveWeights_, other.positiveWeights_);
}
int Graph::MinVertex() const {
if (!IsGraphInizialize()) {
throw std::out_of_range("Graph not inizialize.");
}
return 1;
}
int Graph::MaxVertex() const {
if (!IsGraphInizialize()) {
throw std::out_of_range("Graph not inizialize.");
}
return adjacencyMatrix_->get_rows();
}
bool Graph::IsGraphInizialize() const {
return adjacencyMatrix_ != nullptr ? true : false;
}
const bool &Graph::GraphType() const {
if (!IsGraphInizialize()) {
throw std::out_of_range("Graph not inizialize");
}
return graphType_;
}
const bool &Graph::GraphWeights() const {
if (!IsGraphInizialize()) {
throw std::out_of_range("Graph not inizialize.");
}
return graphWeights_;
}
const bool &Graph::PositiveWeights() const {
if (!IsGraphInizialize()) {
throw std::out_of_range("Graph not inizialize.");
}
return positiveWeights_;
}
const Matrix<double> &Graph::graphMatrix() const {
if (!IsGraphInizialize()) {
throw std::out_of_range("Graph not inizialize.");
}
return *adjacencyMatrix_;
}
void Graph::LoadGraphFromFile(const std::string &filename) {
std::ifstream stream(filename);
if (stream.is_open()) {
std::string line{};
std::getline(stream, line);
InstallSizeAdjacencyMatrix_(line);
int indexRow = 0;
while (!stream.eof()) {
std::getline(stream, line);
if (line.empty()) {
continue;
}
InstallRowAdjacencyMatrix_(line, indexRow);
++indexRow;
}
if (indexRow != adjacencyMatrix_->get_rows()) {
throw std::invalid_argument(
"Incorrect load File. Try choose another file.");
}
InstallTypeGraph_();
stream.close();
} else {
throw std::out_of_range("No such file.");
}
}
void Graph::ExportGraphToDot(const std::string &filename) {
if (!IsGraphInizialize()) {
throw std::out_of_range(
"Graph not inizialize. Inizialize graph before export!");
}
if (graphType_ == GraphType::ORIENTED) {
SaveDigraph(filename);
} else {
SaveGraph(filename);
}
}
void Graph::ExportGraphToTxt(const std::string &filename) {
if (!IsGraphInizialize()) {
throw std::out_of_range(
"Graph not inizialize. Inizialize graph before export!");
}
std::ofstream stream(filename);
if (stream.is_open()) {
stream << adjacencyMatrix_->get_rows() << std::endl;
for (int i = 0; i < adjacencyMatrix_->get_rows(); ++i) {
for (int j = 0; j < adjacencyMatrix_->get_columns(); ++j) {
stream << (*adjacencyMatrix_)(i, j) << " ";
}
stream << std::endl;
}
stream.close();
} else {
throw std::out_of_range("File not open.");
}
}
void Graph::GenerateOrientedGraph(const int &size) {
if (size <= 1) {
throw std::out_of_range("Graph size must be '>1'.");
}
graphType_ = GraphType::ORIENTED;
graphWeights_ = GraphWeights::WEIGHTED;
positiveWeights_ = true;
//
delete adjacencyMatrix_;
adjacencyMatrix_ = new Matrix<double>(size, size);
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if (i != j) {
bool addEdge = rand_r(&random_seed_) % 2;
if (addEdge) {
(*adjacencyMatrix_)(i, j) = GetRandomInt();
}
}
}
}
}
void Graph::GenerateUnorientedGraph(const int &size, const bool &weighted) {
if (size <= 1) {
throw std::out_of_range("Graph size must be '>1'.");
}
graphType_ = GraphType::UNORIENTED;
if (weighted == true) {
graphWeights_ = GraphWeights::WEIGHTED;
} else {
graphWeights_ = GraphWeights::UNWEIGHTED;
}
positiveWeights_ = true;
//
delete adjacencyMatrix_;
adjacencyMatrix_ = new Matrix<double>(size, size);
std::set<std::pair<int, int>> vertex;
for (int i = 0; i < size; ++i) {
int sumEdgesVertex = 0;
for (int j = 0; j < size; ++j) {
if (vertex.find({j, i}) == vertex.end() && (i != j)) {
bool addEdge = rand_r(&random_seed_) % 2;
if (addEdge == true || ((j == size - 1) && sumEdgesVertex == 0)) {
++sumEdgesVertex;
int valueEdge;
if (weighted == true) {
valueEdge = GetRandomInt();
} else {
valueEdge = 1.0;
}
(*adjacencyMatrix_)(i, j) = valueEdge;
(*adjacencyMatrix_)(j, i) = valueEdge;
vertex.insert({j, i});
}
}
}
}
}
void Graph::InstallAdjacencyMatrix_(const int &size) {
if (IsGraphInizialize()) {
delete adjacencyMatrix_;
}
adjacencyMatrix_ = new Matrix<double>(size, size);
}
void Graph::InstallSizeAdjacencyMatrix_(const std::string &strValue) {
bool correct = true;
for (size_t i = 0; i < strValue.size(); ++i) {
if (strValue[i] < '1' || strValue[i] > '9') {
correct = false;
}
}
if (correct == false) {
throw std::invalid_argument(
"Incorrect load File. Try choose another file.");
}
InstallAdjacencyMatrix_(std::stoi(strValue));
}
void Graph::InstallRowAdjacencyMatrix_(const std::string &strValue,
const int &indexRow) {
if (indexRow >= adjacencyMatrix_->get_rows()) {
throw std::invalid_argument(
"Incorrect load File. Try choose another file.");
}
std::string value{};
int indexColumn = 0;
for (size_t i = 0; i < strValue.size(); ++i) {
if (indexColumn >= adjacencyMatrix_->get_columns()) {
throw std::invalid_argument(
"Incorrect load File. Try choose another file.");
}
if (strValue[i] == ' ' && !value.empty()) {
(*adjacencyMatrix_)(indexRow, indexColumn) = std::stod(value);
value.clear();
++indexColumn;
} else if (i == strValue.size() - 1) {
value.push_back(strValue[i]);
(*adjacencyMatrix_)(indexRow, indexColumn) = std::stod(value);
++indexColumn;
} else {
if (strValue[i] != ' ') {
value.push_back(strValue[i]);
}
}
}
if (indexColumn != adjacencyMatrix_->get_columns()) {
throw std::invalid_argument(
"Incorrect load File. Try choose another file.");
}
}
void Graph::InstallTypeGraph_() {
/*---default---*/
graphType_ = GraphType::UNORIENTED;
graphWeights_ = GraphWeights::UNWEIGHTED;
positiveWeights_ = true;
/*-------------*/
for (int i = 0; i < adjacencyMatrix_->get_rows(); ++i) {
for (int j = 0; j < adjacencyMatrix_->get_columns(); ++j) {
if ((*adjacencyMatrix_)(i, j) != 0) {
if ((*adjacencyMatrix_)(i, j) > 1) {
graphWeights_ = GraphWeights::WEIGHTED;
} else if ((*adjacencyMatrix_)(i, j) < 0) {
graphWeights_ = GraphWeights::WEIGHTED;
positiveWeights_ = false;
}
if ((*adjacencyMatrix_)(j, i) != (*adjacencyMatrix_)(i, j)) {
graphType_ = GraphType::ORIENTED;
}
}
}
}
}
void Graph::SaveDigraph(const std::string &filename) {
std::ofstream stream(filename);
if (stream.is_open()) {
stream << "digraph {" << std::endl;
std::string connection = " -> ";
for (int i = 0; i < adjacencyMatrix_->get_rows(); ++i) {
for (int j = 0; j < adjacencyMatrix_->get_columns(); ++j) {
if ((*adjacencyMatrix_)(i, j) != 0) {
if (graphWeights_ == GraphWeights::WEIGHTED) {
double valueWeight = (*adjacencyMatrix_)(i, j);
std::string weight{};
if (valueWeight - (int)valueWeight == 0) {
weight = " [label = " + std::to_string((int)valueWeight) + "];";
} else {
weight = " [label = " + std::to_string(valueWeight) + "];";
}
stream << " " << i + 1 << connection << j + 1 << weight
<< std::endl;
} else {
stream << " " << i + 1 << connection << j + 1 << ";"
<< std::endl;
}
}
}
}
stream << "}" << std::endl;
stream.close();
} else {
throw std::out_of_range("File not open.");
}
}
void Graph::SaveGraph(const std::string &filename) {
std::ofstream stream(filename);
if (stream.is_open()) {
stream << "graph {" << std::endl;
std::string connection = " -- ";
std::vector<std::pair<int, int>> sameNode;
bool skip = false;
for (int i = 0; i < adjacencyMatrix_->get_rows(); ++i) {
for (int j = 0; j < adjacencyMatrix_->get_columns(); ++j) {
if ((*adjacencyMatrix_)(i, j) != 0) {
//
for (size_t k = 0; k < sameNode.size(); ++k) {
std::pair<int, int> node = sameNode[k];
if (node.first == i && node.second == j) {
skip = true;
break;
}
}
//
if (skip == false) {
if (graphWeights_ == GraphWeights::WEIGHTED) {
double valueWeight = (*adjacencyMatrix_)(i, j);
std::string weight{};
if (valueWeight - (int)valueWeight == 0) {
weight = " [label = " + std::to_string((int)valueWeight) + "];";
} else {
weight = " [label = " + std::to_string(valueWeight) + "];";
}
stream << " " << i + 1 << connection << j + 1 << weight
<< std::endl;
} else {
stream << " " << i + 1 << connection << j + 1 << ";"
<< std::endl;
}
}
sameNode.push_back({j, i});
skip = false;
}
}
}
stream << "}" << std::endl;
stream.close();
} else {
throw std::out_of_range("File not open.");
}
}
double Graph::GetRandomInt() {
double randomDel = rand_r(&random_seed_) % 10 + 1;
double randomValueEdge = (rand_r(&random_seed_) % 1000 + 1) / randomDel;
return randomValueEdge;
}