Breadth First Search — An Overview

source

Overview

Graph traversal means visiting every vertex and edge exactly once in a well-defined order. The order in which the vertices are visited depend on the type of algorithm methods you are using for a specific task. For the article, we will create the following Binary Tree where we will explore the Breadth First Search method to traverse through the tree and search a specific thing (anecdotally a specific fruit on a tree 🍎)

Breadth First Search. “What Is It?”

Traversing through the binary tree can come in 2 popular ways. One, you can use the Depth First Search method where the search goes through branch by branch in the binary tree and search something (say a target value).

But the second, which this article will discuss, will be the Breadth First Search method. Breadth First Search is the most commonly used approach and traverses through the tree layer by layer.

Breadth First Search (BFS) algorithm traverses a graph in a breadth ward motion and uses a queue to remember to get the next vertex to start a search. BFS starts from a selected node (source or starting node) and traverse the graph layer wise exploring the neighbor nodes (nodes which are directly connected to source node). Then, the search move towards the next level neighbor nodes.

Queues, Queues, Queues

Queues are an array of to-visit nodes where it is populated while visiting nodes layer by layer and evaluating the children. So the enque occurs if the current node has children and every children is pushed into the Queue array so that it will be evaluated after the algorithm moves onto the next layer. And as the Queue’d nodes are visited, deque occurs as the algorithm removes the front element of the Queue array and stashes it into the Stash/Visited Nodes Array.

Implementing the Breadth First Search

Now let’s dive into the code with the instructions in mind. We will walkthrough how to implement the BFS algorithm which starts from constructing a binary tree and adding children to each nodes to finally return an array that has traversed through all the nodes of the tree while using the Breadth First Search method.

Let’s dissect the construction of the binary tree for the BFS search.

Foundations of the tree object is created like this:

Children of a node is added with the following method:

Now toward the breadth first search:

Time/Space Complexity

For the time complexity of the Breadth First Search, there are two things to keep in mind. First, we will need to traverse through every vertices as we are performing the search. This means that the time to perform the algorithm will increase as size of the vertices increase. In addition, edges connect the vertices and determine how they are connected to the children. So that edges take into the factor of time complexity as well.

Taking in all the factors, the time complexity will look like the following at it’s worst:

O (v + e)

where:

v represents the number of vertices in the binary tree

e represents the number of edges in the binary tree that are connecting the vertices

Now onto the space complexity of the Breadth First Search, there is one thing to keep in mind as we are traversing through the tree with the method. It’s the queue size as we are visiting the nodes layer by layer. So at its worst, space complexity increases by O(v), where again, v represents the number of vertices in the binary tree.

O(v)

Recursive Solution?? — “no”

Now, the above solution uses iteration to store queues to remember the children nodes as the algorithm performs the search layer to layer. However, the question is, does breadth first search have a recursive solution? Although this is definitely a valid question when we are creating search algorithms but the simple answer is — no.

The reason is that breadth first search uses queues (First In First Out) and the recursive method depends on the stacks (Last In Last Out). Since the breadth first search method requires queues to perform the search, recursive solutions cannot be implemented in this case because the call stacks cannot be performed to create queues as iteration can.

So no recursive will be performed for this overview to explore the breadth for search algorithm.

Concept Summary

So in short, here are the gist steps as to what the BFS does so far:

1) Move horizontally and visit all the nodes of the current layer the search is in.

2) Store the queue of all the future Nodes, and stash the current node into a stash/visited nodes array.

3) Move on to the next layer while following the queue.

With the Breadth First Search, you can finally look for that target (ex. 🍎) you are looking for as you traverse through the binary tree!

Hi, i’m a software engineer based in NYC!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store