For functions f(x) and g(x), we will say that “f(x) is O(g(x))” [pronounced “f(x) is big-oh of g(x)”] if there are positive constants C and k such that |f(x)|≤C|g(x)| for all x>k. |
|
|
The outer loop runs N time. The inner loop is dependent on the growth of i2. To figure out the time it takes for these loops run together I will need to use the summation formula. a0 = 0 and a2 = (N – 1)2 - 1 because the last element will not be multiplied by itself, only the second to last. The total value is subtracted by 1 because 0 and 1 are skipped. ( N(0 + (N – 1)2 – 1) )/ 2 = N ( N2 – 2N ) / 2 = N3 – 2N2 / 2 The last for-loop runs in relation to j. Once again we need to do a summation notation to figure out what the final loop does. We know that since k < j the inner for loop is essentially run j * j times or j2 times. So a0 = 0 and afj = N3 – 2N3 / 2. The N value outside is (N3 – 2N2 / 2) because that is the number of times the loop will be run. (N3 – 2N2 / 2) ((N3 – 2N2 / 2) / 2)) = (1/2) N3 – N2 ( (1/8)N3 – (1/4) N2) = (1/16) N6 – (1/8) N5 – (1/8) N5 + (1/4) N4 = ( N6 + 4N4 / 16 ) So in total the algorithm will be an element of O(N6). |
sum = n(2n4-5n3+5n-2)/20 |
sum = n(n-1)(n+1)(n-2)(2n-1)/20 |
|
|
2 0 0 3 6 6 4 42 42 5 162 162 6 462 462 7 1092 1092 8 2268 2268 9 4284 4284 10 7524 7524 |
Mind you, I am not just interested in finding out what this algorithm is big-Oh of, I would like to find as exact a value as possible. |
So I tried doing the summation formula starting from the inner most loop and I wanted to know what you all thought of my work and the result: To figure out the time complexity of this algorithm, I find it is best to start with a summation formula on the inner most loop. It will run N – 1 (because the iterator never makes it to 10) multiplied by 4 + N2 – 2N + 5 all divided by 2. ( (N – 1) (N2 – 2N + 9) ) / 2 = (N3 – 3N2 + 11N – 9) / 2 So far the algorithm is an element of O(N3). Now we input this value in to another summation formula that represents how many times the middle function is called. The value will be added together N – 2 times. The outermost loop also runs N – 2 times. ( ( (N – 1) ( ( ((2)3 – 3(2)2 + 11(2)2 – 9)/ 2 ) + ( ( (N – 1)3 – 3(N – 1)2 + 11(N – 1) – 9 ) / 2 ) ) / 2 We know the first value of N is 2 so it is input in to the value we found in the last summation algorithm. We also input (N – 1) in to another copy of it because that represents the final iteration. (N – 1) ( 1/2(N3) – N2 + 10N – 12 + 31/ 4 ) / 2 (½(N4) – (1/2)N3 + 10N2 – 12N + (31/4)N) / 2 1/4(N4) – ¼(N3) + 5N2 – 6N + (31/8)N And then finally we input this value for the final summation formula which is performed by the outermost loop. ( (N – 1) ( (¼(2)4 – ¼(2)3 + 5(2)2 – 6(2) + (31 / 8) (2)) / 2) + ((1/4(N - 1)4 – ¼(N - 1)3 + 5(N – 1)2 – 6(N – 1) + (31/8) (N – 1) ) / 2 ) ) ) / 2 ( (N - 1)( ( (10 + 31/4 ) / 2 ) + ( ( ¼(N4 – 4N3 + 6N2 – 4N + 1) – ¼(N3 – 3N2 + 3N – 1) + 5(N2 – 2N + 1) – 6N +(31/8)N + 6 – 31/8 ) / 2 ) / 2 ( (N – 10) ( 1/8(N4) – 3/8(N3) + 7N2 – 7/2(N) + (31/16)(N) + 8 – 93/16 ) ) / 2 (1/8(N5) – 9/8(N4) + (7 – 30/8)N3 + 63(N2) + (31/16 + 70/2)(N2) + 8N – 93/16(N) + 80 + 930/16) / 2 1/16(N5) – 9/16(N4) + (19/16)(N3) + (1599/32)(N2) + 4N – 35/32(N) + 1871/32 |
It will run N – 1 (because the iterator never makes it to 10) multiplied by 4 + N2 – 2N + 5 all divided by 2. |