Graph BFS - Visit Order

graphs · function

Description

Given an adjacency-list graph and a start node, return the order in
which nodes are visited by breadth-first search starting from `start`.

Enqueue neighbors in the order they appear in graph[node]. Mark a node
visited when you enqueue it (not when you dequeue) to avoid duplicates.
Only nodes reachable from `start` should appear.

Example:
    graph = {0: [1, 2], 1: [0, 3, 4], 2: [0], 3: [1], 4: [1]}
    start = 0
    BFS order: [0, 1, 2, 3, 4]

Constraints

- Nodes are integers
- Graph may contain cycles
- Graph may be disconnected (only return the component reachable from start)
- 0 <= number of nodes <= 1000
solution.py
output
Run the cases to see results here.