Back to Practice Dashboard
DSA SectionEasy

DFS

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

Problem Statement

Easy

Write a function dfs_traversal(graph, start) that performs a Depth-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. In case of multiple neighbors, visit them in the order they appear in the adjacency list.

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, 1, 3]
Explanation

From 2, we visit neighbor 0. From 0, we visit neighbor 1. Neighbor 2 of 1 is already visited. Backtrack to 0, then to 2, then visit neighbor 3.

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