Skip to content
DeepForgeby svx
All problems
HardGraph Algorithmsgraph-039

SCC Count (Kosaraju)

Problem

Count the strongly connected components of a directed graph with n nodes.

Run Kosaraju's algorithm: DFS to get finish order, then DFS on the reversed graph in reverse finish order. edges is a list of [u, v] pairs.

Hint

Components of the reversed graph processed in reverse finish order come out one SCC at a time.

Test cases

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

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