Use clang-format
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
#include <src/include/nanobench.h>
|
#include <src/include/nanobench.h>
|
||||||
#include <doctest/doctest.h>
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
@@ -8,6 +9,7 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,21 @@
|
|||||||
#ifndef BREADTH_FIRST_SEARCH_H_
|
#ifndef BREADTH_FIRST_SEARCH_H_
|
||||||
#define BREADTH_FIRST_SEARCH_H_
|
#define BREADTH_FIRST_SEARCH_H_
|
||||||
|
|
||||||
|
#include "graph/graph.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class BreadthFirstSearch {
|
||||||
class BreadthFirstSearch {
|
|
||||||
public:
|
public:
|
||||||
BreadthFirstSearch() = default;
|
BreadthFirstSearch() = default;
|
||||||
|
|
||||||
explicit BreadthFirstSearch(std::unordered_map<T, std::unordered_set<T>> adjList)
|
explicit BreadthFirstSearch(
|
||||||
|
std::unordered_map<T, std::unordered_set<T>> adjList)
|
||||||
: adjList(adjList) {}
|
: adjList(adjList) {}
|
||||||
|
|
||||||
// Traverse whole graph using the BFS search, and save the tree graph
|
// Traverse whole graph using the BFS search, and save the tree graph
|
||||||
@@ -23,13 +24,15 @@ public:
|
|||||||
|
|
||||||
// Search if target vertex exists in graph
|
// Search if target vertex exists in graph
|
||||||
bool query(const T &root, const T &target);
|
bool query(const T &root, const T &target);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Represents the graph on which the algorithm will be executed
|
// Represents the graph on which the algorithm will be executed
|
||||||
std::unordered_map<T, std::unordered_set<T>> adjList;
|
std::unordered_map<T, std::unordered_set<T>> adjList;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
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>>
|
||||||
|
BreadthFirstSearch<T>::execute(const T &root) {
|
||||||
std::unordered_map<T, std::unordered_set<T>> tree;
|
std::unordered_map<T, std::unordered_set<T>> tree;
|
||||||
std::unordered_map<T, bool> visited;
|
std::unordered_map<T, bool> visited;
|
||||||
std::queue<T> Q;
|
std::queue<T> Q;
|
||||||
@@ -63,7 +66,8 @@ bool BreadthFirstSearch<T>::query(const T& root, const T& target) {
|
|||||||
const auto v = Q.front();
|
const auto v = Q.front();
|
||||||
Q.pop();
|
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]) {
|
if (!visited[u]) {
|
||||||
|
|||||||
@@ -4,9 +4,7 @@
|
|||||||
#include "graph/digraph.h"
|
#include "graph/digraph.h"
|
||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
|
template <typename T> class DecrementalReachability {
|
||||||
template<typename T>
|
|
||||||
class DecrementalReachability {
|
|
||||||
public:
|
public:
|
||||||
virtual ~DecrementalReachability() = default;
|
virtual ~DecrementalReachability() = default;
|
||||||
|
|
||||||
@@ -19,6 +17,7 @@ public:
|
|||||||
|
|
||||||
// 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 std::vector<std::pair<T, T>> &edges) = 0;
|
virtual void remove(const std::vector<std::pair<T, T>> &edges) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Digraph<T> G;
|
Digraph<T> G;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include "algorithm/decremental_reachability.h"
|
#include "algorithm/decremental_reachability.h"
|
||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class DynamicReachability : public DecrementalReachability<T> {
|
class DynamicReachability : public DecrementalReachability<T> {
|
||||||
|
|
||||||
@@ -12,7 +11,6 @@ class DynamicReachability : public DecrementalReachability<T> {
|
|||||||
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
|
} // namespace algo
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -8,8 +8,7 @@
|
|||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class Frigioni : public DecrementalReachability<T> {
|
||||||
class Frigioni : public DecrementalReachability<T> {
|
|
||||||
public:
|
public:
|
||||||
Frigioni() = default;
|
Frigioni() = default;
|
||||||
|
|
||||||
@@ -24,6 +23,7 @@ public:
|
|||||||
|
|
||||||
// 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) override;
|
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;
|
||||||
@@ -55,11 +55,11 @@ private:
|
|||||||
void repairTrees();
|
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>
|
template <typename T> void Frigioni<T>::init() {
|
||||||
void Frigioni<T>::init() {
|
|
||||||
rodittyZwick = RodittyZwick<T>(this->G);
|
rodittyZwick = RodittyZwick<T>(this->G);
|
||||||
rodittyZwick.init();
|
rodittyZwick.init();
|
||||||
auto C = rodittyZwick.getComponentMap();
|
auto C = rodittyZwick.getComponentMap();
|
||||||
@@ -87,8 +87,7 @@ void Frigioni<T>::init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> bool Frigioni<T>::query(const T &u, const T &v) {
|
||||||
bool Frigioni<T>::query(const T& u, const T& v) {
|
|
||||||
return TC[u][v];
|
return TC[u][v];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,21 +97,23 @@ void Frigioni<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
|||||||
std::vector<std::pair<T, T>> Eext;
|
std::vector<std::pair<T, T>> Eext;
|
||||||
|
|
||||||
for (const auto &[u, v] : edges) {
|
for (const auto &[u, v] : edges) {
|
||||||
if (!this->G.adjList[u].contains(v)) continue;
|
if (!this->G.adjList[u].contains(v))
|
||||||
|
continue;
|
||||||
|
|
||||||
if (rodittyZwick.query(u, v))
|
if (rodittyZwick.query(u, v))
|
||||||
Eint.push_back({u, v});
|
Eint.push_back({u, v});
|
||||||
else
|
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] : Eint)
|
||||||
for (const auto& [u, v] : Eext) removeExternal(u, v);
|
removeInternal(u, v);
|
||||||
|
for (const auto &[u, v] : Eext)
|
||||||
|
removeExternal(u, v);
|
||||||
|
|
||||||
repairTrees();
|
repairTrees();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void Frigioni<T>::removeInternal(const T &u, const T &v) {
|
||||||
void Frigioni<T>::removeInternal(const T& u, const T& v) {
|
|
||||||
this->G.remove(u, v);
|
this->G.remove(u, v);
|
||||||
auto L = rodittyZwick.getComponentMap();
|
auto L = rodittyZwick.getComponentMap();
|
||||||
rodittyZwick.remove(u, v);
|
rodittyZwick.remove(u, v);
|
||||||
@@ -127,15 +128,15 @@ void Frigioni<T>::removeInternal(const T& u, const T& v) {
|
|||||||
E[L[u]].out.erase({u, v});
|
E[L[u]].out.erase({u, v});
|
||||||
E.erase(L[v]);
|
E.erase(L[v]);
|
||||||
splitEdges(L, C, v);
|
splitEdges(L, C, v);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
E[L[v]].inc.erase({u, v});
|
E[L[v]].inc.erase({u, v});
|
||||||
E.erase(L[u]);
|
E.erase(L[u]);
|
||||||
splitEdges(L, C, u);
|
splitEdges(L, C, u);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto &w : std::views::keys(C)) {
|
for (const auto &w : std::views::keys(C)) {
|
||||||
if (!RT[C[w]].contains(v)) continue;
|
if (!RT[C[w]].contains(v))
|
||||||
|
continue;
|
||||||
|
|
||||||
RT[C[w]].removeEdgeTo(v);
|
RT[C[w]].removeEdgeTo(v);
|
||||||
if (E[C[v]].inc.size() > 0) {
|
if (E[C[v]].inc.size() > 0) {
|
||||||
@@ -152,15 +153,15 @@ void Frigioni<T>::removeInternal(const T& u, const T& v) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void Frigioni<T>::removeExternal(const T &u, const T &v) {
|
||||||
void Frigioni<T>::removeExternal(const T& u, const T& v) {
|
|
||||||
this->G.remove(u, v);
|
this->G.remove(u, v);
|
||||||
auto C = rodittyZwick.getComponentMap();
|
auto C = rodittyZwick.getComponentMap();
|
||||||
E[C[v]].inc.erase({u, v});
|
E[C[v]].inc.erase({u, v});
|
||||||
E[C[u]].out.erase({u, v});
|
E[C[u]].out.erase({u, v});
|
||||||
|
|
||||||
for (const auto &w : std::views::keys(C)) {
|
for (const auto &w : std::views::keys(C)) {
|
||||||
if (!RT[C[w]].contains(v)) continue;
|
if (!RT[C[w]].contains(v))
|
||||||
|
continue;
|
||||||
|
|
||||||
RT[C[w]].removeEdgeTo(v);
|
RT[C[w]].removeEdgeTo(v);
|
||||||
if (E[C[v]].inc.size() > 0) {
|
if (E[C[v]].inc.size() > 0) {
|
||||||
@@ -177,8 +178,7 @@ void Frigioni<T>::removeExternal(const T& u, const T& v) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void Frigioni<T>::repairTrees() {
|
||||||
void Frigioni<T>::repairTrees() {
|
|
||||||
auto C = rodittyZwick.getComponentMap();
|
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) {
|
while (H[C[w]].size() > 0) {
|
||||||
@@ -193,7 +193,8 @@ void Frigioni<T>::repairTrees() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (foundHook) continue;
|
if (foundHook)
|
||||||
|
continue;
|
||||||
|
|
||||||
for (const auto &x : C[w].vertices()) {
|
for (const auto &x : C[w].vertices()) {
|
||||||
for (const auto &y : C[h].vertices()) {
|
for (const auto &y : C[h].vertices()) {
|
||||||
@@ -210,7 +211,8 @@ void Frigioni<T>::repairTrees() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Frigioni<T>::splitEdges(std::unordered_map<T, SCC<T>>& L, std::unordered_map<T, SCC<T>> C, const T& w) {
|
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)
|
for (const auto &[a, b] : E[L[w]].inc)
|
||||||
E[C[b]].inc.insert({a, b});
|
E[C[b]].inc.insert({a, b});
|
||||||
|
|
||||||
@@ -220,8 +222,7 @@ void Frigioni<T>::splitEdges(std::unordered_map<T, SCC<T>>& L, std::unordered_ma
|
|||||||
for (const auto &[a, b] : E[L[w]].in) {
|
for (const auto &[a, b] : E[L[w]].in) {
|
||||||
if (C[a] == C[b]) {
|
if (C[a] == C[b]) {
|
||||||
E[C[a]].in.insert({a, b});
|
E[C[a]].in.insert({a, b});
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
E[C[a]].out.insert({a, b});
|
E[C[a]].out.insert({a, b});
|
||||||
E[C[b]].in.insert({a, b});
|
E[C[b]].in.insert({a, b});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ constexpr int threshold = 5;
|
|||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class HenzingerKing : public DynamicReachability<T> {
|
||||||
class HenzingerKing : public DynamicReachability<T> {
|
|
||||||
public:
|
public:
|
||||||
HenzingerKing() = default;
|
HenzingerKing() = default;
|
||||||
|
|
||||||
@@ -25,12 +24,13 @@ public:
|
|||||||
|
|
||||||
// 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) 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
|
||||||
void insert(const T& c, const std::vector<T>& vertices);
|
void insert(const T &c, const std::vector<T> &vertices) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Decremental maintenance data structure
|
// Decremental maintenance data structure
|
||||||
Frigioni<T> frigioni;
|
Frigioni<T> frigioni;
|
||||||
@@ -43,22 +43,18 @@ private:
|
|||||||
std::unordered_map<T, BreadthFirstTree<T>> Out;
|
std::unordered_map<T, BreadthFirstTree<T>> Out;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void HenzingerKing<T>::init() {
|
||||||
void HenzingerKing<T>::init() {
|
|
||||||
S.clear();
|
S.clear();
|
||||||
frigioni = Frigioni<T>(this->G);
|
frigioni = Frigioni<T>(this->G);
|
||||||
frigioni.init();
|
frigioni.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> bool HenzingerKing<T>::query(const T &u, const T &v) {
|
||||||
bool HenzingerKing<T>::query(const T& u, const T& v) {
|
|
||||||
if (frigioni.query(u, v))
|
if (frigioni.query(u, v))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return std::ranges::any_of(S.begin(), S.end(),
|
return std::ranges::any_of(S.begin(), S.end(), [&](const T &w) {
|
||||||
[&](const T& w) {
|
return In[w].adjList.contains(u) && Out[w].adjList.contains(v);
|
||||||
return In[w].adjList.contains(u) &&
|
|
||||||
Out[w].adjList.contains(v);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,7 @@
|
|||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class Italiano : public DecrementalReachability<T> {
|
||||||
class Italiano : public DecrementalReachability<T> {
|
|
||||||
public:
|
public:
|
||||||
Italiano() = default;
|
Italiano() = default;
|
||||||
|
|
||||||
@@ -26,6 +25,7 @@ public:
|
|||||||
|
|
||||||
// Delete edge e(u, v) and explicitly maintain the transitive closure
|
// 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:
|
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;
|
||||||
@@ -47,8 +47,7 @@ private:
|
|||||||
void repairTrees();
|
void repairTrees();
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void Italiano<T>::init() {
|
||||||
void Italiano<T>::init() {
|
|
||||||
for (const auto &u : this->G.vertices()) {
|
for (const auto &u : this->G.vertices()) {
|
||||||
for (const auto &v : this->G.adjList[u]) {
|
for (const auto &v : this->G.adjList[u]) {
|
||||||
E[v].inc.insert(u);
|
E[v].inc.insert(u);
|
||||||
@@ -62,27 +61,27 @@ void Italiano<T>::init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> bool Italiano<T>::query(const T &u, const T &v) {
|
||||||
bool Italiano<T>::query(const T& u, const T& v) {
|
|
||||||
return TC[u][v];
|
return TC[u][v];
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Italiano<T>::remove(const std::vector<std::pair<T, T>> &edges) {
|
void Italiano<T>::remove(const std::vector<std::pair<T, T>> &edges) {
|
||||||
for (const auto &[u, v] : edges) {
|
for (const auto &[u, v] : edges) {
|
||||||
if (!this->G.adjList[u].contains(v)) continue;
|
if (!this->G.adjList[u].contains(v))
|
||||||
|
continue;
|
||||||
remove(u, v);
|
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) {
|
|
||||||
this->G.remove(u, v);
|
this->G.remove(u, v);
|
||||||
E[u].out.erase(v);
|
E[u].out.erase(v);
|
||||||
E[v].inc.erase(u);
|
E[v].inc.erase(u);
|
||||||
|
|
||||||
for (const auto &w : this->G.vertices()) {
|
for (const auto &w : this->G.vertices()) {
|
||||||
if (!RT[w].adjList[u].contains(v)) continue;
|
if (!RT[w].adjList[u].contains(v))
|
||||||
|
continue;
|
||||||
|
|
||||||
RT[w].adjList[u].erase(v);
|
RT[w].adjList[u].erase(v);
|
||||||
if (E[v].inc.size() > 0) {
|
if (E[v].inc.size() > 0) {
|
||||||
@@ -96,8 +95,7 @@ void Italiano<T>::remove(const T& u, const T& v) {
|
|||||||
repairTrees();
|
repairTrees();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void Italiano<T>::repairTrees() {
|
||||||
void Italiano<T>::repairTrees() {
|
|
||||||
for (const auto &w : this->G.vertices()) {
|
for (const auto &w : this->G.vertices()) {
|
||||||
while (H[w].size() > 0) {
|
while (H[w].size() > 0) {
|
||||||
const auto &h = H[w].top();
|
const auto &h = H[w].top();
|
||||||
@@ -111,7 +109,8 @@ void Italiano<T>::repairTrees() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (foundHook) continue;
|
if (foundHook)
|
||||||
|
continue;
|
||||||
|
|
||||||
TC[w][h] = false;
|
TC[w][h] = false;
|
||||||
for (const auto &o : E[h].out) {
|
for (const auto &o : E[h].out) {
|
||||||
|
|||||||
@@ -6,8 +6,7 @@
|
|||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class King : public DynamicReachability<T> {
|
||||||
class King : public DynamicReachability<T> {
|
|
||||||
public:
|
public:
|
||||||
King() = default;
|
King() = default;
|
||||||
|
|
||||||
@@ -30,14 +29,14 @@ public:
|
|||||||
|
|
||||||
// Insert edge e(u, v) by reconstructing all reachability trees
|
// 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:
|
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;
|
||||||
std::unordered_map<T, Italiano<T>> Out;
|
std::unordered_map<T, Italiano<T>> Out;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void King<T>::init() {
|
||||||
void King<T>::init() {
|
|
||||||
for (const auto &u : this->G.vertices()) {
|
for (const auto &u : this->G.vertices()) {
|
||||||
In[u] = Italiano<T>(BreadthFirstTree<T>(this->G.reverse(), u));
|
In[u] = Italiano<T>(BreadthFirstTree<T>(this->G.reverse(), u));
|
||||||
In[u].init();
|
In[u].init();
|
||||||
@@ -46,12 +45,10 @@ void King<T>::init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> bool King<T>::query(const T &u, const T &v) {
|
||||||
bool King<T>::query(const T& u, const T& v) {
|
return std::any_of(
|
||||||
return std::ranges::any_of(this->G.vertices().begin(), this->G.vertices().end(),
|
this->G.vertices().begin(), this->G.vertices().end(),
|
||||||
[&](const T& w) {
|
[&](const T &w) { return In[w].query(w, u) && Out[w].query(w, v); });
|
||||||
return In[w].query(w, u) && Out[w].query(w, v);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -60,8 +57,7 @@ void King<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
|||||||
remove(u, v);
|
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);
|
||||||
for (const auto &w : this->G.vertices()) {
|
for (const auto &w : this->G.vertices()) {
|
||||||
In[w].remove(v, u);
|
In[w].remove(v, u);
|
||||||
|
|||||||
@@ -7,8 +7,7 @@
|
|||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class RodittyZwick : public DecrementalReachability<T> {
|
||||||
class RodittyZwick : public DecrementalReachability<T> {
|
|
||||||
public:
|
public:
|
||||||
RodittyZwick() = default;
|
RodittyZwick() = default;
|
||||||
|
|
||||||
@@ -30,6 +29,7 @@ public:
|
|||||||
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; }
|
std::unordered_map<T, SCC<T>> getComponentMap() { return C; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Array used to answer strong connectivity queries in O(1) time
|
// Array used to answer strong connectivity queries in O(1) time
|
||||||
std::unordered_map<T, T> A;
|
std::unordered_map<T, T> A;
|
||||||
@@ -42,13 +42,9 @@ private:
|
|||||||
std::unordered_map<T, BreadthFirstTree<T>> Out;
|
std::unordered_map<T, BreadthFirstTree<T>> Out;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void RodittyZwick<T>::init() { findSCC(this->G); }
|
||||||
void RodittyZwick<T>::init() {
|
|
||||||
findSCC(this->G);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void RodittyZwick<T>::findSCC(graph::Digraph<T> G) {
|
||||||
void RodittyZwick<T>::findSCC(graph::Digraph<T> G) {
|
|
||||||
auto SCCs = Tarjan<T>(G.adjList).execute();
|
auto SCCs = Tarjan<T>(G.adjList).execute();
|
||||||
|
|
||||||
for (auto &c : SCCs) {
|
for (auto &c : SCCs) {
|
||||||
@@ -64,8 +60,7 @@ void RodittyZwick<T>::findSCC(graph::Digraph<T> G) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> bool RodittyZwick<T>::query(const T &u, const T &v) {
|
||||||
bool RodittyZwick<T>::query(const T& u, const T& v) {
|
|
||||||
return A[u] == A[v];
|
return A[u] == A[v];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,8 +70,7 @@ void RodittyZwick<T>::remove(const std::vector<std::pair<T, T>>& edges) {
|
|||||||
remove(u, v);
|
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];
|
||||||
C[w].remove(u, v);
|
C[w].remove(u, v);
|
||||||
this->G.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])
|
if (A[u] != A[v])
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// If edge (u,v) is not contained in both inTree and outTree do nothing TODO:remove useless comments
|
// If edge (u,v) is not contained in both inTree and outTree do nothing
|
||||||
// is this not better if i utilize A matrix, since we are going traversing between components.. ? TODO
|
// 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))
|
if (!In[w].adjList[u].contains(v) && !Out[w].adjList[u].contains(v))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
@@ -3,24 +3,25 @@
|
|||||||
|
|
||||||
#include "graph/scc.h"
|
#include "graph/scc.h"
|
||||||
|
|
||||||
|
#include <ranges>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <ranges>
|
|
||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class Tarjan {
|
||||||
class Tarjan {
|
|
||||||
public:
|
public:
|
||||||
Tarjan() = default;
|
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();
|
auto execute();
|
||||||
|
|
||||||
//
|
//
|
||||||
void strongConnect(const T &u);
|
void strongConnect(const T &u);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<T, std::unordered_set<T>> adjList;
|
std::unordered_map<T, std::unordered_set<T>> adjList;
|
||||||
std::stack<T> S;
|
std::stack<T> S;
|
||||||
@@ -36,8 +37,7 @@ private:
|
|||||||
std::unordered_map<T, Vertex> V;
|
std::unordered_map<T, Vertex> V;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void Tarjan<T>::strongConnect(const T &u) {
|
||||||
void Tarjan<T>::strongConnect(const T& u) {
|
|
||||||
V[u].index = V[u].lowlink = index++;
|
V[u].index = V[u].lowlink = index++;
|
||||||
S.push(u);
|
S.push(u);
|
||||||
V[u].onStack = true;
|
V[u].onStack = true;
|
||||||
@@ -69,8 +69,7 @@ void Tarjan<T>::strongConnect(const T& u) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> auto Tarjan<T>::execute() {
|
||||||
auto Tarjan<T>::execute() {
|
|
||||||
for (const auto &u : std::views::keys(adjList)) {
|
for (const auto &u : std::views::keys(adjList)) {
|
||||||
if (V[u].index == -1)
|
if (V[u].index == -1)
|
||||||
strongConnect(u);
|
strongConnect(u);
|
||||||
|
|||||||
@@ -5,8 +5,7 @@
|
|||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class BreadthFirstTree : public Digraph<T> {
|
||||||
class BreadthFirstTree : public Digraph<T> {
|
|
||||||
public:
|
public:
|
||||||
BreadthFirstTree() = default;
|
BreadthFirstTree() = default;
|
||||||
|
|
||||||
@@ -25,8 +24,7 @@ BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) {
|
|||||||
this->adjList = algo::BreadthFirstSearch<T>(G.adjList).execute(root);
|
this->adjList = algo::BreadthFirstSearch<T>(G.adjList).execute(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void BreadthFirstTree<T>::removeEdgeTo(const T &u) {
|
||||||
void BreadthFirstTree<T>::removeEdgeTo(const T& u) {
|
|
||||||
for (const auto &x : this->vertices()) {
|
for (const auto &x : this->vertices()) {
|
||||||
for (const auto &y : this->adjList[x]) {
|
for (const auto &y : this->adjList[x]) {
|
||||||
if (y == u) {
|
if (y == u) {
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
#ifndef DIGRAPH_H_
|
#ifndef DIGRAPH_H_
|
||||||
#define DIGRAPH_H_
|
#define DIGRAPH_H_
|
||||||
|
|
||||||
#include "graph.h"
|
|
||||||
#include "algorithm/breadth_first_search.h"
|
#include "algorithm/breadth_first_search.h"
|
||||||
|
#include "graph.h"
|
||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class Digraph : public Graph<T> {
|
||||||
class Digraph : public Graph<T> {
|
|
||||||
public:
|
public:
|
||||||
Digraph() = default;
|
Digraph() = default;
|
||||||
|
|
||||||
@@ -28,8 +27,8 @@ public:
|
|||||||
//
|
//
|
||||||
auto contains(const T &u);
|
auto contains(const T &u);
|
||||||
|
|
||||||
friend std::ostream& operator<<<>(std::ostream& os, Digraph<T>& G);
|
template <typename U>
|
||||||
|
friend std::ostream &operator<<(std::ostream &os, const Digraph<U> &G);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -37,24 +36,20 @@ Digraph<T>::Digraph(std::unordered_map<T, std::unordered_set<T>> G) {
|
|||||||
this->adjList = G;
|
this->adjList = G;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> bool Digraph<T>::contains(const T &u, const T &v) {
|
||||||
bool Digraph<T>::contains(const T& u, const T& v) {
|
|
||||||
return algo::BreadthFirstSearch<T>(this->adjList).query(u, v);
|
return algo::BreadthFirstSearch<T>(this->adjList).query(u, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void Digraph<T>::insert(const T &u, const T &v) {
|
||||||
void Digraph<T>::insert(const T& u, const T& v) {
|
|
||||||
this->adjList[u].insert(v);
|
this->adjList[u].insert(v);
|
||||||
this->adjList[v];
|
this->adjList[v];
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void Digraph<T>::remove(const T &u, const T &v) {
|
||||||
void Digraph<T>::remove(const T& u, const T& v) {
|
|
||||||
this->adjList[u].erase(v);
|
this->adjList[u].erase(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> auto Digraph<T>::reverse() {
|
||||||
auto Digraph<T>::reverse() {
|
|
||||||
std::unordered_map<T, std::unordered_set<T>> revMatrix;
|
std::unordered_map<T, std::unordered_set<T>> revMatrix;
|
||||||
|
|
||||||
for (const auto &u : this->vertices()) {
|
for (const auto &u : this->vertices()) {
|
||||||
@@ -66,8 +61,7 @@ auto Digraph<T>::reverse() {
|
|||||||
return revMatrix;
|
return revMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> auto Digraph<T>::contains(const T &u) {
|
||||||
auto Digraph<T>::contains(const T& u) {
|
|
||||||
return this->adjList.count(u);
|
return this->adjList.count(u);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#ifndef GRAPH_H_
|
#ifndef GRAPH_H_
|
||||||
#define GRAPH_H_
|
#define GRAPH_H_
|
||||||
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <unordered_set>
|
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
@@ -12,8 +12,7 @@ namespace graph {
|
|||||||
template <typename T> class Graph;
|
template <typename T> class Graph;
|
||||||
template <typename T> std::ostream &operator<<(std::ostream &os, Graph<T> &G);
|
template <typename T> std::ostream &operator<<(std::ostream &os, Graph<T> &G);
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class Graph {
|
||||||
class Graph {
|
|
||||||
public:
|
public:
|
||||||
virtual ~Graph() = default;
|
virtual ~Graph() = default;
|
||||||
|
|
||||||
@@ -37,21 +36,17 @@ public:
|
|||||||
|
|
||||||
// Adjacency matrix representation
|
// Adjacency matrix representation
|
||||||
std::unordered_map<T, std::unordered_set<T>> adjList;
|
std::unordered_map<T, std::unordered_set<T>> adjList;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> auto Graph<T>::vertices() const {
|
||||||
auto Graph<T>::vertices() const{
|
|
||||||
return std::views::keys(adjList);
|
return std::views::keys(adjList);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> std::uint16_t Graph<T>::V() {
|
||||||
std::uint16_t Graph<T>::V() {
|
|
||||||
return static_cast<std::uint16_t>(adjList.size());
|
return static_cast<std::uint16_t>(adjList.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> std::uint16_t Graph<T>::E() {
|
||||||
std::uint16_t Graph<T>::E() {
|
|
||||||
std::uint16_t edges = 0;
|
std::uint16_t edges = 0;
|
||||||
for (const auto &u : vertices()) {
|
for (const auto &u : vertices()) {
|
||||||
edges += static_cast<std::uint16_t>(adjList[u].size());
|
edges += static_cast<std::uint16_t>(adjList[u].size());
|
||||||
@@ -59,9 +54,6 @@ std::uint16_t Graph<T>::E() {
|
|||||||
return edges;
|
return edges;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace graph
|
} // namespace graph
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -8,13 +8,14 @@
|
|||||||
|
|
||||||
namespace graph {
|
namespace graph {
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> class SCC : public Digraph<T> {
|
||||||
class SCC : public Digraph<T> {
|
|
||||||
public:
|
public:
|
||||||
SCC() = default;
|
SCC() = default;
|
||||||
|
|
||||||
SCC(std::unordered_map<T, std::unordered_set<T>> G, T id)
|
SCC(std::unordered_map<T, std::unordered_set<T>> G, T id)
|
||||||
: Digraph<T>(G), id(id) { normalize(); }
|
: Digraph<T>(G), id(id) {
|
||||||
|
normalize();
|
||||||
|
}
|
||||||
|
|
||||||
SCC(Digraph<T> G, T id) : id(id) { normalize(); }
|
SCC(Digraph<T> G, T id) : id(id) { normalize(); }
|
||||||
|
|
||||||
@@ -28,13 +29,13 @@ public:
|
|||||||
std::unordered_map<T, std::unordered_set<T>> neighboorList;
|
std::unordered_map<T, std::unordered_set<T>> neighboorList;
|
||||||
|
|
||||||
bool operator==(const SCC &o) const;
|
bool operator==(const SCC &o) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Erase all edges that include vertices outside this SCC
|
// Erase all edges that include vertices outside this SCC
|
||||||
void normalize();
|
void normalize();
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> void SCC<T>::normalize() {
|
||||||
void SCC<T>::normalize() {
|
|
||||||
for (const auto &u : this->vertices()) {
|
for (const auto &u : this->vertices()) {
|
||||||
for (const auto &v : this->adjList[u]) {
|
for (const auto &v : this->adjList[u]) {
|
||||||
if (!this->contains(v)) {
|
if (!this->contains(v)) {
|
||||||
@@ -46,18 +47,15 @@ void SCC<T>::normalize() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> bool SCC<T>::contains(const T &u) const {
|
||||||
bool SCC<T>::contains(const T& u) const {
|
|
||||||
return this->adjList.count(u);
|
return this->adjList.count(u);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> bool SCC<T>::operator==(const SCC &o) const {
|
||||||
bool SCC<T>::operator==(const SCC& o) const {
|
|
||||||
return id == o.id;
|
return id == o.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template <typename T> struct HashSCC {
|
||||||
struct HashSCC {
|
|
||||||
std::size_t operator()(const SCC<T> &C) const {
|
std::size_t operator()(const SCC<T> &C) const {
|
||||||
return static_cast<std::size_t>(C.id);
|
return static_cast<std::size_t>(C.id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#ifndef RODITTY_ZWICK_H_
|
#ifndef RODITTY_ZWICK_H_
|
||||||
#define RODITTY_ZWICK_H_
|
#define RODITTY_ZWICK_H_
|
||||||
|
|
||||||
#include "algorithm/italiano.h"
|
|
||||||
#include "algorithm/frigioni.h"
|
#include "algorithm/frigioni.h"
|
||||||
#include "algorithm/king.h"
|
|
||||||
#include "algorithm/henzinger_king.h"
|
#include "algorithm/henzinger_king.h"
|
||||||
|
#include "algorithm/italiano.h"
|
||||||
|
#include "algorithm/king.h"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
#include "algorithm/frigioni.h"
|
#include "algorithm/frigioni.h"
|
||||||
#include "algorithm/italiano.h"
|
#include "algorithm/italiano.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
|
|
||||||
TEST_SUITE("Decremental Reachability Test") {
|
TEST_SUITE("Decremental Reachability Test") {
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
#include "algorithm/henzinger_king.h"
|
#include "algorithm/henzinger_king.h"
|
||||||
#include "algorithm/king.h"
|
#include "algorithm/king.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
|
|
||||||
TEST_SUITE("Dynamic Reachability Test") {
|
TEST_SUITE("Dynamic Reachability Test") {
|
||||||
|
|||||||
Reference in New Issue
Block a user