Challenge: Implement Breadth First Search
Now, we shall build the BFS algorithm in JavaScript code.
We'll cover the following
Problem Statement
You have to implement the Breadth First Search traversal in JavaScript.
Note: Your solution should work for both connected and unconnected graphs.
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 |
---|---|
1 | 3, 2 |
2 | 5, 4 |
3 | null |
4 | null |
5 | null |
Source: 1
Sample Output
"13254"
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.