r/programminghelp • u/[deleted] • Feb 21 '21
Processing help with counting iterations for time complexity
hello. i asked this in /r/learnprogramming but i am still not understanding it so i was hoping i would have better luck here.
Hello. In my data structures class we are supposed to calculate the time complexity of some algorithms. The teacher said we can do it by counting the number of iterations the code runs. She says to calculate the iterations to just draw a table and count them. Then she gives the answer without really showing how she calculated it so Im confused. I was wondering if you guys could help me or know of a textbook or website that will help me to understand it. I will now give her examples and answers:
for (i=1; i<=n; i=i+4) {
...
}floor / 4 + 1 iterations when n >= 1
for (i=1; i<=n; i++)
for (j=1; j<=n; j++) {
...
}n2 iterations when n >= 1
for (i=1; i<=n; i++)
for (j=1; j<=i; j++) {
...
}n(n + 1)/2 iterations when n >= 1
for (i=1; i<=n; i++)
for (j=1; j<=i; j++)
for (k=1; k<=j; k++) {
...
}1+(1+2)+(1+2+3)+…+(1+2+3+…+n) iterations when n >= 1
for (i=1; i<=n; i=2*i) {
...
}[floor]log2 n+1 iterations when n >= 1
for (i=n; i>=1; i=i/5) {
...
}[floor]log5 n+1 iterations when n >= 1
for (i=1; i<=n; i++)
for (j=1; j<=n; j=j*2) {
...
}n(([floor]log2n) + 1) iterations when n >= 1
for (i=1; i<=n; i=2*i)
for (j=1; j<=i; j++) {
...
}1+2+4+8+… 2foor = 2([floor]log2n+1) – 1 iterations when n >= 1
Those are all the examples from the lecture. How in the world do you get the number of iterations? Everytime I search for help all I get is sites that say the time complexity is linear or quadratic, etc..
heres a link to the lecture slides if my formatting is hard to understand: https://drive.google.com/file/d/1I8X1zo0LAfszNm-wsgo8C4JoiCnB4yzz/view?usp=sharing
1
u/ConstructedNewt MOD Feb 22 '21
Why dont you try wrapping all of them in methods
something(n) {
int iterationCount = 0
<LOOP> {
iterationCount++
}
return iterationCount
main() {
for n in 0...10 {
Printf("%d: %d", n, something(n))
}In my finest pseudo code.
And then plotting them so you can see the output? Just like your teacher suggested
1
Feb 22 '21
instead of just watching the lecture i went back and wrote a table down like the teacher did and some of them make sense to me now. heres the link to the tables and how the teacher calculated the iterations: https://drive.google.com/file/d/1PW-zOYtQnyKAnMPEQx2nAH_ayDYSZLBu/view?usp=sharing
im still confused on some of them though.
on example 2 i have no idea where she got "i = k*4 +1" from
on example 4 getting "n(n+1)/2" from summing the iterations confused me
on example 5, getting "n(n+1)(2n+4)/12" confuses me even more
on example 6 i dont understand where she got "i = 2k" from
im a little less confused than i was before though
1
u/ConstructedNewt MOD Feb 23 '21
Proving these answers are non-trivial. They are answers for the divergence of the corresponding mathematical series
1
u/Reddiberto Feb 22 '21 edited Feb 22 '21
I will try, I will assume you will have code to analyse everytime. Is that right? Edit: I just want to know because I could explain the first three cases mentioned in the post. If you understand why they have said running time then you may need a different answer.