Finish italiano and tests
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#include "graph/breadth_first_tree.h"
|
#include "graph/breadth_first_tree.h"
|
||||||
|
|
||||||
#include <forward_list>
|
#include <forward_list>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
|
|
||||||
@@ -32,26 +33,25 @@ private:
|
|||||||
|
|
||||||
// Incoming / Outgoing edges
|
// Incoming / Outgoing edges
|
||||||
struct Edges {
|
struct Edges {
|
||||||
std::forward_list<T> inc;
|
std::set<T> inc;
|
||||||
std::forward_list<T> out;
|
std::set<T> out;
|
||||||
};
|
};
|
||||||
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 : G.vertices()) {
|
||||||
for (const auto& v : G.adjMatrix[u]) {
|
for (const auto& v : G.adjMatrix[u]) {
|
||||||
E[v].inc.push_front(u);
|
E[v].inc.insert(u);
|
||||||
E[u].out.push_front(v);
|
E[u].out.insert(v);
|
||||||
TC[u][v] = false;
|
TC[u][v] = false;
|
||||||
}
|
}
|
||||||
RT[u] = BreadthFirstTree(G, u);
|
RT[u] = BreadthFirstTree<T>(G, u);
|
||||||
TC[u][u] = true;
|
TC[u][u] = true;
|
||||||
for (const auto& v : std::views::keys(RT[u].adjMatrix)) {
|
for (const auto& v : RT[u].vertices())
|
||||||
TC[u][v] = true;
|
TC[u][v] = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -61,41 +61,43 @@ 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::forward_list<T>> R;
|
std::map<T, std::stack<T>> H;
|
||||||
for (const auto& w : std::views::keys(G.adjMatrix)) {
|
|
||||||
if (RT[w].contains(u, v) &&
|
for (const auto& w : G.vertices()) {
|
||||||
std::distance(E[w].inc.begin(), E[w].inc.end()) > 1) {
|
if (RT[w].contains(u, v)) {
|
||||||
if (E[v].inc.front() == u) {
|
if (E[v].inc.size() > 1)
|
||||||
E[v].inc.pop_front();
|
H[w].push(v);
|
||||||
R[w].push_front(E[v].inc.front());
|
else {
|
||||||
E[v].inc.push_front(u);
|
TC[w][v] = false;
|
||||||
} else {
|
for (const auto& c : E[v].out)
|
||||||
R[w].push_front(E[v].inc.front());
|
H[w].push(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
E[u].inc.remove(v);
|
|
||||||
E[u].out.remove(u);
|
E[u].out.erase(v);
|
||||||
|
E[v].inc.erase(u);
|
||||||
G.remove(u, v);
|
G.remove(u, v);
|
||||||
|
|
||||||
for (const auto& w : std::views::keys(G.adjMatrix)) {
|
for (const auto& z : G.vertices()) {
|
||||||
bool hooked = false;
|
RT[z].adjMatrix[u].erase(v);
|
||||||
// R contains all vertices that need a hook-parent
|
while (H[z].size() > 0) {
|
||||||
while (!R[w].empty()) {
|
auto& h = H[z].top();
|
||||||
// z is a candidate hook-parent if it is in RT[w]
|
bool found = false;
|
||||||
const auto& toHook = R[w].front();
|
for (const auto& i : E[h].inc) {
|
||||||
for (const auto& z : E[toHook].inc) {
|
if (RT[z].contains(z, i)) {
|
||||||
if (RT[w].contains(u, z)) {
|
RT[z].adjMatrix[i].insert(h);
|
||||||
R[w].remove(toHook);
|
H[z].pop();
|
||||||
hooked = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!hooked) {
|
if (!found) {
|
||||||
TC[w][toHook] = false;
|
TC[u][v] = false;
|
||||||
for (const auto& z : E[toHook].out) {
|
for (const auto& o : E[h].out) {
|
||||||
if (RT[w].contains(toHook, z))
|
if (RT[z].contains(z, o)) {
|
||||||
R[w].push_front(z);
|
H[z].push(o);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,8 +102,8 @@ TEST_SUITE("Decremental algorithms") {
|
|||||||
SUBCASE("Italiano::remove") {
|
SUBCASE("Italiano::remove") {
|
||||||
italiano.remove(4, 6);
|
italiano.remove(4, 6);
|
||||||
|
|
||||||
CHECK_EQ(italiano.query(1, 8), true);
|
|
||||||
CHECK_EQ(italiano.query(3, 6), true);
|
CHECK_EQ(italiano.query(3, 6), true);
|
||||||
|
CHECK_EQ(italiano.query(4, 6), true);
|
||||||
|
|
||||||
italiano.remove(5, 6);
|
italiano.remove(5, 6);
|
||||||
|
|
||||||
@@ -113,43 +113,54 @@ TEST_SUITE("Decremental algorithms") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Frigioni 1") {
|
TEST_CASE("Frigioni 1") {
|
||||||
// 1 --> 2 --> 3 --> 1
|
// 1 --> 2 --> 4 --> 6 --> 8 --> 9 --> 5
|
||||||
// 3 --> 4 --> 5 --> 3
|
// 4 --> 5 --> 7 --> 8
|
||||||
// 2 --> 6 --> 7 --> 8 --> 6
|
// 5 --> 6
|
||||||
// 7 --> 9
|
// 7 --> 9
|
||||||
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
// 2 --> 3 --> 1
|
||||||
// 12 --> 10
|
// 3 --> 5
|
||||||
|
// 3 --> 9
|
||||||
Digraph<std::uint16_t> G;
|
Digraph<std::uint16_t> G;
|
||||||
G.insert(1, 2);
|
G.insert(1, 2);
|
||||||
|
G.insert(2, 4);
|
||||||
G.insert(2, 3);
|
G.insert(2, 3);
|
||||||
G.insert(3, 1);
|
G.insert(3, 1);
|
||||||
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, 5);
|
||||||
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::Frigioni<std::uint16_t> frigioni(G);
|
algo::Frigioni<std::uint16_t> frigioni(G);
|
||||||
frigioni.init();
|
frigioni.init();
|
||||||
|
/*
|
||||||
SUBCASE("Frigioni::query") {
|
SUBCASE("Frigioni::query") {
|
||||||
|
CHECK_EQ(frigioni.query(1, 9), true);
|
||||||
}
|
CHECK_EQ(frigioni.query(2, 8), true);
|
||||||
|
CHECK_EQ(frigioni.query(3, 9), true);
|
||||||
|
CHECK_EQ(frigioni.query(4, 3), false);
|
||||||
|
CHECK_EQ(frigioni.query(5, 4), false);
|
||||||
|
CHECK_EQ(frigioni.query(6, 1), false);
|
||||||
|
}*/
|
||||||
|
/*
|
||||||
SUBCASE("Frigioni::remove") {
|
SUBCASE("Frigioni::remove") {
|
||||||
|
frigioni.remove(4, 6);
|
||||||
|
|
||||||
}
|
CHECK_EQ(frigioni.query(1, 8), true);
|
||||||
|
CHECK_EQ(frigioni.query(3, 6), true);
|
||||||
|
|
||||||
|
frigioni.remove(4, 6);
|
||||||
|
|
||||||
|
CHECK_EQ(frigioni.query(1, 9), true);
|
||||||
|
CHECK_EQ(frigioni.query(1, 6), false);
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user