Introduction to complexity of algorithm

“How will you calculate complexity of algorithm” is very common question in interview.How will you compare two algorithm? How running time get affected when input size is quite large? So these are some question which is frequently asked in interview.In this post,We will have basic introduction on complexity of algorithm and also to big o notation

What is an algorithm?

An algorithm is step by step instructions to solve given problem.
Lets take a simple example.You want to write an algorithm for listening particular song.
1) Search for song on computer.
2) Is song available?
            i.If Yes,Then listen that song.
           ii.If no,download that song and then listen that song.
So we are solving a problem by step by step procedure.This step by step instructions is called Algorithm.

Why do you need to evaluate an algorithm?

You need to evaluate an algorithm so that you can find most optimize algorithm for solving given problem and also considering various factors and constraints.
For example:
You want to go from city A to City B.Then there are various choices available i.e. by flight,bus or train.So you need to choose among different options depending on your budget and urgency.

Counting number of instructions:

Number of instructions can be different for different programming languages.
Lets count number of instruction for searching a element in a array.
Let’s assume our processor takes one instruction for each of the operation:

  • For assigning a value to a variable
  • For comparting two values
  • Multiply or addition
  • Return statement

In Worst case:

If element which we want to search is last element in sorted array then it will be worst case here.
So :
Hence f(n)=3n+3

Asymptotic behaviour :

Here We will see how f(n) performs with larger value of n.Now in above function, we have two parts i.e. 3n and 3. Here you can note two points:
  • As n grows larger, we can ignore constant 3 as it will be always 3 irrespective of value of n. It makes sense as you can consider 3 as initialization constant and different language may take different time for initialization.So other function remains f(n)=3n.
  • We can ignore constant multiplier as different programming language may compile the code differently. For example array look up may take different number of instructions in different languages. So what we are left with is f(n)=n

How will you compare algorithms?

You can compare algorithms by its rate of growth with input size n
Lets take a example.For solving same problem, you have two functions:
f(n) =4n^2 +2n+4 and f(n) =4n+4
For f(n) =4n^22 +2n+4
so here
f(1)=4+2+4
f(2)=16+4+4
f(3)=36+6+4
f(4)=64+8+4
….
As you can see here contribution of n^22 increasing with increasing value of n.So for very large value of n,contribution of n^2 will be 99% of value on f(n).So here we can ignore low order terms as they are relatively insignificant as described above.In this f(n),we can ignore 2n and 4.so
n^2+2n+4 ——–>n^2
For f(n) =4n+4
so here
f(1)=4+4
f(2)=8+4
f(3)=12+4
f(4)=16+4
….
As you can see here contribution of n increasing with increasing value of n.So for very large value of n,contribution of n will be 99% of value on f(n).So here we can ignore low order terms as they are relatively insignificant.In this f(n),we can ignore 4 and also 4 as constant multiplier as seen above so
4n+4 ——–>nSo here n is highest rate of growth.
Point to be noted :
We are dropping all the terms which are growing slowly and keep one which grows fastest.

Big O Notation:

This notation is used for theoretical measure of  execution  of an algorithm. It gives tight upper bound of a given function. Generally it is represented as f(n)=O(g(n)) and it reads as “f of n is big o of g of n”.
Formal definition:
f(n) = O(g(n)) means there are positive constants c and n0, such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0. The values of c and n0 must not be depend on n.
When you say O(g(n)) , It means it will never be worst than g(n). Having said that it means O(g(n)) includes smaller or same order of growth as g(n).
So O(n) includes O(n),O(logn) and O(1).

So O(g(n)) is a good way to show complexity of algorithm.

Lets take some example and calculate value for c and n0.
1. f(n)=4n+3
Writing in a form of f(n)<=c*g(n) with f(n)=4n+3 and g(n)=5n

4n+3<=5n for n0=3 and c=5.

or 4n+3<=6n for n0=2 and c=6
Writing in a form of f(n)<=c*g(n) with f(n)=4n+3 and g(n)=6n
so there can be multiple values for n0 and c for which f(n)<=c g(n) will get satisfied.

2. f(n)=4n^2+2n+4
Writing in a form of f(n)<=c*g(n) with f(n)=4n^2 +2n+4 and g(n)=5n^2
4n^2 +2n+4<=5n^2 for n0=4 and c=5

Rules of thumb for calculating complexity of algorithm:

Simple programs can be analyzed using counting number of loops or iterations.

Consecutive statements:
We need to add time complexity of consecutive statements.
f(n)=c1+c2;
So O(f(n))=1

Calculating complexity of a simple loop:

