Skip to main content

How to calculate Running Time of an algorithm



                                           Calculate Running Time of an Algorithm


The running time of algorithm defines the time required to execute an algorithm on the given set of inputs(n). There are mainly three types of complexity cases defines to measure the running time of an algorithm also known as Asymptotic analysis.

1) Best Case : Best case also called (Ω) omega notation which measure the best case scenario of how long an algorithm can possible take to complete given operation on (n) inputs. It's also known as lower bound.

2) Average Case: It represents by (Θ) theta notation which measure the average time requires to complete a given operation on set of inputs. It measures between upper and lower bound running time and calculate average running time.

3) Worst Case: It defines the worst case running time of an algorithm. Also represent using (Ο) Big-oh. It is the upper bound of an algorithm running time and measures the worst case scenario of how long an algorithm can possible take to complete given operation on set of inputs(n). Generally calculates worst case of an algorithm for a problem and then average and best case.


Example:- How to calculate running time of an algorithm.

Given the sequence of the Fibonacci numbers 0,1,1,2,3,5,8,13,21,..
Program for output nth Fibonacci number on given n, and analysis of running time on each step.

               Time taken per Instruction        Number of repetition       Total time taken by instruction

1. fun fib(n){            

2. if( n < 2)  return n |   constant c1     |                  1                            |                  c1
3. else{
4. t1 := 0              |        constant c2       |                 1                            |                  c2
5. t2:= 1               |         constant c3      |                  1                           |                   c3
6. while(n > 1){   |        constant c4      |                  n                            |                   c4 *n
7. sum := t1 + t2  |        constant c5      |                  n-1                         |                   c5 *(n-1)
8. t1 := t2             |        constant c6      |                   n-1                         |                   c6 *(n-1)
9. t2 := sum         |        constant c7       |                  n-1                         |                   c7 *(n-1)   
10. n := n-1         |        constant c8       |                  n-1                         |                    c8 *(n-1)
}
}
11. return sum   |       constant c9       |                 1                            |                       c9
}

Hence the above calculating instructions time and number of repetitions. Further calculate the complete running time of the algorithm as:-

for T(n)  when n>=2 (we uses) Hence T(n) =


c2 + c3 + c4*n + c5*(n-1) + c6*(n-1) + c7*(n-1) + c8*(n-1) + c9
 = [ c2 + c3 + c9 - c5 - c6 - c7 - c8 ] + [ c4 + c5 + c6 + c7 + c8 ] * n
 = k0 + (k1*n), where k0 and k1 are constants.

The running time of given algorithm is calculated step-wise. Hence overall time complexity of calculation nth Fibonacci number from given n inputs is O(n).

So running time of an algorithm is important to know about the algorithm execution time which further improves and optimized according to required problems. The space and time complexity of an algorithm is important aspects which helps to measure performance and computation cost of algorithm on particular problem which will further be optimized according to requirements and resource capacity.

Comments

Popular posts from this blog

Rising of the AI in the human centric Development

Rising of the AI in the human centric Development The rising of the artificial intelligence in later 90's have make a rapid impact in field of technology and from 21st century the blooming of a mechanism makes several impact in various industries including software, education, healthcare and many more. As the world becomes increasingly reliant on technology, the role of  artificial intelligence  (AI) in human-centric development has risen to the forefront. From healthcare to transportation to education, AI is being used to improve the lives of people around the globe. Major areas where artificial intelligence AI makes an Impact One area where AI has made significant strides is in the healthcare industry. AI-powered virtual assistants can now assist doctors in diagnosing and treating patients, freeing up valuable time for medical professionals. In addition, AI-powered wearable devices can track a person's health and alert them to any potential issues. The transportation industr...

The Magic of Data Visualization using Matplotlib

      The Magic of Data Visualization Using Matplotlib Matplotlib is a multiplatform data visualization library built on Numpy arrays and designed to work with broader Scipy Stack. Matplotlib was developed by John Hunter in 2003 with version 0.1. This project is supported by Space Telescopic institute for complete development and extension for better capabilities. Matplotlib library enhances the plotting and visualization technique in python. As using the matplotlib we can create various plots, histogram, maps, chart and many more plotting. Visualization of Data     Important features of Matplotlib   It play and operates well with many operating systems and graphics back-ends.   Matplotlib have strength of running cross platform graphics engine smoothly and reliable to different types of graphics system.   There are various API’s and wrappers make this library to useful to dive into Matplotlib’s syntax to adjust the final plot output. Customizatio...

Components of Data Science Life Cycle

                                            Components of Data Science Life Cycle Data Science continues to evolve as the one of the most promising and demanding career of 21st century. The insights drawn from the data is very much useful and profitable for the businesses when processed with intelligent algorithms to find pattern and insights from it. The complete Data science follows a life cycle pattern which defines the steps of each stage of data and apply them to make it processed in more informative and easier way. The components of Data Science life cycle consist of five stages. Each stage have different tasks which perform on data during complete life-cycle span of Data science.                                                        ...