Вы находитесь на странице: 1из 21

Asymptotic Analysis

O(n)
(n)
(n)
Design & Analysis of Algorithms
Asymptotic Analysis
Dr. Iftikhar Ahmad
Department of CS & IT
University of Enginnering & Technology, Peshawar
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 1 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Overview
1
Asymptotic Analysis
Motivation
Asymptotic Analysis
Basic Complexity Classes
2
O(n)
Formal Denition
Representation
Examples
3
(n)
Formal Denition
Representation
Examples
4
(n)
Formal Denition
Representation
Examples
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 2 / 17
Overview
1
Asymptotic Analysis
Motivation
Asymptotic Analysis
Basic Complexity Classes
2
O(n)
Formal Denition
Representation
Examples
3
(n)
Formal Denition
Representation
Examples
4
(n)
Formal Denition
Representation
Examples
Asymptotic Analysis
O(n)
(n)
(n)
Motivation
Asymptotic Analysis
Basic Complexity Classes
Motivation
Suppose we have two algorithm A
1
, A
2
for an imaginary
problem P.
Running time of A
1
: 2n
2
n + 4.
Running time of A
2
: n
2
+ 4n 1.
Which algorithm is better?.
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 3 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Motivation
Asymptotic Analysis
Basic Complexity Classes
Asymptotic Analysis
A tool for analyzing time and space complexity of algorithms.
Based on the input size n.
Gives time and space bounds as function of n.
Focuses only on rate of growth.
Ignores multiplicative and Additive constant.
100n, 5n + 2, 10n 5 are all treated equally.
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 4 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Motivation
Asymptotic Analysis
Basic Complexity Classes
Basic Complexity Classes
Three basic classes.
O (Big Oh).
(Big Omega).
(Big Theta).
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 5 / 17
Overview
1
Asymptotic Analysis
Motivation
Asymptotic Analysis
Basic Complexity Classes
2
O(n)
Formal Denition
Representation
Examples
3
(n)
Formal Denition
Representation
Examples
4
(n)
Formal Denition
Representation
Examples
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
O(n)
Denition
f (n) O(g(n)) if f (n) is bounded above by some constant
multiple of g(n), n n
0
.
i.e., f (n) c.g(n), n n
0
.
O(g(n)) is upper bound on the growth of f (n).
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 6 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
O(n)
n
Does not
matter
f(n)
c.g(n)
n
0
Figure : O Notation
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 7 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
Examples
If f (n) = n
2
+ 8n 4.
Show that f (n) = O(n
2
).
Proof;
We need to nd c and n
0
such that f (n) c.g(n), where
g(n) = n
2
and n n
0
Alternatively, we need to nd c and n
0
such that
n
2
+ 8n 4 c.n
2
, n n
0
.
1 + 8/n 4/n
2
c.
If n > 1, then c 4.
i.e., n > 1, n
2
+ 8n 4 4.n
2
.
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 8 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
Examples
Express the following in O notation.
n
3
5n
2
+ 2n + 6.
2n
2
+ 3n 5.
log(n) + 100.
10n 7.

n
i =1
i .
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 9 / 17
Overview
1
Asymptotic Analysis
Motivation
Asymptotic Analysis
Basic Complexity Classes
2
O(n)
Formal Denition
Representation
Examples
3
(n)
Formal Denition
Representation
Examples
4
(n)
Formal Denition
Representation
Examples
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
(n)
Denition
f (n) (g(n)) if f (n) is bounded below by some positive
constant multiple of g(n), n n
0
.
i.e., 0 c.g(n) f (n), n n
0
.
(g(n)) is lower bound on the growth of f (n).
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 10 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
(n)
n
Does not
matter
f(n)
c.g(n)
n
0
Figure : Notation
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 11 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
Examples
If f (n) = n
3
.
Show that f (n) = (n
2
).
Proof;
We need to nd c and n
0
such that c.g(n) f (n), where
g(n) = n
2
and n n
0
Alternatively, we need to nd c and n
0
such that
c.n
2
n
3
, n n
0
.
Consider c = 1 and n
0
= 0.
n n
0
,
c.n
2
n
3
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 12 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
Examples
Express the following in notation.
n
2
2.
2n
3
+ 4.

n
i =1
i
2
.
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 13 / 17
Overview
1
Asymptotic Analysis
Motivation
Asymptotic Analysis
Basic Complexity Classes
2
O(n)
Formal Denition
Representation
Examples
3
(n)
Formal Denition
Representation
Examples
4
(n)
Formal Denition
Representation
Examples
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
(n)
Denition
f (n) (g(n)) if f (n) is bounded both below and above by
some positive constant multiple of g(n), such that
n n
0
, c
1
, c
2
.
c
1
.g(n) f (n) c
2
.g(n).
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 14 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
(n)
n
Does not
matter
f(n)
c
1
.g(n)
n
0
c
2
.g(n)
Figure : Notation
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 15 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
Examples
If f (n) =
1
2
n(n 1).
Show that f (n) = (n
2
).
Proof;
We need to nd c
1
, c
2
and n such that
c
1
.g(n) f (n) c
2
.g(n), where g(n) = n
2
f (n) c
2
.g(n)
1
2
n(n 1) =
1
2
n
2

1
2
n,
1
2
n(n 1)
1
2
n
2
. (n 0)
f (n) c
1
.g(n)
1
2
n(n 1) =
1
2
n
2

1
2
n,
1
2
n(n 1)
1
2
n
2

1
2
n
1
2
n.
1
2
n(n 1)
1
4
n
2
.
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 16 / 17
Asymptotic Analysis
O(n)
(n)
(n)
Formal Denition
Representation
Examples
Examples
Show that;
1/2n
2
3n (n
2
).
n
2
/2 2n (n
2
).
Dr. Iftikhar Ahmad Design & Analysis of Algorithms 17 / 17

Вам также может понравиться