SlideShare a Scribd company logo
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1
Analysis of algorithms
 Issues:
• correctness
• time efficiency
• space efficiency
• optimality
 Approaches:
• theoretical analysis
• empirical analysis
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2
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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3
Input size and basic operation examples
Problem Input size measure Basic operation
Searching for key in a
list of n items
Number of list’s items,
i.e. n
Key comparison
Multiplication of two
matrices
Matrix dimensions or
total number of elements
Multiplication of two
numbers
Checking primality of
a given integer n
n’size = number of digits
(in binary representation)
Division
Typical graph problem #vertices and/or edges
Visiting a vertex or
traversing an edge
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4
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
 We mostly do theoretical analysis (may do empirical in
assignment)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 5
Best-case, average-case, worst-case
For some algs C(n) is independent of the input set. For others,
C(n) depends on which input set (of size n) is used. Example
on next slide (Search). Consider three possibilities:
 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 of size n
• Consider all possible input sets of size n, average C(n) for all sets
 Some algs are same for all three (eg all case performance)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6
Example: Sequential search
 Worst case
 Best case
 Average case: depends on assumputions about input (eg
proportion of found vs not-found keys)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7
Example: Find maximum
 Worst case
 Best case
 Average case: depends on assumputions about input (eg
proportion of found vs not-found keys)
 All case
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8
Types of formulas for basic operation’s count
 Formula for C(n) can have different levels of detail
 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
 Which depends on problem, analysis technique, need
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 9
Order of growth
 Most important: Order of growth within a constant multiple
as n→∞
 Examples:
• How much faster will algorithm run on computer that is
twice as fast? What say you?
– Time = …
• How much longer does it take to solve problem of double
input size? What say you?
– Time =
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10
Values of some important functions as n  
Focus: asymptotic order of growth:
Main concern: which function describes behavior.
Less concerned with constants
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11
Asymptotic order of growth
Critical factor for problem size n:
- IS NOT the exact number of basic ops executed for given n
- IS how number of basic ops grows as n increases
Consequently:
- Focus on how performance grows as n  
- Ignore constant factors, constants, and small input sizes
- Example: We consider 100n^2 +1000 better than n+1
Call this: Asymptotic Order of Growth
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 12
Order of growth: Sets of functions
Approach: classify functions (ie of alg performance) based on
their growth rates and divide into sets
Example: Θ(n^2) is set of functions whose growth rate is n^2
These are all in Θ(n^2):
- f(n) = 100n^2 + 1000
- f(n) = n^2 + 1
- f(n) = 0.001n^2 + 1000000
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 13
Order of growth: Upper, tight, lower bounds
More formally:
- Θ(g(n)): class of functions f(n) that grow at same rate as g(n)
Upper, tight, and lower bounds on performance:
 O(g(n)): class of functions f(n) that grow no faster than g(n)
• [ie f ’s speed is same as or faster than g, f bounded above by g]
 Θ(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)
