Refactor: Rename adjMatrix to adjList

This commit is contained in:
stefiosif
2022-10-12 16:57:48 +03:00
parent 8b6d341d18
commit c525aeaa43
14 changed files with 56 additions and 56 deletions

View File

@@ -20,7 +20,7 @@ public:
template<typename T>
BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) {
this->adjMatrix = algo::BreadthFirstSearch<T>(G.adjMatrix).execute(root);
this->adjList = algo::BreadthFirstSearch<T>(G.adjList).execute(root);
}
} // namespace graph

View File

@@ -28,23 +28,23 @@ public:
template<typename T>
Digraph<T>::Digraph(std::map<T, std::set<T>> G) {
this->adjMatrix = G;
this->adjList = G;
}
template<typename T>
bool Digraph<T>::contains(const T& u, const T& v) {
return algo::BreadthFirstSearch<T>(this->adjMatrix).query(u, v);
return algo::BreadthFirstSearch<T>(this->adjList).query(u, v);
}
template<typename T>
void Digraph<T>::insert(const T& u, const T& v) {
this->adjMatrix[u].insert(v);
this->adjMatrix[v];
this->adjList[u].insert(v);
this->adjList[v];
}
template<typename T>
void Digraph<T>::remove(const T& u, const T& v) {
this->adjMatrix[u].erase(v);
this->adjList[u].erase(v);
}
template<typename T>
@@ -52,7 +52,7 @@ auto Digraph<T>::reverse() {
std::map<T, std::set<T>> revMatrix;
for (const auto& u : this->vertices()) {
for (const auto& v : this->adjMatrix[u]) {
for (const auto& v : this->adjList[u]) {
revMatrix[v].insert(u);
}
}

View File

@@ -36,31 +36,31 @@ public:
std::uint16_t E();
// Adjacency matrix representation
std::map<T, std::set<T>> adjMatrix;
std::map<T, std::set<T>> adjList;
friend std::ostream& operator<<<>(std::ostream& os, Graph<T>& G);
};
template<typename T>
Graph<T>::~Graph() {
adjMatrix.clear();
adjList.clear();
}
template<typename T>
auto Graph<T>::vertices() {
return std::views::keys(adjMatrix);
return std::views::keys(adjList);
}
template<typename T>
std::uint16_t Graph<T>::V() {
return static_cast<std::uint16_t>(adjMatrix.size());
return static_cast<std::uint16_t>(adjList.size());
}
template<typename T>
std::uint16_t Graph<T>::E() {
std::uint16_t edges = 0;
for (const auto& u : vertices()) {
edges += static_cast<std::uint16_t>(adjMatrix[u].size());
edges += static_cast<std::uint16_t>(adjList[u].size());
}
return static_cast<std::uint16_t>(edges);
}
@@ -69,8 +69,8 @@ template<typename T>
std::ostream& operator<<(std::ostream& os, Graph<T>& G) {
os << "V: " << G.V() << " E: " << G.E() << '\n';
for (const auto& u : this->vertices()) {
if (!this->adjMatrix[u].empty()) {
for (const auto& v : this->adjMatrix[u]) {
if (!this->adjList[u].empty()) {
for (const auto& v : this->adjList[u]) {
os << u << "->" << v << ' ';
}
os << '\n';

View File

@@ -32,10 +32,10 @@ private:
template<typename T>
void SCC<T>::normalize() {
for (const auto& u : this->vertices()) {
for (const auto& v : this->adjMatrix[u]) {
if (!this->adjMatrix.count(v)) {
this->adjMatrix[u].erase(v);
this->adjMatrix.erase(v);
for (const auto& v : this->adjList[u]) {
if (!this->adjList.count(v)) {
this->adjList[u].erase(v);
this->adjList.erase(v);
}
}
}