Artificial Intelligence Search Algorithms @codewithhasan

Breadth-First Search Algorithm in Javascript

Hasan Gökçe

--

Warning! This post is in the draft phase

1. Introduction

This is a graph traversal algorithm. That means, there is a tree structure, and we want to visit all points of the tree. Also, when we visit a point, we set new places to visit. At each visit, we log visited points and we set a new point to continue the journey. By doing so, we’ll be traveling all over this tree.

Figure 1.1. A simplified road map of part of Turkey

There are a lot of algorithm’s to do this

One of them is Breath-first search. It explores all directions equally. It’s perfect for some problems and quite easy to understand and implement. Let’s look at visuals to understand how breadth-first search works:

Figure 1.2. The Breath-first search algorithm tries to find the red point.
Green tries to find red with Breadth-first algorithm
Figure 1.3. Breadth-first search made 923 operations to be able to find red’s location.

2. Warm-up

2.1. Definitions:

  • node
  • edges
  • vertex
  • starting point
  • end point
  • parent
  • queue

2.2. Definitions for a node:

  • n.state:
  • n.

3. Code Examples

--

--