I am analyzing an algorithm and I just want to know if I am on the right track.
For this algorithm, I am only counting the multiplications on the line which have *** in them.
Here's the algorithm:
p=p*20*z
gets executed exactly (j) + (j-1)+(j-2)+(j-3)...1
times. This happens to be equal to j(j+1)/2
.2 * (j(j+1)/2)
. n(2 * (n(n+1)/2))
.That's my thought process behind this. Am I correct? Thanks.
Your thought process is correct. You need to replace the j term with an n (n being the largest value j can assume), but that is probably a typo.
Furthermore, you can simplify further from where you are:
n(2*(n(n+1)/2))
2*n*(n^2+n)/2
n^3+n^2
=> O(n^3)
The last step is because the n cubed term will grow at a much larger rate than the n squared term we can say it will dominate the runtime for large n. Only other point I would mention is that you should perhaps consider the store to p as an operation as well as the two multiplication, although obviously this will not change the simplified big-o runtime.
Computation in this particular example could be simplified if you can find out that all three loops has the same exit condition up to n
:
i <= n
j <= n
k <= j
Basically third loop would run n
iterations as well because j <= n
so k <= n
,
this mean that complexity would be n * n * n = O(n ^ 3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With