You are on page 1of 3

Both depth-first and breadth-first searches are traversal algorithms that are instrumental in navigating

data structures. Some of such applications include Graph Theory, Pathfinding, Puzzles and network
analysis. It should however be noted that these 2 algorithms have distinctive differences which them
applicable to different problem types.

A. Breadth-first Search for Graph Traversal

We use the BFS to search for a node that meets some conditions in a graph data structure. The
algorithm starts by looking through all the nodes at the root of the graph, then it advances to the
nodes at the next depth, and so on. It is quite possible for the BFS to visit a graph more than once
since a graph is cyclic, so to avoid this we divide the vetices of the graph into those visited and those
yet to be visited during a breadth-first search.

1. The algorithm starts from the root node and visits all levels, using a queue. The unvisited nodes
of the current level are pushed into the queue and the visited ones are marked as visited and
popped off the queue.

2, The node 0 is also pushed into the queue and marked as visited:

3. Node 0 is removed from the front of the queue as the algorithm visits unvisited nodes and
puts them into the queue:
4. Algorithm removes node 1 from the front od queue, and push in the unvisited nodes:

5. Algorithm removes node 2 from front index and pushes in the unvisited nodes:

6. The algorithm pops off node 3 from front of queue and pushes unvisited nodes on the queue:

7. Algorithm pops off node 4 from front of queue and places the unvisited nodes on the queue:

B. Depth-first Search:

This is another algorithm for visiting all nodes in a graph, where it starts at the root node, traverses
along each branch and eventually backtracks. In a depth-first search, all nodes in a branch are visited
before any other branch. One major benefit of depth first search is that uses less memory as
compared to breadth first search. This is because it does not have to keep tack of all the nodes in
any depth, but only the visited nodes , and the branch up to the deepest level; then it backtracks.

Given the simple graph below:

The above graph is traversed as A  B  D  C  E  F

Differences Between BFS and DFS

Breadth-First Search Depth-First Search


Breadth first search is a perfect option for Depth first search is a great option for deriving
finding the shortest path in unweighted graphs connected components, and the cycles of a
graph
Breadth first search is characterized by a time Depth first search is characterized by a time
complexity of O(V+E) complexity of O(V+E)
V=number of vertices V=number of vertices
E=number of edges E=number of edges
Breadth first search usually requires a larger Depth first search usually requires a lower
amount of memory to store all vertices at the amount of memory to store the vertices on the
current level of the queue current path in the stack.
Breadth first search visits all vertices on the Breadth first search visits all vertices on the
graph, prioritizing the breadth graph, prioritizing depth
Breadth first search uses queues to store Depth first search uses a search to store visited
visited and unvisited vertices of the graph and unvisited vertices

References

GeeksforGeeks. (Feb, 2024). Breadth First Search for a Graph.


https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/

Ankit Kochar. Nov 2023. Differences Between BFS and DFS.


https://www.prepbytes.com/blog/graphs/difference-between-bfs-and-dfs/

You might also like