CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009
    Posts
    1

    Algorithm Complexity

    hello
    i have an algorithm that i try to figure out it's complexity

    for x = 1 to n/2
    for y = x to n-x
    for w = x to y
    do (operation)

    is it some kind of a familiar series?
    how do i analyse it?
    thanks
    mike

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    Re: Algorithm Complexity

    Without more information about the 'operation' part, it is impossible to say. For example, if the 'operation' is to exit all three loops immediately, the complexity will be a lot different than if the operation is to multiply two matrices of size n^2.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Algorithm Complexity

    Centering a screen?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    Re: Algorithm Complexity

    Quote Originally Posted by MikeSan View Post

    for x = 1 to n/2
    for y = x to n-x
    for w = x to y
    do (operation)
    There is something wrong with your loop values. Let's take n = 100 for example:

    x = 1 to 100/2 => x = 1 to 50
    y = 50 to 100 - 50 => x = 50 to 50

    Are you sure these are correct?

  5. #5
    Join Date
    Jun 2009
    Posts
    1

    Re: Algorithm Complexity

    1) Outer loop runs for N/2 times
    2) First Inner loop runs for N/2 Times
    Starts from x, x+1, x+2... n-x. So, exactly for N/2 iterations, X >= N-X.
    3) Innermost loop runs for N/2 Times again.
    Starts from X=1 & Y=1, X=1 & Y=2.... X=1 & Y=N/2

    So the complexity would be near to n/2 * n/2 * n2 ~ O(n*n*n)

    -Kiran Kaki

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Algorithm Complexity

    Quote Originally Posted by MikeSan View Post
    hello
    i have an algorithm that i try to figure out it's complexity

    for x = 1 to n/2
    for y = x to n-x
    for w = x to y
    do (operation)

    is it some kind of a familiar series?
    how do i analyse it?
    The inner loop will perform 'operation' exactly (x^2 + x - y^2 + y) / 2 times (I'll leave it up to you to verify that).
    So, to figure out the complexity, you need to analyse the sum:
    SUM(x=1 to n/2, SUM(y=x to n-x, (x^2 + x - y^2 + y) / 2))).
    Since it's a double sum, it might be helpful to work out an example and write it down in a table (x in the rows and y in the columns, e.g.).
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Tags for this Thread

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