• [ie f ’s speed is same as or slower than g, f bounded below by g]
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 14
t(n)  O(g(n)) iff t(n) <=cg(n) for n > n0
t(n) = 10n3 in O(n3) and in O(n5)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15
t(n)  Ω(g(n)) iff t(n) >=cg(n) for n > n0
t(n) = 10n3 in Ω(n2) and in Ω(n3)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16
t(n) Θ(g(n)) iff t(n)O(g(n)) and Ω(g(n))
t(n) = 10n3 in Θ(n3) but NOT in Ω(n2) or O(n4)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17
Terminology
Informally: f(n) is O(g) means f(n)  O(g)
Example: 10n is O(n2)
Use similar terms for Big Omega and Big Theta
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18
Informal Definitions: Big O, Ω, Θ
Informal Definition: f(n) is in O(g(n)) if order of growth of
f(n) ≤ order of growth of g(n) (within constant multiple).
Informal Definition: f(n) is in Ω(g(n)) if order of growth of
f(n) ≥ order of growth of g(n) (within constant multiple).
Informal Definition: f(n) is in Θ(g(n)) if order of growth of
f(n) = order of growth of g(n) (within constant multiples).
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19
Formal Definitions: Big O, Ω, Θ
Definition: f(n)  O(g(n)) iff there exist positive constant c
and non-negative integer n0 such that
f(n) ≤ c g(n) for every n ≥ n0
Definition: f(n)  Ω(g(n)) iff there exist positive constant c
and non-negative integer n0 such that
f(n) ≥ c g(n) for every n ≥ n0
Definition: f(n)  Θ(g(n)) iff there exist positive constants c1
and c2 and non-negative integer n0 such that
c1 g(n) ≤ f(n) ≤ c2 g(n) for every n ≥ n0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 20
Using the Definition: Big O
Informal Definition: f(n) is in O(g(n)) if order of growth of
f(n) ≤ order of growth of g(n) (within constant multiple),
Definition: f(n)  O(g(n)) iff 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)
• [Can choose c and n0. Solve for 2 different c’s]
 5n + 20 is O(n) [Solve for c and n0]
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 21
Using the Definition: Big Omega
Informal Definition: f(n) is in  (g(n)) iff order of growth of
f(n) ≥ order of growth of g(n) (within constant multiple),
Definition: f(n)  (g(n)) iff there exist positive constant c
and non-negative integer n0 such that
f(n) ≥ c g(n) for every n ≥ n0
Examples:
 10n2 is (n)
 5n + 20 is (n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 22
Using the Definition: Theta
Informal Definition: f(n) is in (g(n)) if order of growth of
f(n) = order of growth of g(n) (within constant multiple),
Definition: f(n)  Θ(g(n)) iff there exist positive constants c1
and c2 and non-negative integer n0 such that
c1 g(n) ≤ f(n) ≤ c2 g(n) for every n ≥ n0
Examples:
 10n2 is (n2)
 5n + 20 is (n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 23
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)})
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 24
Establishing order of growth using limits
lim T(n)/g(n) =
0 order of growth of T(n) < order of growth of g(n)
c > 0 order of growth of T(n) = order of growth of g(n)
∞ order of growth of T(n) > order of growth of g(n)
Examples:
• 10n vs. n2
• n(n+1)/2 vs. n2
n→∞
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 25
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
f(n)
g(n)
lim
n
=
f ´(n)
g ´(n)
lim
n
Example: log n vs. n
Example: 2n vs. n!
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26
Orders of growth of some important functions
 All logarithmic functions loga n 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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 27
Basic asymptotic efficiency classes
1 constant Best case
log n logarithmic Divideignore part
n linear Examine each
n log n n-log-n or
linearithmic
Divideuse all parts
n2 quadratic Nested loops
n3 cubic Nested loops
2n exponential All subsets
n! factorial All permutations
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 28
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 (see
Appendix A)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 29
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n i = 1+2+ ⋯ +n = n(n+1)/2  n2/2  (n2)
1in i2 = 12+22+ ⋯ +n2 = n(n+1)(2n+1)/6  n3/3  (n3)
0in ai = 1 + a + ⋯ + an = (an+1 - 1)/(a - 1) for any a  1
In particular, 0in 2i = 20 + 21 + ⋯ + 2n = 2n+1 - 1  (2n )
(ai ± bi ) = ai ± bi cai = cai liuai = limai + m+1iuai
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 30
Example 1: Maximum element
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 31
Example 2: Element uniqueness problem
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 32
Example 3: Matrix multiplication
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 33
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.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 34
Example 5: Counting binary digits
It cannot be investigated the way the previous examples are.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 35
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.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 36
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
Size:
Basic operation:
Recurrence relation:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 37
Solving the recurrence for M(n)
M(n) = M(n-1) + 1, M(0) = 0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 38
Example 2: The Tower of Hanoi Puzzle
1
2
3
Recurrence for number of moves:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 39
Solving recurrence for number of moves
M(n) = 2M(n-1) + 1, M(1) = 1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 40
Tree of calls for the Tower of Hanoi Puzzle
n
n-1 n-1
n-2 n-2 n-2 n-2
1 1
... ... ...
2
1 1
2
1 1
2
1 1
2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 41
Example 3: Counting #bits
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 42
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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 43
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 r1 and r2 are two distinct real roots: X(n) = αr1
n + βr2
n
if r1 = r2 = r are two equal real roots: X(n) = αrn + βnr n
 Particular solution can be found by using initial conditions
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 44
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:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 45
Computing Fibonacci numbers
1. Definition-based recursive algorithm
2. Nonrecursive definition-based algorithm
3. Explicit formula algorithm
4. Logarithmic algorithm based on formula:
F(n-1) F(n)
F(n) F(n+1)
0 1
1 1
=
n
for n≥1, assuming an efficient way of computing matrix powers.

