2 - Algorithm Efficiency and Big-O
2 - Algorithm Efficiency and Big-O
Running Time
2
Running Time
80
with the input size.
60
Average case time is often
difficult to determine. 40
running time. 0
1000 2000 3000 4000
Easier to analyze Input Size
Crucial to applications such as
games, finance and robotics
Analysis of Algorithms
Experimental 9000
Studies 8000
7000
Time (ms)
implementing the algorithm 5000
© 2014 Goodrich,
3 Analysis of Algorithms
Tamassia, Goldwasser
Limitations of Experiments
4
© 2014 Goodrich,
Analysis of Algorithms
Tamassia, Goldwasser
Theoretical Analysis
5
© 2014 Goodrich,
Analysis of Algorithms
Tamassia, Goldwasser
Pseudocode
6
© 2014 Goodrich,
Analysis of Algorithms
Tamassia, Goldwasser
Pseudocode Details
Analysis of
Algorithms
A RAM consists of
A CPU
T (n )
1E+16
◼ N-Log-N n log n 1E+14
◼ Quadratic n2 1E+12
1E+10
◼ Cubic n3 1E+8
◼ Exponential 2n 1E+6
1E+4
1E+2
❑ In a log-log chart, the slope 1E+0
of the line corresponds to 1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
the growth rate n
© 2014 Goodrich,
Analysis of Algorithms
Tamassia, Goldwasser
Log-log chart/plot
10
y = xa
log y = a log x
Slope is a
g(n) = n lg n
g(n) = 1 g(n) = 2n
g(n) = n2
g(n) = lg n
g(n) = n
g(n) = n3
© 2014 Goodrich,
Analysis of Algorithms
Tamassia, Goldwasser
Primitive Operations
Analysis of
Algorithms
Basic computations
Examples:
performed by an algorithm
Evaluating an
Identifiable in pseudocode expression
Largely independent from the Assigning a value to
a variable
programming language
Indexing into an
Exact definition not important array
(we will see why later) Calling a method
Assumed to take a constant Returning from a
method
amount of time in the RAM
model
© 2014 Goodrich,
12
Tamassia, Goldwasser
Algorithm Efficiency and Big-O
Getting a precise measure of the performance of
an algorithm is difficult
Big-O notation expresses the performance of an
algorithm as a function of the number of items to be
processed
This permits algorithms to be compared for
efficiency
For more than a certain number of data items, some
problems cannot be solved by any computer
Linear Growth Rate
If processing time increases in proportion to the
number of inputs n, the algorithm grows at a linear
rate
T(n) = 3(n – 1) + 3 (n – 2) + … + 3
Factoring out the 3,
3(n – 1 + n – 2 + n – 3 + … + 1)
1 + 2 + … + n – 1 = (n x (n-1))/2
Big-O Example 2 (cont.)
Therefore T(n) = 1.5n2 – 1.5n
When n = 0, the polynomial has the value 0
For values of n > 1, 1.5n2 > 1.5n2 – 1.5n
Therefore T(n) is O(n2) when n0 is 1 and c is 1.5
Big-O Example 2 (cont.)
Symbols Used in Quantifying
Performance
Common Growth Rates
Different Growth Rates
Effects of Different Growth Rates
Algorithms with Exponential and
Factorial Growth Rates
Algorithms with exponential and factorial growth
rates have an effective practical limit on the size of
the problem they can be used to solve
With an O(2n) algorithm, if 100 inputs takes an
hour then,
101 inputs will take 2 hours
105 inputs will take 32 hours