Untitled
Anonymous
python
12/23/2024 3:23 PM
608 B
14
Indexable
def dfs(n, graph):
# find largest connected component that include node n
q = [(n, {n})]
seen = set(tuple([n]))
while q:
node, req = q.pop()
if tuple(sorted(req)) in seen:
continue
seen.add(tuple(sorted(req)))
for nb in graph[node]:
if nb in req: continue
if not req <= graph[nb]: continue
q.append((nb, req | {nb}))
return max(seen, key=len)Editor is loading...
Leave a Comment