You are on page 1of 34

Design and Analysis of

Algorithms

Adapted from slides by Dr A. Sattar


Complexity Analysis
• Add Array Members

ARRAY-ADD(A, n) cost Times


• result=0 c1 ?
• for (i=1; i<=n; i++) c2 ?
• result=result + A[i] c3 ?
To compute T(n), the running time of the algorithm, we sum product of cost and
time column

T(n) = c1+c2(n+1)+c3(n) =a+bn (where a and b are constants that depend on ci )


Complexity Analysis (contd…)
• In the previous example, there are always n passes through the for
loop no matter what the value of the numbers in the array

• This is known as the Every-case running time

• Other examples include ….

• Consider the example of search in an array


Complexity Analysis (contd…)
• Search Array Members

ARRAY-SEARCH(A,n,key) cost Times


1.For (i=1; i<=n; i++) c1 ?
2. if A[i]=key c2 ?
3. return i c3 ?
4.return -1 c4 ?

In the worst case, loop will be executed n+1 times

• Worst case analysis


T(n)=a+bn
Complexity Analysis (contd…)
• Search Array Members

ARRAY-SEARCH(A,n,key) cost Times


1.For (i=1; i<=n; i++) c1 ?
2. if A[i]=key c2 ?
3. return i c3 ?
4.return -1 c4 ?

In the worst case, loop will be executed n+1 times

• Worst case analysis


T(n)=a+bn
Complexity Analysis (contd…)
• It may interest us to know how an algorithm performs on an average

• It is more difficult to analyze the average case than the worst case

• To compute average case complexity, we need to assign probabilities

• In case of linear search, equal probabilities are assigned to all array slots i.e. the key is equally
likely in any array slot

• Assuming that key has equal probability 1/n of being in any position, and unit cost is c, the average
running time is given by

1c.1/n+2c.1/n+3c.1/n+….+nc.1/n=c/n(1+2+….+n)=cn(n+1)/2n= c(n+1)/2
Complexity Analysis (contd…)
• Average case analysis
A(n)=c(n+1)/2

• Best case analysis


B(n)=1

Worst-case and average-case analysis are done much more often than best-
case analysis
Order of Growth
• We require some simplifying assumptions to ease our analysis

• We do this by ignoring the actual cost of each statement, and even


the abstract costs

• Another simplifying assumption is that it is only the rate of growth or


order of growth of a function that interests us
24 Adapted from slides by Dr A. Sattar
24
24
24
24
24
24
Asymptotic Analysis
• When we consider rate of growth, we need only consider the leading
term of a formula, since lower order terms are insignificant in
comparison
• Consider an algorithm with running time c1n2 and another with
running time c2nlgn. Even if c2 is larger than c1, once n is large,
algorithm1 is beaten by algorithm2.
• Suppose c1=10 and C2=100 .. Apply different values of n and calculate your
self
Asymptotic Analysis (contd…)
• We may try to determine the exact running time of an algorithm, but the extra
precision is not worth the effort

• For large inputs, the multiplicative constants and lower order terms are
dominated by effects of input size

• When we are looking at input sizes that are large enough to make only order of
growth of the running time relevant, we are studying asymptotic efficiency of
algorithms

• We are concerned with how running time of an algorithm increases with the size
of the input in the limit
Asymptotic
• Definition
• line that continually approaches a given
curve but does not meet it at any finite
distance.
• Example
• x is asymptotic with x + 1

Lecture #5 Adapted from slides by Dr A. Sattar


24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
24 Adapted from slides by Dr A. Sattar
Asymptotic Notation

24 Adapted from slides by Dr A. Sattar

You might also like