Files
reachability-algorithms/algorithm/frigioni.h

92 lines
2.0 KiB
C++

#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 <forward_list>
#include <utility>
#include <iostream>
using namespace graph;
namespace algo {
template<typename T>
class Frigioni : public RodittyZwick<T> {
public:
Frigioni(Digraph<T> G) : G(G) {}
void init();
bool query(const T& u, const T& v);
void remove(const T& u, const T& v);
private:
Digraph<T> G;
// 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
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;
};
template<typename T>
void Frigioni<T>::init() {
auto SCCs = Tarjan<T>(G.adjMatrix).execute();
for (auto& scc : SCCs) {
RT[scc.id] = BreadthFirstTree<T>(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<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