In a previous blog post we talked about how to apply the Breadth First Search algorithm to the graph data structure. Today, let’s figure out how Depth First Search (DFS) works.

DFS is one of the fundamental algorithms used to search nodes and edges in a graph. It’s a form of a traversal algorithm.

Just a reminder, this is how our graph looks:

Image for post

And this is our adjacency matrix (read here about adjacency matrix representation):

Image for post

Based on the name we can assume that BFS focuses on the depth of the graph. The search starts at some root node and it keeps searching as far as possible each branch before backtracking.

Our task is to write an algorithm that explores routes as deep as possible before going back and exploring other routes. Let’s see how we can do it.

#data-structures #graph #javascript #algorithms #algorithms

Graphs Data Structure: Depth First Search
1.35 GEEK