82 lines
1.6 KiB
C++
82 lines
1.6 KiB
C++
#ifndef TARJAN_H_
|
|
#define TARJAN_H_
|
|
|
|
#include "graph/scc.h"
|
|
|
|
#include <ranges>
|
|
#include <stack>
|
|
#include <vector>
|
|
|
|
namespace algo {
|
|
|
|
template <typename T> class Tarjan {
|
|
public:
|
|
Tarjan() = default;
|
|
|
|
explicit Tarjan(std::unordered_map<T, std::unordered_set<T>> adjList)
|
|
: adjList(adjList) {}
|
|
|
|
//
|
|
auto execute();
|
|
|
|
//
|
|
void strongConnect(const T &u);
|
|
|
|
private:
|
|
std::unordered_map<T, std::unordered_set<T>> adjList;
|
|
std::stack<T> S;
|
|
std::int16_t index = 0;
|
|
std::vector<graph::SCC<T>> SCCs;
|
|
T cid{};
|
|
|
|
struct Vertex {
|
|
int index = -1;
|
|
int lowlink = -1;
|
|
bool onStack = false;
|
|
};
|
|
std::unordered_map<T, Vertex> V;
|
|
};
|
|
|
|
template <typename T> void Tarjan<T>::strongConnect(const T &u) {
|
|
V[u].index = V[u].lowlink = index++;
|
|
S.push(u);
|
|
V[u].onStack = true;
|
|
|
|
for (const auto &w : adjList[u]) {
|
|
if (V[w].index == -1) {
|
|
strongConnect(w);
|
|
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 (V[u].lowlink == V[u].index) {
|
|
std::unordered_map<T, std::unordered_set<T>> scc;
|
|
bool finished = false;
|
|
cid = S.top();
|
|
|
|
do {
|
|
const auto w = S.top();
|
|
S.pop();
|
|
V[w].onStack = false;
|
|
scc[w] = adjList[w];
|
|
finished = (w == u);
|
|
} while (!finished);
|
|
|
|
SCCs.push_back({scc, static_cast<T>(cid)});
|
|
}
|
|
}
|
|
|
|
template <typename T> auto Tarjan<T>::execute() {
|
|
for (const auto &u : std::views::keys(adjList)) {
|
|
if (V[u].index == -1)
|
|
strongConnect(u);
|
|
}
|
|
return SCCs;
|
|
}
|
|
|
|
} // namespace algo
|
|
|
|
#endif |