Update decremental scc algorithm

This commit is contained in:
stefiosif
2022-09-12 00:08:45 +03:00
parent 5c1b13b400
commit 42f824a249
3 changed files with 57 additions and 151 deletions

View File

@@ -3,25 +3,30 @@
#include "algorithm/roditty_zwick.h"
#include "algorithm/tarjan.h"
#include "tree/breadth_first_tree.h"
#include "graph/breadth_first_tree.h"
using namespace graph;
using namespace tree;
namespace algo {
template<typename T>
class DecrementalSCC : public RodittyZwick<T> {
public:
DecrementalSCC() = default;
DecrementalSCC(Digraph<T> G) : G(G) {}
void init();
void findSCC();
// Return true if u and v are in the same SCC
bool query(const T& u, const T& v);
// Remove edge (u,v) and update A accordingly for fast checking query
void remove(const T& u, const T& v);
void setGraph(Digraph<T> G);
private:
Digraph<T> G;
@@ -33,7 +38,7 @@ private:
std::map<T, BreadthFirstTree<T>> outTree;
// Connect each representative with its SCC
std::map<T, SCC<T>> connection;
std::map<T, SCC<T>> SCC;
};
template<typename T>
@@ -43,20 +48,18 @@ void DecrementalSCC<T>::init() {
template<typename T>
void DecrementalSCC<T>::findSCC() {
auto SCCs = Tarjan<T>(G).execute();
auto SCCs = Tarjan<T>(G.adjMatrix).execute();
for (auto& C : SCCs) {
const auto& w = C.representative();
const auto& w = C.id;
for (const auto& v : C.adjMatrix)
A[v.first] = w;
for (const auto& v : std::views::keys(C.adjMatrix))
A[v] = w;
outTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C).execute(w));
inTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C.reverse()).execute(w));
outTree[w] = BreadthFirstTree<T>(C, w);
inTree[w] = BreadthFirstTree<T>(C.reverse(), w);
connection[w] = C;
SCC[w] = C;
}
}
@@ -67,27 +70,30 @@ bool DecrementalSCC<T>::query(const T& u, const T& v) {
template<typename T>
void DecrementalSCC<T>::remove(const T& u, const T& v) {
G.adjMatrix[u].erase(v);
const auto& w = A[u];
SCC[w].remove(u, v);
G.remove(u, v);
// If u and v are not in the same SCC, do nothing
if (A[u] != A[v]) return;
const auto& w = A[u];
connection[w].remove(u, v);
// If edge (u,v) is not contained in both inTree and outTree do nothing
if (!inTree[w].adjMatrix[u].contains(v) &&
!outTree[w].adjMatrix[u].contains(v))
return;
// Update In(w) and Out(w) if they contain the edge
if (inTree[w].contains(u, v) || outTree[w].contains(u, v)) {
auto C = connection[w];
inTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C.reverse()).execute(w));
outTree[w] =
BreadthFirstTree<T>(BreadthFirstSearch<T>(C).execute(w));
}
// Update In(w) and Out(w)
outTree[w] = BreadthFirstTree<T>(SCC[w], w);
inTree[w] = BreadthFirstTree<T>(SCC[w].reverse(), w);
// If a SCC is broken, compute all SCCs again
if (!inTree[w].contains(u) || !outTree[w].contains(v)) {
if (!inTree[w].adjMatrix.count(u) || !outTree[w].adjMatrix.count(v))
findSCC();
}
}
template<typename T>
void DecrementalSCC<T>::setGraph(Digraph<T> G) {
this->G = G;
}
}; // namespace algo

View File

@@ -65,7 +65,7 @@ void Tarjan<T>::strongConnect(const T& u) {
finished = (w == u);
} while (!finished);
SCCs.push_back({ scc, static_cast<T>(vmap[u].lowlink) });
SCCs.push_back({ scc, static_cast<T>(vmap[u].lowlink + 1) });
}
}

View File

