algorithm - Triple Nested Big O with dependent indices -
i working on finding big o problem, , having difficulty third nested loop. here code
for (int = 1 n) (int j = n) (int k = j*j n) //some constant operation
the i forloop o(n).
the j forloop (n + n-1 + n-2 + ... + 2 + 1) = (n-1)n/2 = o(n^2).
but unsure on how consider k forloop. know 1 full loop of j (1 n), summation (n + n-4 + n-6 + ...) = sum_{k=1}^n (n - k^2), unsure on go there.
any advice on how proceed great!
when j
greater sqrt(n), inner loop isn't entered. same true when i
greater sqrt(n), because j
starts @ i
.
therefore, can split work done 2 cases:
- on iterations
i
and/orj
moresqrt(n)
:k
loop isn't entered, , it's not difficult prove time complexity done outer 2 loops theta(n^2). - on iterations both
i
,j
lesssqrt(n)
: first 2 loops runsqrt(n)
times each, , last loop runs n times, therefore upper bound of number of total iterationso(sqrt(n) * sqrt(n) * n) = o(n^2)
.
in both cases, upper bound o(n^2), , first case shows lower bound. therefore, total time complexity theta(n^2).
Comments
Post a Comment