Untitled
unknown
python
a year ago
455 B
7
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