Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-003

BFS Traversal Order

Problem

Return the order in which nodes are visited by a breadth-first traversal starting at start.

The graph is undirected with n nodes. Visit neighbors in ascending order and skip nodes unreachable from start.

Hint

Use a FIFO queue and mark nodes when you enqueue them.

Test cases

case 1
in: [6,[[0,1],[0,2],[1,3],[2,3],[3,4]],0]
expected: [0,1,2,3,4]
case 2
in: [4,[[0,1],[1,2],[2,3]],1]
expected: [1,0,2,3]
case 3
in: [3,[[1,2]],0]
expected: [0]
case 4
in: [1,[],0]
expected: [0]
Starter codePython
def bfs_order(n, edges, start):
    # Your code here
    pass

4 test cases · run your code against them in the editor