BFS Traversal in a Graph

Given an undirected and disconnected graph G(V, E) where V vertices are numbered from 0 to V-1, and E represents edges, your task is to output the BFS traversal starting from the 0th vertex.

Explanation:

BFS, or Breadth-First Traversal, is an algorithm for visiting all nodes of a graph by exploring neighbor nodes first before moving to the next level neighbors. An undirected graph means each edge is bidirectional. In a disconnected graph, not all pairs of vertices have paths connecting them.

Input:

V  E
Edge1A Edge1B
...
EdgeEA EdgeEB

Output:

BFS Traversal for each test case in a separate line.

Example:

Consider this graph:

example
Starting from vertex 0, BFS traverses nodes 1 and 2 directly connected to 0. Thus, the traversal sequence becomes [0, 1, 2]. Since vertex 2 is connected to 3, the sequence extends to [0, 1, 2, 3].

Constraints:

  • 0 ≤ V ≤ 10^4
  • 0 ≤ E ≤ (V * (V - 1)) / 2
  • 0 ≤ A ≤ V - 1
  • 0 ≤ B ≤ V - 1
Note:

Ensure the BFS path starts from vertex 0. In a situation where node-order matters, connected nodes are printed in numerical sort order.

AnswerBot
4mo

BFS traversal in a disconnected graph starting from vertex 0.

  • Use BFS algorithm to traverse the graph starting from vertex 0.

  • Explore neighbor nodes first before moving to the next level neighbors.

  • Consi...read more

Help your peers!
Select
Add answer anonymously...

ZIGRAM Software Developer interview questions & answers

A Software Developer was asked Q. Rotting Oranges Problem Statement You are given a grid containing oranges where ...read more
A Software Developer was asked Q. Sort a Stack Problem Statement You are given a stack. Your task is to sort the e...read more
A Software Developer was asked Q. Hourglass Pattern Problem Statement Given an integer 'N', print an hourglass pat...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Rotting Oranges Problem Statement You are given a grid containing oranges where ...read more
A Software Developer was asked Q2. Sort a Stack Problem Statement You are given a stack. Your task is to sort the e...read more
A Software Developer was asked Q3. Hourglass Pattern Problem Statement Given an integer 'N', print an hourglass pat...read more
ZIGRAM Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits