Back to the 2023 paper

Module 4: Graph and Tree Algorithms

20232m

BFS on a graph G=(V,E)G=(V,E) has running time:

  • (i) O(∣V∣+∣E∣)O(|V|+|E|)
  • (ii) O(∣V∣)O(|V|)
  • (iii) O(∣E∣)O(|E|)
  • (iv) None of the above
Worked SolutionAI Assisted

Answer

Correct Option: (i) O(∣V∣+∣E∣)O(|V|+|E|)

Explanation

Breadth-First Search (BFS) using adjacency list:

Complexity Analysis

  1. Initialization: O(∣V∣)O(|V|) — mark all vertices unvisited, enqueue source.
  2. Each vertex is enqueued and dequeued exactly once → O(∣V∣)O(|V|).
  3. Each edge is examined twice (once from each endpoint in undirected graph, once in directed) when exploring adjacency list of its source vertex → O(∣E∣)O(|E|).

Total:
T(V,E)=O(∣V∣)+O(∣E∣)=O(∣V∣+∣E∣) T(V,E) = O(|V|) + O(|E|) = O(|V|+|E|)

Why other options are wrong

  • (ii) O(∣V∣)O(|V|): Ignores edge scanning. Correct only if graph is empty (∣E∣=0|E|=0).
  • (iii) O(∣E∣)O(|E|): Ignores vertex initialization and isolated vertices.
  • Adjacency matrix BFS would be O(∣V∣2)O(|V|^2), but with adjacency list (standard) it is O(∣V∣+∣E∣)O(|V|+|E|).

Final Answer

BFS running time is O(∣V∣+∣E∣)O(|V|+|E|) → Option (i).

Similar questions

AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.