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

View File

@@ -1,27 +1,27 @@
#ifndef BREADTH_FIRST_TREE_H_ #ifndef BREADTH_FIRST_TREE_H_
#define BREADTH_FIRST_TREE_H_ #define BREADTH_FIRST_TREE_H_
#include "out_tree.h" #include "directed_rooted_tree.h"
#include "algorithm/breadth_first_search.h" #include "algorithm/breadth_first_search.h"
using namespace graph; using namespace graph;
namespace tree { namespace tree {
template<typename T> template<typename T>
class BreadthFirstTree : public OutTree<T> { class BreadthFirstTree : public DirectedRootedTree<T> {
public: public:
BreadthFirstTree() = default; BreadthFirstTree() = default;
BreadthFirstTree(Digraph<T> G, T root); BreadthFirstTree(Digraph<T> G, T root);
BreadthFirstTree(std::map<T, std::set<T>> G) BreadthFirstTree(std::map<T, std::set<T>> G)
: OutTree<T>::OutTree(G) {} : DirectedRootedTree<T>::DirectedRootedTree(G) {}
}; };
template<typename T> template<typename T>
BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) { BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) {
auto bfs = algo::BreadthFirstSearch<T>(G).execute(root); auto bfs = algo::BreadthFirstSearch<T>(G).execute(root);
Graph<T>::adjMatrix = bfs; this->adjMatrix = bfs;
} }
} // namespace tree } // namespace tree

View File

@@ -1,20 +0,0 @@
#ifndef IN_TREE_H_
#define IN_TREE_H_
#include "directed_rooted_tree.h"
using namespace graph;
namespace tree {
template<typename T>
class InTree : public DirectedRootedTree<T> {
public:
InTree() = default;
InTree(std::map<T, std::set<T>> G)
: DirectedRootedTree<T>::DirectedRootedTree(G) {}
};
} // namespace tree
#endif

View File

@@ -1,20 +0,0 @@
#ifndef OUT_TREE_H_
#define OUT_TREE_H_
#include "directed_rooted_tree.h"
using namespace graph;
namespace tree {
template<typename T>
class OutTree : public DirectedRootedTree<T> {
public:
OutTree() = default;
OutTree(std::map<T, std::set<T>> G)
: DirectedRootedTree<T>::DirectedRootedTree(G) {}
};
} // namespace tree
#endif