Breadth-First Searches
January 12, 2024
Breadth First searches (BFS) begin at a certain node before moving onto the node's neighbors.
Following this, it moves onto the neighbor's neighbors, and the sequence continues. This
enables us with the ability to calculate the distance from the beginning node to the other
node. The time complexity of a BFS is O(N+E) because in the worst case
it has to pass each of the nodes and edges!
To understand how a BFS works, we can use the following diagram: here!
The search will begin at 1, and then move onto its neighbors, 2 and 3. From this,
the search will move onto 2 and 3's neighbors, which are: 4, 5, 6, and 7. Moving
onto their neighbors, the search will then reach 8, 9, and 10. Note that the nodes
the search meets are in the order of its distance from the first node.
The order should be 1, 2, 3, 4, 5, 6, 7, 8, 10, 9. We can use this in our
program to have the following: here!
Which outputs what we want, yay!
Contact Me!
Feel free to message me anytime about anything! :)