Complete Frigioni::query and Frigioni::init and tests
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
|
||||
using namespace graph;
|
||||
|
||||
TEST_SUITE("Decremental algorithms") {
|
||||
TEST_SUITE("Decremental Algorithm") {
|
||||
|
||||
TEST_CASE("Decremental maintenance of SCCs") {
|
||||
TEST_CASE("DecrementalSCC") {
|
||||
// 1 --> 2 --> 3 --> 1
|
||||
// 3 --> 4 --> 5 --> 3
|
||||
// 2 --> 6 --> 7 --> 8 --> 6
|
||||
@@ -112,7 +112,7 @@ TEST_SUITE("Decremental algorithms") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Frigioni 1") {
|
||||
TEST_CASE("Frigioni") {
|
||||
// 1 --> 2 --> 4 --> 6 --> 8 --> 9 --> 5
|
||||
// 4 --> 5 --> 7 --> 8
|
||||
// 5 --> 6
|
||||
@@ -141,15 +141,16 @@ TEST_SUITE("Decremental algorithms") {
|
||||
|
||||
algo::Frigioni<std::uint16_t> frigioni(G);
|
||||
frigioni.init();
|
||||
/*
|
||||
|
||||
SUBCASE("Frigioni::query") {
|
||||
CHECK_EQ(frigioni.query(1, 9), true);
|
||||
CHECK_EQ(frigioni.query(2, 8), true);
|
||||
CHECK_EQ(frigioni.query(3, 9), true);
|
||||
CHECK_EQ(frigioni.query(9, 5), true);
|
||||
CHECK_EQ(frigioni.query(4, 3), false);
|
||||
CHECK_EQ(frigioni.query(5, 4), false);
|
||||
CHECK_EQ(frigioni.query(6, 1), false);
|
||||
}*/
|
||||
}
|
||||
/*
|
||||
SUBCASE("Frigioni::remove") {
|
||||
frigioni.remove(4, 6);
|
||||
Reference in New Issue
Block a user