BFS running time [00MQ]

Since the implementation of BFS shown above has two nested loops, it must take \Theta (n^2) time, right? Not necessarily! (Also, we don't even have anything here named n.) However, by a similar argument as we used to analyze DFS, this algorithm examines each vertex at most once and each edge at most twice, performing various constant-time operations along the way (in particular we can implement a queue so that enqueue and dequeue both take constant time). Hence, the entire algorithm is O(V+E).

This is about the best we could possibly hope for—any interesting graph algorithm has to at least look at the entire graph, and simply reading every vertex and every edge takes \Theta (V+E) time.