Complete Frigioni::query and Frigioni::init and tests

This commit is contained in:
stefiosif
2022-09-21 19:35:23 +03:00
parent 813c63a4ea
commit ffc3ae4ec3
2 changed files with 49 additions and 17 deletions

View File

@@ -3,12 +3,14 @@
#include "algorithm/roditty_zwick.h"
#include "algorithm/tarjan.h"
#include "tree/breadth_first_tree.h"
#include "graph/breadth_first_tree.h"
#include "algorithm/decremental_scc.h"
#include <forward_list>
#include <utility>
#include <iostream>
using namespace graph;
using namespace tree;
namespace algo {
@@ -25,30 +27,59 @@ public:
private:
Digraph<T> G;
// Connect each vertex with its representative SCC
std::map<T, SCC<T>> TC;
// Transitive closure matrix, used to answer reachability queries in O(1)
std::map<T, std::map<T, bool>> TC;
// Reachability trees
std::map<SCC<T>, std::set<SCC<T>>> RT;
// 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::forward_list<T> Ein;
std::forward_list<T> Eout;
std::forward_list<T> Eint;
std::set<std::pair<T, T>> in;
std::set<std::pair<T, T>> inc;
std::set<std::pair<T, T>> out;
};
std::map<SCC<T>, Edges> E;
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] == TC[v];
return TC[u][v];
}
template<typename T>