#ifndef FRIGIONI_H_ #define FRIGIONI_H_ #include "algorithm/roditty_zwick.h" #include "algorithm/tarjan.h" #include "graph/breadth_first_tree.h" #include "algorithm/decremental_scc.h" #include #include #include using namespace graph; namespace algo { template class Frigioni : public RodittyZwick { public: Frigioni(Digraph G) : G(G) {} void init(); bool query(const T& u, const T& v); void remove(const T& u, const T& v); private: Digraph G; // Transitive closure matrix, used to answer reachability queries in O(1) std::map> TC; // Connect each vertex with its representative SCC std::map> C; // Each scc's representative vertex reachability tree std::map> RT; // Incoming / Outgoing / Internal edges struct Edges { std::set> in; std::set> inc; std::set> out; }; std::map E; }; template void Frigioni::init() { auto SCCs = Tarjan(G.adjMatrix).execute(); for (auto& scc : SCCs) { RT[scc.id] = BreadthFirstTree(G, scc.id); for (const auto& u : G.vertices()) { C[u] = scc; for (const auto& v : 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 : G.vertices()) { if (scc.member(u)) { for (const auto& v : RT[scc.id].vertices()) { TC[u][v] = true; } } } } } template bool Frigioni::query(const T& u, const T& v) { return TC[u][v]; } template void Frigioni::remove(const T& u, const T& v) { } } // namespace algo #endif