Update italiano's query and remove methods
This commit is contained in:
@@ -3,12 +3,11 @@
|
|||||||
|
|
||||||
#include "algorithm/roditty_zwick.h"
|
#include "algorithm/roditty_zwick.h"
|
||||||
#include "algorithm/tarjan.h"
|
#include "algorithm/tarjan.h"
|
||||||
#include "tree/breadth_first_tree.h"
|
#include "graph/breadth_first_tree.h"
|
||||||
|
|
||||||
#include <forward_list>
|
#include <forward_list>
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
using namespace tree;
|
|
||||||
|
|
||||||
namespace algo {
|
namespace algo {
|
||||||
|
|
||||||
@@ -25,60 +24,59 @@ public:
|
|||||||
private:
|
private:
|
||||||
Digraph<T> G;
|
Digraph<T> G;
|
||||||
|
|
||||||
// Connect each vertex with its representative SCC
|
// Transitive closure matrix, used to answer reachability queries in O(1)
|
||||||
std::map<T, SCC<T>> TC;
|
std::map<T, std::map<T, bool>> TC;
|
||||||
|
|
||||||
// Reachability trees
|
// Each vertex's reachability tree
|
||||||
std::map<T, std::set<T>> RT;
|
std::map<T, BreadthFirstTree<T>> RT;
|
||||||
|
|
||||||
// Incoming / Outgoing edges
|
// Incoming / Outgoing edges
|
||||||
struct Edges {
|
struct Edges {
|
||||||
std::forward_list<T> Ein;
|
std::forward_list<T> inc;
|
||||||
std::forward_list<T> Eout;
|
std::forward_list<T> out;
|
||||||
};
|
};
|
||||||
std::map<SCC<T>, Edges> E;
|
std::map<T, Edges> E;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Italiano<T>::init() {
|
void Italiano<T>::init() {
|
||||||
|
for (const auto& u : std::views::keys(G.adjMatrix)) {
|
||||||
|
for (const auto& v : G.adjMatrix[u]) {
|
||||||
|
E[v].inc.push_front(u);
|
||||||
|
E[u].out.push_front(v);
|
||||||
|
TC[u][v] = false;
|
||||||
|
}
|
||||||
|
RT[u] = BreadthFirstTree(G, u);
|
||||||
|
TC[u][u] = true;
|
||||||
|
for (const auto& v : std::views::keys(RT[u].adjMatrix)) {
|
||||||
|
TC[u][v] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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] == TC[v];
|
return TC[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) {
|
||||||
|
std::map<T, std::set<T>> R;
|
||||||
for (const auto& w : G.adjMatrix) {
|
for (const auto& w : std::views::keys(G.adjMatrix)) {
|
||||||
// Check if (u,v) edge of T(w) || p(w,v) points to (u,v)
|
if (RT[w].contains(u, v)) {
|
||||||
|
R[w].insert(v);
|
||||||
// If yes => set p(w, v) to point to the next edge (u',v) in Ein(v)
|
}
|
||||||
// If (u,v) the last edge, then set it to point to null
|
E[u].inc.remove(v);
|
||||||
|
E[u].out.remove(u);
|
||||||
// If T(w) contains u' then the new edge reconnects v to T(w)
|
G.remove(u, v);
|
||||||
// As this needs to be checked, we set R(w) <- {v}
|
|
||||||
|
|
||||||
// If (u,v) is not edge of T(w) or is the last edge in Ein(v), R(w) <- phi
|
|
||||||
|
|
||||||
// Remove (u,v) from Eout(u) and Ein(v)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& w : G.adjMatrix) {
|
//for (const auto& w : std::views::keys(G.adjMatrix)) {
|
||||||
|
// while (R[w].size() > 0) {
|
||||||
// Check tentative pointers of the vertices in R(w).
|
// auto pop = R[w].
|
||||||
// While there is a vertex x in R(w), scan the edges in Ein(x), starting
|
// }
|
||||||
// from the edge pointed to by p(w,x), until an edge (u',x) for which
|
//}
|
||||||
// p(w,u') != null is found or Ein(x) is exhausted.
|
|
||||||
|
|
||||||
// If such edge is found, x is removed from R(w).
|
|
||||||
// If the list is exhausted, set p(w,x) to null and M(w,x) to 0
|
|
||||||
|
|
||||||
// Then scan all Eout(x). If (x,x') is tree edge, add x' to R(w)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace algo
|
} // namespace algo
|
||||||
|
|||||||
@@ -93,11 +93,16 @@ TEST_SUITE("Decremental algorithms") {
|
|||||||
|
|
||||||
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
||||||
|
|
||||||
algo::Italiano<std::uint16_t> rz(G);
|
algo::Italiano<std::uint16_t> italiano(G);
|
||||||
rz.init();
|
italiano.init();
|
||||||
|
|
||||||
SUBCASE("Italiano::query") {
|
SUBCASE("Italiano::query") {
|
||||||
|
CHECK_EQ(italiano.query(1, 13), true);
|
||||||
|
CHECK_EQ(italiano.query(6, 10), true);
|
||||||
|
CHECK_EQ(italiano.query(9, 11), true);
|
||||||
|
CHECK_EQ(italiano.query(8, 2), false);
|
||||||
|
CHECK_EQ(italiano.query(9, 3), false);
|
||||||
|
CHECK_EQ(italiano.query(9, 8), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
SUBCASE("Italiano::remove") {
|
SUBCASE("Italiano::remove") {
|
||||||
@@ -134,8 +139,8 @@ TEST_SUITE("Decremental algorithms") {
|
|||||||
|
|
||||||
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
||||||
|
|
||||||
algo::Frigioni<std::uint16_t> rz(G);
|
algo::Frigioni<std::uint16_t> frigioni(G);
|
||||||
rz.init();
|
frigioni.init();
|
||||||
|
|
||||||
SUBCASE("Frigioni::query") {
|
SUBCASE("Frigioni::query") {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user