The Fundamental Graph Traversal Algorithms: Breadth-First-Search and Depth-First-Search
Introduction to Graphs
Binary trees are a type of data structure used widely in data science and other programming fields, as they allow for efficient data storage in a hierarchical method. This type of data structure represents an acyclic graph with each element corresponding to a node (or vertex). Each node within a graph contains two pieces of information: 1) some sort of data stored within that node, and 2) a reference link to the next node in the graph. Being such vital components to data structures and algorithms, traversing through these graphs to find specific nodes with the desired data is essential.
The two standard traversing techniques commonly employed within a graph are the Breadth-First-Search (BFS) and Depth-First-Search (DFS) searching algorithms. To explain briefly, the BFS method explores nodes adjacent to the current node. If two nodes are adjacent to a vertex, the chances of either being explored first are equal, unless there are other specifications involved. Initially, a starting node is required to proceed with the exploration. In BFS, traversed nodes are stored in a queue in the order in which they were explored, until no further nodes are left to explore.
Visualizing BFS Traversal
To understand this process, let us consider the following visual:
Suppose a starting node was selected to be 3.
Node 3 will be added to the queue.
Since 6, 1 and 10 are adjacent to 3, they will be added to the queue in any order, such as 10, 6 and then 1. Subsequently, 3 is removed from the queue, having been explored, and added to the explored list.
As 10 is the next node in the queue, it will be explored first. Though since there are no other nodes adjacent to 10, nothing will be added from the queue, and 10 will be removed.
Since 6 is the next node in the queue, it will be explored, and its adjacent node 4 is added to the queue, with 1 already present.
This process continues until either the desired node is explored, or there are no other nodes left to explore. In this case, selecting the order of adjacent nodes is arbitrary, thus there can be multiple different ways nodes are explored. For instance, the above graph can be traversed in the order: 3,10,6,1,4,2,5,9,7,8
, or even as: 3,1,6,10,2,4,8,7,9,5
.
Depth-First Search (DFS) Algorithm
The second search algorithm in discussion is the Depth-First-Search (DFS) method. The fundamental logic of this algorithm involves exploring all paths of a singular node before moving on to the next. In simple terms, unlike BFS, DFS will follow a path of nodes until hit by a node with multiple connecting nodes. Similar to BFS, unless specified further, a path will arbitrarily be chosen if a node has multiple linked nodes. Once a node no longer leads to any additional nodes, that path will be marked as "explored." The algorithm will then backtrack to a previous node that connects multiple paths, initiating the search for a path that was not initially chosen. This process is then repeated until all paths are explored. Much like BFS, DFS can also store explored nodes in a list, however for temporary storage, it will use a stack instead of a queue, which adheres to the Last-In-First-Out (LIFO) order as opposed to a queue’s First-In-First-Out (FIFO) order.
Visualizing DFS Traversal
Let's examine the same example used for BFS to gain a deeper understanding of DFS:
To maintain consistency, the starting node will remain 3.
Node 3 will be added to the stack and the explored nodes list.
An adjacent node to 3 will randomly be selected to continue the exploration. In this case, it will be 10, which is also added to the stack.
Since 10 leads to no other new nodes, it will be removed from the stack as it is the “last in,” and will be added to the explored nodes list.
Node 6 will be randomly selected as the new path to explore, adding it to the stack.
Since Node 6 has multiple adjacent nodes, a new node will randomly be selected and added to the stack for further exploration.
This process continues until all nodes have been explored. The order in which nodes are traversed can vary, resulting in multiple possible outcomes much like BFS. For example, 3,10,1,2,8,9,6,4,5,7
and 3,6,4,7,5,1,2,9,8,10
both have an equal likelihood of being the traversal order of the graph.
Practical Applications of BFS and DFS
Fundamentally, the BFS algorithm can be perceived as a slow expansion in all directions, while DFS represents a swift exploration along a single path. Due to the differences between these algorithms, both positive and negative factors are associated with using each, depending on the task at hand. Additionally, whether a graph is infinite or not, can significantly impact the choice of algorithm used. In infinite graph scenarios, BFS generally proves more beneficial, as DFS could potentially get trapped in an infinite loop while searching for a specific node. Furthermore, BFS outperforms DFS when the desired node is closer to the starting node or when the objective is to find the shortest path to a particular node. This is due to DFS’s arbitrarily chosen path, which may result in suboptimal time delays. Conversely, DFS is more efficient at finding nodes located farther away from the starting node in a finite graph, using less memory compared to BFS.
Binary trees provide an efficient hierarchical data storage solution, and traversing them using Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms is crucial for exploring and locating specific nodes. BFS offers a systematic exploration of adjacent nodes, making it beneficial for finite graphs, while DFS enables faster path-focused exploration, excelling in finding distant nodes. The choice between BFS and DFS depends on the problem's nature, target node proximity, and graph type. By understanding the strengths and trade-offs of these algorithms, programmers can optimize their approaches and effectively navigate binary trees.
Subscribe to my newsletter
Read articles from Aayush Deshpande directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by