The Magician’s Guide to Algorithms, part 5: The Breadth First Search

Andrew Bonner Blocked Unblock Follow Following Nov 12, 2017

So this week we’ll be talking one of the more difficult algorithms, or rather incantations, on our list. This is right up there with finally casting a fully formed Patronus, so be proud when you’ve cracked this one.

The Breadth First Search

So the Breadth First Search (bfs) spell won’t make much sense at all until we understand what kind of data structure it’s meant to traverse. This means we need to know a little about trees.

A tree is a data structure that looks something like this:

6

/ \

5 7

/ \ \

3 4 9

/ / \

2 8 10

/

1

In fact, this it’s what’s called a binary tree. This means that the root node, which is 6, and every child node after it can have no more than two children, one on each side. Furthermore, each child node on the left must be of lesser value than it’s parent and even node to the right must be of greater value than it’s parent.

Take a moment to examine the code below. The above tree can be represented in an array of objects like so:

var tree = [

{value: 6, left: 1, right: 2},

{value: 5, left: 3, right: 4},

{value: 7, left: null, right: 5},

{value: 3, left: 6, right: null},

{value: 4, left: null, right: null},

{value: 9, left: 7, right: 8},

{value: 2, left: 9, right: null},

{value: 8, left: null, right: null},

{value: 10, left: null, right: null},

{value: 1, left: null, right: null}

]

So let’s check out the spell:

The bfs spell takes two arguments. The first is the tree, and the second it the value we are trying to find. To start the ball rolling we push the node at tree[0] to the queue — this is the root node. This is known as “enqueuing”

Immediately, we enter a while loop that exits if we either find the correct node or run out of nodes in the queue.

So for each node in the queue, we first shift it from the array. This is known as “dequeuing.” We first check to see if the value of the node we just dequeued is equal to the value we are searching for. If so, we don’t have to continue any further in the loop and need simply to return the node.

Otherwise, we check to see if there is a left node and a right node, adding either or both to the queue depending on what we find. We don’t need if/else clause here since we will always need to check both conditions.

If we reach the bottom of the tree without finding the correct node, a message will be logged to the console informing us the node was not found.

And there you have it.

Why a “Breadth First” search?

So let’s quickly go over what this is called a breadth first search. This algorithm traverses the tree by reading all of the nodes within the same row of the tree before descending to the next, making it a useful spell for Madame Pince for finding books on the shelves by scanning each on level at a time.

In our above tree if we wanted to find the node with a value of 1, it would read them like so: 6 5 7 3 4 9 2 8 10 1, which it would then return.

The BFS algorithm has a cousin called the DFS algorithm, or Depth First Search that we will be covering next week.

Until next time. Cheers!

Check out last week’s post on Selection Sorts here.