Rotting Oranges Problem Statement
You are given an undirected and disconnected graph G(V, E) with V vertices numbered from 0 to V-1 and E edges. Your task is to perform a BFS traversal starting from the 0th vertex and print the traversal order.
Explanation:
BFS, or Breadth-First Search, is a graph traversal algorithm that visits all the nodes of a graph level by level. In an undirected graph, edges are bidirectional, meaning they can be traversed in both directions. A graph is disconnected if there is no path between at least two vertices.
Input:
The first line of input contains two integers representing the number of vertices V and the number of edges E.
Each of the following E lines contains two space-separated integers representing an edge between vertex A and B.
Output:
Print the BFS traversal of the graph starting from vertex 0. Each test case should be printed on a new line.
Example:
Input:
4 4
0 1
0 2
1 2
2 3
Output:
0 1 2 3
Constraints:
- 0 <= V <= 104
- 0 <= E <= (V * (V - 1)) / 2
- 0 <= A <= V - 1
- 0 <= B <= V - 1
- Time Limit: 1 second
Note:
The BFS traversal should be printed in sorted order for each node's connected nodes. You do not need to print anything; just implement the function.
Be the first one to answer
Add answer anonymously...
Top Delhivery Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app