

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 9
click for more info
Not enough gems
Cost: 6 gems
1: Breadth First Search (BFS)
incomplete
2: Complete Graph
incomplete
3: Depth First Search (DFS)
incomplete
4: DFS vs. BFS
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at a root (some arbitrary node on a graph), and explores all of the neighbor nodes at the present depth before going on to the nodes at the next level.
Click to play video
Try stepping through a small graph:
Interactive example available with JavaScript enabled.
Non-integer sets are not "stable" in Python - the order of elements in a set is not guaranteed to be the same each time you iterate over it.
While testing, we want our algorithm to search the same way every time to make debugging easier. Python offers a sorted() function we can call on our set() that will return a sorted iterable.
sorted_items = sorted(unsorted_set)
Graphs are useful at LockedIn for more than just representing social connections! We'll also use it to build our geographic search feature. We have a graph of cities, where each city is a vertex, and each road connecting two cities is an edge.
Complete the breadth_first_search method on the Graph class.
This method traverses a graph level by level starting from a specified vertex v, and returns all vertices in the order they were visited. The breadth-first approach ensures we explore all vertices at the current distance from the start before moving further away.
We're using unique strings now as our vertices, rather than integers