Back to Practice Dashboard
DSA SectionEasy

BFS

Learn how to solve the 'BFS' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Write a function bfs_traversal(graph, start) that performs a Breadth-First Search traversal on a graph (represented as an adjacency list of neighbor lists) starting from the node start. Return a list of nodes in the order they were first visited.

Constraints
  • 1 <= V <= 500
  • 0 <= E <= 1000

Examples

Example 1
Input
graph = {0: [1, 2], 1: [2], 2: [0, 3], 3: [3]}, start = 2
Output
[2, 0, 3, 1]
Explanation

Starting at 2, we visit neighbors 0 and 3. Then from 0 we visit neighbor 1. 2 has already been visited.

Example 2
Input
graph = {0: [1], 1: []}, start = 0
Output
[0, 1]
Need a Hint?
Represent graph node connections as an adjacency list/matrix, then use standard BFS or DFS graph traversal.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor