What is the time complexity of breadth-first search (BFS) on a graph?

Practice Questions

Q1
What is the time complexity of breadth-first search (BFS) on a graph?
  1. O(V + E)
  2. O(V)
  3. O(E)
  4. O(V^2)

Questions & Step-by-Step Solutions

What is the time complexity of breadth-first search (BFS) on a graph?
  • Step 1: Understand that BFS is a way to explore a graph.
  • Step 2: Identify that a graph consists of vertices (points) and edges (connections between points).
  • Step 3: Realize that during BFS, we visit each vertex to explore it.
  • Step 4: Note that we also look at each edge to see which vertices are connected.
  • Step 5: Count the total number of vertices in the graph as V.
  • Step 6: Count the total number of edges in the graph as E.
  • Step 7: Understand that BFS will visit each vertex once and each edge once.
  • Step 8: Combine the visits: the total time taken is proportional to the number of vertices plus the number of edges.
  • Step 9: Conclude that the time complexity of BFS is O(V + E).
No concepts available.
Soulshift Feedback ×

On a scale of 0–10, how likely are you to recommend The Soulshift Academy?

Not likely Very likely