Open In App

Time and Space Complexity of Breadth First Search (BFS)

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

What is Breadth First Search?

The Breadth First Search (BFS) algorithm is used to search a graph data structure for a node that meets a set of criteria. It starts at the root of the graph and visits all nodes at the current depth level before moving on to the nodes at the next depth level.

Although there are other methods for traversing the graph, BFS is the one that is most frequently employed. The method to search every vertex in a tree or graph data structure is recursive. Each vertex in the graph is classified as visited or non-visited using BFS. It looks at every node that is next to the node it has chosen in a graph after picking just one.

Time Complexity of Breadth First Search (BFS):

Best Case: O(V + E)

  • The best-case time complexity of BFS occurs when the target node is found after exploring only a few vertices and edges.
  • In this case, the algorithm may terminate early without having to visit all vertices and edges.
  • Thus, the time complexity is O(V + E), where V is the number of vertices and E is the number of edges.

Average Case: O(V + E)

  • The average-case time complexity of BFS is also O(V + E).
  • This complexity holds true across various graph structures and densities.
  • BFS typically explores vertices and edges in a breadth-first manner, which results in a linear time complexity proportional to the sum of vertices and edges.

Worst Case: O(V + E)

  • The worst-case time complexity of BFS also remains O(V + E).
  • In the worst case, BFS explores all vertices and edges reachable from the source node.
  • The algorithm systematically visits each vertex and edge in a breadth-first manner, ensuring all reachable nodes are discovered.
  • Therefore, the time complexity is O(V + E) as it traverses all vertices and edges in the graph.

Auxiliary Space of Breadth First Search (BFS):

The auxiliary space complexity of Breadth-First Search algorithm is O(V), where V is the number of vertices in the graph. Here’s why the auxiliary space complexity is O(V):

Queue Data Structure:

  • BFS typically uses a queue to keep track of the vertices to visit.
  • At most, the queue will contain all vertices reachable from the starting vertex.
  • Since the maximum number of vertices in the queue is bounded by the number of vertices in the graph, the space complexity of the queue is O(V).

Visited Array:

  • BFS often employs a visited array to keep track of visited vertices to avoid revisiting them.
  • This array has a size equal to the number of vertices in the graph, contributing to an auxiliary space complexity of O(V).

Additional Space:

  • While traversing the graph, BFS may use other auxiliary data structures or variables.
  • However, the space required by these additional elements is typically minimal compared to the queue and visited array and does not significantly affect the overall space complexity.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads