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.
Example 1
Input
5 4 0 1 0 2 1 3 2 4 0
Output
0 1 2 3 4
Explanation: BFS visits vertices level by level.
- 1 <= V <= 100000
- 0 <= E <= 200000
- 0 <= u, v, S < V