Improve readability

This commit is contained in:
stefiosif
2023-02-10 18:26:48 +02:00
parent 2175ac0ca9
commit 3591c58f6d
6 changed files with 217 additions and 158 deletions

View File

@@ -18,7 +18,7 @@ public:
//
auto execute();
//
void strongConnect(const T& u);
private:
@@ -26,33 +26,33 @@ private:
std::stack<T> S;
std::int16_t index = 0;
std::vector<graph::SCC<T>> SCCs;
T cid;
T cid{};
struct Vertex {
int index = -1;
int lowlink = -1;
bool onStack = false;
};
std::unordered_map<T, Vertex> vmap;
std::unordered_map<T, Vertex> V;
};
template<typename T>
void Tarjan<T>::strongConnect(const T& u) {
vmap[u].index = vmap[u].lowlink = index++;
V[u].index = V[u].lowlink = index++;
S.push(u);
vmap[u].onStack = true;
V[u].onStack = true;
for (const auto& w : adjList[u]) {
if (vmap[w].index == -1) {
if (V[w].index == -1) {
strongConnect(w);
vmap[u].lowlink = std::min(vmap[u].lowlink, vmap[w].lowlink);
} else if (vmap[w].onStack) {
vmap[u].lowlink = std::min(vmap[u].lowlink, vmap[w].index);
V[u].lowlink = std::min(V[u].lowlink, V[w].lowlink);
} else if (V[w].onStack) {
V[u].lowlink = std::min(V[u].lowlink, V[w].index);
}
}
// If u is a root node, pop the stack and generate an SCC
if (vmap[u].lowlink == vmap[u].index) {
if (V[u].lowlink == V[u].index) {
std::unordered_map<T, std::unordered_set<T>> scc;
bool finished = false;
cid = S.top();
@@ -60,7 +60,7 @@ void Tarjan<T>::strongConnect(const T& u) {
do {
const auto w = S.top();
S.pop();
vmap[w].onStack = false;
V[w].onStack = false;
scc[w] = adjList[w];
finished = (w == u);
} while (!finished);
@@ -72,7 +72,7 @@ void Tarjan<T>::strongConnect(const T& u) {
template<typename T>
auto Tarjan<T>::execute() {
for (const auto& u : std::views::keys(adjList)) {
if (vmap[u].index == -1)
if (V[u].index == -1)
strongConnect(u);
}
return SCCs;