Skip to content
CampusEduX

BFS Traversal

Easyjavapythonccppjavascript

Given an undirected graph and a starting vertex S, visit all reachable vertices using Breadth-First Search. When a vertex is expanded, consider 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 BFS visit order (only reachable vertices), separated by single spaces.

Related Problems

  • Graph Using Adjacency List
    Easy · java, python, c, cpp, javascript
    Solve Problem
  • DFS 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...