Add BreadthFirstTree's to decremental maintenance algorithm and delete In/Out tree classes

This commit is contained in:
stefiosif
2022-07-20 23:04:06 +03:00
parent 227749b9e4
commit 31567f9e57
4 changed files with 24 additions and 68 deletions

View File

@@ -7,6 +7,7 @@
#include "tree/breadth_first_tree.h"
using namespace graph;
using namespace tree;
namespace algo {
@@ -28,10 +29,9 @@ private:
// Array used to answer strong connectivity queries in O(1) time
std::map<T, T> A;
// Connect SCC/SCC representative with 2 SPTs
// pair.first: Shortest-paths out-tree
// pair.second: Shortest-paths in-tree
std::map<T, std::pair<Digraph<T>, Digraph<T>>> SPT;
// Maintain in-out bfs trees
std::map<T, BreadthFirstTree<T>> inTree;
std::map<T, BreadthFirstTree<T>> outTree;
// Connect each representative with its SCC
std::map<T, SCC<T>> connection;
@@ -49,15 +49,14 @@ void DecrementalSCC<T>::findSCC() {
for (auto& C : SCCs) {
const auto& w = C.representative();
// Create shortest-paths out-tree/in-tree
auto outTree = Digraph<T>(BreadthFirstSearch<T>(C).execute(w));
SPT[w] = std::make_pair(outTree, outTree.reverse());
// Update A with current SCCs vertices
for (const auto& v : C.vertices)
A[v] = w;
outTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C).execute(w));
inTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C.reverse()).execute(w));
// Link SCC with its representative w
connection[w] = C;
}
}
@@ -69,30 +68,27 @@ bool DecrementalSCC<T>::query(const T& u, const T& v) {
template<typename T>
void DecrementalSCC<T>::remove(const T& u, const T& v) {
G.adjMatrix[u].erase(v);
// If u and v are not in the same SCC, do nothing
if (A[u] != A[v]) return;
const auto& w = A[u];
connection[w].remove(u, v);
// If edge e(u, v) is not contained in In(w) and Out(w), do nothing
if (!SPT[w].first.vertices.contains(w) &&
!SPT[w].second.vertices.contains(w)){
connection[w].adjMatrix[u].erase(v);
G.adjMatrix[u].erase(v);
return;
// Update In(w) and Out(w) if they contain the edge
if (inTree[w].contains(u, v) || outTree[w].contains(u, v)) {
auto C = connection[w];
inTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C.reverse()).execute(w));
outTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C).execute(w));
}
// Update In(w) and Out(w)
connection[w].adjMatrix[u].erase(v);
G.adjMatrix[u].erase(v);
auto inTree = Digraph<T>(BreadthFirstSearch<T>(connection[w]).execute(w));
SPT[w] = std::make_pair(inTree, inTree.reverse());
// If a SCC is broken, compute all SCCs again
if (!SPT[w].second.vertices.contains(u) ||
!SPT[w].first.vertices.contains(v)) {
if (!inTree[w].contains(u) || !outTree[w].contains(v)) {
findSCC();
}
}
}; // namespace algo