Update decremental maintenance of SCC algorithm
This commit is contained in:
@@ -20,40 +20,71 @@ public:
|
||||
void init();
|
||||
|
||||
//
|
||||
void query();
|
||||
bool query(const T& u, const T& v);
|
||||
|
||||
//
|
||||
void remove();
|
||||
void remove(const T& u, const T& v);
|
||||
private:
|
||||
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>
|
||||
void DecrementalMaintenance<T>::init() {
|
||||
std::map<T, T> A;
|
||||
Tarjan<T> tarjan(G);
|
||||
auto tarjan = Tarjan<T>(G).run();
|
||||
|
||||
auto sccs = tarjan.run();
|
||||
for (auto& C : tarjan) {
|
||||
const auto& w = C.representative();
|
||||
|
||||
for (auto& scc : sccs) {
|
||||
const auto& root = scc.representative();
|
||||
BFS<T> bfs(scc);
|
||||
auto spt = bfs.run(root);
|
||||
for (const auto& vertex : scc.vertices) {
|
||||
A[vertex] = root;
|
||||
}
|
||||
// Create shortest-paths out-tree/in-tree
|
||||
auto outTree = BFS<T>(C).run(w);
|
||||
SPT[w] = std::make_pair(outTree, outTree.reverse());
|
||||
|
||||
// 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>
|
||||
inline void DecrementalMaintenance<T>::query()
|
||||
{
|
||||
bool DecrementalMaintenance<T>::query(const T& u, const T& v) {
|
||||
return A[u] == A[v];
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -14,10 +14,10 @@ public:
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user