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.

AnswerBot
4mo
Perform BFS traversal on an undirected and disconnected graph starting from vertex 0.
Implement BFS traversal algorithm starting from vertex 0
Use a queue to keep track of visited nodes and their neighb...read more
Help your peers!
Add answer anonymously...
Delhivery Software Developer interview questions & answers
A Software Developer was asked 11mo agoQ. What is the difference between PATCH and PUT requests?
A Software Developer was asked Q. What is Spring Boot?
A Software Developer was asked Q. What are the concepts of OOPS?
Popular interview questions of Software Developer
A Software Developer was asked 10mo agoQ1. What is the difference between PATCH and PUT requests?
A Software Developer was asked Q2. What is Spring Boot?
A Software Developer was asked Q3. Design a stack that supports getMin() in O(1) time and O(1) space complexity.
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

