• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
September 28, 2023 |340 Views
SDE Sheet - Depth First Search
  Share   Like
Description
Discussion

This video is part of Graphs section under GFG SDE Sheet.

In this video, we are given, You are given a connected undirected graph. Perform a Depth First Traversal of the graph.
Note: Use the recursive approach to find the DFS traversal of the graph starting from the 0th vertex from left to right according to the graph.


Example :

Input: V = 5 , adj = [[2,3,1] , [0], [0,4], [0], [2]]

Output: 0 2 4 3 1

Explanation: 
0 is connected to 2, 3, 1.
1 is connected to 0.
2 is connected to 0 and 4.
3 is connected to 0.
4 is connected to 2.
so starting from 0, it will go to 2 then 4,
and then 3 and 1.
Thus dfs will be 0 2 4 3 1.

Try it out before watching the implementation of the problem in the video. We recommend watching the video, even if you can solve the problem. You may discover something new. All the best!!!

Do check out:-

Article: https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/
Problem: https://practice.geeksforgeeks.org/problems/depth-first-traversal-for-a-graph/1

Read More