More Related Content

Similar to 5261506.ppt

ch03-2018.02.02.pptx
ch03-2018.02.02.pptxch03-2018.02.02.pptx
ch03-2018.02.02.pptx
SYAMDAVULURI
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
Deepak John
 
analysis.ppt
analysis.pptanalysis.ppt
analysis.ppt
AarushSharma69
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
NANDINI SHARMA
 
algorithm_analysis1
algorithm_analysis1algorithm_analysis1
algorithm_analysis1
Mohamed Elsayed
 
04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx
arslanzaheer14
 
Skiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationSkiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationzukun
 
AlgorithmAnalysis2.ppt
AlgorithmAnalysis2.pptAlgorithmAnalysis2.ppt
AlgorithmAnalysis2.ppt
REMEGIUSPRAVEENSAHAY
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
http://algorithmtraining.com/advanced-python-training-hyderabad/
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
http://algorithmtraining.com/advanced-python-training-hyderabad/
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
http://algorithmtraining.com/advanced-python-training-hyderabad/
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpall...
Algorithm Class at KPHB  (C, C++ Course Training Institute in KPHB, Kukatpall...Algorithm Class at KPHB  (C, C++ Course Training Institute in KPHB, Kukatpall...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpall...
http://algorithmtraining.com/advanced-python-training-hyderabad/
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 
ch08-2019-03-27 (1).ppt
ch08-2019-03-27 (1).pptch08-2019-03-27 (1).ppt
ch08-2019-03-27 (1).ppt
SaimaShaheen14
 
Algorithms
Algorithms Algorithms
Algorithms
yashodhaHR2
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 
Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1
Vai Jayanthi
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
allyn joy calcaben
 

Similar to 5261506.ppt (20)

ch03-2018.02.02.pptx
ch03-2018.02.02.pptxch03-2018.02.02.pptx
ch03-2018.02.02.pptx
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
analysis.ppt
analysis.pptanalysis.ppt
analysis.ppt
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
algorithm_analysis1
algorithm_analysis1algorithm_analysis1
algorithm_analysis1
 
04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx04. Growth_Rate_AND_Asymptotic Notations_.pptx
04. Growth_Rate_AND_Asymptotic Notations_.pptx
 
Big o
Big oBig o
Big o
 
Skiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notationSkiena algorithm 2007 lecture02 asymptotic notation
Skiena algorithm 2007 lecture02 asymptotic notation
 
AlgorithmAnalysis2.ppt
AlgorithmAnalysis2.pptAlgorithmAnalysis2.ppt
AlgorithmAnalysis2.ppt
 
Slide2
Slide2Slide2
Slide2
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpall...
Algorithm Class at KPHB  (C, C++ Course Training Institute in KPHB, Kukatpall...Algorithm Class at KPHB  (C, C++ Course Training Institute in KPHB, Kukatpall...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpall...
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
ch08-2019-03-27 (1).ppt
ch08-2019-03-27 (1).pptch08-2019-03-27 (1).ppt
ch08-2019-03-27 (1).ppt
 
Algorithms
Algorithms Algorithms
Algorithms
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1Cs6402 daa-2 marks set 1
Cs6402 daa-2 marks set 1
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
 

Recently uploaded

MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 

Recently uploaded (20)

MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 

5261506.ppt

  • 1. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1 Analysis of algorithms  Issues: • correctness • time efficiency • space efficiency • optimality  Approaches: • theoretical analysis • empirical analysis
  • 2. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2 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
  • 3. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3 Input size and basic operation examples Problem Input size measure Basic operation Searching for key in a list of n items Number of list’s items, i.e. n Key comparison Multiplication of two matrices Matrix dimensions or total number of elements Multiplication of two numbers Checking primality of a given integer n n’size = number of digits (in binary representation) Division Typical graph problem #vertices and/or edges Visiting a vertex or traversing an edge
  • 4. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4 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  We mostly do theoretical analysis (may do empirical in assignment)
  • 5. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 5 Best-case, average-case, worst-case For some algs C(n) is independent of the input set. For others, C(n) depends on which input set (of size n) is used. Example on next slide (Search). Consider three possibilities:  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 of size n • Consider all possible input sets of size n, average C(n) for all sets  Some algs are same for all three (eg all case performance)
  • 6. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6 Example: Sequential search  Worst case  Best case  Average case: depends on assumputions about input (eg proportion of found vs not-found keys)
  • 7. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7 Example: Find maximum  Worst case  Best case  Average case: depends on assumputions about input (eg proportion of found vs not-found keys)  All case
  • 8. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8 Types of formulas for basic operation’s count  Formula for C(n) can have different levels of detail  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  Which depends on problem, analysis technique, need
  • 9. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 9 Order of growth  Most important: Order of growth within a constant multiple as n→∞  Examples: • How much faster will algorithm run on computer that is twice as fast? What say you? – Time = … • How much longer does it take to solve problem of double input size? What say you? – Time =
  • 10. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10 Values of some important functions as n   Focus: asymptotic order of growth: Main concern: which function describes behavior. Less concerned with constants
  • 11. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11 Asymptotic order of growth Critical factor for problem size n: - IS NOT the exact number of basic ops executed for given n - IS how number of basic ops grows as n increases Consequently: - Focus on how performance grows as n   - Ignore constant factors, constants, and small input sizes - Example: We consider 100n^2 +1000 better than n+1 Call this: Asymptotic Order of Growth
  • 12. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 12 Order of growth: Sets of functions Approach: classify functions (ie of alg performance) based on their growth rates and divide into sets Example: Θ(n^2) is set of functions whose growth rate is n^2 These are all in Θ(n^2): - f(n) = 100n^2 + 1000 - f(n) = n^2 + 1 - f(n) = 0.001n^2 + 1000000
  • 13. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 13 Order of growth: Upper, tight, lower bounds More formally: - Θ(g(n)): class of functions f(n) that grow at same rate as g(n) Upper, tight, and lower bounds on performance:  O(g(n)): class of functions f(n) that grow no faster than g(n) • [ie f ’s speed is same as or faster than g, f bounded above by g]  Θ(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) • [ie f ’s speed is same as or slower than g, f bounded below by g]
  • 14. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 14 t(n)  O(g(n)) iff t(n) <=cg(n) for n > n0 t(n) = 10n3 in O(n3) and in O(n5)
  • 15. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15 t(n)  Ω(g(n)) iff t(n) >=cg(n) for n > n0 t(n) = 10n3 in Ω(n2) and in Ω(n3)
  • 16. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16 t(n) Θ(g(n)) iff t(n)O(g(n)) and Ω(g(n)) t(n) = 10n3 in Θ(n3) but NOT in Ω(n2) or O(n4)
  • 17. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17 Terminology Informally: f(n) is O(g) means f(n)  O(g) Example: 10n is O(n2) Use similar terms for Big Omega and Big Theta
  • 18. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18 Informal Definitions: Big O, Ω, Θ Informal Definition: f(n) is in O(g(n)) if order of growth of f(n) ≤ order of growth of g(n) (within constant multiple). Informal Definition: f(n) is in Ω(g(n)) if order of growth of f(n) ≥ order of growth of g(n) (within constant multiple). Informal Definition: f(n) is in Θ(g(n)) if order of growth of f(n) = order of growth of g(n) (within constant multiples).
  • 19. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19 Formal Definitions: Big O, Ω, Θ Definition: f(n)  O(g(n)) iff there exist positive constant c and non-negative integer n0 such that f(n) ≤ c g(n) for every n ≥ n0 Definition: f(n)  Ω(g(n)) iff there exist positive constant c and non-negative integer n0 such that f(n) ≥ c g(n) for every n ≥ n0 Definition: f(n)  Θ(g(n)) iff there exist positive constants c1 and c2 and non-negative integer n0 such that c1 g(n) ≤ f(n) ≤ c2 g(n) for every n ≥ n0
  • 20. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 20 Using the Definition: Big O Informal Definition: f(n) is in O(g(n)) if order of growth of f(n) ≤ order of growth of g(n) (within constant multiple), Definition: f(n)  O(g(n)) iff 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) • [Can choose c and n0. Solve for 2 different c’s]  5n + 20 is O(n) [Solve for c and n0]
  • 21. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 21 Using the Definition: Big Omega Informal Definition: f(n) is in  (g(n)) iff order of growth of f(n) ≥ order of growth of g(n) (within constant multiple), Definition: f(n)  (g(n)) iff there exist positive constant c and non-negative integer n0 such that f(n) ≥ c g(n) for every n ≥ n0 Examples:  10n2 is (n)  5n + 20 is (n)
  • 22. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 22 Using the Definition: Theta Informal Definition: f(n) is in (g(n)) if order of growth of f(n) = order of growth of g(n) (within constant multiple), Definition: f(n)  Θ(g(n)) iff there exist positive constants c1 and c2 and non-negative integer n0 such that c1 g(n) ≤ f(n) ≤ c2 g(n) for every n ≥ n0 Examples:  10n2 is (n2)  5n + 20 is (n)
  • 23. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 23 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)})
  • 24. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 24 Establishing order of growth using limits lim T(n)/g(n) = 0 order of growth of T(n) < order of growth of g(n) c > 0 order of growth of T(n) = order of growth of g(n) ∞ order of growth of T(n) > order of growth of g(n) Examples: • 10n vs. n2 • n(n+1)/2 vs. n2 n→∞
  • 25. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 25 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 f(n) g(n) lim n = f ´(n) g ´(n) lim n Example: log n vs. n Example: 2n vs. n!
  • 26. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26 Orders of growth of some important functions  All logarithmic functions loga n 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
  • 27. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 27 Basic asymptotic efficiency classes 1 constant Best case log n logarithmic Divideignore part n linear Examine each n log n n-log-n or linearithmic Divideuse all parts n2 quadratic Nested loops n3 cubic Nested loops 2n exponential All subsets n! factorial All permutations
  • 28. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 28 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 (see Appendix A)
  • 29. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 29 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n i = 1+2+ ⋯ +n = n(n+1)/2  n2/2  (n2) 1in i2 = 12+22+ ⋯ +n2 = n(n+1)(2n+1)/6  n3/3  (n3) 0in ai = 1 + a + ⋯ + an = (an+1 - 1)/(a - 1) for any a  1 In particular, 0in 2i = 20 + 21 + ⋯ + 2n = 2n+1 - 1  (2n ) (ai ± bi ) = ai ± bi cai = cai liuai = limai + m+1iuai
  • 30. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 30 Example 1: Maximum element
  • 31. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 31 Example 2: Element uniqueness problem
  • 32. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 32 Example 3: Matrix multiplication
  • 33. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 33 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.
  • 34. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 34 Example 5: Counting binary digits It cannot be investigated the way the previous examples are.
  • 35. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 35 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.
  • 36. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 36 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 Size: Basic operation: Recurrence relation:
  • 37. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 37 Solving the recurrence for M(n) M(n) = M(n-1) + 1, M(0) = 0
  • 38. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 38 Example 2: The Tower of Hanoi Puzzle 1 2 3 Recurrence for number of moves:
  • 39. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 39 Solving recurrence for number of moves M(n) = 2M(n-1) + 1, M(1) = 1
  • 40. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 40 Tree of calls for the Tower of Hanoi Puzzle n n-1 n-1 n-2 n-2 n-2 n-2 1 1 ... ... ... 2 1 1 2 1 1 2 1 1 2
  • 41. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 41 Example 3: Counting #bits
  • 42. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 42 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
  • 43. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 43 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 r1 and r2 are two distinct real roots: X(n) = αr1 n + βr2 n if r1 = r2 = r are two equal real roots: X(n) = αrn + βnr n  Particular solution can be found by using initial conditions
  • 44. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 44 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:
  • 45. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 45 Computing Fibonacci numbers 1. Definition-based recursive algorithm 2. Nonrecursive definition-based algorithm 3. Explicit formula algorithm 4. Logarithmic algorithm based on formula: F(n-1) F(n) F(n) F(n+1) 0 1 1 1 = n for n≥1, assuming an efficient way of computing matrix powers.