Jobs
Interviews

Solve DFS of Graph using Python Language

Solve DFS of Graph using Python to enhance your skills with python coding practice , master coding concepts, and prepare for interviews with practical exercises and detailed solutions.

DFS of Graph

Difficulty : Easy

Categories :

  • Graphs

Given a connected undirected graph represented as an adjacency list adj, perform a Depth First Search (DFS) traversal starting from vertex 0. Return a list containing the DFS traversal of the graph.

The traversal should visit neighbors in the same order as they appear in the adjacency list.

Constraints:

  • 1 ≤ adj.size() ≤ 104
  • 1 ≤ adj[i][j] ≤ 104
  • Graph is connected and undirected
  • Start vertex is always 0

Examples:

Input: adj = [[2,3,1], [0], [0,4], [0], [2]]
Output: [0,2,4,3,1]
Explanation: DFS path:
1. Start at 0
2. Visit first neighbor 2, then its neighbor 4
3. Backtrack to 0, visit 3
4. Backtrack to 0, visit 1
Input: adj = [[1,2], [0,2], [0,1,3,4], [2], [2]]
Output: [0,1,2,3,4]
Explanation: Follow adjacency list order for neighbor selection

Coding works best on a laptop or desktop, with a keyboard and a wider editor. You can read the problem here and solve it there.

Problem Solving

Input

What You'll Find Here

Interactive Exercises Practice coding with problems designed for beginners and experts.

Step-by-Step Solutions Understand every step of the solution process.

Real-World Scenarios Apply your skills to real-world problems and boost your confidence.

Choose from the following categories