Challenge: Implement Breadth First Search
Now, we shall build the BFS algorithm in C++ code.
We'll cover the following
Problem Statement
You have to implement the Breadth First Search traversal in C++. We have already covered the logic behind this algorithm. All that’s left to do is to flesh it out in code.
Note: Your solution should work for both connected and unconnected graphs.
To solve this problem, all the previously implemented data structures will be available to us.
Input
A graph in the form of an adjacency list
Note: You can start the traversal from any vertex.
Output
A string containing the vertices of the graph listed in the correct order of traversal.
Sample Input
Graph:
Vertex | Edges |
---|---|
0 | None |
1 | 3, 2 |
2 | 5, 4 |
3 | None |
4 | None |
5 | None |
Sample Output
"013254"
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.