![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: Oct 2008
Location: Sydney
Posts: 749
|
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 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. |
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 |
|
Member
Join Date: Aug 2001
Location: London
Posts: 497
|
My understanding is your example is O(N^2)
Check this site out - http://rob-bell.net/2009/06/a-beginn...ig-o-notation/ |
|
|
|
|
|
#3 | |
|
New Member
Join Date: Aug 2012
Posts: 1
|
Wouldn't something like
Quote:
But I agree with Zoltag above that your example is O(N^2) |
|
|
|
|
|
|
#4 |
|
Member
Join Date: Aug 2001
Location: London
Posts: 497
|
|
|
|
|
|
|
#5 |
|
SLATYE, not SLAYTE
Join Date: Nov 2002
Location: Canberra
Posts: 25,773
|
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];
}
}
}
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];
}
}
}
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. |
|
|
|
|
|
#6 |
|
Member
Join Date: Oct 2008
Location: Sydney
Posts: 749
|
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 |
|
|
|
|
|
#7 |
|
Member
Join Date: Apr 2003
Location: Melbourne, AU
Posts: 3,324
|
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/ |
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|