School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,015 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,075 people online right now. Registration is fast and FREE... Join Now!




Code fragment analysis

 

Code fragment analysis, Trying to figure out what algorithm this is.

gnewfenix

13 Sep, 2009 - 10:57 PM
Post #1

New D.I.C Head
*

Joined: 13 Sep, 2009
Posts: 2


My Contributions
Hi everybody! biggrin.gif I'm new to these boards and am hoping somebody could help me out. In my Data Structures and Algorithm Analysis course we're studying Big Oh analysis and it's proving to be pretty confusing.

Here's the code fragment I'm analyzing.

CODE

for( int i = 1; i <= n; i++ )
     for( int j = 1; j <= i*i; j++ )
          if( j%i == 0 )
               for( int k = 0; k < j; k++ )
                    sum++;


Braces were excluded to save space.
I've been trying to figure out what type of algorithm this falls under. I first started with the inner most loop, figuring out how many times it executes for each value of i, as well as the total for each value of n. What I noticed is that for values of n, n^3 isn't that far off from the total. Is that just a coincidence and I'm looking at this the wrong way? I thought if the innermost is executing roughly n^3 times then it might be O(N^3). If anybody could help me out I would be so thankful.

User is offlineProfile CardPM
+Quote Post


mattman059

RE: Code Fragment Analysis

14 Sep, 2009 - 02:42 AM
Post #2

D.I.C Addict
Group Icon

Joined: 23 Oct, 2006
Posts: 537



Thanked: 8 times
Dream Kudos: 225
My Contributions
KYA has a nice Big O notation tutorial.
http://www.dreamincode.net/forums/showtopic125427.htm
User is offlineProfile CardPM
+Quote Post

KYA

RE: Code Fragment Analysis

14 Sep, 2009 - 09:34 AM
Post #3

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,492



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Taking this a piece at a time:

Outer Loop: O(n)
First Inner Loop: it goes from 1 to i^2, at i's largest value this is n^2: O(n^2)

So far we have O(n^3) not counting anything else

Conditional check is constant: O(1)

Most inner loop: 0 - largest value of j which is n^2. The key point is that this loop isn't executed every iteration, only when the remainder of j divided by i is 0 (i.e. they are the same or i is a multiple of j). Knowing that, it'll get called at least once for each iteration of the outer most loop.

So if we did a rough (I emphasize rough) estimate we'd have O(n^2) * O(n^2) * O(n) = O(n^5) if and only if there was no conditional for the inner most loop

I'd say O(n^3) though.
User is online!Profile CardPM
+Quote Post

Neumann

RE: Code Fragment Analysis

14 Sep, 2009 - 09:52 AM
Post #4

I can judge a book by its cover
Group Icon

Joined: 8 Jul, 2009
Posts: 686



Thanked: 93 times
Dream Kudos: 225
My Contributions
Big-O is a worst-case complexity, so never cut any operations off while calculating it. It's definitely not O(n^3), let me check if it's O(n^5)...

Edit: Bah, stupid me, I was playing around with the infinite series to solve this simple little thing. I need to get more sleep and some coffee... The if-statement will be true i times. Because out of i^2 numbers, only i of them are divisible by i itself. So the second loop can be treated as O(n) because that's how many times it will be fully executed. So the whole thing is O(n^4)

This post has been edited by Neumann: 14 Sep, 2009 - 10:56 AM
User is offlineProfile CardPM
+Quote Post

gnewfenix

RE: Code Fragment Analysis

14 Sep, 2009 - 03:35 PM
Post #5

New D.I.C Head
*

Joined: 13 Sep, 2009
Posts: 2


My Contributions
Neumann, that logic sounds a lot simpler than mine. lol.
I ended up looking at the summation of n, 2n, 3n, ... , nn (from the amount of times j is executing), factored out the n and had 1/2(n(n(n+1), which is 1/2(n^3 + n^2). From here I was still convinced on O(n^3) but by furthering that as the summation of 1/2(n^3 + n^2) I broke it apart for the summation of both, which gave me
1/2 [ (n^4 + 2n^3 + 2n^2)/4 + (2n^3 + 3n^2 + n)/6 ]; which is O(n^4) + O(n^3) = O(n^4). Overkill I guess? anime2.gif Thanks for pointing out a much simpler solution to this problem though. Seriously would have saved me a lot of work >.> (and googling because I didn't know the summation of n^3).


mattman059, that tutorial was pretty good btw. It really helped clarify a bunch of things, so that, along with reading a chapter in my book again seriously helped.

This post has been edited by gnewfenix: 14 Sep, 2009 - 03:38 PM
User is offlineProfile CardPM
+Quote Post

Neumann

RE: Code Fragment Analysis

14 Sep, 2009 - 04:08 PM
Post #6

I can judge a book by its cover
Group Icon

Joined: 8 Jul, 2009
Posts: 686



Thanked: 93 times
Dream Kudos: 225
My Contributions
No problem. Now, even though doing what I did is very simple and lends the correct result, these are still just words. A lot of times you will be required to show all of this stuff mathematically, in which case you would just have to get down and dirty with series and inequalities smile.gif

Edit: I hope I'm not beating a dead horse here, but let me suggest an easy mathematical way of solving this thing.
First of all, I see that you have already noticed the limits of your last loop - i, 2i, 3i, 4i, ...i^2, because these are the i numbers from 1 to i^2 that are divisible by i itself. Now, here's my suggestion - rewrite your code so that it performs the same number of calculations, but without the annoying if-statement. Here is how the code can be rewritten:

CODE

for( int i =1; i <= n; i++ )
  for( int j =1; j <= i; j++ )
    for( int k=1; k <= i*j; k++ )
      sum++;


Two things to note:

1. The 2nd loop now goes until i. And that's fine, because that's how many times it actually operates.
2. Now the k goes up to i*j, and that makes sense. The i is fixed, and j iterates from 1 to i, giving us the same pattern.

Now this is a lot easier to calculate Big-O of.

This post has been edited by Neumann: 14 Sep, 2009 - 04:46 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 07:33AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month