-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixAdjacency.cpp
More file actions
69 lines (61 loc) · 1.27 KB
/
matrixAdjacency.cpp
File metadata and controls
69 lines (61 loc) · 1.27 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
#include "matrixAdjacency.h"
MatrixAdjacency::MatrixAdjacency(int size)
{
/*
How works this function?
- The matrix is a 2D array of integers
- The size of the matrix is the number of vertices
- The matrix is initialized with 0s
*/
matrix = new int* [size];
for (int i = 0; i < size; i++) {
matrix[i] = new int[size];
for (int j = 0; j < size; j++) {
matrix[i][j] = -1;
}
}
this->size = size;
}
MatrixAdjacency::~MatrixAdjacency()
{
for (int i = 0; i < size; i++) {
delete[] matrix[i];
}
delete[] matrix;
/*
How works this function?
- Deallocate the memory of the matrix
*/
}
void MatrixAdjacency::addEdge(int i, int j, int w)
{
/*
How works this function?
- If there is an edge between vertex i and vertex j, then matrix[i][j] = 1
*/
if (i < size && j < size) {
matrix[i][j] = w;
}
}
void MatrixAdjacency::removeEdge(int i, int j)
{
/*
How works this function?
- If there is no edge between vertex i and vertex j, then matrix[i][j] = 0
*/
if (i < size && j < size) {
matrix[i][j] = -1;
}
}
int MatrixAdjacency::getWeight(int i, int j) {
return matrix[i][j];
}
void MatrixAdjacency::printMatrix()
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}