\(O(1)\) would mean that the algorithm runs in constant time. This isn’t true because there are several comparisons happening in the algorithm.
\(O(n^3)\)
\(O(n^3)\) suggests that there are three consecutively nested loops. If you look at the example algorithm, you can see that there are not three nested loops.
\(O(n)\)
\(O(n)\) is linear time. The time it takes for this program to run doesn’t grow linearly.
\(O(n^2)\)
Correct. Since you are not only comparing the weight of a branch but also if the branch has already been connected to, this would make the Big-O of the algorithm \(O(n^2)\text{.}\)
Modify the depth-first search to produce strongly connected components. Hint: produce an ArrayList<ArrayList<T>> (where T is your data type). The main ArrayList contains one ArrayList per forest, with each forest containing the keys for that forest.
Using breadth-first search, write an algorithm that can determine the shortest path from each vertex to every other vertex. This is called the “all pairs shortest path problem.”