Update decremental maintenance of SCC algorithm
This commit is contained in:
@@ -20,40 +20,71 @@ public:
|
|||||||
void init();
|
void init();
|
||||||
|
|
||||||
//
|
//
|
||||||
void query();
|
bool query(const T& u, const T& v);
|
||||||
|
|
||||||
//
|
//
|
||||||
void remove();
|
void remove(const T& u, const T& v);
|
||||||
private:
|
private:
|
||||||
Digraph<T> G;
|
Digraph<T> G;
|
||||||
|
|
||||||
|
// Array used to answer strong connectivity queries in O(1) time
|
||||||
|
std::map<T, T> A;
|
||||||
|
|
||||||
|
// Connect SCC/SCC representative with 2 SPTs
|
||||||
|
// pair.first: Shortest-paths out-tree
|
||||||
|
// pair.second: Shortest-paths in-tree
|
||||||
|
std::map<T, std::pair<Digraph<T>, Digraph<T>>> SPT;
|
||||||
|
|
||||||
|
// Connect each representative with its SCC
|
||||||
|
std::map<T, SCC<T>> SCCs;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void DecrementalMaintenance<T>::init() {
|
void DecrementalMaintenance<T>::init() {
|
||||||
std::map<T, T> A;
|
auto tarjan = Tarjan<T>(G).run();
|
||||||
Tarjan<T> tarjan(G);
|
|
||||||
|
|
||||||
auto sccs = tarjan.run();
|
for (auto& C : tarjan) {
|
||||||
|
const auto& w = C.representative();
|
||||||
|
|
||||||
for (auto& scc : sccs) {
|
// Create shortest-paths out-tree/in-tree
|
||||||
const auto& root = scc.representative();
|
auto outTree = BFS<T>(C).run(w);
|
||||||
BFS<T> bfs(scc);
|
SPT[w] = std::make_pair(outTree, outTree.reverse());
|
||||||
auto spt = bfs.run(root);
|
|
||||||
for (const auto& vertex : scc.vertices) {
|
|
||||||
A[vertex] = root;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Update A with current SCCs vertices
|
||||||
|
for (const auto& v : C.vertices)
|
||||||
|
A[v] = w;
|
||||||
|
|
||||||
|
// Link SCC with its representative w
|
||||||
|
SCCs[w] = C;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline void DecrementalMaintenance<T>::query()
|
bool DecrementalMaintenance<T>::query(const T& u, const T& v) {
|
||||||
{
|
return A[u] == A[v];
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline void DecrementalMaintenance<T>::remove()
|
void DecrementalMaintenance<T>::remove(const T& u, const T& v) {
|
||||||
{
|
// If u and v are not in the same SCC, do nothing
|
||||||
|
if (A[u] != A[v]) return;
|
||||||
|
|
||||||
|
const auto& w = A[u];
|
||||||
|
|
||||||
|
// If edge e(u, v) is not contained in In(w) and Out(w), do nothing
|
||||||
|
if (!SPT[w].first.vertices.contains(w) &&
|
||||||
|
!SPT[w].second.vertices.contains(w))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Update In(w) and Out(w)
|
||||||
|
auto inTree = BFS<T>(SCCs[w]).run(w);
|
||||||
|
SPT[w] = std::make_pair(inTree, inTree.reverse());
|
||||||
|
|
||||||
|
if (!SPT[w].second.vertices.contains(u) ||
|
||||||
|
!SPT[w].first.vertices.contains(v)) {
|
||||||
|
// TODO: Decompose C into C1, C2, ... Ck
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}; // namespace algo
|
}; // namespace algo
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ public:
|
|||||||
virtual void init() =0;
|
virtual void init() =0;
|
||||||
|
|
||||||
//
|
//
|
||||||
virtual void query() =0;
|
virtual bool query(const T& u, const T& v) =0;
|
||||||
|
|
||||||
//
|
//
|
||||||
virtual void remove() =0;
|
virtual void remove(const T& u, const T& v) =0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ namespace graph {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
class SCC : public Graph<T> {
|
class SCC : public Graph<T> {
|
||||||
public:
|
public:
|
||||||
|
SCC() = default;
|
||||||
|
|
||||||
SCC(std::map<T, std::set<T>> scc);
|
SCC(std::map<T, std::set<T>> scc);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include "graph/scc.h"
|
#include "graph/scc.h"
|
||||||
#include "algorithm/tarjan.h"
|
#include "algorithm/tarjan.h"
|
||||||
#include "algorithm/bfs.h"
|
#include "algorithm/bfs.h"
|
||||||
#include "algorithm/roditty_zwick.h"
|
|
||||||
#include "algorithm/decremental_maintenance.h"
|
#include "algorithm/decremental_maintenance.h"
|
||||||
|
|
||||||
using namespace graph;
|
using namespace graph;
|
||||||
@@ -133,5 +132,7 @@ TEST_SUITE("Algorithm") {
|
|||||||
|
|
||||||
algo::DecrementalMaintenance<std::uint16_t> rz(G);
|
algo::DecrementalMaintenance<std::uint16_t> rz(G);
|
||||||
rz.init();
|
rz.init();
|
||||||
|
|
||||||
|
//CHECK_EQ(rz.query(1, 2), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user