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

@@ -29,7 +29,7 @@ private:
std::unordered_map<T, std::unordered_map<T, bool>> TC;
// For each SCC, store a reachability tree created as a BFS tree
std::unordered_map<T, BreadthFirstTree<T>> RT;
std::unordered_map<SCC<T>, BreadthFirstTree<T>, HashSCC<T>> RT;
// For each SCC, store collections of incoming, outgoing and internal edges
struct Edges {
@@ -37,37 +37,50 @@ private:
std::set<std::pair<T, T>> inc;
std::set<std::pair<T, T>> out;
};
std::unordered_map<T, Edges> E;
std::unordered_map<SCC<T>, Edges, HashSCC<T>> E;
// Structure that keeps candidate components to use as a hook
std::unordered_map<SCC<T>, std::stack<T>, HashSCC<T>> H;
// Decremental maintenance of strongly connected components
RodittyZwick<T> rodittyZwick;
// Delete internal edge e(u, v)
void removeInternal(const T& u, const T& v);
// Delete external edge e(u, v)
void removeExternal(const T& u, const T& v);
// Repair reachability trees
void repairTrees();
//
void splitEdges(std::unordered_map<T, SCC<T>>& L, std::unordered_map<T, SCC<T>> C, const T& w);
};
template<typename T>
void Frigioni<T>::init() {
auto SCCs = Tarjan<T>(this->G.adjList).execute();
rodittyZwick = RodittyZwick<T>(this->G);
rodittyZwick.init();
auto C = rodittyZwick.getComponentMap();
for (auto& scc : SCCs) {
RT[scc.id] = BreadthFirstTree<T>(this->G, scc.id);
for (const auto& w : std::views::keys(C)) {
RT[C[w]] = BreadthFirstTree<T>(this->G, w);
for (const auto& u : this->G.vertices()) {
for (const auto& v : this->G.adjList[u]) {
if (scc.member(u)) {
if (scc.member(v))
E[scc.id].in.insert(std::make_pair(u, v));
else
E[scc.id].out.insert(std::make_pair(u, v));
} else if (scc.member(v)) {
E[scc.id].inc.insert(std::make_pair(u, v));
}
if (!C[w].contains(u) && C[w].contains(v))
E[C[w]].inc.insert({ u, v });
else if (C[w].contains(u) && !C[w].contains(v))
E[C[w]].out.insert({ u, v });
else
E[C[w]].in.insert({ u, v });
TC[u][v] = false;
}
}
}
for (auto& scc : SCCs) {
for (const auto& u : scc.vertices()) {
for (const auto& v : RT[scc.id].vertices()) {
for (const auto& w : std::views::keys(C)) {
for (const auto& u : C[w].vertices()) {
for (const auto& v : RT[C[w]].vertices()) {
TC[u][v] = true;
}
}
@@ -85,104 +98,132 @@ void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) {
std::vector<std::pair<T, T>> Eext;
for (const auto& [u, v] : edges) {
if (!this->G.adjList[u].contains(v)) continue;
if (rodittyZwick.query(u, v))
Eint.push_back({ u, v });
else
Eext.push_back({ u, v });
}
for (const auto& [u, v] : Eint) removeInternal(u, v);
for (const auto& [u, v] : Eext) removeExternal(u, v);
std::unordered_map<T, std::stack<T>> H;
for (const auto& [u, v] : Eint) {
this->G.remove(u, v);
rodittyZwick.remove(u, v);
repairTrees();
}
if (!rodittyZwick.query(u, v)) {
TC[u][v] = false;
auto C = rodittyZwick.getSCCs();
template<typename T>
void Frigioni<T>::removeInternal(const T& u, const T& v) {
this->G.remove(u, v);
auto L = rodittyZwick.getComponentMap();
rodittyZwick.remove(u, v);
auto C = rodittyZwick.getComponentMap();
E[C[u].id].inc.erase({ u, v });
E[C[u].id].out.erase({ u, v });
E[C[u].id].in.erase({ u, v });
auto D = E[C[u].id];
E.erase(C[u].id);
for (const auto& id : std::views::keys(C)) {
if (RT[id].contains(id, v)) {
if (E[C[v].id].inc.size() > 1)
H[id].push(C[v].id);
else {
for (const auto& w : C[id].vertices())
TC[w][v] = false;
for (const auto& c : E[v].out)
H[id].push(C[c.second].id);
}
RT[id].adjList[u].erase(v);
}
}
for (const auto& [w, z] : D.inc) {
E[C[z].id].inc.insert({ w, z });
}
for (const auto& [w, z] : D.out) {
E[C[w].id].out.insert({ w, z });
}
for (const auto& [w, z] : D.in) {
if (C[w] == C[z])
E[C[w].id].in.insert({ w, z });
else {
E[C[w].id].out.insert({ w, z });
E[C[z].id].in.insert({ w, z });
}
}
} else {
E[u].in.erase({ u, v });
}
if (rodittyZwick.query(u, v)) {
E[C[u]].in.erase({ u, v });
return;
}
for (const auto& [u, v] : Eext) {
this->G.remove(u, v);
auto C = rodittyZwick.getSCCs();
for (const auto& id : std::views::keys(C)) {
if (RT[id].contains(id, v)) {
if (E[C[v].id].inc.size() > 1)
H[id].push(C[v].id);
else {
for (const auto& w : C[id].vertices())
TC[w][v] = false;
for (const auto& c : E[v].out)
H[id].push(C[c.second].id);
}
RT[id].adjList[u].erase(v);
}
}
if (L[u] == C[u]) {
E[L[u]].out.erase({ u, v });
E.erase(L[v]);
splitEdges(L, C, v);
}
else {
E[L[v]].inc.erase({ u, v });
E.erase(L[u]);
splitEdges(L, C, u);
}
auto C = rodittyZwick.getSCCs();
for (const auto& id : std::views::keys(rodittyZwick.getSCCs())) {
while (H[id].size() > 0) {
const auto& h = H[id].top();
bool found = false;
for (const auto& [u, v] : E[h].inc) {
if (RT[id].contains(id, u)) {
RT[id].adjList[u].insert(h);
found = true;
for (const auto& w : std::views::keys(C)) {
if (!RT[C[w]].contains(v)) continue;
RT[C[w]].removeEdgeTo(v);
if (E[C[v]].inc.size() > 0) {
H[C[w]].push(v); /*h mipos C[v].id?*/
continue;
}
for (const auto& x : C[w].vertices()) {
for (const auto& y : C[v].vertices()) {
TC[x][y] = false;
}
}
for (const auto& [a, b] : E[C[v]].out)
H[C[w]].push(b);
}
}
template<typename T>
void Frigioni<T>::removeExternal(const T& u, const T& v) {
this->G.remove(u, v);
auto C = rodittyZwick.getComponentMap();
E[C[v]].inc.erase({ u, v });
E[C[u]].out.erase({ u, v });
for (const auto& w : std::views::keys(C)) {
if (!RT[C[w]].contains(v)) continue;
RT[C[w]].removeEdgeTo(v);
if (E[C[v]].inc.size() > 0) {
H[C[w]].push(v);
continue;
}
for (const auto& x : C[w].vertices()) {
for (const auto& y : C[v].vertices()) {
TC[x][y] = false;
}
}
for (const auto& [a, b] : E[C[v]].out)
H[C[w]].push(b);
}
}
template<typename T>
void Frigioni<T>::repairTrees() {
auto C = rodittyZwick.getComponentMap();
for (const auto& w : std::views::keys(C)) {
while (H[C[w]].size() > 0) {
const auto& h = H[C[w]].top();
H[C[w]].pop();
bool foundHook = false;
for (const auto& [a, b] : E[C[h]].inc) {
if (RT[C[w]].adjList[w].contains(a)) {
RT[C[w]].insert(a, h);
foundHook = true;
break;
}
}
H[id].pop();
if (!found) {
auto C = rodittyZwick.getSCCs();
for (const auto& w : C[id].vertices())
TC[w][h] = false;
if (foundHook) continue;
for (const auto& [u, v] : E[h].out) {
H[id].push(v);
for (const auto& x : C[w].vertices()) {
for (const auto& y : C[h].vertices()) {
TC[x][y] = false;
}
}
for (const auto& [a, b] : E[C[h]].out) {
if (RT[C[w]].adjList[h].contains(a)) {
H[C[w]].push(b);
}
}
}
}
}
template<typename T>
void Frigioni<T>::splitEdges(std::unordered_map<T, SCC<T>>& L, std::unordered_map<T, SCC<T>> C, const T& w) {
for (const auto& [a, b] : E[L[w]].inc)
E[C[b]].inc.insert({ a, b });
for (const auto& [a, b] : E[L[w]].out)
E[C[a]].out.insert({ a, b });
for (const auto& [a, b] : E[L[w]].in) {
if (C[a] == C[b]) {
E[C[a]].in.insert({ a, b });
}
else {
E[C[a]].out.insert({ a, b });
E[C[b]].in.insert({ a, b });
}
}
}