Improve readability

This commit is contained in:
stefiosif
2023-02-10 18:26:48 +02:00
parent 2175ac0ca9
commit 3591c58f6d
6 changed files with 217 additions and 158 deletions

View File

@@ -3,7 +3,6 @@
#include "algorithm/decremental_reachability.h"
#include "graph/breadth_first_tree.h"
#include <stack>
namespace algo {
@@ -25,7 +24,7 @@ public:
// Delete collection of edges
void remove(const std::vector<std::pair<T, T>>& edges) override;
// Delete edge e(u, v) and explicitely maintain the transitive closure
// Delete edge e(u, v) and explicitly maintain the transitive closure
void remove(const T& u, const T& v);
private:
// Transitive closure matrix
@@ -41,8 +40,11 @@ private:
};
std::unordered_map<T, Edges> E;
//
std::unordered_map<T, std::stack<T>> H;
// Repair reachability trees after edge deletions
void repairTrees(std::unordered_map<T, std::stack<T>>& H);
void repairTrees();
};
template<typename T>
@@ -67,53 +69,54 @@ bool Italiano<T>::query(const T& u, const T& v) {
template<typename T>
void Italiano<T>::remove(const std::vector<std::pair<T, T>>& edges) {
for (const auto& [u, v] : edges)
for (const auto& [u, v] : edges) {
if (!this->G.adjList[u].contains(v)) continue;
remove(u, v);
}
}
template<typename T>
void Italiano<T>::remove(const T& u, const T& v) {
if (!this->G.adjList[u].contains(v)) return;
std::unordered_map<T, std::stack<T>> H;
for (const auto& w : this->G.vertices()) {
if (RT[w].contains(u, v)) {
if (E[v].inc.size() > 1)
H[w].push(v);
else {
TC[w][v] = false;
for (const auto& c : E[v].out)
H[w].push(c);
}
RT[w].adjList[u].erase(v);
}
}
this->G.remove(u, v);
E[u].out.erase(v);
E[v].inc.erase(u);
this->G.remove(u, v);
for (const auto& w : this->G.vertices()) {
if (!RT[w].adjList[u].contains(v)) continue;
repairTrees(H);
RT[w].adjList[u].erase(v);
if (E[v].inc.size() > 0) {
H[w].push(v);
continue;
}
TC[w][v] = false;
for (const auto& c : E[v].out)
H[w].push(c);
}
repairTrees();
}
template<typename T>
void Italiano<T>::repairTrees(std::unordered_map<T, std::stack<T>>& H) {
for (const auto& z : this->G.vertices()) {
while (H[z].size() > 0) {
const auto& h = H[z].top();
bool found = false;
void Italiano<T>::repairTrees() {
for (const auto& w : this->G.vertices()) {
while (H[w].size() > 0) {
const auto& h = H[w].top();
H[w].pop();
bool foundHook = false;
for (const auto& i : E[h].inc) {
if (RT[z].contains(z, i)) {
RT[z].adjList[i].insert(h);
found = true;
if (RT[w].adjList[w].contains(i)) {
RT[w].adjList[i].insert(h);
foundHook = true;
break;
}
}
H[z].pop();
if (!found) {
TC[z][h] = false;
for (const auto& o : E[h].out) {
H[z].push(o);
if (foundHook) continue;
TC[w][h] = false;
for (const auto& o : E[h].out) {
if (RT[w].adjList[h].contains(o)) {
H[w].push(o);
}
}
}