-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
648 lines (578 loc) · 20.5 KB
/
Graph.cpp
File metadata and controls
648 lines (578 loc) · 20.5 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
// NAME: GAL BEN AMI
#include <iostream>
#include <vector>
#include "Algorithms.hpp"
#include <stdexcept>
#include "Graph.hpp"
#include <climits>
#include <iomanip> // for std::setw
using namespace std;
namespace ariel
{
// Determines the weight type of the graph.
int whatWeightType(const vector<vector<int>> &matrix)
{
int type = 0;
size_t matSize = matrix.size();
for (size_t i = 0; i < matSize; i++)
{
for (size_t j = 0; j < matSize; j++)
{
if (matrix[i][j] < 0)
{
return -1;
}
if (matrix[i][j] > 1)
{
type = 1;
}
}
}
return type;
}
/*
Loads a graph from an adjacency matrix.
Throws an exception if the matrix is not square or if the diagonal is not zero.
If the graph is undirected and the matrix is not symmetric, we set the graph to be directed.
*/
void Graph::loadGraph(const vector<vector<int>> &matrix)
{
if (matrix.empty() || matrix[0].size() < 2)
{
throw invalid_argument("Input matrix is empty");
}
this->numVertices = matrix.size();
this->adjacencyMatrix = matrix;
size_t matSize = matrix.size();
for (size_t i = 0; i < matSize; i++)
{
if (matrix[i].size() != matSize)
{
throw invalid_argument("Input matrix is not a square matrix");
}
for (size_t j = 0; j < matSize; j++)
{
if (i == j && matrix[i][j] != 0)
{
throw invalid_argument("In adjacency matrix, the diagonal elements should be zeros");
}
if (matrix[i][j] != matrix[j][i] && !this->isDirected)
{
this->isDirected = true;
}
}
}
this->weightType = whatWeightType(matrix);
}
// Prints the graph, stating whether it's directed or undirected,
// and the number of vertices and edges.
void Graph::printGraph()
{
int edges = 0;
for (size_t i = 0; i < adjacencyMatrix.size(); ++i)
{
for (size_t j = 0; j < adjacencyMatrix[i].size(); ++j)
{
if (i != j && adjacencyMatrix[i][j] != 0)
{
++edges;
}
}
}
if (!this->isDirected)
{
edges /= 2;
cout << "Undirected graph with " << numVertices << " vertices and " << edges << " edges." << endl;
}
else
{
cout << "Directed graph with " << numVertices << " vertices and " << edges << " edges." << endl;
}
}
void Graph::setContainsNegativeCycle(bool flag)
{
this->containsNegativeCycle = flag;
}
bool Graph::getContainsNegativeCycle() const
{
return this->containsNegativeCycle;
}
void Graph::setIsDirected(bool type)
{
this->isDirected = type;
}
void Graph::setWeightsType(int type)
{
this->weightType = type;
}
size_t Graph::getNumVertices() const
{
return numVertices;
}
vector<vector<int>> Graph::getAdjacencyMatrix() const
{
return adjacencyMatrix;
}
bool Graph::getIsDirected() const
{
return this->isDirected;
}
int Graph::getWeightsType() const
{
return this->weightType;
}
size_t Graph::getNumEdges() const
{
size_t edges = 0;
for (size_t i = 0; i < adjacencyMatrix.size(); ++i)
{
for (size_t j = 0; j < adjacencyMatrix[i].size(); ++j)
{
if (i != j && adjacencyMatrix[i][j] != 0)
{
++edges;
}
}
}
if (!this->isDirected)
{
size_t ans = edges / 2;
return ans;
}
return edges;
}
/*
the following method will check if g1 contains g2.
first, if the number of vertices in g2 is greater than the number of vertices in g1,
then g1 can't contain g2.
then we will check if the adjacency matrix of g2 is a submatrix of the adjacency matrix of g1.
both of the graphs are represented as adjacency matrices,
so we know the diagonal should be all zeros, so we dont need to go over
all the possibilities of the submatrices.
will helps us in operator< overloading.
*/
bool Graph::isContains(const Graph &g) const
{
size_t n = this->adjacencyMatrix.size();
size_t m = g.adjacencyMatrix.size();
if (m > n)
return false; // If submatrix is larger than the matrix, it can't be a submatrix
for (size_t k = 0; k <= n - m; k++)
{
for (size_t l = 0; l <= n - m; l++)
{
bool isSubMatrix = true; // Assume it is a submatrix until proven otherwise
for (size_t i = 0; i < m && isSubMatrix; i++)
{
for (size_t j = 0; j < m && isSubMatrix; j++)
{
if (this->adjacencyMatrix[k + i][l + j] != g.adjacencyMatrix[i][j])
{
isSubMatrix = false; // Mismatch found, set flag to false
}
}
}
if (isSubMatrix)
{
return true; // If all elements matched, return true
}
}
}
return false; // No matching submatrix found
}
/*
implementing + operator overloading.
*/
// Overloading the + operator to add two graphs together
// by adding their adjacency matrices.
Graph Graph::operator+(const Graph &g)
{
Graph resGraph(*this);
// check if the matrices have the same dimensions
if (this->adjacencyMatrix.size() != g.adjacencyMatrix.size() ||
this->adjacencyMatrix[0].size() != g.adjacencyMatrix[0].size())
{
throw invalid_argument("The matrices should have the same dimensions.");
}
vector<vector<int>> resMatrix;
// iterating over each row of the matrix.
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
// creating an empty row to store the sum of the two matrices.
vector<int> row;
for (size_t j = 0; j < this->adjacencyMatrix[i].size(); j++)
{
// adding the corresponding elements of the two matrices.
// and storing the sum in the row.
row.push_back(this->adjacencyMatrix[i][j] + g.adjacencyMatrix[i][j]);
}
// adding the row to the result matrix.
resMatrix.push_back(row);
}
resGraph.loadGraph(resMatrix);
return resGraph;
}
/*
implementing += operator overloading.
*/
// Overloading the += operator to add two graphs together
// by adding their adjacency matrices.
Graph &Graph::operator+=(const Graph &g)
{
// check if the matrices have the same dimensions
if (this->adjacencyMatrix.size() != g.adjacencyMatrix.size() ||
this->adjacencyMatrix[0].size() != g.adjacencyMatrix[0].size())
{
throw invalid_argument("The matrices should have the same dimensions.");
}
// iterating over each row of the matrix.
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
for (size_t j = 0; j < this->adjacencyMatrix[i].size(); j++)
{
// adding the corresponding elements of the two matrices.
this->adjacencyMatrix[i][j] += g.adjacencyMatrix[i][j];
}
}
return *this;
}
// Overloading the += operator to add a scalar to each cell of the adjacency matrix.
Graph &Graph::operator+=(const int scalar)
{
// iterating over each row of the matrix.
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
for (size_t j = 0; j < this->adjacencyMatrix[i].size(); j++)
{
// adding the scalar to each cell of the matrix.
this->adjacencyMatrix[i][j] += scalar;
}
}
return *this;
}
// Unary plus operator overloading, returns the graph itself.
// made it const to avoid changing the graph, and because it doesn't need to change it.
Graph Graph::operator+() const
{
return *this;
}
// Overloading the Prefix version of the ++ operator to increment all the elements of the graph by 1.
Graph &Graph::operator++()
{
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
for (size_t j = 0; j < this->adjacencyMatrix[i].size(); j++)
{
if (i != j && this->adjacencyMatrix[i][j] != 0) // we dont want to create new edges or self loops.
{
this->adjacencyMatrix[i][j]++;
}
}
}
return *this;
}
// Overloading the Postfix version of the ++ operator
// the int argument is a flag to differentiate between the prefix and postfix versions.
Graph Graph::operator++(int)
{
// calling copy constructor to copy the graph.
Graph temp(*this);
++*this;
return temp;
}
// Overloading the Prefix -- operator to reduce all the elements of the graph by 1.
Graph &Graph::operator--()
{
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
for (size_t j = 0; j < this->adjacencyMatrix[i].size(); j++)
{
if (i != j && this->adjacencyMatrix[i][j] != 0) // we dont want to create new edges, self loops.
{
this->adjacencyMatrix[i][j]--;
}
}
}
this->weightType = whatWeightType(this->adjacencyMatrix);
return *this;
}
// Overloading the Postfix -- operator to reduce all the elements of the graph by 1.
Graph Graph::operator--(int)
{
Graph temp(*this);
--*this;
return temp;
}
/*
implementing - operator overloading.
*/
// Overloading the - operator to subtract two graphs together
// by subtracting their adjacency matrices.
Graph Graph::operator-(const Graph &g) const
{
Graph resGraph(*this);
// check if the matrices have the same dimensions
if (this->adjacencyMatrix.size() != g.adjacencyMatrix.size() ||
this->adjacencyMatrix[0].size() != g.adjacencyMatrix[0].size())
{
throw invalid_argument("The matrices should have the same dimensions.");
}
vector<vector<int>> resMatrix;
// iterating over each row of the matrix.
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
// creating an empty row to store the subtraction of the two matrices.
vector<int> row;
for (size_t j = 0; j < this->adjacencyMatrix[i].size(); j++)
{
// subtracting the corresponding elements of the two matrices.
row.push_back(this->adjacencyMatrix[i][j] - g.adjacencyMatrix[i][j]);
}
// adding the row to the result matrix.
resMatrix.push_back(row);
}
resGraph.loadGraph(resMatrix);
return resGraph;
}
Graph &Graph::operator-=(const Graph &g)
{
// check if the matrices have the same dimensions
if (this->adjacencyMatrix.size() != g.adjacencyMatrix.size() ||
this->adjacencyMatrix[0].size() != g.adjacencyMatrix[0].size())
{
throw invalid_argument("The matrices should have the same dimensions.");
}
// iterating over each row of the matrix.
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
for (size_t j = 0; j < this->adjacencyMatrix[i].size(); j++)
{
// subtracting the corresponding elements of the two matrices.
this->adjacencyMatrix[i][j] -= g.adjacencyMatrix[i][j];
}
}
this->weightType = whatWeightType(this->adjacencyMatrix);
return *this;
}
// Unary minus operator overloading, returns the graph with all its elements negated.
Graph Graph::operator-() const
{
Graph resGraph(*this);
for (size_t i = 0; i < resGraph.adjacencyMatrix.size(); i++)
{
for (size_t j = 0; j < resGraph.adjacencyMatrix[i].size(); j++)
{
if (resGraph.adjacencyMatrix[i][j] != 0)
{
resGraph.adjacencyMatrix[i][j] = -resGraph.adjacencyMatrix[i][j];
}
}
}
resGraph.setWeightsType(whatWeightType(resGraph.adjacencyMatrix));
return resGraph;
}
/*
implementing * operator overloading.
*/
Graph Graph::operator*(const Graph &g) const
{
Graph resGraph(*this);
vector<vector<int>> resMatrix;
// check if the matrices have the same dimensions
if (this->getNumVertices() != g.getNumVertices())
{
throw invalid_argument("The size of the matrices is not the same.");
}
// iterating over each row of the first matrix.
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
// creating an empty row to store the multiplication of the two matrices.
vector<int> row;
for (size_t j = 0; j < g.adjacencyMatrix[0].size(); j++)
{
int sum = 0;
// iterating over each column of the second matrix.
for (size_t k = 0; k < g.adjacencyMatrix.size(); k++)
{
// multiplying the corresponding elements of the two matrices.
// and adding the result to the sum.
sum += this->adjacencyMatrix[i][k] * g.adjacencyMatrix[k][j];
}
// adding the sum to the row.
row.push_back(sum);
}
// adding the row to the result matrix.
resMatrix.push_back(row);
}
// resetting the result matrix diagonal to zeroes.
for (size_t i = 0; i < resMatrix.size(); i++)
{
// in my implementation there is no self loops, and the diagonal should be zero.
// load graph will throw an exception if the diagonal is not zero.
resMatrix[i][i] = 0;
}
resGraph.loadGraph(resMatrix);
return resGraph;
}
// Overloading the * operator to multiply the graph by a scalar.
// the method returns a new graph with all its elements multiplied by the scalar.
Graph Graph::operator*(int scalar) const
{
Graph resGraph(*this);
for (size_t i = 0; i < resGraph.adjacencyMatrix.size(); i++)
{
for (size_t j = 0; j < resGraph.adjacencyMatrix[i].size(); j++)
{
// dont mind the zeros as they will remain zeros.
resGraph.adjacencyMatrix[i][j] *= scalar;
}
}
return resGraph;
}
// to allow commutative multiplication of the graph by a scalar.
Graph operator*(int scalar, const Graph &g)
{
return g * scalar;
}
// Overloading the *= operator to multiply the graph by a scalar.
// the method returns the graph itself with all its elements multiplied by the scalar.
Graph &Graph::operator*=(const int &scalar)
{
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
for (size_t j = 0; j < this->adjacencyMatrix[i].size(); j++)
{
// dont mind the zeros as they will remain zeros.
this->adjacencyMatrix[i][j] *= scalar;
}
}
return *this;
}
/*
implementing /= operator overloading.
*/
// Overloading the /= operator to divide the graph by a scalar.
// the method returns the graph itself with all its elements divided by the scalar.
Graph &Graph::operator/=(const int &scalar)
{
if (scalar == 0)
{
throw invalid_argument("Division by zero is not allowed.");
}
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
for (size_t j = 0; j < this->adjacencyMatrix[i].size(); j++)
{
if (this->adjacencyMatrix[i][j] != 0)
{
this->adjacencyMatrix[i][j] /= scalar;
}
}
}
return *this;
}
/*
implementing comparison operators.
*/
// Overloading the < operator, G1<G2 if G1 matrix is submatrix of G2 matrix.
// if G1 is not a submatrix of G2 and G2 is not a submatrix of G1,
// then G1<G2 if the sum of edges in G1 is less than the sum of edges in G2.
// if the sum of edges is equal in both graphs, then G1<G2 if the number of vertices in G1
// is less than the number of vertices in G2.
bool Graph::operator<(const Graph &g) const
{
// If g contains this, return true as this < g.
if (g.isContains(*this))
{
if (g.getNumVertices() == this->getNumVertices())
{
// If the number of vertices is the same, it means they are the same matrix
return false;
}
return true;
}
// If this contains g, return false as this > g.
if (this->isContains(g))
{
return false;
}
// Compare the number of edges
size_t thisEdges = this->getNumEdges();
size_t gEdges = g.getNumEdges();
if (thisEdges != gEdges)
{
return (thisEdges < gEdges);
}
// If the number of edges is equal, compare the number of vertices
return (this->numVertices < g.numVertices);
}
bool Graph::operator<=(const Graph &g) const
{
return (*this < g) || (*this == g);
}
bool Graph::operator>(const Graph &g) const
{
return g < *this;
}
bool Graph::operator>=(const Graph &g) const
{
return g <= *this;
}
/**
* Overloads the == operator to compare two Graph objects.
* This method compares the adjacency matrices of two Graph objects.
* If the number of vertices in both graphs are the same, it compares each element in the adjacency matrices.
* If the number of vertices are different, it checks if neither graph is less than the other.
*/
bool Graph::operator==(const Graph &g) const
{
// If the number of vertices are equal, compare the adjacency matrices
if (this->numVertices == g.numVertices)
{
for (size_t i = 0; i < this->adjacencyMatrix.size(); i++)
{
for (size_t j = 0; j < this->adjacencyMatrix[i].size(); j++)
{
if (this->adjacencyMatrix[i][j] != g.adjacencyMatrix[i][j])
{
return false; // Mismatch found
}
}
}
return true; // All elements matched
}
return false; // Graphs are not equal
}
bool Graph::operator!=(const Graph &g) const
{
return !(*this == g);
}
/**
* Overloads the << operator to allow easy printing of the Graph object.
*
* @param os The output stream (e.g., std::cout) where the graph will be printed.
* @param g The Graph object that contains the adjacency matrix to be printed.
* @return std::ostream& The same output stream to enable chaining of << operators. (e.g., std::cout << g1 << g2 << g3;)
*
* The matrix will be printed row by row, with each row inside square brackets.
*/
std::ostream &operator<<(std::ostream &os, const Graph &g)
{
for (size_t i = 0; i < g.getAdjacencyMatrix().size(); i++)
{
os << "[";
for (size_t j = 0; j < g.getAdjacencyMatrix()[i].size(); j++)
{
// to format the output with a minimum field width of 2 for better alignment.
os << std::setw(2) << g.getAdjacencyMatrix()[i][j];
// if not the last element in the row, add a comma.
if (j != g.getAdjacencyMatrix()[i].size() - 1)
{
os << ",";
}
}
os << "]\n";
}
return os;
}
}