-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
315 lines (275 loc) · 8.37 KB
/
Copy pathGraph.cpp
File metadata and controls
315 lines (275 loc) · 8.37 KB
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
// Graph.cpp
#include "Graph.hpp"
#include <stdexcept>
#include <algorithm>
using namespace ariel;
using namespace std;
// Function to load a graph from an adjacency matrix
void Graph::loadGraph(const std::vector<std::vector<int>>& matrix) {
// Check if the matrix is empty
if (matrix.empty()) {
throw std::invalid_argument("The graph cannot be empty");
}
// Check if the matrix is square
size_t size = matrix.size();
for (const auto& row : matrix) {
if (row.size() != size) {
throw std::invalid_argument("Invalid graph: The graph is not a square matrix.");
}
}
// Set the graph's directed flag and load the matrix
directed = this->isDirected();
this->adjacencyMatrix = matrix;
}
// Function to print the graph
void Graph::printGraph() const {
size_t vertexCount = getNumberOfNodes();
int edgeCount = 0;
for (const auto& row : adjacencyMatrix) {
for (const auto& element : row) {
if (element != 0) {
edgeCount++;
}
}
}
// Adjust edge count for undirected graphs
if (!getDirected()) {
edgeCount /= 2;
}
// Print graph details
std::cout << "Graph with " << vertexCount << " vertices and " << edgeCount << " edges.\n";
for (const auto& row : adjacencyMatrix) {
for (const auto& element : row) {
std::cout << element << ' ';
}
std::cout << '\n';
}
}
// Function to check if the graph is directed
bool Graph::isDirected() const {
for (size_t i = 0; i < adjacencyMatrix.size(); ++i) {
for (size_t j = 0; j < adjacencyMatrix[i].size(); ++j) {
if (adjacencyMatrix[i][j] != adjacencyMatrix[j][i]) {
return true;
}
}
}
return false;
}
// Function to get all edges of the graph
std::vector<std::pair<size_t, std::pair<size_t, int>>> Graph::getEdges() const {
std::vector<std::pair<size_t, std::pair<size_t, int>>> edges;
for (size_t i = 0; i < adjacencyMatrix.size(); ++i) {
for (size_t j = 0; j < adjacencyMatrix[i].size(); ++j) {
if (adjacencyMatrix[i][j] != 0) {
edges.push_back({i, {j, adjacencyMatrix[i][j]}});
}
}
}
return edges;
}
// Function to get the neighbors of a specific node
std::vector<size_t> Graph::getNeighbors(size_t node) const {
std::vector<size_t> neighbors;
if (node >= adjacencyMatrix.size()) {
throw std::out_of_range("Node index out of range");
}
for (size_t i = 0; i < adjacencyMatrix[node].size(); ++i) {
if (adjacencyMatrix[node][i] != 0) {
neighbors.push_back(i);
}
}
return neighbors;
}
// Function to get the weight of an edge between two nodes
int Graph::getEdgeWeight(size_t node1, size_t node2) const {
if (node1 >= adjacencyMatrix.size() || node2 >= adjacencyMatrix.size()) {
throw std::out_of_range("Node index out of range");
}
return adjacencyMatrix[node1][node2];
}
// Arithmetic operators
Graph Graph::operator+(const Graph& other) const {
checkSameSize(other);
std::vector<std::vector<int>> result = adjacencyMatrix;
for (size_t i = 0; i < adjacencyMatrix.size(); ++i) {
for (size_t j = 0; j < adjacencyMatrix[i].size(); ++j) {
result[i][j] += other.adjacencyMatrix[i][j];
}
}
return Graph(result);
}
Graph& Graph::operator+=(const Graph& other) {
checkSameSize(other);
for (size_t i = 0; i < adjacencyMatrix.size(); ++i) {
for (size_t j = 0; j < adjacencyMatrix[i].size(); ++j) {
adjacencyMatrix[i][j] += other.adjacencyMatrix[i][j];
}
}
return *this;
}
Graph Graph::operator+() const {
return *this;
}
Graph Graph::operator-(const Graph& other) const {
checkSameSize(other);
std::vector<std::vector<int>> result = adjacencyMatrix;
for (size_t i = 0; i < adjacencyMatrix.size(); ++i) {
for (size_t j = 0; j < adjacencyMatrix[i].size(); ++j) {
result[i][j] -= other.adjacencyMatrix[i][j];
}
}
return Graph(result);
}
Graph& Graph::operator-=(const Graph& other) {
checkSameSize(other);
for (size_t i = 0; i < adjacencyMatrix.size(); ++i) {
for (size_t j = 0; j < adjacencyMatrix[i].size(); ++j) {
adjacencyMatrix[i][j] -= other.adjacencyMatrix[i][j];
}
}
return *this;
}
Graph Graph::operator-() const {
std::vector<std::vector<int>> result = adjacencyMatrix;
for (auto& row : result) {
for (auto& elem : row) {
elem = -elem;
}
}
return Graph(result);
}
// Comparison operators
bool Graph::operator==(const Graph& other) const {
return adjacencyMatrix == other.adjacencyMatrix;
}
bool Graph::operator!=(const Graph& other) const {
return !(*this == other);
}
bool Graph::operator<(const Graph& other) const {
if (adjacencyMatrix.size() != other.adjacencyMatrix.size()) {
return adjacencyMatrix.size() < other.adjacencyMatrix.size();
}
return getEdgeCount() < other.getEdgeCount();
}
bool Graph::operator<=(const Graph& other) const {
return *this < other || *this == other;
}
bool Graph::operator>(const Graph& other) const {
return !(*this <= other);
}
bool Graph::operator>=(const Graph& other) const {
return !(*this < other);
}
// Increment and decrement operators
Graph& Graph::operator++() {
for (auto& row : adjacencyMatrix) {
for (auto& elem : row) {
++elem;
}
}
return *this;
}
Graph Graph::operator++(int) {
Graph temp = *this;
++(*this);
return temp;
}
Graph& Graph::operator--() {
for (auto& row : adjacencyMatrix) {
for (auto& elem : row) {
--elem;
}
}
return *this;
}
Graph Graph::operator--(int) {
Graph temp = *this;
--(*this);
return temp;
}
// Scalar multiplication
Graph Graph::operator*(int scalar) const {
std::vector<std::vector<int>> result = adjacencyMatrix;
for (auto& row : result) {
for (auto& elem : row) {
elem *= scalar;
}
}
return Graph(result);
}
// Scalar multiplication assignment operator
Graph& Graph::operator*=(int scalar) {
for (auto& row : adjacencyMatrix) {
for (auto& elem : row) {
elem *= scalar;
}
}
return *this;
}
// Scalar division assignment operator
Graph& Graph::operator/=(int scalar) {
if (scalar == 0) {
throw std::invalid_argument("Division by zero is not allowed.");
}
for (auto& row : adjacencyMatrix) {
for (auto& elem : row) {
elem /= scalar;
}
}
return *this;
}
// Graph multiplication
Graph Graph::operator*(const Graph& other) const {
if (adjacencyMatrix.size() != other.adjacencyMatrix.size()) {
throw std::invalid_argument("Graphs must be of the same size to multiply.");
}
std::vector<std::vector<int>> result(adjacencyMatrix.size(), std::vector<int>(adjacencyMatrix.size(), 0));
for (size_t i = 0; i < adjacencyMatrix.size(); ++i) {
for (size_t j = 0; j < adjacencyMatrix.size(); ++j) {
for (size_t k = 0; k < adjacencyMatrix.size(); ++k) {
result[i][j] += adjacencyMatrix[i][k] * other.adjacencyMatrix[k][j];
}
}
}
return Graph(result);
}
namespace ariel
{
// Output operator
std::ostream& operator<<(std::ostream& os, const Graph& graph) {
size_t vertexCount = graph.getNumberOfNodes();
int edgeCount = graph.getEdgeCount();
os << "Graph with " << vertexCount << " vertices and " << edgeCount << " edges.\n";
for (const auto& row : graph.adjacencyMatrix) {
for (const auto& elem : row) {
os << elem << ' ';
}
os << '\n';
}
return os;
}
}
// Private helper functions
// size_t Graph::getNumberOfNodes() const {
// return adjacencyMatrix.size();
// }
int ariel::Graph::getEdgeCount() const {
int edgeCount = 0;
for (const auto& row : adjacencyMatrix) {
for (const auto& elem : row) {
if (elem != 0) {
edgeCount++;
}
}
}
if (!getDirected()) {
edgeCount /= 2;
}
return edgeCount;
}
void Graph::checkSameSize(const Graph& other) const {
if (adjacencyMatrix.size() != other.adjacencyMatrix.size()) {
throw std::invalid_argument("Graphs must be of the same size.");
}
}