Skip to content
DeepForgeby svx
All problems
HardGraph Algorithmsgraph-055

Tarjan SCC Iterative Count

Problem

Count the strongly connected components of a directed graph with n nodes using an iterative Tarjan algorithm.

Track discovery indices, low-link values, and an explicit DFS work stack so no recursion is used. When low[v] equals index[v], pop the component off the component stack. edges is a list of [u, v] pairs.

Hint

Emit an SCC whenever low[v] == index[v] and pop the component stack down to v.

Test cases

case 1
in: [5,[[0,1],[1,2],[2,0],[3,4],[4,3]]]
expected: 2
case 2
in: [4,[[0,1],[1,2],[2,3]]]
expected: 4
case 3
in: [1,[]]
expected: 1
case 4
in: [3,[[0,1],[1,2],[2,0],[0,2]]]
expected: 1
case 5
in: [6,[[0,1],[1,2],[2,1],[3,4],[4,5],[5,3],[1,3]]]
expected: 3
Starter codePython
def tarjan_scc_count(n, edges):
    # Your code here
    pass

5 test cases · run your code against them in the editor