Skip to content
CampusEduX

DFS Traversal

Easyjavapythonccppjavascript

Given an undirected graph and a starting vertex S, visit all reachable vertices using Depth-First Search. When a vertex is expanded, visit its neighbours in increasing order.

Input Format

The first line contains two integers V and E. Each of the next E lines contains an undirected edge u v. The last line contains the starting vertex S.

Output Format

Print the DFS visit order (only reachable vertices), separated by single spaces.

Related Problems

  • Graph Using Adjacency List
    Easy · java, python, c, cpp, javascript
    Solve Problem
  • BFS Traversal
    Easy · java, python, c, cpp, javascript
    Solve Problem
  • Connected Components
    Easy · java, python, c, cpp, javascript
    Solve Problem

Input (stdin)

Output

Run your code to see output here...