Challenge: Implement Breadth First Search

Now, we shall implement the BFS algorithm in Pythonic code.

Problem statement

You have to implement the Breadth First Search traversal in Python. We have already covered the logic behind this algorithm. All that’s left to do is to flesh it out in code.

To solve this problem, all the previously implemented data structures will be available to us.

Note: Your solution should work for both connected and unconnected graphs.

Input

A directed graph in the form of an adjacency list and an integer indicating the starting vertex number (source).

Output

A string containing the vertices of the graph listed in the correct order of traversal.

Sample input

Graph:

Vertex Connected to by an Edge
0 2, 1
1 4, 3
2 None
3 None
4 None

Source: 0

Sample output

"02143" // Not the only possible output

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.