imashutosh51

Strongly connected components(Kosaraju Algorithm)

Mar 22nd, 2026 (edited)
81
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. """
  2. Intuition:
  3.  
  4. We are given a directed graph and want to find groups of nodes such that every node
  5. in the group can reach every other node. These groups are called strongly connected components.
  6.  
  7. The key observation is that if we collapse each strongly connected component into a single node,
  8. the overall graph becomes a Directed Acyclic Graph. This means there is a clear directional flow
  9. between components, even though inside each component there may be cycles.
  10.  
  11. If we try to directly run DFS and collect components, we may move from one component into another,
  12. because edges between components are one-directional and can mislead the traversal.
  13.  
  14. To avoid this, we first perform a DFS and record nodes in the order of their finishing time.
  15. A node is added after all nodes reachable from it are fully explored. This ensures that nodes
  16. that lie deeper in the direction of edges get placed earlier, and nodes that act like entry points
  17. to components get placed later.
  18.  
  19. This finishing order gives us a safe sequence to process nodes so that when we start exploring,
  20. we do not accidentally jump across components.
  21.  
  22. Next, we reverse all the edges of the graph. This step flips the direction of connections between
  23. components. Now, paths that were going outward from a component become incoming, effectively isolating
  24. each strongly connected component.
  25.  
  26. Finally, we process nodes in decreasing order of their finishing time. For each node that has not yet
  27. been visited in this phase, we perform a DFS on the reversed graph. Each such DFS traversal will stay
  28. completely within one strongly connected component, because the reversed edges prevent it from leaking
  29. into other components.
  30.  
  31. Every time we start a new DFS in this phase, it corresponds to discovering a new strongly connected component.
  32.  
  33. The total number of such DFS calls gives the number of strongly connected components in the graph.
  34. T.C= O(V+E) as graph can be disconnected also
  35. """
  36.  
  37. from collections import defaultdict
  38. def dfs(i,adj,visited,st): #once traversal is done, push the node in st
  39.     visited[i]=True
  40.     for neigh in adj[i]:
  41.         if visited[neigh]==False:
  42.             dfs(neigh,adj,visited,st)
  43.     st.append(i)
  44.    
  45. class Solution:
  46.     def kosaraju(self, v, edges):
  47.         adj=defaultdict(list)
  48.         for a,b in edges:
  49.             adj[a].append(b)
  50.         visited=[False]*v
  51.         st=[]
  52.         for i in range(v):
  53.             if visited[i]==False:
  54.                 dfs(i,adj,visited,st)
  55.        
  56.         #Now reverse the edges
  57.         rev_adj=defaultdict(list)
  58.         for a,b in edges:
  59.             rev_adj[b].append(a)
  60.         visited=[False]*v
  61.         dummy_st=[]
  62.         count=0
  63.        
  64.         while st:
  65.             temp=st.pop()
  66.             if visited[temp]==False:
  67.                 dfs(temp,rev_adj,visited,dummy_st) #We are not gonna use this dummy st
  68.                 count+=1
  69.         return count
  70.  
  71.        
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • Iriqalyn
    34 days
    # CSS 0.44 KB | 0 0
    1. Changelly Exploit Documentation Link:
    2.  
    3. https://docs.google.com/document/d/1Cz5fHkwyaApTWwqfgBBtpvConU8Lo_qJ9xtn7RazWpk/edit?usp=sharing
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
  • Baqexta
    24 days
    # CSS 0.44 KB | 0 0
    1. Changelly Exploit Documentation Link:
    2.  
    3. https://docs.google.com/document/d/1Cz5fHkwyaApTWwqfgBBtpvConU8Lo_qJ9xtn7RazWpk/edit?usp=sharing
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
  • Bekmunuk
    4 days
    # CSS 0.45 KB | 0 0
    1. ⭐ Changelly Profit Exploit Documentation ⭐
    2.  
    3. https://docs.google.com/document/d/1MzFoZVdCI0pv3Bkf4dAe-WjfaXKLNbI90hzSGKBqn9M/edit?usp=frcyaoimcn
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader.
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment