1

How to work out the time complexity of this algorithm, both O() and Ω().

enter image description here This nested loop is different from the common nested loop analysis, because m and n are independent such that |A| = n ≥ |B| = m.

What I am thinking is for each iteration, it will run O(1) time.

For the times of iteration (line 3 and line 4), it should be

m + (m − 1) + ... + 1 + (n - m) = O(m^2 + n)

Therefore, the time of this algorithm should be O(m^2 + n) and so is Ω.

However, the solution tells me it should be O(mn) and Ω(mn). I cannot figure it out how to get that answer.

1 Answer 1

1

A quite terse explanation of Can Berk Güder

O denotes an upper bound, but this bound might or might not be tight.

Ω denotes a lower bound, but this bound might or might not be tight.

In your case, we don't consider the specific values (possible loops) there will be, but just in mathematical perspective, it's O(mn) and Ω(mn) since there is a n outer loop and m inner loop.

And a more straightforward definition in wiki perhaps shed more light on your puzzle.

Since an algorithm's running time may vary among different inputs of the same size, one commonly considers the worst-case time complexity, which is the maximum amount of time required for inputs of a given size. Less common, and usually specified explicitly, is the average-case complexity, which is the average of the time taken on inputs of a given size (this makes sense because there are only a finite number of possible inputs of a given size).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.