Refactor: Change reachability interfaces to handle multiple removals/insertions

This commit is contained in:
stefiosif
2022-10-15 10:40:08 +03:00
parent 04ab33888e
commit e7fd88f82c
8 changed files with 41 additions and 37 deletions

View File

@@ -18,7 +18,7 @@ public:
virtual bool query(const T& u, const T& v) =0; virtual bool query(const T& u, const T& v) =0;
// Remove edge e(u,v) and maintain the transitive closure matrix // Remove edge e(u,v) and maintain the transitive closure matrix
virtual void remove(const T& u, const T& v) =0; virtual void remove(const std::vector<std::pair<T, T>>& edges) =0;
protected: protected:
Digraph<T> G; Digraph<T> G;
}; };

View File

@@ -9,7 +9,7 @@ template<typename T>
class DynamicReachability : public DecrementalReachability<T> { class DynamicReachability : public DecrementalReachability<T> {
// Insert edge e(u,v) and maintain the transitive closure matrix // Insert edge e(u,v) and maintain the transitive closure matrix
virtual void insert(const T& u, const T& v) =0; virtual void insert(const T& c, const std::vector<T>& vertices) =0;
}; };

View File

@@ -24,11 +24,8 @@ public:
// in O(1) using the transitive closure matrix // in O(1) using the transitive closure matrix
bool query(const T& u, const T& v) override; bool query(const T& u, const T& v) override;
// Delete edge e(u, v)
void remove(const T& u, const T& v) override;
// Delete set of edges and explicitely maintain the transitive closure // Delete set of edges and explicitely maintain the transitive closure
void remove(const std::vector<std::pair<T, T>>& edges); void remove(const std::vector<std::pair<T, T>>& edges) override;
private: private:
// Transitive closure matrix, used to answer reachability queries in O(1) // Transitive closure matrix, used to answer reachability queries in O(1)
std::unordered_map<T, std::unordered_map<T, bool>> TC; std::unordered_map<T, std::unordered_map<T, bool>> TC;
@@ -84,11 +81,6 @@ bool Frigioni<T>::query(const T& u, const T& v) {
return TC[u][v]; return TC[u][v];
} }
template<typename T>
void Frigioni<T>::remove(const T& u, const T& v) {
}
template<typename T> template<typename T>
void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) { void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) {
std::vector<std::pair<T, T>> Eint; std::vector<std::pair<T, T>> Eint;

View File

@@ -25,16 +25,10 @@ public:
// and then if necessary check the set S // and then if necessary check the set S
bool query(const T& u, const T& v) override; bool query(const T& u, const T& v) override;
// Delete edge e(u, v)
void remove(const T& u, const T& v) override;
// Remove collection of edges from the decremental maintenance data structure // Remove collection of edges from the decremental maintenance data structure
// and for every vertex in set S, rebuilt reachability trees from scratch // and for every vertex in set S, rebuilt reachability trees from scratch
void remove(const std::vector<std::pair<T,T>>& edges); void remove(const std::vector<std::pair<T,T>>& edges);
// Add edge e(u, v)
void insert(const T& u, const T& v) override;
// Insert collection of edges in set S, if threshold is reached re-initialize // Insert collection of edges in set S, if threshold is reached re-initialize
// algorithm, otherwise construct reachability trees for the vertex that is // algorithm, otherwise construct reachability trees for the vertex that is
// the center-of-insertions // the center-of-insertions
@@ -70,15 +64,10 @@ bool HenzingerKing<T>::query(const T& u, const T& v) {
}); });
} }
template<typename T>
void HenzingerKing<T>::remove(const T& u, const T& v) {
this->G.remove(u, v);
}
template<typename T> template<typename T>
void HenzingerKing<T>::remove(const std::vector<std::pair<T, T>>& edges) { void HenzingerKing<T>::remove(const std::vector<std::pair<T, T>>& edges) {
for (const auto& [u, v]: edges) { for (const auto& [u, v] : edges) {
remove(u, v); this->G.remove(u, v);
} }
frigioni.remove(edges); frigioni.remove(edges);
@@ -88,15 +77,10 @@ void HenzingerKing<T>::remove(const std::vector<std::pair<T, T>>& edges) {
} }
} }
template<typename T>
void HenzingerKing<T>::insert(const T& u, const T& v) {
this->G.insert(u, v);
}
template<typename T> template<typename T>
void HenzingerKing<T>::insert(const T& c, const std::vector<T>& vertices) { void HenzingerKing<T>::insert(const T& c, const std::vector<T>& vertices) {
for (const auto& w : vertices) for (const auto& w : vertices)
insert(c, w); this->G.insert(c, w);
S.insert(c); S.insert(c);
if (S.size() > threshold) { if (S.size() > threshold) {

View File

@@ -24,8 +24,11 @@ public:
// in O(1) using the transitive closure matrix // in O(1) using the transitive closure matrix
bool query(const T& u, const T& v) override; bool query(const T& u, const T& v) override;
// 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 explicitely maintain the transitive closure
void remove(const T& u, const T& v) override; void remove(const T& u, const T& v);
private: private:
// Transitive closure matrix // Transitive closure matrix
std::unordered_map<T, std::unordered_map<T, bool>> TC; std::unordered_map<T, std::unordered_map<T, bool>> TC;
@@ -64,6 +67,12 @@ bool Italiano<T>::query(const T& u, const T& v) {
return TC[u][v]; return TC[u][v];
} }
template<typename T>
void Italiano<T>::remove(const std::vector<std::pair<T, T>>& edges) {
for (const auto& [u, v] : edges)
remove(u, v);
}
template<typename T> template<typename T>
void Italiano<T>::remove(const T& u, const T& v) { void Italiano<T>::remove(const T& u, const T& v) {
if (!this->G.adjList[u].contains(v)) return; if (!this->G.adjList[u].contains(v)) return;

View File

@@ -23,12 +23,15 @@ public:
// in O(n) using the stored decremental maintenance data structure for DAGs // in O(n) using the stored decremental maintenance data structure for DAGs
bool query(const T& u, const T& v) override; bool query(const T& u, const T& v) override;
// Delete collection of edges
void remove(const std::vector<std::pair<T, T>>& edges) override;
// Delete edge e(u, v) from all reachability trees and update each one of // Delete edge e(u, v) from all reachability trees and update each one of
// them using the decremental reachability algorithm for DAGs // them using the decremental reachability algorithm for DAGs
void remove(const T& u, const T& v) override; void remove(const T& u, const T& v);
// Insert edge e(u, v) by reconstructing all reachability trees // Insert edge e(u, v) by reconstructing all reachability trees
void insert(const T& u, const T& v) override; void insert(const T& c, const std::vector<T>& vertices) override;
private: private:
// Connect each reachabiliy tree with decremental maintenance data structure // Connect each reachabiliy tree with decremental maintenance data structure
std::unordered_map<T, Italiano<T>> In; std::unordered_map<T, Italiano<T>> In;
@@ -53,6 +56,12 @@ bool King<T>::query(const T& u, const T& v) {
}); });
} }
template<typename T>
void King<T>::remove(const std::vector<std::pair<T, T>>& edges) {
for (const auto& [u, v] : edges)
remove(u, v);
}
template<typename T> template<typename T>
void King<T>::remove(const T& u, const T& v) { void King<T>::remove(const T& u, const T& v) {
this->G.remove(u, v); this->G.remove(u, v);
@@ -63,8 +72,9 @@ void King<T>::remove(const T& u, const T& v) {
} }
template<typename T> template<typename T>
void King<T>::insert(const T& u, const T& v) { void King<T>::insert(const T& c, const std::vector<T>& vertices) {
this->G.insert(u, v); for (const auto& v : vertices)
this->G.insert(c, v);
init(); init();
} }

View File

@@ -25,8 +25,11 @@ public:
// Return true if u and v are in the same SCC // Return true if u and v are in the same SCC
bool query(const T& u, const T& v) override; bool query(const T& u, const T& v) override;
// Delete collection of edges
void remove(const std::vector<std::pair<T, T>>& edges) override;
// Remove edge (u,v) and update A accordingly for fast checking query // Remove edge (u,v) and update A accordingly for fast checking query
void remove(const T& u, const T& v) override; void remove(const T& u, const T& v);
std::unordered_map<T, SCC<T>> getSCCs() { return C; } std::unordered_map<T, SCC<T>> getSCCs() { return C; }
private: private:
@@ -68,6 +71,12 @@ bool RodittyZwick<T>::query(const T& u, const T& v) {
return A[u] == A[v]; return A[u] == A[v];
} }
template<typename T>
void RodittyZwick<T>::remove(const std::vector<std::pair<T, T>>& edges) {
for (const auto& [u, v] : edges)
remove(u, v);
}
template<typename T> template<typename T>
void RodittyZwick<T>::remove(const T& u, const T& v) { void RodittyZwick<T>::remove(const T& u, const T& v) {
const auto& w = A[u]; const auto& w = A[u];

View File

@@ -60,7 +60,7 @@ TEST_SUITE("Dynamic Reachability Test") {
} }
SUBCASE("King::insert") { SUBCASE("King::insert") {
king.insert(3, 10); king.insert(3, { 10 });
CHECK_EQ(king.query(1, 10), true); CHECK_EQ(king.query(1, 10), true);
CHECK_EQ(king.query(2, 10), false); CHECK_EQ(king.query(2, 10), false);