Refactor: Change reachability interfaces to handle multiple removals/insertions
This commit is contained in:
@@ -18,7 +18,7 @@ public:
|
||||
virtual bool query(const T& u, const T& v) =0;
|
||||
|
||||
// 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:
|
||||
Digraph<T> G;
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ template<typename T>
|
||||
class DynamicReachability : public DecrementalReachability<T> {
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -24,11 +24,8 @@ public:
|
||||
// in O(1) using the transitive closure matrix
|
||||
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
|
||||
void remove(const std::vector<std::pair<T, T>>& edges);
|
||||
void remove(const std::vector<std::pair<T, T>>& edges) override;
|
||||
private:
|
||||
// Transitive closure matrix, used to answer reachability queries in O(1)
|
||||
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];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Frigioni<T>::remove(const T& u, const T& v) {
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
||||
std::vector<std::pair<T, T>> Eint;
|
||||
|
||||
@@ -25,16 +25,10 @@ public:
|
||||
// and then if necessary check the set S
|
||||
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
|
||||
// and for every vertex in set S, rebuilt reachability trees from scratch
|
||||
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
|
||||
// algorithm, otherwise construct reachability trees for the vertex that is
|
||||
// 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>
|
||||
void HenzingerKing<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
||||
for (const auto& [u, v] : edges) {
|
||||
remove(u, v);
|
||||
this->G.remove(u, v);
|
||||
}
|
||||
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>
|
||||
void HenzingerKing<T>::insert(const T& c, const std::vector<T>& vertices) {
|
||||
for (const auto& w : vertices)
|
||||
insert(c, w);
|
||||
this->G.insert(c, w);
|
||||
|
||||
S.insert(c);
|
||||
if (S.size() > threshold) {
|
||||
|
||||
@@ -24,8 +24,11 @@ public:
|
||||
// in O(1) using the transitive closure matrix
|
||||
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
|
||||
void remove(const T& u, const T& v) override;
|
||||
void remove(const T& u, const T& v);
|
||||
private:
|
||||
// Transitive closure matrix
|
||||
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];
|
||||
}
|
||||
|
||||
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>
|
||||
void Italiano<T>::remove(const T& u, const T& v) {
|
||||
if (!this->G.adjList[u].contains(v)) return;
|
||||
|
||||
@@ -23,12 +23,15 @@ public:
|
||||
// in O(n) using the stored decremental maintenance data structure for DAGs
|
||||
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
|
||||
// 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
|
||||
void insert(const T& u, const T& v) override;
|
||||
void insert(const T& c, const std::vector<T>& vertices) override;
|
||||
private:
|
||||
// Connect each reachabiliy tree with decremental maintenance data structure
|
||||
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>
|
||||
void King<T>::remove(const T& u, const T& v) {
|
||||
this->G.remove(u, v);
|
||||
@@ -63,8 +72,9 @@ void King<T>::remove(const T& u, const T& v) {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void King<T>::insert(const T& u, const T& v) {
|
||||
this->G.insert(u, v);
|
||||
void King<T>::insert(const T& c, const std::vector<T>& vertices) {
|
||||
for (const auto& v : vertices)
|
||||
this->G.insert(c, v);
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
@@ -25,8 +25,11 @@ public:
|
||||
// Return true if u and v are in the same SCC
|
||||
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
|
||||
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; }
|
||||
private:
|
||||
@@ -68,6 +71,12 @@ bool RodittyZwick<T>::query(const T& u, const T& 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>
|
||||
void RodittyZwick<T>::remove(const T& u, const T& v) {
|
||||
const auto& w = A[u];
|
||||
|
||||
@@ -60,7 +60,7 @@ TEST_SUITE("Dynamic Reachability Test") {
|
||||
}
|
||||
|
||||
SUBCASE("King::insert") {
|
||||
king.insert(3, 10);
|
||||
king.insert(3, { 10 });
|
||||
|
||||
CHECK_EQ(king.query(1, 10), true);
|
||||
CHECK_EQ(king.query(2, 10), false);
|
||||
|
||||
Reference in New Issue
Block a user