0

Let A be an array[1..n] which has zeros and ones in it.and func() be function whose complexity is theta(m).For the given pseudo code what would be the complexity?

  counter=0;
    for(i=0;i<n;i++)
       {
          if(a[i]==1)
           counter++;
         else
           func();
   }

Accorlding to me the worst case for the function func() to be called at most n times would be when the arrays are completly filled with zeroes . Hence as the theta noation of func() is given as theta(m)

The complexity for the above code would be :theta(mn) ....??? If no,please help me with a proper validation.

Diljit PR
  • 301
  • 3
  • 14

1 Answers1

0

The time complexity of the above code will be O(mn) (Big O mn).
Why?
func() as you say is theta(m) Now when calculating the time complexity, as you noted yourself, the worst case when func is called n times would result in O(mn).
Why not Theta(mn)?
Theta is a tighter bound on the time complexity. It not only means that it will always performs at worst mn* but also that at best mn* (*give or take a multiplicative or additive constant). So it increases proportionally with the input. However we cannot guarantee a lower bound(a.k.a omega) of omega(mn).
Go here to find more information on big O vs vs Big Theta

And you should be able to answer why we cannot gauarantee a lower bound of Theta(mn) yourself.

Best, Digvijay

Community
  • 1
  • 1
digvijay91
  • 697
  • 1
  • 6
  • 21
  • Please correct me if i am wrong if omega of f() is (ab) and big O of f() is (ab) ,then theta of f() is (ab) ?? – Diljit PR Oct 27 '13 at 07:35
  • Yes you are right in essence (and wrong in the language). When writing it is written as f is omega(ab), f is O(ab), and not like "omega of f" or "big O of f". – digvijay91 Oct 27 '13 at 07:38
  • Consider marking the question solved, if your question has been answered. – digvijay91 Oct 27 '13 at 07:45