Iam_Sandeep

find no of strongly connected components using kosaraju algorithm

Jul 13th, 2021 (edited)
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. """
  2. Given a Directed Graph with V vertices (Numbered from 0 to V-1) and E edges, Find the number of strongly connected components in the graph.
  3. You don't need to read input or print anything. Your task is to complete the function kosaraju() which takes the number of vertices V and adjacency list of the graph as inputs and returns an integer denoting the number of strongly connected components in the given graph.
  4. Expected Time Complexity: O(V+E).
  5. Expected Auxiliary Space: O(V).
  6. Procedure:
  7. 1.Find out the dfs traversal and store the processed(black) vertices in a stack .
  8. 2.Transpose matrix
  9. 3.pop each node from q and do dfs(called as ndfs in code) traversal of trans list if node is not visited
  10. 4. Count no of times the ndfs traversal took place which is nothing but no of SCC.Roots of these SSC are the ones that ndfs traversal through which has taken place
  11. """
  12.  
  13.  
  14. #User function Template for python3
  15. from collections import deque
  16. class Solution:
  17.    
  18.     #Function to find number of strongly connected components in the graph.
  19.     def kosaraju(self, v, adj):
  20.         def transpose():
  21.             t={i:[] for i in range(v)}
  22.             for i in range(v):
  23.                 for j in adj[i]:
  24.                     t[j].append(i)
  25.             return t
  26.            
  27.         def dfs(src,q):
  28.             vis[src]=True
  29.             for i in adj[src]:
  30.                 if vis[i]==False:
  31.                     dfs(i,q)
  32.             q.append(src)
  33.         def ndfs(src):
  34.             vis[src]=True
  35.             for i in tran[src]:
  36.                 if vis[i]==False:
  37.                     ndfs(i)
  38.         q=deque()
  39.         vis=[False for i in range(v)]
  40.         for i in range(v):
  41.             if vis[i]==False:
  42.                 dfs(i,q)
  43.         for i in range(v):
  44.             vis[i]=False
  45.         tran={}
  46.         tran=transpose()
  47.         count=0
  48.         while q:
  49.             t=q.pop()
  50.             if vis[t]==False:
  51.                 count+=1
  52.                 ndfs(t)
  53.         return count
  54.            
  55.            
  56.        
  57.  
  58. #{
  59. #  Driver Code Starts
  60. #Initial Template for Python 3
  61.  
  62. from collections import OrderedDict
  63. import sys
  64.  
  65. sys.setrecursionlimit(10**6)
  66. if __name__ == '__main__':
  67.     t = int(input())
  68.     for i in range(t):
  69.         V,E = list(map(int, input().strip().split()))
  70.         adj = [[] for i in range(V)]
  71.         for i in range(E):
  72.             a,b = map(int,input().strip().split())
  73.             adj[a].append(b)
  74.         ob = Solution()
  75.        
  76.         print(ob.kosaraju(V, adj))
  77. # } Driver Code Ends
Add Comment
Please, Sign In to add comment