Finish italiano and tests

This commit is contained in:
stefiosif
2022-09-20 13:41:15 +03:00
parent 40f23c5bf8
commit 7d2d9fd82c
2 changed files with 73 additions and 60 deletions

View File

@@ -6,6 +6,7 @@
#include "graph/breadth_first_tree.h"
#include <forward_list>
#include <iostream>
using namespace graph;
@@ -32,25 +33,24 @@ private:
// Incoming / Outgoing edges
struct Edges {
std::forward_list<T> inc;
std::forward_list<T> out;
std::set<T> inc;
std::set<T> out;
};
std::map<T, Edges> E;
};
template<typename T>
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]) {
E[v].inc.push_front(u);
E[u].out.push_front(v);
E[v].inc.insert(u);
E[u].out.insert(v);
TC[u][v] = false;
}
RT[u] = BreadthFirstTree(G, u);
RT[u] = BreadthFirstTree<T>(G, u);
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;
}
}
}
@@ -61,41 +61,43 @@ bool Italiano<T>::query(const T& u, const T& v) {
template<typename T>
void Italiano<T>::remove(const T& u, const T& v) {
std::map<T, std::forward_list<T>> R;
for (const auto& w : std::views::keys(G.adjMatrix)) {
if (RT[w].contains(u, 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());
std::map<T, std::stack<T>> H;
for (const auto& w : G.vertices()) {
if (RT[w].contains(u, v)) {
if (E[v].inc.size() > 1)
H[w].push(v);
else {
TC[w][v] = false;
for (const auto& c : E[v].out)
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);
for (const auto& w : std::views::keys(G.adjMatrix)) {
bool hooked = false;
// 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;
for (const auto& z : G.vertices()) {
RT[z].adjMatrix[u].erase(v);
while (H[z].size() > 0) {
auto& h = H[z].top();
bool found = false;
for (const auto& i : E[h].inc) {
if (RT[z].contains(z, i)) {
RT[z].adjMatrix[i].insert(h);
H[z].pop();
found = 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);
if (!found) {
TC[u][v] = false;
for (const auto& o : E[h].out) {
if (RT[z].contains(z, o)) {
H[z].push(o);
}
}
}
}