complete construction, environmental and economics information of biomass com...
Â
Design and Analysis of Algorithms
1. Parul Institute of Engineering & Technology
Subject Code : 150703
Name Of Subject : DESIGN & ANALYSIS OF ALGORITHM
Name of Unit : ANALYSIS OF ALGORITHM
Topic : Introduction,Analysis,Efficeincy of algorithm
Name of Student: Agrawal Swapnil
2. CONTENTS
ďąINTRODUCTION TO ALGORITHMS
ďąANALYSIS OF ALGORITHM
ďąEFFIECIENCY OF ALGORITHM
ďąBEST CASE
ďąWORST CASE
ďąAVERAGE CASE
ďąEXAMPLES
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
3. Introduction of algorithms
⢠An Algorithm is a Step by Step solution of a specific
mathematical or computer related problem
⢠There are different types of Algorithms
â Greedy
â Dynamic
â Divide & Conquer ,etc
⢠They can be expressed in different ways.
â Pseudocode
â By using simple English statements(Steps)
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
4. Analysis of algorithms
⢠Issues:
â correctness
â time efficiency
â space efficiency
â optimality
⢠Approaches:
â theoretical analysis
â empirical analysis
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
5. Theoretical analysis of time efficiency
Time efficiency is analyzed by determining the number
of repetitions of the basic operation as a function of
input size
⢠Basic operation: the operation that contributes
most towards the running time of the algorithm
T(n) â copC(n)
running time
execution time
for basic operation
Number of times
basic operation is
executed
input size
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
6. Input size and basic operation examples
ProblemProblem Input size measureInput size measure Basic operationBasic operation
Searching for key in aSearching for key in a
list oflist of nn itemsitems
Number of listâs items, i.e.Number of listâs items, i.e.
nn
Key comparisonKey comparison
Multiplication of twoMultiplication of two
matricesmatrices
Matrix dimensions or totalMatrix dimensions or total
number of elementsnumber of elements
Multiplication of twoMultiplication of two
numbersnumbers
Checking primality of aChecking primality of a
given integergiven integer nn
nânâsize = number of digitssize = number of digits
(in binary representation)(in binary representation)
DivisionDivision
Typical graph problemTypical graph problem #vertices and/or edges#vertices and/or edges
Visiting a vertex orVisiting a vertex or
traversing an edgetraversing an edge
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
7. Empirical analysis of time
efficiency
⢠Select a specific (typical) sample of inputs
⢠Use physical unit of time (e.g., milliseconds)
or
Count actual number of basic operationâs
executions
⢠Analyze the empirical data
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
8. Best-case, average-case, worst-case
For some algorithms efficiency depends on form of input:
⢠Worst case: Cworst(n) â maximum over inputs of size n
⢠Best case: Cbest(n) â minimum over inputs of size n
⢠Average case: Cavg(n) â âaverageâ over inputs of size n
â Number of times the basic operation will be executed
on typical input
â NOT the average of worst and best case
â Expected number of basic operations considered as a
random variable under some assumption about the
probability distribution of all possible inputs
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
9. Example: Sequential search
⢠Worst case
⢠Best case
⢠Average case
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
10. Types of formulas for basic operationâs count
⢠Exact formula
e.g., C(n) = n(n-1)/2
⢠Formula indicating order of growth with specific
multiplicative constant
e.g., C(n) â 0.5 n2
⢠Formula indicating order of growth with unknown
multiplicative constant
e.g., C(n) â cn2
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
11. Order of growth
⢠Most important: Order of growth within a constant
multiple as nââ
⢠Example:
â How much faster will algorithm run on computer
that is twice as fast?
â How much longer does it take to solve problem of
double input size?
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
12. Values of some important functions as n â â
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
13. Asymptotic order of growth
A way of comparing functions that ignores constant factors
and small input sizes
⢠O(g(n)): class of functions f(n) that grow no faster than
g(n)
⢠Î(g(n)): class of functions f(n) that grow at same rate as
g(n)
⢠Ί(g(n)): class of functions f(n) that grow at least as fast
as g(n)
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
14. Big-oh
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
15. Big-omega
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
16. Big-theta
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
17. Establishing order of growth using
the definition
Definition: f(n) is in O(g(n)) if order of growth of f(n) â¤
order of growth of g(n) (within constant multiple),
i.e., there exist positive constant c and non-negative
integer n0 such that
f(n) ⤠c g(n) for every n ⼠n0
Examples:
⢠10n is O(n2
)
⢠5n+20 is O(n)
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
18. Some properties of asymptotic order of growth
⢠f(n) â O(f(n))
⢠f(n) â O(g(n)) iff g(n) ââŚ(f(n))
⢠If f(n) â O(g(n)) and g(n) â O(h(n)) , then f(n) â
O(h(n))
Note similarity with a ⤠b
⢠If f1(n) â O(g1(n)) and f2(n) â O(g2(n)) , then
f1(n) + f2(n) â O(max{g1(n), g2(n)})
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
19. Establishing order of growth using limits
limT(n)/g(n) =
00 order of growth of TT((n)n) < order of growth of gg((nn))
cc > 0> 0 order of growth of TT((n)n) = order of growth of gg((nn))
ââ order of growth of TT((n)n) > order of growth of gg((nn))
Examples:Examples:
⢠1010nn vs.vs. nn22
⢠nn((nn+1)/2 vs.+1)/2 vs. nn22
nnââââ
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
20. LâHĂ´pitalâs rule and Stirlingâs formula
LâHĂ´pitalâs rule: If limnââf(n) = limnââg(n) = â and
the derivatives f´, g´ exist, then
Stirlingâs formula: n! â (2Ďn)1/2
(n/e)n
ff((nn))
gg((nn))
limlim
nnââââ
=
ff ´(´(nn))
gg ´(´(nn))
limlim
nnââââ
Example: logExample: log nn vs.vs. nn
Example: 2Example: 2nn
vs.vs. nn!!
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
21. Orders of growth of some important functions
⢠All logarithmic functions logan belong to the same class
Î(log n) no matter what the logarithmâs base a > 1 is
⢠All polynomials of the same degree k belong to the same
class: aknk
+ ak-1nk-1
+ ⌠+ a0â Î(nk
)
⢠Exponential functions an
have different orders of growth
for different aâs
⢠order log n < order nι
(Îą>0) < order an
< order n! <
order nn
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
22. Basic asymptotic efficiency classes
11 constantconstant
loglog nn logarithmiclogarithmic
nn linearlinear
nn loglog nn n-n-loglog-n-n
nn22
quadraticquadratic
nn33
cubiccubic
22nn
exponentialexponential
nn!! factorialfactorial
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
23. Time efficiency of nonrecursive
algorithms
General Plan for Analysis
⢠Decide on parameter n indicating input size
⢠Identify algorithmâs basic operation
⢠Determine worst, average, and best cases for input of
size n
⢠Set up a sum for the number of times the basic
operation is executed
⢠Simplify the sum using standard formulas and rules
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
24. Useful summation formulas and rules
⢠Σlâ¤iâ¤u1 = 1+1+âŚ+1 = u - l + 1
⢠In particular, ÎŁlâ¤iâ¤u1 = n - 1 + 1 = n â Î(n)
⢠Σ1â¤iâ¤ni = 1+2+âŚ+n = n(n+1)/2 â n2
/2 â Î(n2
)
⢠Σ1â¤iâ¤ni2
= 12
+22
+âŚ+n2
= n(n+1)(2n+1)/6 â n3
/3 â Î(n3
)
⢠Σ0â¤iâ¤nai
= 1+ a +âŚ+ an
= (an+1
- 1)/(a - 1) for any a â 1
⢠In particular, ÎŁ0â¤iâ¤n2i
= 20
+ 21
+âŚ+ 2n
= 2n+1
- 1 â Î(2n
)
⢠Σ(ai¹ bi) = Σai¹ Σbi
⢠Σcai = cΣai
⢠Σlâ¤iâ¤uai = ÎŁlâ¤iâ¤mai+ ÎŁm+1â¤iâ¤uai
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
25. Example 1: Maximum element
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
26. Example 2: Element uniqueness
problem
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
27. Example 3: Matrix multiplication
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
28. Example 4: Gaussian elimination
Algorithm GaussianElimination(A[0..n-1,0..n])
//Implements Gaussian elimination of an n-by-(n+1)
matrix A
for i â 0 to n - 2 do
for j â i + 1 to n - 1 do
for k â i to n do
A[j,k] â A[j,k] - A[i,k] â A[j,i] / A[i,i]
Find the efficiency class and a constant factor
improvement.
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
29. Example 5: Counting binary digits
It cannot be investigated the way the previous
examples are.
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
30. Plan for Analysis of Recursive
Algorithms
⢠Decide on a parameter indicating an inputâs size.
⢠Identify the algorithmâs basic operation.
⢠Check whether the number of times the basic op. is
executed may vary on different inputs of the same size.
(If it may, the worst, average, and best cases must be
investigated separately.)
⢠Set up a recurrence relation with an appropriate initial
condition expressing the number of times the basic op.
is executed.
⢠Solve the recurrence (or, at the very least, establish its
solutionâs order of growth) by backward substitutions
or another method.
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
31. Example 1: Recursive evaluation of n!
Definition: n ! = 1 â 2 â ⌠â(n-1) â n for n ⼠1
and 0! = 1
Recursive definition of n!: F(n) = F(n-1) â n for n ⼠1
and F(0) = 1
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
32. Example 2: Counting #bits
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
33. Fibonacci numbers
ď The Fibonacci numbers:
ď0, 1, 1, 2, 3, 5, 8, 13, 21, âŚ
ď The Fibonacci recurrence:
ďF(n) = F(n-1) + F(n-2)
ďF(0) = 0
ďF(1) = 1
ď General 2nd
order linear homogeneous recurrence
with constant coefficients:
ď aX(n) + bX(n-1) + cX(n-2) = 0
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
34. Solving aX(n) + bX(n-1) + cX(n-2) = 0
⢠Set up the characteristic equation (quadratic)
ar2
+ br + c = 0
⢠Solve to obtain roots r1 and r2
⢠General solution to the recurrence
if r1and r2 are two distinct real roots: X(n) = Îąr1
n
+ βr2
n
if r1=r2= r are two equal real roots: X(n) = Îąrn
+ βnrn
⢠Particular solution can be found by using initial
conditions
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
35. Application to the Fibonacci
numbers
F(n) = F(n-1) + F(n-2) or F(n) - F(n-1) - F(n-2) = 0
Characteristic equation:
Roots of the characteristic equation:
General solution to the recurrence:
Particular solution for F(0) =0, F(1)=1:
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
36. Computing Fibonacci numbers
1. Definition-based recursive algorithm
2. Non recursive definition-based algorithm
3. Explicit formula algorithm
4. Logarithmic algorithm based on formula:
FF((nn-1)-1) FF((nn))
FF((nn)) FF((nn+1)+1)
0 10 1
1 11 1
==
nn
forfor nnâĽâĽ1,1, assuming an efficient way of computing matrix powersassuming an efficient way of computing matrix powers..
Sub: DESIGN & ANALYSIS OF ALGORITHM
Topic: Introduction,Analysis,Effieciency Of Algorithm
37. REFERENCES
Books:-
1. Introduction to design & analysis of algorithm
By Anany levitin, Pearson Publication
2. Design & Analysis of Algorithm
By A.A.Puntambekar , Technical Publication
3. Introduction to Algorithms
By Thomas Cormen , MIT Press
Web Resources:-
1.Google
2.Wikipedia
Hinweis der Redaktion
Example: cn2
how much faster on twice as fast computer? (2)
how much longer for 2n? (4)