Analysis of algorithm

17
Presented By: Presented To: Rajendra Dangwal Mr. Yogesh Jain CS Branch Lecturer (IV Sem) CS Deptt.

Transcript of Analysis of algorithm

Page 1: Analysis of algorithm

Presented By: Presented To:

Rajendra Dangwal Mr. Yogesh Jain

CS Branch Lecturer

(IV Sem) CS Deptt.

Page 2: Analysis of algorithm

Algorithm

• Algorithm is step by step procedure to solve any problem.

Analyzing / Judgment of the Algorithm

• An algorithm can be written in different ways for solving a single problem.

• So by analyzing the algorithms we can find the best solution (algorithm) to the problem.

Page 3: Analysis of algorithm

Order of Growth

• Any algorithm is expected to work fast for any input size.

• For smaller input size our algorithm will work fine but for higher input size the execution time is much higher

• By increasing the size of n (input size) we can analyze how well our algorithm works.

Page 4: Analysis of algorithm

• Let input size, n=5 and we have to sort the list of elements for e.g. 25,29,10,15,2

• So for n=5 our algorithm will work fine but what if n=5000?

• So our algorithm will take much longer time to sort the elementsor cause small delays to give the result

• So how the behaviour of algorithm changes with the no. of inputs will give the analysis of the algorithm and is called the Order of Growth.

Page 5: Analysis of algorithm

• For calculating the order of growth we have to go for higher value of n, because

i. as the input size grow higher algorithm makes delays.

ii. for all real time applications we have higher values for n.

Page 6: Analysis of algorithm

Efficiencies of the Algorithm

There are three cases

i. Best case

ii. Worst case

iii. Average case

• Let we have 5 nos.(n=5) 25,31,42,71,105 and we have to find any element in the list.

Page 7: Analysis of algorithm

Best case efficiency

• let we have to find 25 in List =>25,31,42,71,105

• k=25

• 25 is present at the first position

• Since only single comparison is made to search the element so we say that it is best case efficiency

• CBest (n)=1

Page 8: Analysis of algorithm

Worst case efficiency

• If we want to search the element which is present at the last of the list or not present at all in the list then such cases are called the worst case efficiency.

• let we have to find 105 in List =>25,31,42,71,105

• k=105

• Therefore we have to make 5 (=n) comparisons to search the element

• CWorst (n)=n

• And if we have to find 110

• k=110

• Since the element is not in the list even then we have to make 5 (=n) comparisons to search the element

• CWorst (n)=n

Page 9: Analysis of algorithm

Average case efficiency

• Let the element is not present at the first or the last position

• Let it is present somewhere in middle of the list

• We know that probability of a successful search = p where 0≤p≤1.

• And probability of unsuccessful search = 1-p

Page 10: Analysis of algorithm

• Let the element we are searching for is present at position 'i' in the list

• Therefore probability of the element to be found is given by p/n.

• Therefore CAvg (n)

=[1*p/n + 2*p/n + ... + i*p/n + ... + n*p/n] + n(1-p)

=p/n[1+2+...+i+...+n] + n(1-p)

=p/n[n*(n+1)/2] + n(1-p)

=p[(n+1)/2] + n(1-p)

I II

Page 11: Analysis of algorithm

• case 1. If element is available therefore p=1 (for successful search)Now substituting p=1 in above eqn

• CAvg (n) = 1[(n+1)/2] + n(1-1)= (n+1)/2

• case 2. If element is unavailabletherefore p=0 (for unsuccessful search)Now substituting p=0 in above eqn

• CAvg (n) = 0[(n+1)/2] + n(1-0)= n

Therefore on average half of list will be searched to find the element in the list

Page 12: Analysis of algorithm

Operations in Order of Growth

• i.Big 'O‘ (Oh)

• ii.Big Ω (Omega)

• iii.Big θ (Theta)

• Consider an algorithm representing two functions f(n) and g(n)

Page 13: Analysis of algorithm

Big O

• It represents the maximum time that the algorithm takes for its execution.

g(n)

f(n)

• Growth rate of f(n) is not more than growth rate of g(n).

• f(n)≤O[g(n)]

Tim

e

No. of inputs

Page 14: Analysis of algorithm

Big Ω

• It represents the minimum time that the algorithm takes for its execution.

f(n)

g(n)

• Growth rate of f(n) is not less than growth rate of g(n).

• f(n)≥ Ω[g(n)]

Tim

e

No. of inputs

Page 15: Analysis of algorithm

Big θ

• It represents the average time an algorithm takes for its execution.

C1.g(n)

f(n)

C2.g(n)

• Let C1*g(n) and C2*g(n) are two constant multiples of g(n)

• f(n) is bound above and below by two constant multiples of g(n). f(n)<=O[g(n)]

Tim

e

No. of inputs

Page 16: Analysis of algorithm

• The growth rate of f(n) will not be less than the growth rate of C2*g(n) and not more than c1*g(n).

• C2*g(n)<=f(n)<=C1*g(n)

Page 17: Analysis of algorithm