Thread: Algorithm Question ?

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    74

    Algorithm Question ?

    I am having difficulty understand the solution behind a frequent exam question , any help would be greatly appreciated .

    Question :

    Two algorithms to solve a problem have complexities of
    (i) 1/2 n ^ 2 and (ii) 20n for what value of n does algorithm (i) need less operations ?

    Solution :

    The complexity of an algorithm is the the number of operations / time units the algorithm requires as a function of the size of the problem n .
    It is a function of n , f(n) .Algorithm (i) needs less operations if
    1/2 n ^ 2 < 20 , or n^2 < 40 , or n < 4 .

    I just dont understand where the answer is coming from or how it is calculated.

    Thanks for your help ,

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    The question is, when is the following true:
    1/2 n2 < 20 n

    Multiply both sides by 2 to get
    n2 < 40n
    then divide by n (we can do that without worrying about possibly having to flip the <, because we know that n has to be positive), to get
    n < 40

    To verify, check the n = 39 case,
    1/2 n2 = 1/2 × 392 = 760.5
    20 n = 20 × 39 = 780
    and the n = 41 case,
    1/2 n2 = 1/2 × 412 = 840.5
    20 n = 20 × 41 = 820
    Remember, n is a positive integer. (A dataset cannot have a fractional number of entries, can it? No, n has to be a natural number.)

    The fact that they happen to be equal at n = 40,
    1/2 n2 = 1/2 × 402 = 800
    20 n = 20 × 40 = 800
    just means that the two algorithms need equal number of operations at n = 40.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    thanks that has cleared things up quite a bit : )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. algorithm question..
    By henri in forum C Programming
    Replies: 0
    Last Post: 12-13-2011, 07:39 AM
  2. algorithm question..
    By henri in forum C Programming
    Replies: 6
    Last Post: 12-02-2011, 08:25 AM
  3. Question on algorithm
    By Brimak86 in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2008, 07:02 PM
  4. algorithm question?
    By s_ny33 in forum C Programming
    Replies: 8
    Last Post: 10-10-2005, 06:53 PM
  5. Algorithm question
    By PJYelton in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 10:52 AM