Overclockers Australia Forums

OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 7th August 2012, 9:13 PM   #1
chi Thread Starter
Member
 
chi's Avatar
 
Join Date: Oct 2008
Location: Sydney
Posts: 749
Default need help understanding code optimisation principle

I've been scratching my head over this for a while so can anyone give me a hint?

say i've got an array A consisting of n integers A[0],A[1]......A[n-1], and i want to compute:
C [i][j] = A[i] + A[i + 1] +......+ A[j] for 0 <= i <= j < n.
The brute force algorithm would be something like this:
Code:
1. for i = 0,......,n-1
2.      for j = i,.....,n-1
3.           add up entries A[i] through A[j]
4.           store result in C[i][j]
5. return C
now i know the upperbound run time should be O(n^3), but dont understand how that works.

So far my line of thought is the first line runs n times, the second runs n times and the third is just the calculation so O(1) for that, and putting all that together:
n(n(1))=n^2?
__________________
Main: [G1.Sniper M3][3570K OC 4.4GHz 1.28v][Asus GTX560 Ti DCII 950/1900/2400 1.025v][8GB 1866MHz 9-10-9-28][HX650W][OCZ Vertex II 60GB][Epic Black Diamond]
Backup: [GA-P67A-UD3-B3][2500K OC 4.2 GHz 1.07v][EVGA GTX460 900/1800/2200 1.087][8GB 1600Mhz 9-9-9-24 1.5v][HX650W][F3 1TB][Antec 300]
Audio: X-Fi Titanium HD -> NAD 3020 + Denon PMA-250 II -> Wharfedale 9.1 + ATH-AD900/AD1000

Last edited by chi; 7th August 2012 at 9:17 PM.
chi is offline   Reply With Quote

Join OCAU to remove this ad!
Old 7th August 2012, 9:38 PM   #2
Zoltag
Member
 
Join Date: Aug 2001
Location: London
Posts: 497
Default

My understanding is your example is O(N^2)

Check this site out - http://rob-bell.net/2009/06/a-beginn...ig-o-notation/
Zoltag is offline   Reply With Quote
Old 9th August 2012, 8:10 PM   #3
~NJ
New Member
 
Join Date: Aug 2012
Posts: 1
Default

Wouldn't something like
Quote:
for(x=i, x<j, x++)
C[i][j] += A[x]
give you an O(N) first order execution time?

But I agree with Zoltag above that your example is O(N^2)
~NJ is offline   Reply With Quote
Old 9th August 2012, 9:25 PM   #4
Zoltag
Member
 
Join Date: Aug 2001
Location: London
Posts: 497
Default

Quote:
Originally Posted by ~NJ View Post
Wouldn't something like

Code:
for(x=i, x<j, x++)
C[i][j] += A[x]
give you an O(N) first order execution time?

But I agree with Zoltag above that your example is O(N^2)
I believe so, though I doubt it would give the result OP is looking for.
Zoltag is offline   Reply With Quote
Old 9th August 2012, 9:53 PM   #5
SLATYE
SLATYE, not SLAYTE
 
SLATYE's Avatar
 
Join Date: Nov 2002
Location: Canberra
Posts: 25,773
Default

I think his original example is indeed O(N³). The full code would look something like this:

Code:
for (i = 0; i < n; i++) {
   for (j = 0; j < n; j++) {
      C[i][j] = 0;
      for (k = i; k < j; k++) {
         C[i][j] += A[k];
      }
   }
}
(it doesn't look like that in his example because he just left the 'k' loop as "add up entries A[i] through A[j]"). Three loops suggests N³ scaling, unless there's something I'm missing.

I would presume that an optimisation would be this:
Code:
for (i = 0; i < n; i++) {
   for (j = 0; j < n; j++) {
      if (j <= i) {
         C[i][j] = 0; // Makes no sense to add from a larger index to a smaller.
      } else {
         C[i][j] = C[i][j - 1] + A[j];
      }
   }
}
(not tested, but it looks okay to me). That'd be N².

Edit: and I'm pretty sure that this problem cannot be reduced to anything less than N², simply because you have to fill every element in an n*n array.
__________________
Main system: Phenom II X4 920 | 8GB (4x 2GB) DDR2-800 | Gigabyte M57SLI-S4 v2.0 | Leadtek Geforce 9600GSO 384MB | Enermax Modu82+ 525W | 1TB Hitachi HDD | 3.5" + 5.25" FDD
Laptop: Compal EL80 | C2D T7200 | 320GB Fujistu HDD | 2GB DDR2-667 | GF Go 7600

Last edited by SLATYE; 10th August 2012 at 12:06 AM.
SLATYE is offline   Reply With Quote
Old 9th August 2012, 11:31 PM   #6
chi Thread Starter
Member
 
chi's Avatar
 
Join Date: Oct 2008
Location: Sydney
Posts: 749
Default

cheers for the help guys, after sleeping on it i think i've pieced it together
__________________
Main: [G1.Sniper M3][3570K OC 4.4GHz 1.28v][Asus GTX560 Ti DCII 950/1900/2400 1.025v][8GB 1866MHz 9-10-9-28][HX650W][OCZ Vertex II 60GB][Epic Black Diamond]
Backup: [GA-P67A-UD3-B3][2500K OC 4.2 GHz 1.07v][EVGA GTX460 900/1800/2200 1.087][8GB 1600Mhz 9-9-9-24 1.5v][HX650W][F3 1TB][Antec 300]
Audio: X-Fi Titanium HD -> NAD 3020 + Denon PMA-250 II -> Wharfedale 9.1 + ATH-AD900/AD1000
chi is offline   Reply With Quote
Old 10th August 2012, 1:07 PM   #7
akashra
Member
 
akashra's Avatar
 
Join Date: Apr 2003
Location: Melbourne, AU
Posts: 3,324
Default

I know there's an SSE instruction to do what you describe here, I just can't think what it was. (was thinking phaddd, but no).
__________________
Punctuation is the difference between "Let's eat, grandma!" and "Let's eat grandma!".
Building a Metricon Delta 21 in Craigieburn: http://homeofzero.blogspot.com/
akashra is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 8:11 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!