Move headers into include folder and add single header to include all reachability algorithms
This commit is contained in:
113
include/algorithm/henzinger_king.h
Normal file
113
include/algorithm/henzinger_king.h
Normal file
@@ -0,0 +1,113 @@
|
||||
#ifndef HENZINGER_KING_H_
|
||||
#define HENZINGER_KING_H_
|
||||
|
||||
#include "algorithm/dynamic_reachability.h"
|
||||
#include "algorithm/frigioni.h"
|
||||
|
||||
using namespace graph;
|
||||
|
||||
constexpr int threshold = 5;
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class HenzingerKing : public DynamicReachability<T> {
|
||||
public:
|
||||
HenzingerKing() = default;
|
||||
|
||||
HenzingerKing(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;
|
||||
|
||||
// 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
|
||||
void insert(const T& c, const std::vector<T>& vertices);
|
||||
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::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 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);
|
||||
}
|
||||
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& 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);
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user