CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2005
    Posts
    336

    Computational Complexity

    Code:
    for ( int i = 0; i < 100; i++)
    {
    }
    Is complexity for this loop O(1) or O(n)?
    Last edited by sawer; November 25th, 2008 at 09:53 AM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Computational Complexity

    O(n) as it would vary linearly with the number of times the loop is executed.

  3. #3
    Join Date
    Oct 2006
    Posts
    616

    Re: Computational Complexity

    Actually, if the upper limit is constant (100), and will remain a constant, then it is O(1).
    Algorithm with run-time complexity of O(n) merely implies that the run-time of the algorithm exhibits linear growth with relation to the size of the input.
    Your code has no input and each time you run it, it will execute exactly 100 iterations - which means that no matter what input you will give the algorithm, the run-time is constant - 100 iterations, hence it's run-time complexity is O(1).

    A smart compiler will not even translate this loop into machine code since the for loop is empty.

    Regards,
    Zachm

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Computational Complexity

    Why someone say this is O(1) and O(n) ?

    I believe that analysis of this algorithm run in o(n).
    Thanks for your help.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Computational Complexity

    Quote Originally Posted by Peter_APIIT
    Why someone say this is O(1) and O(n) ?
    That "someone" is not the same since GCDEF says that it is in O(n) but Zachm says that it is in O(1).

    Quote Originally Posted by Peter_APIIT
    I believe that analysis of this algorithm run in o(n).
    I disagree as Zachm's analysis looks correct to me. If the complexity of this algorithm is in O(n), then what is n?

    EDIT:
    That said, if an algorithm's complexity is in O(1) then it is also in O(n), but I presume that we are abusing big-O notation here as it is often abused to mean theta (or something like that, I have not taken my algorithms module in university yet, so I can claim ignorance ).
    Last edited by laserlight; November 26th, 2008 at 02:19 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Computational Complexity

    I did algorithm complexity this semester at uni and as Zachm said, that is O(1) since it has no input, ie. will always be run 100 times.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  7. #7
    Join Date
    Oct 2006
    Posts
    616

    Re: Computational Complexity

    Quote Originally Posted by laserlight View Post
    That said, if an algorithm's complexity is in O(1) then it is also in O(n), but I presume that we are abusing big-O notation here as it is often abused to mean theta (or something like that, I have not taken my algorithms module in university yet, so I can claim ignorance ).
    You don't need to claim ignorance , the following is always true:
    Code:
    if f(n) = O(1) then f(n) = O(n)
    The other direction is not true.
    That said, it is customary to give the tighter bound, in this case it's O(1), but it is true that it is ALSO O(n).

    Regards,
    Zachm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured