Skip to content

Re-adding Tarjan's SCC algorithm in correct path #2973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions graph/strongly_connected_components.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @file strongly_connected_components.cpp
* @author Kia Pousti
* @brief Tarjan’s Algorithm to find Strongly Connected Components (SCC) in a directed graph
* @date 2025-07-29
*/

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

class TarjanSCC {
private:
int V, time;
vector<vector<int> > adj;
vector<int> disc, low;
vector<bool> inStack;
stack<int> stk;
vector<vector<int> > components;

void dfs(int u) {
disc[u] = low[u] = time++;
stk.push(u);
inStack[u] = true;

for (int v : adj[u]) {
if (disc[v] == -1) {
dfs(v);
low[u] = min(low[u], low[v]);
} else if (inStack[v]) {
low[u] = min(low[u], disc[v]);
}
}

if (disc[u] == low[u]) {
vector<int> scc;
int v;
do {
v = stk.top(); stk.pop();
inStack[v] = false;
scc.push_back(v);
} while (u != v);
components.push_back(scc);
}
}

public:
TarjanSCC(int V) : V(V), time(0), adj(V), disc(V, -1), low(V, -1), inStack(V, false) {}

void addEdge(int u, int v) {
adj[u].push_back(v);
}

vector<vector<int> > getSCCs() {
for (int i = 0; i < V; ++i) {
if (disc[i] == -1)
dfs(i);
}
return components;
}
};

int main() {
TarjanSCC g(5);
g.addEdge(1, 0);
g.addEdge(0, 2);
g.addEdge(2, 1);
g.addEdge(0, 3);
g.addEdge(3, 4);

auto sccs = g.getSCCs();

// Assert the expected number of SCCs
assert(sccs.size() == 3);

// Print SCCs
cout << "Strongly Connected Components:\n";
for (const auto& scc : sccs) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests should be in a tests function

for (int node : scc)
cout << node << " ";
cout << "\n";
}

return 0;
}