The running time of an algorithm is \(𝑂(𝑔(𝑛))\) if and only if

2020

The running time of an algorithm isΒ \(𝑂(𝑔(𝑛))\)Β if and only if

  1. A.

    its worst-case running time isΒ \(𝑂(𝑔(𝑛))\)Β and its best-case running time isΒ \(\Omega(g(n)) \cdot (O= \textit{ big }O)\)

  2. B.

    its worst-case running time isΒ \(Ξ©(𝑔(𝑛))\)Β and its best-case running time isΒ \(O(g(n)) \cdot (O= \textit{ big }O)\)

  3. C.

    \(O(g(n))= \Omega(g(n))(O=\textit{big } O)\)

  4. D.

    \(O(g(n))\cap \omega(g(n))\)Β is non-empty set,Β \((o = \textit{ small } o)\)

Attempted by 611 students.

Show answer & explanation

Correct answer: A

Formal definition: A function T(n) is O(g(n)) if there exist constants c > 0 and n0 such that for every n β‰₯ n0 we have T(n) ≀ cΒ·g(n).

Interpretation:

  • This is an asymptotic upper bound on the running-time function T(n). When applied to an algorithm, it commonly refers to the worst-case running time, because that is the function one often wants to bound from above.

  • There is no requirement that the best-case running time be Ξ©(g(n)). The best-case can be much smaller than g(n) and still the algorithm's running time can be O(g(n)).

Counterexample:

  • An algorithm with best-case time 1 and worst-case time n^2 has worst-case running time O(n^2), but its best-case is not Ξ©(n^2). This shows the best-case need not be a lower bound of order g(n).

Conclusion: The correct condition for saying the running time is O(g(n)) is simply that the running-time function T(n) satisfies T(n) ≀ cΒ·g(n) for sufficiently large n (for some constants c and n0). None of the provided answer choices exactly states only this requirement without adding incorrect or irrelevant conditions.

A video solution is available for this question β€” log in and enroll to watch it.

Explore the full course: Coding For Placement