Time complexity of a loop can be determined by running time of statements inside loop multiplied by total number of iterations.

f(n)=c2*n+c1;
So O(n)=n

 
Calculating complexity of a nested loop:

It is product of iterations of each loop.

f(n)=c2*n*n + c1
So O(f(n))=n^2

 
If and else:

When you have if and else statement, then time complexity is calculated with whichever of them is larger.

f(n)=c1+c2+c3+(c4+c5+c6)*n
So o(f(n))=n

Logarithmic complexity

Lets understand logarithmic complexity with the help of example.You might know about binary search.When you want to find a value in sorted array, we use binary search.

Now let’s assume our soreted array is:

and we want to search for 74 in above array. Below diagram will explain how binary search will work here.

When you observe closely, in each of the iteration you are cutting scope of array to the half. In every iteration, we are overriding value of first or last depending on soretedArray[mid].
So for
0th iteration : n
1th iteration: n/2
2nd iteration n/4
3rd iteration n/8.
Generalizing above equation:
For ith iteration : n/2i

So iteration will end , when we have 1 element left i.e. for any i, which will be our last iteration:
1=n/2i;
2i=n;
after taking log
i= log(n);
so it concludes that number of iteration requires to do binary search is log(n) so complexity of binary search is log(n)
It makes sense as in our example, we have n as 8 . It took 3 iterations(8->4->2->1) and 3 is log(8).
So If we are dividing input size by k in each iteration,then its complexity will be O(logk(n)) that is log(n) base k.

Lets take an example:

Complexity of above code will be O(log(n)).

Exercise:

Lets do some exercise and find complexity of given code:

1.

Ans:

Complexity will be O(n)

2.

Ans:

Complexity will be :n+n*n —>O(n^2)

3.

Ans:

Complexity will be n*n/2*log(n)–> n^2log(n)

 4.

Ans:

Complexity will be n/2*n/2*n –> n^3

Related Posts

  • nvarchar vs varchar
    29 July

    nvarchar vs varchar in SQL Server

    Table of ContentsIntroductionVARCHARNVARCHARExamples to Demonstrate Difference between VARCHAR and NVARCHARVARCHAR vs NVARCHAR Comparison Chart This article will look at the difference between the data types NVARCHAR and VARCHAR in SQL Server. These data types are commonly used for storing textual data or strings for various design purposes. We will look at the description of each […]

  • Stream2watch
    29 July

    Stream2watch alternatives -13 Best Websites Like Streamtowatch in 2021

    Table of ContentsBest Working Proxies of the Stream2watch in 2021Stream2Watch alternativesSport365SportP2PCricFree12th PlayerVIP BoxLiveTVWiziwigStopStreamSport LemonMamaHDLAOLA1Sport RARTime4TVfuboTVFirstRowSportsVIP LeagueFrequently Asked Questions**Is Stream2watch a safe website?What are the best Streamtowatch Alternatives?Do you need an account for Stream2watch?Final Words Are you after the best Stream2watch alternatives? Good News! this article is all about efficient web-based resources for streaming football, baseball, […]

  • 29 July

    AirPod Case not Charging – Common Issues and Fixes

    Table of ContentsBefore you StartWhy Is My AirPod Case Not Charging?Five Easy Solutions for AirPods Case Not Charging1. Reset Your AirPods Case2. Clean the Charging Port 3. Get a new Charger and Cable4. Update the AirPods Firmware5. Replace the CaseTips for Avoid AirPod not Charging IssueFrequently Asked QuestionsWhy isn’t my AirPod case charging, but my AirPods […]

  • Mobdro alternatives
    29 July

    Mobdro Alternatives – 14 Best apps like Mobdro in 2021

    Table of ContentsBest Mobdro Alternatives Working in 2021Cyberflix TVMorph TVTerrarium TVTea TVVisit Thop TVTubi Tv123MoviesMovie BoxVuduOreo TVMegaBox HDFreeFlixYuppTVFrequently Asked QuestionsWhat is Mobdro?Why do you need Mobdro alternatives?Are there free Mobdro alternatives?Wrapping Up Can’t access Mobdro anymore and are worried about watching your favorite movies? Take a breath; you still have a long list of working […]

  • Select rows by multiple conditions using loc in Pandas
    29 July

    Select rows by multiple conditions using loc in Pandas

    The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It returns the rows and columns which match the labels. We can use this function to extract rows from a DataFrame based on some conditions also. First, let us understand what happens when we provide a […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in Python In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we may want to […]

Comments

  1. superb explanation.I went through many explanations to understand time complex before this.But couldnt understand.thankyou soo much for this explanation

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.