Comments

Notes

MCS 211

18 topics

Running Time Of Algorithm

Updated Jan 14, 20255 min read

The Running time of an algorithm refers to the amount of time it takes for the algorithm to complete its execution as a function of the input size. It helps measure an algorithm's efficiency and predict its performance.

For a given problem, more than one algorithm can be designed. However, one algorithm may be better than the other. To compare two algorithms for a problem, running time is generally used which is defined as the time taken by an algorithm in generating the output.

An algorithm is better if it takes less running time. The “time” here is not necessarily the clock time. However, this measure should be invariant to any hardware used. Therefore, the running time of an algorithm can be represented in terms of the number of operations executed for a given input.

This running time of an algorithm for producing the output is also known as time complexity.

Therefore, two algorithms can be compared in terms of time complexity. an Algorithm is better compared to others having smaller running time (time complexity). 

Running time of an algorithm is represented as a function T(n), where n is the input size. Let, an algorithm has a running time T(n) = cn, where c is a constant. The running time for this algorithm is linearly dependent on the size of the input. The unit of T(n) is unspecified.

Determinants of Time Complexity

  • Input Size (n): The input variables for the algorithm, that is, the amount of information and the total number of variables.
  • Operations Count: The total number of operations that the algorithm will be able to accomplish such as multiplication, addition and comparison.
  • Hardware: The system’s microprocessor’s speed, memory and other configurations.
  • Implementation Details: How the code is written, performance optimizations and libraries called by the code.

Forms of Running Time in Brief

Best Case: The best scenario is when the algorithm runs within a predefined time frame, assuming it takes the most approachable input.

Worst Case: The worst scenario is when all the least-favorable inputs are fed to an algorithm in an attempt to maximize the time, making the algorithm inefficient.

Average Case: The time frame estimated while taking the average input of an algorithm is said to be the Average Case.


Evaluating the Duration of an Algorithm

There are three basic asymptotic(i.e., when input size n which are used to express the running time of an algorithm in terms of function, whose domain is the set of natural numbers N={1,2,3,…..}. These are:

  • O\OmicronO​(Big – 'Oh') [This notation is used to express Upper bound (maximum steps) required to solve a problem] .

  • Ω\OmegaΩ​('Omega') [This notation is used to express Lower bound i.e. minimum (at least) steps required to solve a problem]

  • Θ\ThetaΘ​('Theta') Notations.[Used to express both Upper & Lower bound, called tight bound]

Below are the generalized form of running time for the algorithms:

Constant Time(O(1)): If the running time does not depend on the input size (n) then it is known as constant running time. It can be represented as 

Constant Time

Linear Time O(kn): If the time complexity is at most a constant factor times the size of the input, then it is known as linear time complexity and is presented as T(n) <=kn where k is a constant or T(n) =O(n). An algorithm of this type of complexity generally completes the execution in a single pass.

Linear Time

Logarithmic Time(log(n)): If the time complexity of an algorithm is proportional to the logarithm of the input size, then it is known as logarithmic time complexity and depicted as O(log n)time. For example, the running time of a binary search algorithm is O(log n).

Logarithmic Time

Quadratic Time: (T(n)= O(n)2)- It occurs when the algorithm is having a pair of nested loops. The outer loop iterates O(n) time and for each iteration the inner loop takes O(n) time so we get O(n2 ) by multiplying these two factors of n. Practically this is useful for problem for small input size or elementary sorting algorithms. The worst case time complexity for Bubble sort, Insertion sort, Selection sort and insertion sort running time complexities are O(n2 ).

Quadratic Time

Cubic Time: (T(n)= O(n3 )): It often occurs when the algorithm is having there nested loops and each loop has a maximum n iterations. Let us have one interesting example which requires cubic time complexity. Suppose we are given n sets: S1, S2,….Sn. Size of each set is n (i.e. each set has n elements). The problem is to find whether some pairs of these sets are disjoint, i.e there are no common elements in these pairs and what is the time complexity ?

Cubic Time

Polynomial Time: (0(nk )):-This running time is obtained when the search over all subsets of a set of a size kin performed. To understand the complexity of running time we have to find how many distinct subsets of a size k of n elements of a set can be chosen. For that we have to take a combination of n elements taken k at a time .As an example let us consider a problem to find an independent set in a graph which can be defined as a set of nodes in which no pair of nodes have an edge between them. Let us formulate the independent set problem in the following way: given a constant k and a graph G having n nodes (vertices) , find an independent set of size k.

Plolynomial Time

Exponential Time: (O(kn )) Beyond the polynomial time complexity there are other two types of bounds :exponential time O(2N )and factorial time O(n!): let us refine the independent set problem that we are given a graph of a size n and want to find out an independent of a maximum value instead of some constant k which is less than n. The modified version of the pseudo-code is presented below.

Exponential Time

Pseudo-code : Pseudo-code for finding an Independent Set of a graph

Input G(V,E) 

{ for each subset s of n number of nodes verify whether s is an independent set 

if s is the largest among all the subsets examined so for 

print “s is the largest independent set ” 

end if

 end for 

}

end of code fragment



Share