Asymptotic Notation
Big-O Notation
Definition: f (n) = O(g(n)) iff there are two
positive constants c and n0 such that
|f (n)| c |g(n)| for all n n0
cg(n)
f(n)
f(n) = O(g(n))
n0
Asymptotic Notation
Example: n2 + n = O(n3 )
Proof:
Here, we have f (n) = n2 + n, and g(n) = n3
Asymptotic Notation
Big- notation
Definition: f (n) = (g(n)) iff there are two
positive constants c and n0 such that
|f (n)| c |g(n)| for all n n0
cg(n)
f(n) = O(g(n))
n0
Asymptotic Notation
Asymptotic Notation
Big- notation
Definition: f (n) = (g(n)) iff there are three
positive constants c1 , c2 and n0 such that
c1 |g(n)| |f (n)| c2 |g(n)| for all n n0
f(n)
c2 g(n)
c1 g(n)
n0
Asymptotic Notation
Example: n2 + 5n + 7 = (n2 )
Proof:
When n 1,
n2 + 5n + 7 n2 + 5n2 + 7n2 13n2
When n 0,
n2 n2 + 5n + 7
Thus, when n 1
1n2 n2 + 5n + 7 13n2
Thus, we have shown that n2 + 5n + 7 = (n2 )
(by definition of Big-, with n0 = 1, c1 = 1, and
c2 = 13.)
Asymptotic Notation
Asymptotic Notation
Asymptotic Notation
10
Asymptotic Notation
11
1 2
1
7
n + 3n n2 + 3n2 = n2
2
2
2
Thus,
1 2
n + 3n = O(n2 )
2
Also, when n 0,
1 2
1 2
n n + 3n
2
2
So
1 2
n + 3n = (n2 )
2
Asymptotic Notation
12
c n log n n log n 2 n,
which is equivalent to
2
c1
, when n > 1.
log n
If n 8, then 2/(log n) 2/3, and picking c = 1/3
suffices. Thus if c = 1/3 and n0 = 8, then for all
n n0 , we have
0 c n log n n log n 2 n n log n 2 n + 13.
Thus (n log n 2 n + 13) = (n log n).
Asymptotic Notation
13
1 2
n 3 n c2 n2 for all n n0
2
Dividing by n2 , we get
0 c1
c1
1
2
1
2
3
n
3
n
3
1
c2
2 n
1 2
n 3 n c2 n2 for all n n0 .
2
Asymptotic Notation
Answer: O(n2 )
We will see more examples later.
14
Asymptotic Notation
15
Asymptotic Notation
16
Quadratic: (n2 )
Polynomial: (nk )
Exponential: (k n )
Well take a closer look at each of these classes.
Asymptotic Notation
17
Asymptotic Notation
void sum_first_n(int n) {
int i,sum=0;
for (i=1;i<=n;i++)
sum = sum + i;
}
void m_sum_first_n(int n) {
int i,k,sum=0;
for (i=1;i<=n;i++)
for (k=1;k<7;k++)
sum = sum + i;
}
18
Asymptotic Notation
19
Asymptotic Notation
20
Asymptotic Notation
21
Asymptotic Notation
22
Asymptotic Notation
23
n log n
n2
n3
2n
0
0.6931
1.099
1.386
1.609
1.792
1.946
2.079
2.197
2.303
2.398
2.485
2.565
2.639
2.708
2.773
2.833
2.890
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
0
1.39
3.30
5.55
8.05
10.75
13.62
16.64
19.78
23.03
26.38
29.82
33.34
36.95
40.62
44.36
48.16
52.03
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
1
8
27
64
125
216
343
512
729
1000
1331
1728
2197
2744
3375
4096
4913
5832
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
log log m
log m
Asymptotic Notation
24
100n
n2
11n2
n3
2n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
100
200
300
400
500
600
700
800
900
1000
1100
1200
1300
1400
1500
1600
1700
1800
1900
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
11
44
99
176
275
396
539
704
891
1100
1331
1584
1859
2156
2475
2816
3179
3564
3971
1
8
27
64
125
216
343
512
729
1000
1331
1728
2197
2744
3375
4096
4913
5832
6859
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
Asymptotic Notation
25
n2
n2 n
n2 + 99
n3
n3 + 234
2
6
10
14
18
22
26
30
34
38
42
46
50
54
58
62
66
70
74
4
36
100
196
324
484
676
900
1156
1444
1764
2116
2500
2916
3364
3844
4356
4900
5476
2
30
90
182
306
462
650
870
1122
1406
1722
2070
2450
2862
3306
3782
4290
4830
5402
103
135
199
295
423
583
775
999
1255
1543
1863
2215
2599
3015
3463
3943
4455
4999
5575
8
216
1000
2744
5832
10648
17576
27000
39304
54872
74088
97336
125000
157464
195112
238328
287496
343000
405224
242
450
1234
2978
6066
10882
17810
27234
39538
55106
74322
97570
125234
157698
195346
238562
287730
343234
405458
Asymptotic Notation
Polynomial Functions
40000
x
x**2
x**3
x**4
35000
30000
25000
20000
15000
10000
5000
0
10
15
20
25
30
35
40
26
Asymptotic Notation
log(x)
x
x*log(x)
x**2
200
150
100
50
0
10
15
20
25
30
35
40
27
Asymptotic Notation
x
x**3
x**4
2**x
10
28
Asymptotic Notation
x
x**3
x**4
2**x
10
15
20
29
Asymptotic Notation
10
15
20
25
30
30
Asymptotic Notation
31
Classification Summary
We have seen that when we analyze functions
asymptotically:
Only the leading term is important.
Constants dont make a significant difference.
The following inequalities hold asymptotically:
2