Lecture2 M 131122062242 Phpapp02
Lecture2 M 131122062242 Phpapp02
Lecture2 M 131122062242 Phpapp02
1
Informally Definition
As a sequence of computational
steps that transforms the input
into output
Algorithms
Properties of algorithms:
4
?
Suppose computers were infinitely fast and
computer memory are free
Yes
– Demonstrate that solution methods
terminates and does so with correct
answer.
If computers were infinitely fast,
any correct method for solving a
problem would do.
8
Complexity
9
Complexity
Comparison: time complexity of algorithms A and B
10
Complexity
11
Growth Function
12
Asymptotic Efficiency
Algorithm
When the input size is large enough so
that the rate of growth / order of
growth of the running time is relevant
14
Asymptotic Notation
15
Asymptotic Notation
16
Asymptotic Notation
17
Asymptotic Notation
Let f(n) and g(n) be two positive
functions, representing the
number of basic calculations
(operations, instructions) that an
algorithm takes (or the number
of memory words an algorithm
needs).
18
Asymptotic Notation
Q - Big Theta
O – Big O
W - Big Omega
o – Small o
w - Small Omega
19
- Notation
For a given function g(n), we denote
by (g(n)) the set of functions
g(n) is an
asymptotically tight
bound for f(n).
21
Example
(g(n)) = {f(n) : positive constants c1, c2,
and n0, such that n n0, 0 c1g(n)
f(n) c2g(n) }
1/2n2 - 3n = Q(n2)
Determine the positive constant n0,
c1, and c2 such that
c1 n2 1/2n2 - 3n c2 n2 n n0
22
Example Contd ...
c1 = 1/14 , c2 = ½ , n0 = 7
1/2n2 - 3n = Q(n2)
23
O-Notation
For a given function g(n)
O(g(n)) = {f(n) : there exist
positive constants c and n0 such
that 0 f(n) c g(n) for all n
n0 }
Intuitively: Set of all functions whose rate of
growth is the same as or lower than that of
c. g(n)
24
O-Notation
26
Big-O Notation
(Examples)
f(n) = 5n+2 = O(n) // g(n) = n
– f(n) 6n, for n 3 (C=6, n0=3)
f(n)=n/2 –3 = O(n)
– f(n) 0.5 n for n 0 (C=0.5, n0=0)
n2-n = O(n2) // g(n) = n2
– n2-n n2 for n 0 (C=1, n0=0)
n(n+1)/2 = O(n2)
– n(n+1)/2 n2 for n 0 (C=1, n0=0)
27
- Notation
For a given function g(n)
(g(n)) = {f(n) : there exist
positive constants c and n0 such
that 0 c g(n) f(n) for all
n n0 }
Intuitively: Set of all functions whose
rate of growth is the same as or higher
than that of g(n).
28
- Notation
30
Relations Between Q, O, W
For any two function f(n) and g(n),
we have f(n) = (g(n)) if and only
if f(n) = O(g(n)) and f(n) =
(g(n))
That is
(g(n)) = O(g(n)) Ç W(g(n))
31
The Growth of Functions
“Popular” functions g(n) are
n log n, 1, 2n, n2, n!, n, n3, log n
32
Comparing Growth Rates
2n n2
n log2 n
T(n)
log2 n
Problem Size
Example: Find sum of array elements
Algorithm arraySum (A, n)
Input array A of n integers
Output Sum of elements of A # operations
sum 0
1
for i 0 to n 1 do n+1
sum sum + A [i] n
return sum 1
Input size: n (number of array elements)
Total number of steps: 2n + 3
Example: Find max element of an
array
Algorithm arrayMax(A, n)
Input array A of n integers
Output maximum element of A #
operations
currentMax A[0]
1
for i 1 to n 1 do
n
if A [i] currentMax then
n -1
Input size: n (number of array elements)
currentMax A [i]
Total number of steps: 3n
n -1
return currentMax