Understanding Time Complexity in Code: Explained for Beginners"

  • Thread starter Thread starter lee534
  • Start date Start date
  • Tags Tags
    Time
AI Thread Summary
Time complexity analysis can be challenging for beginners, especially when dealing with nested loops and varying growth rates. In the first example, the outer loop runs O(n) times, while the inner loop, which multiplies j by 3 until it reaches n^3, results in an overall complexity of O(n^4). The second example features an outer loop that decreases i by a factor of 3, leading to a complexity of O(n^(1/3)), while the inner loop runs O(n^3), resulting in a final complexity of O(n). Key points include understanding how to combine the complexities of nested loops and recognizing the impact of exponential growth versus polynomial growth. This foundational knowledge is crucial for accurately determining time complexity in algorithms.
lee534
Messages
7
Reaction score
0
Hi this is my first post and I having an extremely hard time finding time complexity in code
note: I have had no compsci experience (since it was career change) and the professor only went over this for 30 mins

Homework Statement


What is the time complexity (in Θ –notation) in terms of n ? (the red is what is throwing me off)
a.sum = 0 ;
for ( i = 0 ; i < n ; i++ )
for ( j = 1 ; j < n3 ; j = 3*j )
sum++ ;
b.sum = 0 ;
for ( i = n ; i > 0; i = i/3 )
for ( j = 0 ; j < n3 ; j++ )
sum++ ;

Homework Equations


none

The Attempt at a Solution


a.
assignments: sum = 0, i = 0, j = 1, j=3*j : (1+1+3*n)
conditions: j<n3, i<n: 3*(n+1+n(n3+1)
increments:i++,sum++; n+n3
O(n4)

b.(this one I'm taking a big educated guess)
assignments: sum = 0, i = n, i = i/3, j=0: 1+1+n+n/3
conditions: j<n3, i>0: n/3+1+n(n3+1)
increments:j++,sum++; n*n3+n*n3
O(n4)

and if you can explain to me as if I'm a child that would be great!
Thank you
 
Last edited:
Physics news on Phys.org
Careful, the loops for (a) are misleading. You have the right idea for the outside loop -- it'll all just be n. But for the inside, essentially what you do is multiply whatever time complexity you have for the inner loop by the complexity of the outer loop (it looks like you were adding them). An easier case for this is two for loops that are both ##O(n)##. The inner loop goes for ##O(n)## ##n## times, so that becomes ##n \cdot O(n)##, or ##O(n^{2})##.

Also for (a), consider what happens when j goes up to n3, but each iteration you multiply j by three. How do you think these effects will compare? Try writing out a list of values j will take on.

For (b), the code in red can be difficult to analyze. If it were instead multiplying by 3 each time, this would be exponential, right? So if you're dividing, then what's the opposite of an exponential function?
 
So is it easier to analyze it by loops? Because I found this method online and the professor did not teach how us to read code as you are describing (he was also using a different format from this problem, but I'm guessing it's analyzed the same?). okay so for a. can we look at it this way?
a.
assignments: sum = 0, i = 0, j = 1, j=3*j : (1+1+3*n) → O(n)
conditions: j<n3, i<n: n+(n)(3n)3 → O(n4)
increments:i++,sum++; n+n(n3)
O(n4)

or

a.sum = 0 ;
for ( i = 0 ; i < n ; i++ ) → O(n)
for ( j = 1 ; j < n3 ; j = 3*j ) →n(3n)3→O(n4)
sum++ ;
O(n) + O(n4) → O(n4)

b.
b.sum = 0 ;
for ( i = n ; i > 0; i = i/3 ) → 3√n → O(n1/3)
for ( j = 0 ; j < n3 ; j++ ) → (n1/3)*(n3) → n
sum++ ;
O(n)

thank you for helping me understand
 
Last edited:

Similar threads

Replies
7
Views
2K
Replies
10
Views
2K
Replies
8
Views
2K
Replies
21
Views
3K
Replies
1
Views
2K
Replies
14
Views
4K
Back
Top