Files
reachability-algorithms/algorithm/frigioni.h

96 lines
2.2 KiB
C++

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