Calculating Time Complexity for Algorithms: Understanding and Solving Problems

  • Thread starter EvLer
  • Start date
  • Tags
    Complexity
In summary, the conversation is about understanding and solving problems related to time complexity of algorithms. Specifically, the conversation discusses how to calculate the running time for different input sizes, such as 500 or a problem that can be solved in 1 minute. The conversation also mentions the use of different algorithms, such as linear, nlogn, n^2, and n^3, and how to find the right answer mathematically. Additionally, the conversation touches on the topic of logarithmic problems and the need for numerical estimation in finding a solution.
  • #1
EvLer
458
0
Hello, I am trying to understand how to solve problems relating to time complexity of algorithms, esp. problems of the following kind:

An algorithm takes 0.5 ms for input size 100. How long will it take
for input size 500 if the running time is the following:
linear, nlogn, n^2, N^3

An algorithm takes 0.5 ms for input size 100. How large a problem
can be solved in 1 min if the running time is the following:
linear, nlogn, n^2, n^3

I think I have a general idea of what is asked but cannot figure out how to find it mathematically. Could someone be kind enough to explain it step by step?
Appreciate your help.
 
Physics news on Phys.org
  • #2
If t varies as f(n) then
[tex]\frac {t}{t_0} = \frac {f(n)}{f(n_0)}}[/tex]
 
  • #3
Thanks a lot, I think I got the first problem type, but I still have trouble with the second. Here is how I reasoned for O(n^3):
0.5/60*10^3 = 100^3/n^3, and then I solve for n^3 and take cube root but do not come up with the right answer. What am I doing wrong?
Thanks in advance.
 
  • #4
It looks like you didn't cube the 60 on the left hand side.
 
  • #5
Why do I need to cube 60?
1 min = 60*10^3(ms), it's time.
t0/t = n0^3/n^3 hence
0.5/60*10^3 = 100^3/n^3. Is that wrong? Could you then explain how this works?
Thanks again.
 
  • #6
Oops! Sorry - I wasn't thinking - that was wrong.

What you did looks okay.
 
  • #7
How do I solve the case where O(nlog(base 2)n)? I know I can represent it in a different way: 2^(number/n) = n, or I can change bases where n*ln(n) = number*ln2, but how do I solve this?
Thanks for help.
 
Last edited:
  • #8
The log problem is different from the others because you can't write a simple solution for it. You will have to resort to obtaining a numerical estimate. For example, you could use successive approximations which means you find a value of N which is too small and one that is too large then pick a point in between and see if that's too large or small. Adjust your range appropriately and repeat until you have the desired accuracy.
 

FAQ: Calculating Time Complexity for Algorithms: Understanding and Solving Problems

What is "calculating complexity"?

"Calculating complexity" refers to the process of determining the time and space efficiency of an algorithm or problem-solving method. It involves analyzing the number of operations or steps required to solve a problem as the input size increases.

Why is calculating complexity important?

Calculating complexity is important because it helps us understand the efficiency and performance of different algorithms. By knowing the time and space complexity, we can choose the most efficient algorithm for a given problem and improve the overall efficiency of our code.

How is complexity measured?

Complexity is measured using big O notation, which represents the worst-case scenario of an algorithm's time or space requirements. It is not a measure of the actual running time of an algorithm, but rather a general estimate of its performance as the input size grows.

What is the difference between time and space complexity?

Time complexity refers to the number of operations or steps required to solve a problem, while space complexity refers to the amount of memory or storage space needed to solve a problem. Both are important measures of an algorithm's efficiency, but they are not directly related to each other.

How can I improve the complexity of my code?

To improve the complexity of your code, you can choose more efficient algorithms, optimize your code for better performance, and reduce unnecessary steps or operations. It is also important to consider the trade-offs between time and space complexity and choose the best balance for your specific problem.

Similar threads

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