#K88922. BFS Traversal of a Directed Graph
BFS Traversal of a Directed Graph
BFS Traversal of a Directed Graph
Given a directed graph with V vertices numbered from 0 to V-1, perform a breadth-first search (BFS) traversal starting from a specified vertex start. The graph is provided as an adjacency list.
The input begins with two integers, V and start. Then, for each vertex from 0 to V-1, a line is given that starts with an integer k (the number of neighbors), followed by k space-separated integers representing the neighbors.
Your task is to output the BFS traversal order, with vertices separated by a single space.
Note: In the BFS traversal, once a vertex is visited, it should not be enqueued again. The traversal processes vertices in the order they are discovered.
The standard BFS algorithm can be summarized as follows:
\(\text{Initialize a queue with the start vertex, mark it as visited, and then while the queue is not empty, dequeue a vertex and enqueue its unvisited neighbors.}\)
inputFormat
The first line contains two integers V (the number of vertices) and start (the starting vertex for the BFS traversal).
Then V lines follow. Each line describes the adjacency list of vertex i (0-indexed). The line starts with an integer k, the number of neighbors, followed by k space-separated integers denoting the neighbors of vertex i.
outputFormat
Output a single line containing the BFS traversal order starting from vertex start. The vertices must be printed in the order they are visited, separated by a single space.
## sample5 0
2 1 2
1 3
1 4
0
1 3
0 1 2 3 4