SESK: Stuff Everyone Should Know · sesk.info/tech-bfs-linear-time
Using adjacency lists, what is breadth-first search's running time on a graph?
O(V + E), linear in the number of vertices plus edges.
- O(V + E) ✔
- O(V × E)
- O(log V)
Why: A graph is a set of vertices (points) joined by edges (links). BFS visits each vertex once and looks along each edge a constant number of times, so the work adds up to vertices plus edges.
Huh, didn't know that: In a graph where every edge counts the same, BFS finds the fewest-hops path from the start to every other vertex.
Breadth-first search is how I look for my keys. Every room on this floor before I go upstairs.
Like this card?
References
Answer: O(V + E). A graph is a set of vertices (points) joined by edges (links). BFS visits each vertex once and looks along each edge a constant number of times, so the work adds up to vertices plus edges.
- MIT 6.006 Introduction to Algorithms, Lecture 9: Breadth-First Search ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2020/resources/mit6_006s20_lec9
So breadth-first search runs in linear time! O(|V | + |E|)
Checked 2026-10-11.
Think this answer is wrong? Tap "Challenge this answer" on the card in the game and tell us why.
More stuff everyone should know
That's stuff everyone should know.