Move headers into include folder and add single header to include all reachability algorithms
This commit is contained in:
96
include/algorithm/roditty_zwick.h
Normal file
96
include/algorithm/roditty_zwick.h
Normal file
@@ -0,0 +1,96 @@
|
||||
#ifndef RODITTY_ZWICK_SCC_H_
|
||||
#define RODITTY_ZWICK_SCC_H_
|
||||
|
||||
#include "algorithm/decremental_reachability.h"
|
||||
#include "algorithm/tarjan.h"
|
||||
#include "graph/breadth_first_tree.h"
|
||||
|
||||
using namespace graph;
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class RodittyZwick : public DecrementalReachability<T> {
|
||||
public:
|
||||
RodittyZwick() = default;
|
||||
|
||||
RodittyZwick(Digraph<T> G) { this->G = G; }
|
||||
|
||||
//
|
||||
void init() override;
|
||||
|
||||
//
|
||||
void findSCC();
|
||||
|
||||
// Return true if u and v are in the same SCC
|
||||
bool query(const T& u, const T& v) override;
|
||||
|
||||
// Remove edge (u,v) and update A accordingly for fast checking query
|
||||
void remove(const T& u, const T& v) override;
|
||||
|
||||
std::unordered_map<T, SCC<T>> getSCCs() { return C; }
|
||||
private:
|
||||
// Array used to answer strong connectivity queries in O(1) time
|
||||
std::unordered_map<T, T> A;
|
||||
|
||||
// Connect each representative with its SCC
|
||||
std::unordered_map<T, SCC<T>> C;
|
||||
|
||||
// Maintain in-out bfs trees
|
||||
std::unordered_map<T, BreadthFirstTree<T>> In;
|
||||
std::unordered_map<T, BreadthFirstTree<T>> Out;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void RodittyZwick<T>::init() {
|
||||
findSCC();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void RodittyZwick<T>::findSCC() {
|
||||
auto SCCs = Tarjan<T>(this->G.adjList).execute();
|
||||
|
||||
for (auto& SCC : SCCs) {
|
||||
const auto& w = SCC.id;
|
||||
|
||||
for (const auto& v : SCC.vertices())
|
||||
A[v] = w;
|
||||
|
||||
Out[w] = BreadthFirstTree<T>(SCC, w);
|
||||
In[w] = BreadthFirstTree<T>(SCC.reverse(), w);
|
||||
|
||||
C[w] = SCC;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool RodittyZwick<T>::query(const T& u, const T& v) {
|
||||
return A[u] == A[v];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void RodittyZwick<T>::remove(const T& u, const T& v) {
|
||||
const auto& w = A[u];
|
||||
C[w].remove(u, v);
|
||||
this->G.remove(u, v);
|
||||
|
||||
// If u and v are not in the same SCC, do nothing
|
||||
if (A[u] != A[v]) return;
|
||||
|
||||
// If edge (u,v) is not contained in both inTree and outTree do nothing
|
||||
if (!In[w].adjList[u].contains(v) &&
|
||||
!Out[w].adjList[u].contains(v))
|
||||
return;
|
||||
|
||||
// Update In(w) and Out(w)
|
||||
Out[w] = BreadthFirstTree<T>(C[w], w);
|
||||
In[w] = BreadthFirstTree<T>(C[w].reverse(), w);
|
||||
|
||||
// If a SCC is broken, compute all SCCs again
|
||||
if (!In[w].adjList.count(u) || !Out[w].adjList.count(v))
|
||||
findSCC();
|
||||
}
|
||||
|
||||
}; // namespace algo
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user