Created
May 18, 2022 16:52
-
-
Save theabbie/a3d77310e6b1e2023296c6b7642ca669 to your computer and use it in GitHub Desktop.
Kosaraju's Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
class Solution: | |
def DFS(self, graph, node, v, s): | |
v.add(node) | |
for j in graph[node]: | |
if j not in v: | |
self.DFS(graph, j, v, s) | |
s.append(node) | |
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]: | |
graph = defaultdict(list) | |
transpose = defaultdict(list) | |
for a, b in connections: | |
graph[a].append(b) | |
transpose[b].append(a) | |
s = [] | |
self.DFS(graph, 0, set(), s) | |
while len(s) > 0: | |
curr = s.pop() | |
v = set() | |
self.DFS(transpose, curr, v, []) | |
print(v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment