diff --git a/graphs/scc_kosaraju.py b/graphs/scc_kosaraju.py index 573c1bf5e..2b3417014 100644 --- a/graphs/scc_kosaraju.py +++ b/graphs/scc_kosaraju.py @@ -1,25 +1,28 @@ +from typing import List + + def dfs(u): - global g, r, scc, component, visit, stack + global graph, reversedGraph, scc, component, visit, stack if visit[u]: return visit[u] = True - for v in g[u]: + for v in graph[u]: dfs(v) stack.append(u) def dfs2(u): - global g, r, scc, component, visit, stack + global graph, reversedGraph, scc, component, visit, stack if visit[u]: return visit[u] = True component.append(u) - for v in r[u]: + for v in reversedGraph[u]: dfs2(v) def kosaraju(): - global g, r, scc, component, visit, stack + global graph, reversedGraph, scc, component, visit, stack for i in range(n): dfs(i) visit = [False] * n @@ -36,16 +39,16 @@ if __name__ == "__main__": # n - no of nodes, m - no of edges n, m = list(map(int, input().strip().split())) - g = [[] for i in range(n)] # graph - r = [[] for i in range(n)] # reversed graph + graph: List[List[int]] = [[] for i in range(n)] # graph + reversedGraph: List[List[int]] = [[] for i in range(n)] # reversed graph # input graph data (edges) for i in range(m): u, v = list(map(int, input().strip().split())) - g[u].append(v) - r[v].append(u) + graph[u].append(v) + reversedGraph[v].append(u) - stack = [] - visit = [False] * n - scc = [] - component = [] + stack: List[int] = [] + visit: List[bool] = [False] * n + scc: List[int] = [] + component: List[int] = [] print(kosaraju())