@@ -8,48 +8,11 @@ using namespace graph;
TEST_SUITE("Decremental algorithms") {
TEST_CASE("Decremental maintenance of SCCs 1") {
// 1 --> 2 --> 5 --> 7 --> 2
// 1 --> 4 --> 3 --> 1
// 4 --> 6 --> 3
Digraph<std::uint16_t> G;
G.insert(1, 2);
G.insert(1, 4);
G.insert(2, 5);
G.insert(3, 1);
G.insert(4, 3);
G.insert(4, 6);
G.insert(5, 7);
G.insert(6, 3);
G.insert(7, 2);
REQUIRE_EQ(G.adjMatrix.size(), 7);
algo::DecrementalSCC<std::uint16_t> rz(G);
rz.init();
SUBCASE("DecrementalSCC::query") {
CHECK_EQ(rz.query(1, 2), false);
CHECK_EQ(rz.query(1, 3), true);
CHECK_EQ(rz.query(1, 7), false);
CHECK_EQ(rz.query(2, 3), false);
CHECK_EQ(rz.query(2, 5), true);
CHECK_EQ(rz.query(5, 7), true);
}
SUBCASE("DecrementalSCC::remove") {
rz.remove(4,3);
CHECK_EQ(rz.query(4, 3), true);
rz.remove(2, 5);
CHECK_EQ(rz.query(5, 7), false);
}
}
TEST_CASE("Decremental maintenance of SCCs 2") {
TEST_CASE("Decremental maintenance of SCCs") {
// 1 --> 2 --> 3 --> 1
// 3 --> 4 --> 5 --> 3
// 2 --> 6 --> 7 --> 8 --> 6
// 7 --> 9
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
// 12 --> 10
Digraph<std::uint16_t> G;
@@ -74,61 +37,38 @@ TEST_SUITE("Decremental algorithms") {
REQUIRE_EQ(G.adjMatrix.size(), 13);
algo::DecrementalSCC<std::uint16_t> rz(G);
rz.init();
algo::DecrementalSCC<std::uint16_t> decremental(G);
decremental.init();
SUBCASE("DecrementalSCC::query") {
CHECK_EQ(rz.query(1, 2), true);
CHECK_EQ(rz.query(1, 3), true);
CHECK_EQ(rz.query(1, 7), false);
CHECK_EQ(rz.query(2, 3), true);
CHECK_EQ(rz.query(2, 13), false);
CHECK_EQ(rz.query(11, 9), true);
CHECK_EQ(decremental.query(1, 5), true);
CHECK_EQ(decremental.query(1, 7), false);
CHECK_EQ(decremental.query(1, 9), false);
}
SUBCASE("DecrementalSCC::remove") {
rz.remove(12, 10);
CHECK_EQ(rz.query(12, 10), true);
decremental.remove(12, 10);
rz.remove(3, 4);
CHECK_EQ(rz.query(2, 5), false);
CHECK_EQ(rz.query(1, 3), true);
CHECK_EQ(decremental.query(12, 10), true);
CHECK_EQ(decremental.query(11, 10), true);
decremental.remove(9, 10);
CHECK_EQ(decremental.query(12, 13), false);
CHECK_EQ(decremental.query(10, 12), false);
decremental.remove(2, 6);
CHECK_EQ(decremental.query(5, 8), false);
CHECK_EQ(decremental.query(6, 8), true);
}
}
TEST_CASE("Italiano 1") {
// 1 --> 2 --> 5 --> 7 --> 2
// 1 --> 4 --> 3 --> 1
// 4 --> 6 --> 3
Digraph<std::uint16_t> G;
G.insert(1, 2);
G.insert(1, 4);
G.insert(2, 5);
G.insert(3, 1);
G.insert(4, 3);
G.insert(4, 6);
G.insert(5, 7);
G.insert(6, 3);
G.insert(7, 2);
REQUIRE_EQ(G.adjMatrix.size(), 7);
algo::Italiano<std::uint16_t> rz(G);
rz.init();
SUBCASE("Italiano::query") {
}
SUBCASE("Italiano::remove") {
}
}
TEST_CASE("Italiano 2") {
TEST_CASE("Italiano") {
// 1 --> 2 --> 3 --> 1
// 3 --> 4 --> 5 --> 3
// 2 --> 6 --> 7 --> 8 --> 6
// 7 --> 9
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
// 12 --> 10
Digraph<std::uint16_t> G;
@@ -169,6 +109,7 @@ TEST_SUITE("Decremental algorithms") {
// 1 --> 2 --> 3 --> 1
// 3 --> 4 --> 5 --> 3
// 2 --> 6 --> 7 --> 8 --> 6
// 7 --> 9
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
// 12 --> 10
Digraph<std::uint16_t> G;
@@ -204,45 +145,4 @@ TEST_SUITE("Decremental algorithms") {
}
}
TEST_CASE("Frigioni 2") {
// 1 --> 2 --> 3 --> 1
// 3 --> 4 --> 5 --> 3
// 2 --> 6 --> 7 --> 8 --> 6
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
// 12 --> 10
Digraph<std::uint16_t> G;
G.insert(1, 2);
G.insert(2, 3);
G.insert(3, 1);
G.insert(3, 4);
G.insert(4, 5);
G.insert(5, 3);
G.insert(2, 6);
G.insert(6, 7);
G.insert(7, 8);
G.insert(7, 9);
G.insert(8, 6);
G.insert(6, 9);
G.insert(9, 10);
G.insert(10, 11);
G.insert(11, 12);
G.insert(12, 13);
G.insert(13, 9);
G.insert(12, 10);
REQUIRE_EQ(G.adjMatrix.size(), 13);
algo::Frigioni<std::uint16_t> rz(G);
rz.init();
SUBCASE("Frigioni::query") {
}
SUBCASE("Frigioni::remove") {
}
}
}