Why is the complexity of both BFS and DFS O(V+E)?
Last Updated :
18 Mar, 2024
The complexity of both BFS and DFS are O(V+E) because every vertex (V) and every edge (E) is explored only once.
In graph theory, two fundamental algorithms used for traversing or searching tree or graph data structures are Breadth-First Search (BFS) and Depth-First Search (DFS). Let's look at Why is the complexity of both BFS and DFS is O(V+E).
Why is the complexity of both BFS and DFS is O(V+E)?
While iterating with this technique, we move over each node and edge exactly once, and once we are over a node that has already been visited then we backtrack, which means we are pruning possibilities that have already been marked. So hence the overall complexity is reduced from exponential to linear.
Pseudocode for DFS:
DFS(Graph, vertex)
vertex.visited = true
for each v1 ∈ Graph.Adj[vertex]
if v1.visited == false
DFS(Graph, v1)
The time complexity of DFS is also O(V+E) due to:
- It visits each vertex once, contributing V to the complexity.
- It checks each edge once when moving from one vertex to its adjacent vertices, contributing E to the complexity.
In this technique, each neighboring vertex is inserted into the queue if it is not visited. This is done by looking at the edges of the vertex. Each visited vertex is marked visited once we visit them hence, each vertex is visited exactly once, and all edges of each vertex are checked. So the complexity of BFS is V + E
Pseudocode for BFS:
create a queue Q
v.visited = true
Q.push(v)
while Q is non-empty
remove the head u of Q
mark and enqueue all (unvisited) neighbours of u
Since we are only iterating over the graph’s edges and vertices only once, hence the time complexity for both the algorithms is linear O(V+E).
The time complexity of BFS is O(V+E) because:
- It visits each vertex once, contributing V to the complexity.
- It checks each edge once when moving from one vertex to its adjacent vertices, contributing E to the complexity.
Conclusion
The time complexity of BFS and DFS is O(V+E) because it need to visit and examine every vertex and edge in the graph. This makes them linear algorithms, which are generally efficient for graph traversal.
Similar Reads
Time and Space Complexity of Breadth First Search (BFS) The Breadth First Search (BFS) algorithm is used to traverse a graph. It starts at a node 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 graph traversal, BFS is commonly used for its level-wise e
4 min read
What does Big O - O(N) complexity mean? Big O notation, typically represented as O(N) is a concept, in computer science and mathematics that allows us to analyze and describe the efficiency of algorithms. It provides a way to measure how the runtime of an algorithm or function changes as the size of the input (N) increases. In this articl
6 min read
What Does Big O(N^2) Complexity Mean? Prerequisite: Asymptotic Notation and AnalysisAnalysis of Algorithms | Big-O analysisIn this article, we're going to explore into the concept of Big O(N^2) complexity, a crucial metric in algorithm analysis. Understanding what O(N^2) signifies is crucial for evaluating the efficiency of algorithms,
15 min read
Time and Space Complexity of DFS and BFS Algorithm The time complexity of both Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms is O(V + E), where V is the number of vertices and E is the number of edges in the graph. The space complexity of DFS is O(V), where V represents the number of vertices in the graph, and for BFS, it is O(V
2 min read
What does Constant Time Complexity or Big O(1) mean? Big O notation is a concept, in computer science and mathematics that allows us to analyse and describe the efficiency of algorithms for worst cases. It provides a way to measure how the runtime of an algorithm or function changes as the input size grows. In this article we'll explore the idea of O(
5 min read