Update decremental algorithms according to project structure changes

This commit is contained in:
stefiosif
2022-09-25 17:42:00 +03:00
parent d63411672d
commit 14a61c92e9
3 changed files with 52 additions and 47 deletions

View File

@@ -1,32 +1,31 @@
#ifndef FRIGIONI_H_ #ifndef FRIGIONI_H_
#define FRIGIONI_H_ #define FRIGIONI_H_
#include "algorithm/decremental_reachability.h"
#include "algorithm/roditty_zwick.h" #include "algorithm/roditty_zwick.h"
#include "algorithm/tarjan.h"
#include "graph/breadth_first_tree.h"
#include "algorithm/decremental_scc.h"
#include <forward_list>
#include <utility> #include <utility>
#include <iostream>
using namespace graph; using namespace graph;
namespace algo { namespace algo {
template<typename T> template<typename T>
class Frigioni : public RodittyZwick<T> { class Frigioni : public DecrementalReachability<T> {
public: public:
Frigioni(Digraph<T> G) : G(G) {} Frigioni() = default;
void init(); Frigioni(Digraph<T> G) { this->G = G; }
bool query(const T& u, const T& v); //
void init() override;
void remove(const T& u, const T& v); //
bool query(const T& u, const T& v) override;
//
void remove(const T& u, const T& v) override;
private: private:
Digraph<T> G;
// Transitive closure matrix, used to answer reachability queries in O(1) // Transitive closure matrix, used to answer reachability queries in O(1)
std::map<T, std::map<T, bool>> TC; std::map<T, std::map<T, bool>> TC;
@@ -36,24 +35,29 @@ private:
// Each scc's representative vertex reachability tree // Each scc's representative vertex reachability tree
std::map<T, BreadthFirstTree<T>> RT; std::map<T, BreadthFirstTree<T>> RT;
// Incoming / Outgoing / Internal edges // Incoming / Outgoing / Internal edges of each SCC
// Maps each SCC representative with struct Edges
struct Edges { struct Edges {
std::set<std::pair<T, T>> in; std::set<std::pair<T, T>> in;
std::set<std::pair<T, T>> inc; std::set<std::pair<T, T>> inc;
std::set<std::pair<T, T>> out; std::set<std::pair<T, T>> out;
}; };
std::map<T, Edges> E; std::map<T, Edges> E;
// Decremental maintenance of strongly connected components
RodittyZwick<T> decremental;
}; };
template<typename T> template<typename T>
void Frigioni<T>::init() { void Frigioni<T>::init() {
auto SCCs = Tarjan<T>(G.adjMatrix).execute(); auto SCCs = Tarjan<T>(this->G.adjMatrix).execute();
decremental = RodittyZwick<T>(this->G);
decremental.init();
for (auto& scc : SCCs) { for (auto& scc : SCCs) {
RT[scc.id] = BreadthFirstTree<T>(G, scc.id); RT[scc.id] = BreadthFirstTree<T>(this->G, scc.id);
for (const auto& u : G.vertices()) { for (const auto& u : this->G.vertices()) {
C[u] = scc; for (const auto& v : this->G.adjMatrix[u]) {
for (const auto& v : G.adjMatrix[u]) {
TC[u][v] = false; TC[u][v] = false;
if (scc.member(u)) { if (scc.member(u)) {
if (scc.member(v)) if (scc.member(v))
@@ -67,7 +71,7 @@ void Frigioni<T>::init() {
} }
} }
for (auto& scc : SCCs) { for (auto& scc : SCCs) {
for (const auto& u : G.vertices()) { for (const auto& u : this->G.vertices()) {
if (scc.member(u)) { if (scc.member(u)) {
for (const auto& v : RT[scc.id].vertices()) { for (const auto& v : RT[scc.id].vertices()) {
TC[u][v] = true; TC[u][v] = true;

View File

@@ -1,37 +1,37 @@
#ifndef ITALIANO_H_ #ifndef ITALIANO_H_
#define ITALIANO_H_ #define ITALIANO_H_
#include "algorithm/roditty_zwick.h" #include "algorithm/decremental_reachability.h"
#include "algorithm/tarjan.h"
#include "graph/breadth_first_tree.h" #include "graph/breadth_first_tree.h"
#include <forward_list>
#include <iostream>
using namespace graph; using namespace graph;
namespace algo { namespace algo {
template<typename T> template<typename T>
class Italiano : public RodittyZwick<T> { class Italiano : public DecrementalReachability<T> {
public: public:
Italiano(Digraph<T> G) : G(G) {} Italiano() = default;
void init(); Italiano(Digraph<T> G) { this->G = G; }
bool query(const T& u, const T& v); // Initialize the decremental maintenance data structure for DAGs
void init() override;
void remove(const T& u, const T& v); // Execute reachability query q(u, v) from vertex u to vertex v
// in O(1) using the transitive closure matrix
bool query(const T& u, const T& v) override;
// Delete edge e(u, v) and explicitely maintain the transitive closure
void remove(const T& u, const T& v) override;
private: private:
Digraph<T> G; // Transitive closure matrix
// Transitive closure matrix, used to answer reachability queries in O(1)
std::map<T, std::map<T, bool>> TC; std::map<T, std::map<T, bool>> TC;
// Each vertex's reachability tree // For each vertex, store a reachability tree created as a BFS tree
std::map<T, BreadthFirstTree<T>> RT; std::map<T, BreadthFirstTree<T>> RT;
// Incoming / Outgoing edges // For each vertex, store collections of its incoming and outgoing edges
struct Edges { struct Edges {
std::set<T> inc; std::set<T> inc;
std::set<T> out; std::set<T> out;
@@ -41,13 +41,13 @@ private:
template<typename T> template<typename T>
void Italiano<T>::init() { void Italiano<T>::init() {
for (const auto& u : G.vertices()) { for (const auto& u : this->G.vertices()) {
for (const auto& v : G.adjMatrix[u]) { for (const auto& v : this->G.adjMatrix[u]) {
E[v].inc.insert(u); E[v].inc.insert(u);
E[u].out.insert(v); E[u].out.insert(v);
TC[u][v] = false; TC[u][v] = false;
} }
RT[u] = BreadthFirstTree<T>(G, u); RT[u] = BreadthFirstTree<T>(this->G, u);
TC[u][u] = true; TC[u][u] = true;
for (const auto& v : RT[u].vertices()) for (const auto& v : RT[u].vertices())
TC[u][v] = true; TC[u][v] = true;
@@ -63,7 +63,7 @@ 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::stack<T>> H; std::map<T, std::stack<T>> H;
for (const auto& w : G.vertices()) { for (const auto& w : this->G.vertices()) {
if (RT[w].contains(u, v)) { if (RT[w].contains(u, v)) {
if (E[v].inc.size() > 1) if (E[v].inc.size() > 1)
H[w].push(v); H[w].push(v);
@@ -77,9 +77,9 @@ void Italiano<T>::remove(const T& u, const T& v) {
E[u].out.erase(v); E[u].out.erase(v);
E[v].inc.erase(u); E[v].inc.erase(u);
G.remove(u, v); this->G.remove(u, v);
for (const auto& z : G.vertices()) { for (const auto& z : this->G.vertices()) {
RT[z].adjMatrix[u].erase(v); RT[z].adjMatrix[u].erase(v);
while (H[z].size() > 0) { while (H[z].size() > 0) {
auto& h = H[z].top(); auto& h = H[z].top();
@@ -87,11 +87,14 @@ void Italiano<T>::remove(const T& u, const T& v) {
for (const auto& i : E[h].inc) { for (const auto& i : E[h].inc) {
if (RT[z].contains(z, i)) { if (RT[z].contains(z, i)) {
RT[z].adjMatrix[i].insert(h); RT[z].adjMatrix[i].insert(h);
H[z].pop(); //
found = true; found = true;
break; break;
} }
} }
//
H[z].pop();
//
if (!found) { if (!found) {
TC[u][v] = false; TC[u][v] = false;
for (const auto& o : E[h].out) { for (const auto& o : E[h].out) {

View File

@@ -50,17 +50,15 @@ void Digraph<T>::insert(const T& u, const T& 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->adjMatrix[u].erase(v); this->adjMatrix[u].erase(v);
//if (this->adjMatrix[u].size() == 0)
// this->adjMatrix.erase(u);
} }
template<typename T> template<typename T>
auto Digraph<T>::reverse() { auto Digraph<T>::reverse() {
std::map<T, std::set<T>> revMatrix; std::map<T, std::set<T>> revMatrix;
for (const auto& u : this->adjMatrix) { for (const auto& u : vertices()) {
for (const auto& v : u.second) { for (const auto& v : this->adjMatrix[u]) {
revMatrix[v].insert(u.first); revMatrix[v].insert(u);
} }
} }