Connected component with iterative DFS
Problem
Implement component(graph, start), returning the set of vertices reachable from start in an adjacency dictionary. Use an explicit stack.
Starter code
def component(graph, start):
passReveal answer or reference solution
def component(graph, start):
stack = [start]
seen = set()
while stack:
node = stack.pop()
if node in seen:
continue
seen.add(node)
stack.extend(graph.get(node, []))
return seenPublic tests
component({1:[2],2:[1,3],3:[2],4:[]}, 1)→{1, 2, 3}component({}, 5)→{5}
Local history
Loading attempts saved in this browser…
Use with your agent
Share this URL and your attempt. Ask the agent to start with a clarifying question or the smallest useful hint.
Tutor me on https://mlprep.iwase.dev/programming/data-structures/original-py-dfs/. If window.mlPrepAgent is available, read attempts for item original-py-dfs before tutoring. Inspect my attempt, keep the item ID, and do not reveal the full answer first. After a real attempt, append its record and read it back.