Update italiano::remove
This commit is contained in:
@@ -38,7 +38,6 @@ private:
|
|||||||
std::map<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& u : std::views::keys(G.adjMatrix)) {
|
||||||
@@ -62,21 +61,45 @@ bool Italiano<T>::query(const T& u, const T& 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;
|
std::map<T, std::forward_list<T>> R;
|
||||||
for (const auto& w : std::views::keys(G.adjMatrix)) {
|
for (const auto& w : std::views::keys(G.adjMatrix)) {
|
||||||
if (RT[w].contains(u, v)) {
|
if (RT[w].contains(u, v) &&
|
||||||
R[w].insert(v);
|
std::distance(E[w].inc.begin(), E[w].inc.end()) > 1) {
|
||||||
|
if (E[v].inc.front() == u) {
|
||||||
|
E[v].inc.pop_front();
|
||||||
|
R[w].push_front(E[v].inc.front());
|
||||||
|
E[v].inc.push_front(u);
|
||||||
|
} else {
|
||||||
|
R[w].push_front(E[v].inc.front());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
E[u].inc.remove(v);
|
E[u].inc.remove(v);
|
||||||
E[u].out.remove(u);
|
E[u].out.remove(u);
|
||||||
G.remove(u, v);
|
G.remove(u, v);
|
||||||
}
|
|
||||||
|
|
||||||
//for (const auto& w : std::views::keys(G.adjMatrix)) {
|
for (const auto& w : std::views::keys(G.adjMatrix)) {
|
||||||
// while (R[w].size() > 0) {
|
bool hooked = false;
|
||||||
// auto pop = R[w].
|
// R contains all vertices that need a hook-parent
|
||||||
// }
|
while (!R[w].empty()) {
|
||||||
//}
|
// z is a candidate hook-parent if it is in RT[w]
|
||||||
|
const auto& toHook = R[w].front();
|
||||||
|
for (const auto& z : E[toHook].inc) {
|
||||||
|
if (RT[w].contains(u, z)) {
|
||||||
|
R[w].remove(toHook);
|
||||||
|
hooked = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!hooked) {
|
||||||
|
TC[w][toHook] = false;
|
||||||
|
for (const auto& z : E[toHook].out) {
|
||||||
|
if (RT[w].contains(toHook, z))
|
||||||
|
R[w].push_front(z);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace algo
|
} // namespace algo
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "algorithm/decremental_scc.h"
|
#include "algorithm/decremental_scc.h"
|
||||||
#include "algorithm/italiano.h"
|
#include "algorithm/italiano.h"
|
||||||
#include "algorithm/frigioni.h"
|
#include "algorithm/frigioni.h"
|
||||||
|
#include "algorithm/tarjan.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
@@ -12,9 +13,22 @@ using namespace graph;
|
|||||||
TEST_SUITE("Decremental algorithms") {
|
TEST_SUITE("Decremental algorithms") {
|
||||||
|
|
||||||
TEST_CASE("Decremental maintenance of SCCs 1") {
|
TEST_CASE("Decremental maintenance of SCCs 1") {
|
||||||
std::ifstream infile("resources/data.txt");
|
std::ifstream infile("resources/dag.txt");
|
||||||
std::uint16_t u, v;
|
std::uint16_t u, v;
|
||||||
|
Digraph<std::uint16_t> G;
|
||||||
|
|
||||||
//while (infile >> u >> v) {}
|
while (infile >> u >> v)
|
||||||
|
G.insert(u, v);
|
||||||
|
|
||||||
|
auto SCCs = algo::Tarjan<std::uint16_t>(G.adjMatrix).execute();
|
||||||
|
|
||||||
|
// Testing whether an scc is a 1-vertex scc which after the normalization
|
||||||
|
// has no edges, in this case we know tgat there exist no 2-vertex sccs
|
||||||
|
for (auto& scc : SCCs) {
|
||||||
|
CHECK_EQ(std::all_of(scc.adjMatrix.begin(), scc.adjMatrix.end(),
|
||||||
|
[](const auto& p) {
|
||||||
|
return p.second.size() == 0;
|
||||||
|
}), true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,48 +65,50 @@ TEST_SUITE("Decremental algorithms") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Italiano") {
|
TEST_CASE("Italiano") {
|
||||||
// 1 --> 2 --> 3 --> 1
|
// 1 --> 2 --> 4 --> 6 --> 8 --> 9
|
||||||
// 3 --> 4 --> 5 --> 3
|
// 4 --> 5 --> 7 --> 8
|
||||||
// 2 --> 6 --> 7 --> 8 --> 6
|
|
||||||
// 7 --> 9
|
// 7 --> 9
|
||||||
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
// 1 --> 3 --> 9
|
||||||
// 12 --> 10
|
// 3 --> 5 --> 6
|
||||||
Digraph<std::uint16_t> G;
|
Digraph<std::uint16_t> G;
|
||||||
G.insert(1, 2);
|
G.insert(1, 2);
|
||||||
G.insert(2, 3);
|
G.insert(1, 3);
|
||||||
G.insert(3, 1);
|
G.insert(2, 4);
|
||||||
G.insert(3, 4);
|
G.insert(3, 5);
|
||||||
|
G.insert(3, 9);
|
||||||
G.insert(4, 5);
|
G.insert(4, 5);
|
||||||
G.insert(5, 3);
|
G.insert(4, 6);
|
||||||
G.insert(2, 6);
|
G.insert(5, 6);
|
||||||
G.insert(6, 7);
|
G.insert(5, 7);
|
||||||
|
G.insert(6, 8);
|
||||||
G.insert(7, 8);
|
G.insert(7, 8);
|
||||||
G.insert(7, 9);
|
G.insert(7, 9);
|
||||||
G.insert(8, 6);
|
G.insert(8, 9);
|
||||||
G.insert(6, 9);
|
|
||||||
G.insert(9, 10);
|
|
||||||
G.insert(10, 11);
|
|
||||||
G.insert(11, 12);
|
|
||||||
G.insert(12, 13);
|
|
||||||
G.insert(13, 9);
|
|
||||||
G.insert(12, 10);
|
|
||||||
|
|
||||||
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
REQUIRE_EQ(G.adjMatrix.size(), 9);
|
||||||
|
|
||||||
algo::Italiano<std::uint16_t> italiano(G);
|
algo::Italiano<std::uint16_t> italiano(G);
|
||||||
italiano.init();
|
italiano.init();
|
||||||
|
|
||||||
SUBCASE("Italiano::query") {
|
SUBCASE("Italiano::query") {
|
||||||
CHECK_EQ(italiano.query(1, 13), true);
|
CHECK_EQ(italiano.query(1, 9), true);
|
||||||
CHECK_EQ(italiano.query(6, 10), true);
|
CHECK_EQ(italiano.query(2, 8), true);
|
||||||
CHECK_EQ(italiano.query(9, 11), true);
|
CHECK_EQ(italiano.query(3, 7), true);
|
||||||
CHECK_EQ(italiano.query(8, 2), false);
|
CHECK_EQ(italiano.query(4, 3), false);
|
||||||
CHECK_EQ(italiano.query(9, 3), false);
|
CHECK_EQ(italiano.query(5, 4), false);
|
||||||
CHECK_EQ(italiano.query(9, 8), false);
|
CHECK_EQ(italiano.query(6, 1), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
SUBCASE("Italiano::remove") {
|
SUBCASE("Italiano::remove") {
|
||||||
|
italiano.remove(4, 6);
|
||||||
|
|
||||||
|
CHECK_EQ(italiano.query(1, 8), true);
|
||||||
|
CHECK_EQ(italiano.query(3, 6), true);
|
||||||
|
|
||||||
|
italiano.remove(5, 6);
|
||||||
|
|
||||||
|
CHECK_EQ(italiano.query(1, 9), true);
|
||||||
|
CHECK_EQ(italiano.query(1, 6), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user