Chapter 1 - Analysis of Algorithms 2
Chapter 1 - Analysis of Algorithms 2
Algorithms Design
and Analysis
Why to learn
algorithms ?
Course Goals
10
Our goal:
Analysis of Algorithms
Measure Performance
Measure Space
Complexity
Running Time
• Most algorithms transform
input objects into output best case
objects. average case
worst case
• The running time of an 120
Running Time
80
• Easier to analyze 0
1000 2000 3000 4000
• Crucial to applications Input Size
such as games, finance
and robotics 12
Why discarding average case,
and choose worst case instead?
• An algorithm may run faster on some inputs
than it does on others of the same size. Thus,
we may wish to express the running time of an
algorithm as the function of the input size
Time (ms)
composition, noting the time 5000
needed: 4000
3000
2000
1000
0
0 50 100
• Plot the results Input Size
14
Limitations of Experiments
• It is necessary to implement the whole
algorithm before conducting any experiment,
which may be difficult.
• Results may not be indicative of the running time
on other inputs not included in the experiment.
• In order to compare two algorithms, the same
hardware and software environments must
be used
15
So we need another way to measure
the performance of the algorithms
• So we need to learn about Theoretical
analysis or Asymptotic analysis.
• Uses a high-level description of the algorithm
instead of an implementation (Pseudo code).
• Characterizes running time as a function of
the input size, n.
• Takes into account all possible inputs.
• Allows us to evaluate the speed of an
algorithm independent of the
hardware/software environment.
Pseudo code
• High-level description of an
algorithm.
• More structured than
English prose.
• Less detailed than a
program.
• Preferred notation for
describing algorithms.
• Hides program design
Big-Oh Notation
• Given functions f(n) and
g(n), we say that f(n) is
O(g(n)) if there are
positive constants
c and n0 such that
big-Theta
f(n) is (g(n)) if there are constants c’ > 0
19
Essential Seven functions to estimate algorithms
performance
g(n) = 1
Print(“Hello
Algorithms”)
Essential Seven functions to estimate algorithms
performance
g(n) = n
for i in
range(0, n):
Print(i)
Essential Seven functions to estimate algorithms
performance
Def power_of_2(a):
x = 0
g(n) = lg n while a > 1:
a = a/2
x = x+1
return x
Essential Seven functions to estimate algorithms
performance
for i in range(0,n):
Def power_of_2(a):
x = 0
while a > 1:
a = a/2
x = x+1
return x
g(n) = n lg n
Essential Seven functions to estimate algorithms
performance
for i in range(0,n):
for j in range(0,n):
print(i*j);
g(n) =
n2
Essential Seven functions to estimate algorithms
performance
g(n) =
n3
Essential Seven functions to estimate algorithms
performance
def F(n):
if n == 0:
return 0
elif n == 1:
return 1
g(n) = 2n else: return
F(n-1) + F(n-2)
Seven Important Functions
• Seven functions that often
appear in algorithm analysis:
• Constant 1
• Logarithmic log n
• Linear n
• N-Log-N n log n
• Quadratic n2
• Cubic n3
• Exponential 2n
28
How to calculate the algorithm’s complexity
analysis
Prefix Averages
(Quadratic)
The following algorithm computes prefix
averages in quadratic time by applying the
definition
31
Prefix Averages 2 (Looks
Better)
The following algorithm uses an internal Python
function to simplify the code
Which means that the time complexity of the time series T(n) is equal to constant
(denoted by c) that represents a constant number of operations that should occur
(for example checking if n is greater than 1 ) in addition to the call of T(n-1)
Ex. 2:
= n + T (n-1) at n>1
T(n) (1)
=1 elsewhere
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
40
General Divide-and-Conquer Recurrence
Examples :
T(n) = aT(n/b) + f (n ), d T(n)
d
0 = T(n/2) + n
•
Here a = 1, b = 2, d = 1, a < bd
Master Theorem: O(n )
If a < bd O(nd)
If a = bd • T(n) = 2T(n/2) + 1
O(nd logb (n))
If a > bd O(nlogb (a) ) Here a = 2, b = 2, d = 0, a > bd
O(n log
2
2
) = O(n )
41
Examples
• T(n) = T(n/2) + 1
Here a = 1, b = 2, d = 0, a = bd
O(log(n) )
• T(n) = 4T(n/2) + n
Here a = 4, b = 2, d = 1, a > bd
O(n log
2
4
) = O(n 2
)
• T(n) = 4T(n/2) + n2
Here a = 4, b = 2, d = 2, a = bd
O(n2 log n)
• T(n) = 4T(n/2) + n3
Here a = 4, b = 2, d = 3, a < bd
O(n3)
42
Analysis of recursive algorithm
Ex.:
= 2 T (n / 2) + c at n>1
T(n) (1)
=1 elsewhere
Solution O(n)
Analysis of recursive algorithm: recursion tree method
Ex. :
= 2 T (n / 2) + n at n>1
T(n) =1 elsewhere