Challenge: Implement Breadth-First Graph Traversal
Implement breadth-first graph traversal
We'll cover the following
Problem statement
You have to implement the breadth-first traversal in Python.
To solve this problem, the previously implemented Graph
class is already prepended.
Input
A graph represented as an adjacency list and a starting vertex
Output
A string containing the vertices of the graph listed in the correct order of traversal
Sample input
Graph:
Vertex | Edges |
---|---|
0 | 2, 1 |
1 | 4, 3 |
2 | None |
3 | None |
4 | None |
Source: 0
Sample output
result = "02143"
or
result = "01234"
Traversal strategy: print the right child first and then, the left child.
Create a free account to view this lesson.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy