Use clang-format
This commit is contained in:
@@ -1,35 +1,38 @@
|
||||
#ifndef BREADTH_FIRST_SEARCH_H_
|
||||
#define BREADTH_FIRST_SEARCH_H_
|
||||
|
||||
#include "graph/graph.h"
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
|
||||
using namespace graph;
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class BreadthFirstSearch {
|
||||
template <typename T> class BreadthFirstSearch {
|
||||
public:
|
||||
BreadthFirstSearch() = default;
|
||||
|
||||
explicit BreadthFirstSearch(std::unordered_map<T, std::unordered_set<T>> adjList)
|
||||
: adjList(adjList) {}
|
||||
explicit BreadthFirstSearch(
|
||||
std::unordered_map<T, std::unordered_set<T>> adjList)
|
||||
: adjList(adjList) {}
|
||||
|
||||
// Traverse whole graph using the BFS search, and save the tree graph
|
||||
// which is created when visiting new vertices (Breadth First Tree)
|
||||
std::unordered_map<T, std::unordered_set<T>> execute(const T& root);
|
||||
std::unordered_map<T, std::unordered_set<T>> execute(const T &root);
|
||||
|
||||
// Search if target vertex exists in graph
|
||||
bool query(const T& root, const T& target);
|
||||
bool query(const T &root, const T &target);
|
||||
|
||||
private:
|
||||
// Represents the graph on which the algorithm will be executed
|
||||
std::unordered_map<T, std::unordered_set<T>> adjList;
|
||||
};
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
std::unordered_map<T, std::unordered_set<T>> BreadthFirstSearch<T>::execute(const T& root) {
|
||||
template <typename T>
|
||||
std::unordered_map<T, std::unordered_set<T>>
|
||||
BreadthFirstSearch<T>::execute(const T &root) {
|
||||
std::unordered_map<T, std::unordered_set<T>> tree;
|
||||
std::unordered_map<T, bool> visited;
|
||||
std::queue<T> Q;
|
||||
@@ -40,7 +43,7 @@ std::unordered_map<T, std::unordered_set<T>> BreadthFirstSearch<T>::execute(cons
|
||||
const auto v = Q.front();
|
||||
Q.pop();
|
||||
|
||||
for (const auto& u : adjList[v]) {
|
||||
for (const auto &u : adjList[v]) {
|
||||
if (!visited[u]) {
|
||||
visited[u] = true;
|
||||
tree[v].insert(u);
|
||||
@@ -52,8 +55,8 @@ std::unordered_map<T, std::unordered_set<T>> BreadthFirstSearch<T>::execute(cons
|
||||
return tree;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool BreadthFirstSearch<T>::query(const T& root, const T& target) {
|
||||
template <typename T>
|
||||
bool BreadthFirstSearch<T>::query(const T &root, const T &target) {
|
||||
std::unordered_map<T, bool> visited;
|
||||
std::queue<T> Q;
|
||||
Q.push(root);
|
||||
@@ -63,9 +66,10 @@ bool BreadthFirstSearch<T>::query(const T& root, const T& target) {
|
||||
const auto v = Q.front();
|
||||
Q.pop();
|
||||
|
||||
if (v == target) return true;
|
||||
if (v == target)
|
||||
return true;
|
||||
|
||||
for (const auto& u : adjList[v]) {
|
||||
for (const auto &u : adjList[v]) {
|
||||
if (!visited[u]) {
|
||||
visited[u] = true;
|
||||
Q.push(u);
|
||||
|
||||
@@ -4,21 +4,20 @@
|
||||
#include "graph/digraph.h"
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class DecrementalReachability {
|
||||
template <typename T> class DecrementalReachability {
|
||||
public:
|
||||
virtual ~DecrementalReachability() = default;
|
||||
|
||||
// This method is implemented and executed by all roditty and zwick
|
||||
// algorithms, it constructs the data structures used in other operations
|
||||
virtual void init() =0;
|
||||
virtual void init() = 0;
|
||||
|
||||
// Answer if vertex v is reachable from vertex u
|
||||
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
|
||||
virtual void remove(const std::vector<std::pair<T, T>>& edges) =0;
|
||||
virtual void remove(const std::vector<std::pair<T, T>> &edges) = 0;
|
||||
|
||||
protected:
|
||||
Digraph<T> G;
|
||||
};
|
||||
|
||||
@@ -4,15 +4,13 @@
|
||||
#include "algorithm/decremental_reachability.h"
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
class DynamicReachability : public DecrementalReachability<T> {
|
||||
|
||||
// Insert edge e(u,v) and maintain the transitive closure matrix
|
||||
virtual void insert(const T& c, const std::vector<T>& vertices) =0;
|
||||
virtual void insert(const T &c, const std::vector<T> &vertices) = 0;
|
||||
};
|
||||
|
||||
|
||||
} // namespace algo
|
||||
|
||||
#endif
|
||||
@@ -8,8 +8,7 @@
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class Frigioni : public DecrementalReachability<T> {
|
||||
template <typename T> class Frigioni : public DecrementalReachability<T> {
|
||||
public:
|
||||
Frigioni() = default;
|
||||
|
||||
@@ -20,10 +19,11 @@ public:
|
||||
|
||||
// Execute reachability query q(u, v) from vertex u to vertex v
|
||||
// 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 set of edges and explicitely maintain the transitive closure
|
||||
void remove(const std::vector<std::pair<T, T>>& edges) override;
|
||||
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;
|
||||
@@ -46,161 +46,162 @@ private:
|
||||
RodittyZwick<T> rodittyZwick;
|
||||
|
||||
// Delete internal edge e(u, v)
|
||||
void removeInternal(const T& u, const T& v);
|
||||
void removeInternal(const T &u, const T &v);
|
||||
|
||||
// Delete external edge e(u, v)
|
||||
void removeExternal(const T& u, const T& v);
|
||||
void removeExternal(const T &u, const T &v);
|
||||
|
||||
// Repair reachability trees
|
||||
void repairTrees();
|
||||
|
||||
//
|
||||
void splitEdges(std::unordered_map<T, SCC<T>>& L, std::unordered_map<T, SCC<T>> C, const T& w);
|
||||
//
|
||||
void splitEdges(std::unordered_map<T, SCC<T>> &L,
|
||||
std::unordered_map<T, SCC<T>> C, const T &w);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void Frigioni<T>::init() {
|
||||
template <typename T> void Frigioni<T>::init() {
|
||||
rodittyZwick = RodittyZwick<T>(this->G);
|
||||
rodittyZwick.init();
|
||||
auto C = rodittyZwick.getComponentMap();
|
||||
|
||||
for (const auto& w : std::views::keys(C)) {
|
||||
for (const auto &w : std::views::keys(C)) {
|
||||
RT[C[w]] = BreadthFirstTree<T>(this->G, w);
|
||||
for (const auto& u : this->G.vertices()) {
|
||||
for (const auto& v : this->G.adjList[u]) {
|
||||
for (const auto &u : this->G.vertices()) {
|
||||
for (const auto &v : this->G.adjList[u]) {
|
||||
if (!C[w].contains(u) && C[w].contains(v))
|
||||
E[C[w]].inc.insert({ u, v });
|
||||
E[C[w]].inc.insert({u, v});
|
||||
else if (C[w].contains(u) && !C[w].contains(v))
|
||||
E[C[w]].out.insert({ u, v });
|
||||
E[C[w]].out.insert({u, v});
|
||||
else
|
||||
E[C[w]].in.insert({ u, v });
|
||||
E[C[w]].in.insert({u, v});
|
||||
TC[u][v] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto& w : std::views::keys(C)) {
|
||||
for (const auto& u : C[w].vertices()) {
|
||||
for (const auto& v : RT[C[w]].vertices()) {
|
||||
for (const auto &w : std::views::keys(C)) {
|
||||
for (const auto &u : C[w].vertices()) {
|
||||
for (const auto &v : RT[C[w]].vertices()) {
|
||||
TC[u][v] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Frigioni<T>::query(const T& u, const T& v) {
|
||||
template <typename T> bool Frigioni<T>::query(const T &u, const T &v) {
|
||||
return TC[u][v];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
||||
template <typename T>
|
||||
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>> Eext;
|
||||
|
||||
for (const auto& [u, v] : edges) {
|
||||
if (!this->G.adjList[u].contains(v)) continue;
|
||||
for (const auto &[u, v] : edges) {
|
||||
if (!this->G.adjList[u].contains(v))
|
||||
continue;
|
||||
|
||||
if (rodittyZwick.query(u, v))
|
||||
Eint.push_back({ u, v });
|
||||
Eint.push_back({u, v});
|
||||
else
|
||||
Eext.push_back({ u, v });
|
||||
Eext.push_back({u, v});
|
||||
}
|
||||
for (const auto& [u, v] : Eint) removeInternal(u, v);
|
||||
for (const auto& [u, v] : Eext) removeExternal(u, v);
|
||||
for (const auto &[u, v] : Eint)
|
||||
removeInternal(u, v);
|
||||
for (const auto &[u, v] : Eext)
|
||||
removeExternal(u, v);
|
||||
|
||||
repairTrees();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Frigioni<T>::removeInternal(const T& u, const T& v) {
|
||||
template <typename T> void Frigioni<T>::removeInternal(const T &u, const T &v) {
|
||||
this->G.remove(u, v);
|
||||
auto L = rodittyZwick.getComponentMap();
|
||||
rodittyZwick.remove(u, v);
|
||||
auto C = rodittyZwick.getComponentMap();
|
||||
|
||||
if (rodittyZwick.query(u, v)) {
|
||||
E[C[u]].in.erase({ u, v });
|
||||
E[C[u]].in.erase({u, v});
|
||||
return;
|
||||
}
|
||||
|
||||
if (L[u] == C[u]) {
|
||||
E[L[u]].out.erase({ u, v });
|
||||
E[L[u]].out.erase({u, v});
|
||||
E.erase(L[v]);
|
||||
splitEdges(L, C, v);
|
||||
}
|
||||
else {
|
||||
E[L[v]].inc.erase({ u, v });
|
||||
} else {
|
||||
E[L[v]].inc.erase({u, v});
|
||||
E.erase(L[u]);
|
||||
splitEdges(L, C, u);
|
||||
}
|
||||
|
||||
for (const auto& w : std::views::keys(C)) {
|
||||
if (!RT[C[w]].contains(v)) continue;
|
||||
for (const auto &w : std::views::keys(C)) {
|
||||
if (!RT[C[w]].contains(v))
|
||||
continue;
|
||||
|
||||
RT[C[w]].removeEdgeTo(v);
|
||||
if (E[C[v]].inc.size() > 0) {
|
||||
H[C[w]].push(v); /*h mipos C[v].id?*/
|
||||
continue;
|
||||
}
|
||||
for (const auto& x : C[w].vertices()) {
|
||||
for (const auto& y : C[v].vertices()) {
|
||||
for (const auto &x : C[w].vertices()) {
|
||||
for (const auto &y : C[v].vertices()) {
|
||||
TC[x][y] = false;
|
||||
}
|
||||
}
|
||||
for (const auto& [a, b] : E[C[v]].out)
|
||||
for (const auto &[a, b] : E[C[v]].out)
|
||||
H[C[w]].push(b);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Frigioni<T>::removeExternal(const T& u, const T& v) {
|
||||
template <typename T> void Frigioni<T>::removeExternal(const T &u, const T &v) {
|
||||
this->G.remove(u, v);
|
||||
auto C = rodittyZwick.getComponentMap();
|
||||
E[C[v]].inc.erase({ u, v });
|
||||
E[C[u]].out.erase({ u, v });
|
||||
E[C[v]].inc.erase({u, v});
|
||||
E[C[u]].out.erase({u, v});
|
||||
|
||||
for (const auto& w : std::views::keys(C)) {
|
||||
if (!RT[C[w]].contains(v)) continue;
|
||||
for (const auto &w : std::views::keys(C)) {
|
||||
if (!RT[C[w]].contains(v))
|
||||
continue;
|
||||
|
||||
RT[C[w]].removeEdgeTo(v);
|
||||
if (E[C[v]].inc.size() > 0) {
|
||||
H[C[w]].push(v);
|
||||
continue;
|
||||
}
|
||||
for (const auto& x : C[w].vertices()) {
|
||||
for (const auto& y : C[v].vertices()) {
|
||||
for (const auto &x : C[w].vertices()) {
|
||||
for (const auto &y : C[v].vertices()) {
|
||||
TC[x][y] = false;
|
||||
}
|
||||
}
|
||||
for (const auto& [a, b] : E[C[v]].out)
|
||||
for (const auto &[a, b] : E[C[v]].out)
|
||||
H[C[w]].push(b);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Frigioni<T>::repairTrees() {
|
||||
template <typename T> void Frigioni<T>::repairTrees() {
|
||||
auto C = rodittyZwick.getComponentMap();
|
||||
for (const auto& w : std::views::keys(C)) {
|
||||
for (const auto &w : std::views::keys(C)) {
|
||||
while (H[C[w]].size() > 0) {
|
||||
const auto& h = H[C[w]].top();
|
||||
const auto &h = H[C[w]].top();
|
||||
H[C[w]].pop();
|
||||
bool foundHook = false;
|
||||
|
||||
for (const auto& [a, b] : E[C[h]].inc) {
|
||||
for (const auto &[a, b] : E[C[h]].inc) {
|
||||
if (RT[C[w]].adjList[w].contains(a)) {
|
||||
RT[C[w]].insert(a, h);
|
||||
foundHook = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundHook) continue;
|
||||
if (foundHook)
|
||||
continue;
|
||||
|
||||
for (const auto& x : C[w].vertices()) {
|
||||
for (const auto& y : C[h].vertices()) {
|
||||
for (const auto &x : C[w].vertices()) {
|
||||
for (const auto &y : C[h].vertices()) {
|
||||
TC[x][y] = false;
|
||||
}
|
||||
}
|
||||
for (const auto& [a, b] : E[C[h]].out) {
|
||||
for (const auto &[a, b] : E[C[h]].out) {
|
||||
if (RT[C[w]].adjList[h].contains(a)) {
|
||||
H[C[w]].push(b);
|
||||
}
|
||||
@@ -209,21 +210,21 @@ void Frigioni<T>::repairTrees() {
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Frigioni<T>::splitEdges(std::unordered_map<T, SCC<T>>& L, std::unordered_map<T, SCC<T>> C, const T& w) {
|
||||
for (const auto& [a, b] : E[L[w]].inc)
|
||||
E[C[b]].inc.insert({ a, b });
|
||||
template <typename T>
|
||||
void Frigioni<T>::splitEdges(std::unordered_map<T, SCC<T>> &L,
|
||||
std::unordered_map<T, SCC<T>> C, const T &w) {
|
||||
for (const auto &[a, b] : E[L[w]].inc)
|
||||
E[C[b]].inc.insert({a, b});
|
||||
|
||||
for (const auto& [a, b] : E[L[w]].out)
|
||||
E[C[a]].out.insert({ a, b });
|
||||
for (const auto &[a, b] : E[L[w]].out)
|
||||
E[C[a]].out.insert({a, b});
|
||||
|
||||
for (const auto& [a, b] : E[L[w]].in) {
|
||||
for (const auto &[a, b] : E[L[w]].in) {
|
||||
if (C[a] == C[b]) {
|
||||
E[C[a]].in.insert({ a, b });
|
||||
}
|
||||
else {
|
||||
E[C[a]].out.insert({ a, b });
|
||||
E[C[b]].in.insert({ a, b });
|
||||
E[C[a]].in.insert({a, b});
|
||||
} else {
|
||||
E[C[a]].out.insert({a, b});
|
||||
E[C[b]].in.insert({a, b});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,7 @@ constexpr int threshold = 5;
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class HenzingerKing : public DynamicReachability<T> {
|
||||
template <typename T> class HenzingerKing : public DynamicReachability<T> {
|
||||
public:
|
||||
HenzingerKing() = default;
|
||||
|
||||
@@ -21,16 +20,17 @@ public:
|
||||
// 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;
|
||||
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);
|
||||
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);
|
||||
void insert(const T &c, const std::vector<T> &vertices) override;
|
||||
|
||||
private:
|
||||
// Decremental maintenance data structure
|
||||
Frigioni<T> frigioni;
|
||||
@@ -43,41 +43,37 @@ private:
|
||||
std::unordered_map<T, BreadthFirstTree<T>> Out;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void HenzingerKing<T>::init() {
|
||||
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) {
|
||||
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);
|
||||
});
|
||||
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) {
|
||||
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) {
|
||||
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)
|
||||
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);
|
||||
|
||||
@@ -7,8 +7,7 @@
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class Italiano : public DecrementalReachability<T> {
|
||||
template <typename T> class Italiano : public DecrementalReachability<T> {
|
||||
public:
|
||||
Italiano() = default;
|
||||
|
||||
@@ -19,17 +18,18 @@ public:
|
||||
|
||||
// Execute reachability query q(u, v) from vertex u to vertex v
|
||||
// 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;
|
||||
void remove(const std::vector<std::pair<T, T>> &edges) override;
|
||||
|
||||
// Delete edge e(u, v) and explicitly maintain the transitive closure
|
||||
void remove(const T& u, const T& v);
|
||||
void remove(const T &u, const T &v);
|
||||
|
||||
private:
|
||||
// Transitive closure matrix
|
||||
std::unordered_map<T, std::unordered_map<T, bool>> TC;
|
||||
|
||||
|
||||
// For each vertex, store a reachability tree created as a BFS tree
|
||||
std::unordered_map<T, BreadthFirstTree<T>> RT;
|
||||
|
||||
@@ -47,42 +47,41 @@ private:
|
||||
void repairTrees();
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void Italiano<T>::init() {
|
||||
for (const auto& u : this->G.vertices()) {
|
||||
for (const auto& v : this->G.adjList[u]) {
|
||||
template <typename T> void Italiano<T>::init() {
|
||||
for (const auto &u : this->G.vertices()) {
|
||||
for (const auto &v : this->G.adjList[u]) {
|
||||
E[v].inc.insert(u);
|
||||
E[u].out.insert(v);
|
||||
TC[u][v] = false;
|
||||
}
|
||||
RT[u] = BreadthFirstTree<T>(this->G, u);
|
||||
TC[u][u] = true;
|
||||
for (const auto& v : RT[u].vertices())
|
||||
for (const auto &v : RT[u].vertices())
|
||||
TC[u][v] = true;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Italiano<T>::query(const T& u, const T& v) {
|
||||
template <typename T> 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) {
|
||||
if (!this->G.adjList[u].contains(v)) continue;
|
||||
template <typename T>
|
||||
void Italiano<T>::remove(const std::vector<std::pair<T, T>> &edges) {
|
||||
for (const auto &[u, v] : edges) {
|
||||
if (!this->G.adjList[u].contains(v))
|
||||
continue;
|
||||
remove(u, v);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Italiano<T>::remove(const T& u, const T& v) {
|
||||
template <typename T> void Italiano<T>::remove(const T &u, const T &v) {
|
||||
this->G.remove(u, v);
|
||||
E[u].out.erase(v);
|
||||
E[v].inc.erase(u);
|
||||
|
||||
for (const auto& w : this->G.vertices()) {
|
||||
if (!RT[w].adjList[u].contains(v)) continue;
|
||||
|
||||
for (const auto &w : this->G.vertices()) {
|
||||
if (!RT[w].adjList[u].contains(v))
|
||||
continue;
|
||||
|
||||
RT[w].adjList[u].erase(v);
|
||||
if (E[v].inc.size() > 0) {
|
||||
@@ -90,31 +89,31 @@ void Italiano<T>::remove(const T& u, const T& v) {
|
||||
continue;
|
||||
}
|
||||
TC[w][v] = false;
|
||||
for (const auto& c : E[v].out)
|
||||
for (const auto &c : E[v].out)
|
||||
H[w].push(c);
|
||||
}
|
||||
repairTrees();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Italiano<T>::repairTrees() {
|
||||
for (const auto& w : this->G.vertices()) {
|
||||
template <typename T> void Italiano<T>::repairTrees() {
|
||||
for (const auto &w : this->G.vertices()) {
|
||||
while (H[w].size() > 0) {
|
||||
const auto& h = H[w].top();
|
||||
const auto &h = H[w].top();
|
||||
H[w].pop();
|
||||
bool foundHook = false;
|
||||
|
||||
for (const auto& i : E[h].inc) {
|
||||
for (const auto &i : E[h].inc) {
|
||||
if (RT[w].adjList[w].contains(i)) {
|
||||
RT[w].adjList[i].insert(h);
|
||||
foundHook = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundHook) continue;
|
||||
if (foundHook)
|
||||
continue;
|
||||
|
||||
TC[w][h] = false;
|
||||
for (const auto& o : E[h].out) {
|
||||
for (const auto &o : E[h].out) {
|
||||
if (RT[w].adjList[h].contains(o)) {
|
||||
H[w].push(o);
|
||||
}
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class King : public DynamicReachability<T> {
|
||||
template <typename T> class King : public DynamicReachability<T> {
|
||||
public:
|
||||
King() = default;
|
||||
|
||||
@@ -19,26 +18,26 @@ public:
|
||||
|
||||
// Execute reachability query q(u, v) from vertex u to vertex v
|
||||
// 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;
|
||||
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);
|
||||
void remove(const T &u, const T &v);
|
||||
|
||||
// Insert edge e(u, v) by reconstructing all reachability trees
|
||||
void insert(const T& c, const std::vector<T>& vertices) 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;
|
||||
std::unordered_map<T, Italiano<T>> Out;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void King<T>::init() {
|
||||
for (const auto& u : this->G.vertices()) {
|
||||
template <typename T> void King<T>::init() {
|
||||
for (const auto &u : this->G.vertices()) {
|
||||
In[u] = Italiano<T>(BreadthFirstTree<T>(this->G.reverse(), u));
|
||||
In[u].init();
|
||||
Out[u] = Italiano<T>(BreadthFirstTree<T>(this->G, u));
|
||||
@@ -46,32 +45,29 @@ void King<T>::init() {
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool King<T>::query(const T& u, const T& v) {
|
||||
return std::ranges::any_of(this->G.vertices().begin(), this->G.vertices().end(),
|
||||
[&](const T& w) {
|
||||
return In[w].query(w, u) && Out[w].query(w, v);
|
||||
});
|
||||
template <typename T> bool King<T>::query(const T &u, const T &v) {
|
||||
return std::any_of(
|
||||
this->G.vertices().begin(), this->G.vertices().end(),
|
||||
[&](const T &w) { return In[w].query(w, u) && Out[w].query(w, v); });
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void King<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
||||
for (const auto& [u, v] : edges)
|
||||
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) {
|
||||
template <typename T> void King<T>::remove(const T &u, const T &v) {
|
||||
this->G.remove(u, v);
|
||||
for (const auto& w : this->G.vertices()) {
|
||||
for (const auto &w : this->G.vertices()) {
|
||||
In[w].remove(v, u);
|
||||
Out[w].remove(u, v);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void King<T>::insert(const T& c, const std::vector<T>& vertices) {
|
||||
for (const auto& v : vertices) {
|
||||
template <typename T>
|
||||
void King<T>::insert(const T &c, const std::vector<T> &vertices) {
|
||||
for (const auto &v : vertices) {
|
||||
this->G.insert(c, v);
|
||||
}
|
||||
In[c] = Italiano<T>(BreadthFirstTree<T>(this->G.reverse(), c));
|
||||
|
||||
@@ -7,8 +7,7 @@
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class RodittyZwick : public DecrementalReachability<T> {
|
||||
template <typename T> class RodittyZwick : public DecrementalReachability<T> {
|
||||
public:
|
||||
RodittyZwick() = default;
|
||||
|
||||
@@ -21,15 +20,16 @@ public:
|
||||
void findSCC(graph::Digraph<T> G);
|
||||
|
||||
// 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;
|
||||
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);
|
||||
void remove(const T &u, const T &v);
|
||||
|
||||
std::unordered_map<T, SCC<T>> getComponentMap() { return C; }
|
||||
|
||||
private:
|
||||
// Array used to answer strong connectivity queries in O(1) time
|
||||
std::unordered_map<T, T> A;
|
||||
@@ -42,21 +42,17 @@ private:
|
||||
std::unordered_map<T, BreadthFirstTree<T>> Out;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void RodittyZwick<T>::init() {
|
||||
findSCC(this->G);
|
||||
}
|
||||
template <typename T> void RodittyZwick<T>::init() { findSCC(this->G); }
|
||||
|
||||
template<typename T>
|
||||
void RodittyZwick<T>::findSCC(graph::Digraph<T> G) {
|
||||
template <typename T> void RodittyZwick<T>::findSCC(graph::Digraph<T> G) {
|
||||
auto SCCs = Tarjan<T>(G.adjList).execute();
|
||||
|
||||
for (auto& c : SCCs) {
|
||||
const auto& w = c.id;
|
||||
|
||||
for (const auto& v : c.vertices())
|
||||
for (auto &c : SCCs) {
|
||||
const auto &w = c.id;
|
||||
|
||||
for (const auto &v : c.vertices())
|
||||
A[v] = w;
|
||||
|
||||
|
||||
Out[w] = BreadthFirstTree<T>(c, w);
|
||||
In[w] = BreadthFirstTree<T>(c.reverse(), w);
|
||||
|
||||
@@ -64,20 +60,18 @@ void RodittyZwick<T>::findSCC(graph::Digraph<T> G) {
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool RodittyZwick<T>::query(const T& u, const T& v) {
|
||||
template <typename T> 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)
|
||||
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];
|
||||
template <typename T> void RodittyZwick<T>::remove(const T &u, const T &v) {
|
||||
const auto &w = A[u];
|
||||
C[w].remove(u, v);
|
||||
this->G.remove(u, v);
|
||||
|
||||
@@ -85,8 +79,9 @@ void RodittyZwick<T>::remove(const T& u, const T& v) {
|
||||
if (A[u] != A[v])
|
||||
return;
|
||||
|
||||
// If edge (u,v) is not contained in both inTree and outTree do nothing TODO:remove useless comments
|
||||
// is this not better if i utilize A matrix, since we are going traversing between components.. ? TODO
|
||||
// If edge (u,v) is not contained in both inTree and outTree do nothing
|
||||
// TODO:remove useless comments is this not better if i utilize A matrix,
|
||||
// since we are going traversing between components.. ? TODO
|
||||
if (!In[w].adjList[u].contains(v) && !Out[w].adjList[u].contains(v))
|
||||
return;
|
||||
|
||||
|
||||
@@ -3,24 +3,25 @@
|
||||
|
||||
#include "graph/scc.h"
|
||||
|
||||
#include <ranges>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
#include <ranges>
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class Tarjan {
|
||||
template <typename T> class Tarjan {
|
||||
public:
|
||||
Tarjan() = default;
|
||||
|
||||
explicit Tarjan(std::unordered_map<T, std::unordered_set<T>> adjList) : adjList(adjList) {}
|
||||
explicit Tarjan(std::unordered_map<T, std::unordered_set<T>> adjList)
|
||||
: adjList(adjList) {}
|
||||
|
||||
//
|
||||
auto execute();
|
||||
|
||||
|
||||
//
|
||||
void strongConnect(const T& u);
|
||||
void strongConnect(const T &u);
|
||||
|
||||
private:
|
||||
std::unordered_map<T, std::unordered_set<T>> adjList;
|
||||
std::stack<T> S;
|
||||
@@ -36,13 +37,12 @@ private:
|
||||
std::unordered_map<T, Vertex> V;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void Tarjan<T>::strongConnect(const T& u) {
|
||||
template <typename T> void Tarjan<T>::strongConnect(const T &u) {
|
||||
V[u].index = V[u].lowlink = index++;
|
||||
S.push(u);
|
||||
V[u].onStack = true;
|
||||
|
||||
for (const auto& w : adjList[u]) {
|
||||
for (const auto &w : adjList[u]) {
|
||||
if (V[w].index == -1) {
|
||||
strongConnect(w);
|
||||
V[u].lowlink = std::min(V[u].lowlink, V[w].lowlink);
|
||||
@@ -64,14 +64,13 @@ void Tarjan<T>::strongConnect(const T& u) {
|
||||
scc[w] = adjList[w];
|
||||
finished = (w == u);
|
||||
} while (!finished);
|
||||
|
||||
SCCs.push_back({ scc, static_cast<T>(cid) });
|
||||
|
||||
SCCs.push_back({scc, static_cast<T>(cid)});
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
auto Tarjan<T>::execute() {
|
||||
for (const auto& u : std::views::keys(adjList)) {
|
||||
template <typename T> auto Tarjan<T>::execute() {
|
||||
for (const auto &u : std::views::keys(adjList)) {
|
||||
if (V[u].index == -1)
|
||||
strongConnect(u);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user