Please review my Adjacency Matrix class based on this code.
Since I use
std::vector
to create the matrix, I would like to store as many nodes as possible. Is it OK to usestd::size_t
all over the code?Are using 1 for connected and 0 for not connected a bad idea?
class AdjMatrix {
public:
AdjMatrix(const std::size_t& vertsize);
std::size_t Vertices() const;
std::size_t Edges() const;
void addEdge(const std::size_t& a, const std::size_t& b);
std::size_t degree(const std::size_t& v) const;
std::size_t maxDegree() const;
std::size_t minDegree() const;
private:
std::size_t vertices;
std::size_t edges;
std::vector<std::vector<std::size_t>> matrix;
};
AdjMatrix::AdjMatrix(const std::size_t& vertsize) :vertices{vertsize < 0? throw std::out_of_range{"necgative values"} : vertsize}, edges{0}, matrix{vertsize} {
for (auto& v : matrix) {
v.resize(vertsize);
}
}
std::size_t AdjMatrix::Vertices() const {
return vertices;
}
std::size_t AdjMatrix::Edges() const {
return edges;
}
void AdjMatrix::addEdge(const std::size_t& a, const std::size_t& b) {
if (a < 0 || b < 0) {
throw std::out_of_range{"negative values"};
}
if (matrix[a][b] == 0) {
edges++;
}
matrix[a][b] = 1;
matrix[b][a] = 1;
}
std::size_t AdjMatrix::degree(const std::size_t& v) const {
std::size_t d = 0;
for (std::size_t i = 0; i < matrix[v].size(); ++i) {
if (matrix[v][i] == 1) {
d++;
}
}
return d;
}
std::size_t AdjMatrix::maxDegree() const {
std::size_t max = 0;
for (std::size_t i = 0; i < matrix.size(); i++) {
std::size_t d = degree(i);
max = d > max ? d : max;
}
return max;
}
std::size_t AdjMatrix::minDegree() const {
std::size_t min;
for (std::size_t i = 0; i < matrix.size(); i++) {
std::size_t d = degree(i);
if (i == 0) {
min = d;
} else {
min = min < d ? min : d;
}
}
return min;
}
How can I improve this code?