Prove that BFS runs in time O(N+M) on any graph with N vertices and M edges. |
Upper bounds on its running time: * O(n2) because the outer loop executes at most \ n times, and each time the inner loop executes, it executes \ at most n times. * O(n+m) because the time can be counted as ∑vertices w 1+degree(w) = n+2m . \ Here n is the number of vertices and m is the number of edges. O(n+m) is linear in the input size, because encoding the graph takes Θ(n+m) space. Algorithms that run in time linear in the input size are often the best possible, because for many problems, any algorithm must at least examine the entire input. |
* GoodrichAndTomassia section 6.3.3. |
* GoodrichAndTomassia section 6.3.3. |