Analysis of Algorithms

Hello, this is my first blog. I am writing this blog because I recently studied about learning in public. I was fascinated by this idea of learning in public and sharing what you have learned. In this blog series, I will share what I am studying right now.

Why Analysis of Algorithms?

Let's take an example. Try to create a program to calculate the sum of the first n natural numbers.

There can be multiple ways to solve this problem. A few are :

Code 1:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;    
    cout << "Enter a number : "; 
    cin >> n;    
    cout << n*(n+1)/2;    
    return 0;
}

Code 2:

#include<bits/stdc++.h> 
using namespace std;

int main()
{
    int n;
    cout << "Enter a number : "; 
    cin >> n;    
    int sum=0;
    for(int i=1;i<=n;i++) 
        sum+=i;
    cout << sum;
    return 0;
}

Code 3:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cout << "Enter a number : ";
  cin >> n;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      sum = sum + 1;
    }
  }
  cout << sum;
  return 0;
}

As we saw there are multiple ways to calculate the sum of the first n natural numbers. For example, we can directly apply the formula n(n+1)/2 which will directly calculate the sum of the first n integers. But, this will take some time. Another approach to the same question is by an iterative method. We can take a number i, which starts from 1 and continues to add all the numbers to i till the number n. The third way is by using two for loops.

Now, the question arises which approach is better?

To decide that, we use different methods. One of the methods is Asymptotic analysis.

Asymptotic Analysis

Asymptotic analysis is a useful tool for measuring the order of the algorithm's growth. It enables us to determine which algorithm is best for calculating a problem in the least amount of time.

What makes asymptotic analysis so valuable is its independence from factors such as test cases, machines, and languages. When you use asymptotic analysis to analyze a code, it will provide you with an accurate assessment of how the code is performing regardless of the language used. Whether you're writing in C++, Java, or Python, the asymptotic analysis will tell you how well your code is functioning. As a result, it is a powerful tool that can be used to optimize the efficiency of algorithms.

The ability to measure the order of growth of an algorithm is invaluable when it comes to writing the most efficient code possible. Asymptotic analysis can be used to do just that, providing us with a precise measure of how well an algorithm performs. It is a great way to quickly evaluate the efficiency of an algorithm. By analyzing the asymptotic behaviour of the code, you can quickly determine how well it is performing and make changes as necessary. This makes it a powerful tool for optimizing algorithms and improving the overall performance of a codebase.

Example:

To illustrate the concept of order of growth, let us again visit our previous problem set program to calculate the sum of first n natural numbers. The program had three different solutions, each of which will have a different order of growth.

  • In code 1, we directly applied the formula for the sum of the first n numbers. Since there was no need for any loop to run, the order of growth is zero or constant(C₁).

  • In code 2, we declared a number i each time the loop runs, so it has a linear growth or (Cn+C₃). Here C₃ represents some constant declarations in the code.

  • Similarly, in code 3, we used two loops to calculate the sum of the first n digits. While running the two loops, there are multiple statements that are running twice, and there are also some statements that are running only once. Thus, its order of growth is quadratic, or( Cn² + Cn + C₆).

Note:

When it comes to analyzing algorithms, we often look at whether or not the algorithm is a linear, quadratic, or higher order of growth. Linear algorithms have a growth rate of n, while quadratic algorithms have a growth rate of n2. Higher-order algorithms have a growth rate of n3 or higher.

An important thing to note is that after a certain point for the value of n, the code can become efficient even if it works on a slow system. This is because when the value of n is large enough, the order of growth of the algorithm can become more important than the speed of the system.

For example, if an algorithm is quadratic, it will still be efficient even if it is running on a slow system. This is because the growth rate of the algorithm is more important than the speed of the system.

The same is true for algorithms with a higher order of growth. If an algorithm has a higher order of growth, then it will have a higher value of a certain value of n. This means that the algorithm will become efficient even if it is running on a slow system.

Conclusion

In conclusion, asymptotic analysis is an incredibly useful tool for understanding the efficiency of algorithms. It is important to understand the order of growth of an algorithm in order to understand how efficient it will be. After a certain point for the value of n, the code can become efficient even if it is running on a slow system. This is because the order of growth of the algorithm can become more important than the speed of the system.

In the next blog, we will learn about how to analyze the time complexity of a given algorithm and which algorithm is better to use. The topics which will be discussed are Big-O notation, Big-Omega notation and Big-Theta notation. Till then, take care and keep learning.