Question

I'm practicing for an upcoming test by completing past tests. One of the questions asks me to determine the worst-case time complexity (in Big-O) for an algorithm. I am unsure of the correctness of my thought process when looking at the following algorithm:

Adjusting the color values of each Pixel in a Picture with height N and width M.

If we consider a simpler case of adjusting color values of each VERTICAL (N) pixel in a picture then this algorithm would be simply O(N). When we factor in the WIDTH (M) then we need to multiply M * N because for every row of pixels there is a horizontal pixel. Thus I conclude that the above algorithm has a worst-time complexity of O(M * N).

Any help or hints would be greatly appreciated! Thank you!

Was it helpful?

Solution

Assuming that "Adjusting the color values of each Pixel" takes constant time, your reasoning is correct, since there are N*M pixels, the complexity is O(N*M).

For your information, to make your answer more complete, you should also mention the assumption, that is that you assume "Adjusting the color values of each Pixel" takes constant time. If that process (which is repeated N*M times) takes, say O(M), then the algorithm is O(N*M*M), since for each pixel you need to do an O(M) operation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top