-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphmatrix.h
More file actions
95 lines (81 loc) · 2.17 KB
/
Copy pathgraphmatrix.h
File metadata and controls
95 lines (81 loc) · 2.17 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
#ifndef GRAPHMATRIX_H_INCLUDED
#define GRAPHMATRIX_H_INCLUDED
#include <iostream>
using namespace std;
class GraphMatrix{
private:
int numVertex;
int numEdge;
bool** adjMatrix; // O primeiro ponteiro aponta para as linhas e o segundo para as colunas.
public:
GraphMatrix(int numVert); // Construtor
~GraphMatrix(); // Desconstrutor
// Definindo métodos
bool hasEdge(int v1, int v2);
void addEdge(int v1, int v2);
void removeEdge(int v1, int v2);
void printEdges();
void printMatrix();
int getNumVertex();
int getNumEdge();
bool** getAdjMatrix();
};
GraphMatrix::GraphMatrix(int numVert):numVertex(numVert), numEdge(0), adjMatrix(nullptr){// Lista de inicialização
// Inicializando matrix de adjacência
adjMatrix = new bool*[numVertex]; // Estou criando um vetor de ponteiros, para isso preciso alocar na memória
// Inserindo valores bool (false)
for(int i=0; i<numVertex; i++){
adjMatrix[i]= new bool[numVertex];
for (int j=0; j<numVertex; j++){
adjMatrix[i][j]=false;
}
}
}
GraphMatrix::~GraphMatrix(){
for (int i=0; i<numVertex; i++){
delete[] adjMatrix[i];
}
delete[] adjMatrix;
}
bool GraphMatrix::hasEdge(int v1, int v2){
return adjMatrix[v1][v2];
}
void GraphMatrix::addEdge(int v1, int v2){
if (!hasEdge(v1,v2)){
adjMatrix[v1][v2] = true;
numEdge++;
}
}
void GraphMatrix::removeEdge(int v1, int v2){
if(hasEdge(v1,v2)){
adjMatrix[v1][v2]=false;
numEdge--;
}
}
void GraphMatrix::printEdges(){
for (int i=0; i<numVertex; i++){
for (int j=0; j<numVertex; j++){
if(hasEdge(i,j)){
std::cout<< "(" << i << "," << j << ")"<< std::endl;
}
}
}
}
void GraphMatrix::printMatrix(){
for (int i=0; i<numVertex; i++){
for (int j=0; j<numVertex; j++){
std::cout << hasEdge(i,j) << " ";
}
std::cout << std::endl;
}
}
int GraphMatrix::getNumVertex(){
return numVertex;
}
int GraphMatrix::getNumEdge(){
return numEdge;
}
bool** GraphMatrix::getAdjMatrix(){
return adjMatrix;
}
#endif // GRAPHMATRIX_H_INCLUDED