Understanding Time Complexity in Code: Explained for Beginners"

  • Thread starter lee534
  • Start date
  • Tags
    Time
In summary, the conversation is about finding the time complexity in code, specifically in terms of n. The code involves two for loops and the question is asking for the time complexity in Θ-notation. The first for loop has an outer loop with a complexity of O(n) and an inner loop with a complexity of O(n^3), resulting in an overall time complexity of O(n^4). The second for loop has an outer loop with a complexity of O(n^(1/3)) and an inner loop with a complexity of O(n), resulting in an overall time complexity of O(n^(4/3)).
  • #1
lee534
7
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
  • #2
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?
 
  • #3
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:

FAQ: Understanding Time Complexity in Code: Explained for Beginners"

1. What is time complexity in code?

Time complexity in code refers to the amount of time it takes for a program to run as a function of its input size. It is a measure of how efficient a program is and is usually denoted by the "big O" notation.

2. Why is understanding time complexity important?

Understanding time complexity is important because it allows us to analyze and compare the efficiency of different algorithms. This is crucial in developing efficient and scalable programs, especially when dealing with large amounts of data.

3. How is time complexity calculated?

Time complexity is calculated by analyzing the number of operations or steps required to solve a problem as the input size increases. It is usually represented by the number of operations, denoted as n, and the time it takes to complete each operation, denoted as O(n).

4. What are the different types of time complexity?

The different types of time complexity are constant (O(1)), logarithmic (O(log n)), linear (O(n)), quadratic (O(n^2)), and exponential (O(2^n)). These types represent the rate at which the time taken to solve a problem increases as the input size increases.

5. How can we improve time complexity in code?

There are various ways to improve time complexity in code, such as using more efficient algorithms or data structures, avoiding unnecessary operations, and optimizing code by reducing the number of loops or recursive calls. It is also important to consider the trade-offs between time complexity and other factors, such as code readability and maintainability.

Similar threads

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