36std::vector<std::vector<typename G::id_type>>
tarjan(
const G& graph)
38 using id_type =
typename G::id_type;
40 std::vector<std::vector<id_type>> sccs;
41 std::map<id_type, int> disc;
42 std::map<id_type, int> low;
43 std::map<id_type, bool> on_stack;
44 std::stack<id_type> st;
47 std::function<void(id_type)> strongconnect = [&](id_type node) {
48 disc[node] = low[node] = ++time;
50 on_stack[node] =
true;
53 for (id_type neighbor : graph.get_neighbors(node)) {
54 if (disc.find(neighbor) == disc.end()) {
56 strongconnect(neighbor);
57 low[node] = std::min(low[node], low[neighbor]);
58 }
else if (on_stack[neighbor]) {
60 low[node] = std::min(low[node], disc[neighbor]);
67 if (low[node] == disc[node]) {
68 std::vector<id_type> component;
74 component.push_back(w);
76 sccs.push_back(component);
81 for (
const auto& node : graph.get_nodes()) {
82 if (disc.find(node) == disc.end()) {