BFS (Breadth First Search) on a Graph
M · mediumP · Verified PYQgraph
Problem
Given a graph with V vertices and E edges (undirected), perform BFS starting from vertex 0 and print the traversal order.
Example
Input
5 4 0 1 0 2 1 3 2 4
Output
0 1 2 3 4
Use queue. Enqueue 0, mark visited. While queue: dequeue u, print u, enqueue unvisited neighbors.