91 lines
2.5 KiB
C++
91 lines
2.5 KiB
C++
#ifndef HENZINGER_KING_H_
|
|
#define HENZINGER_KING_H_
|
|
|
|
#include "algorithm/dynamic_reachability.h"
|
|
#include "algorithm/frigioni.h"
|
|
|
|
constexpr int threshold = 5;
|
|
|
|
namespace algo {
|
|
|
|
template <typename T> class HenzingerKing : public DynamicReachability<T> {
|
|
public:
|
|
HenzingerKing() = default;
|
|
|
|
explicit HenzingerKing(graph::Digraph<T> G) { this->G = G; }
|
|
|
|
// Initialize decremental maintenance data structure and the empty set S
|
|
void init() override;
|
|
|
|
// Execute query q(u, v) from vertex u to vertex v by querying the
|
|
// decremental reachability data structure at the start of the phase
|
|
// and then if necessary check the set S
|
|
bool query(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) 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
|
|
void insert(const T &c, const std::vector<T> &vertices) override;
|
|
|
|
private:
|
|
// Decremental maintenance data structure
|
|
Frigioni<T> frigioni;
|
|
|
|
// Collection of vertices that have been centers of insertions in this phase
|
|
std::set<T> S;
|
|
|
|
// Maintain in-out bfs trees
|
|
std::unordered_map<T, BreadthFirstTree<T>> In;
|
|
std::unordered_map<T, BreadthFirstTree<T>> Out;
|
|
};
|
|
|
|
template <typename T> void HenzingerKing<T>::init() {
|
|
S.clear();
|
|
frigioni = Frigioni<T>(this->G);
|
|
frigioni.init();
|
|
}
|
|
|
|
template <typename T> bool HenzingerKing<T>::query(const T &u, const T &v) {
|
|
if (frigioni.query(u, v))
|
|
return true;
|
|
|
|
return std::ranges::any_of(S.begin(), S.end(), [&](const T &w) {
|
|
return In[w].adjList.contains(u) && Out[w].adjList.contains(v);
|
|
});
|
|
}
|
|
|
|
template <typename T>
|
|
void HenzingerKing<T>::remove(const std::vector<std::pair<T, T>> &edges) {
|
|
for (const auto &[u, v] : edges) {
|
|
this->G.remove(u, v);
|
|
}
|
|
frigioni.remove(edges);
|
|
|
|
for (const auto &w : S) {
|
|
In[w] = BreadthFirstTree(this->G.reverse(), w);
|
|
Out[w] = BreadthFirstTree(this->G, w);
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
void HenzingerKing<T>::insert(const T &c, const std::vector<T> &vertices) {
|
|
for (const auto &w : vertices)
|
|
this->G.insert(c, w);
|
|
|
|
S.insert(c);
|
|
if (S.size() > threshold) {
|
|
init();
|
|
return;
|
|
}
|
|
|
|
In[c] = BreadthFirstTree(this->G.reverse(), c);
|
|
Out[c] = BreadthFirstTree(this->G, c);
|
|
}
|
|
|
|
} // namespace algo
|
|
|
|
#endif |