Professional Documents
Culture Documents
f :R R g:RR
Big-Oh: f ( x) is O( g ( x))
is no larger order than
Big-Omega: f ( x ) is ( g ( x ))
is no smaller order than
Big-Theta: f ( x) is ( g ( x))
is of same order as
Konstantin Busch - LSU 2
1
Big-Oh: f ( x) is O( g ( x))
(Notation abuse: f ( x) O( g ( x)) )
| f ( x) | C | g ( x) |
f ( x) x 2 g ( x) x 2 2 x 1
f ( x) O( g ( x))
x 2 O( x 2 2 x 1)
For x 0 : x2 x2 2x 1
f ( x) g ( x)
Witnesses: C 1, k 0
2
f ( x) x 2 g ( x) x 2 2 x 1
g ( x) O( f ( x))
x 2 2 x 1 O( x 2 )
Witnesses: C 4, k 1
and x 2 x 1
2
Example: x2
are of the same order
Konstantin Busch - LSU 6
3
f ( x) O( g ( x)) and | g ( x) || h( x) |
f ( x) O (h( x))
Example: x 2 2 x 1 O( x 2 )
x 2 2 x 1 O( x 3 )
| x 2 || x 3 |
n 2 O ( n)
Suppose n 2 O ( n)
| n | C
4
Theorem: If f ( x) an x n an 1 x n 1 a1 x a0
then f ( x) O( x n )
Proof: for x 1
| f ( x) | | an x n an 1 x n 1 a1 x a0 |
| an | x n | an 1 | x n 1 | a1 | x | a0 |
| an | x n | an 1 | x n | a1 | x n | a0 | x n
x n (| an | | an 1 | | a1 | | a0 |)
Witnesses: C | an | | an 1 | | a0 |, k 1
End of Proof
Konstantin Busch - LSU 9
1 2 n O(n 2 )
1 2 n n n n n2
Witnesses: C 1, k 1
5
n! 1 2 n O(n n )
n! 1 2 n n n n n n
Witnesses: C 1, k 1
2 n O(n!)
2 n 2 2 n1
2 ( 2 2 2)
2 (2 3 n)
2 n!
Witnesses: C 2, k 2
6
log n! O(n log n)
Witnesses: C 1, k 1
n O(2 n )
log n O(n)
For n 1 : n 2n
Witnesses: C 1, k 1
7
log a n O(log n)
log n
log a n
log a
1
Witnesses: C , k 1
log a
1
constant O (1)
x
x 1 : 1
For 1
x
Witnesses: C 1, k 1
8
Interesting functions
1 log n n n log n n 2 2n n!
Higher growth
Proof: x k1 | f1 ( x) | C1 | g1 ( x) |
x k2 | f 2 ( x) | C2 | g 2 ( x) |
x max( k1 , k 2 ) | ( f1 f 2 )( x) | | f1 ( x) f 2 ( x) | | f1 ( x) | | f 2 ( x) |
C1 | g1 ( x) | C2 | g 2 ( x) |
(C1 C2 ) max(| g1 ( x) |, | g 2 ( x) |)
Witnesses: C C1 C2 , k max( k1 , k 2 )
Konstantin Busch - LSU End of Proof 18
9
Corollary: If f1 ( x) O( g ( x)) , f 2 ( x) O( g ( x))
then ( f1 f 2 )( x) O( g ( x))
Multiplication
3n O(n) 3n log( n!)
O (n n log n) Addition
log( n!) O(n log n)
O (n log n)
2
10
Big-Omega: f ( x) is ( g ( x))
(Notation abuse: f ( x) ( g ( x)) )
| f ( x) | C | g ( x) |
8 x 3 5 x 2 7 ( x 3 )
x 1 8x3 5x 2 7 8x3
Witnesses: C 8, k 1
11
Same order
Big-Theta: f ( x) is ( g ( x))
(Notation abuse: f ( x) ( g ( x)) )
Alternative definition:
f ( x) O( g ( x)) and g ( x) O ( f ( x))
Konstantin Busch - LSU 23
3 x 2 8 x log x ( x 2 )
3x 2 8 x log x 3x 2 8 x 2 11x 2
3 x 2 8 x log x O( x 2 ) Witnesses: C 11, k 1
3 x 2 8 x log x 3 x 2
3 x 2 8 x log x ( x 2 ) Witnesses: C 3, k 1
12
Theorem: If f ( x) an x n an 1 x n 1 a1 x a0
then f ( x) ( x n )
Case 1: an 0 ( x 1)
b max(| an 1 |, | an 2 |,, | a0 |)
f ( x) an x n an 1 x n 1 a1 x a0
an x n nbx n 1
ax n
nb
For 0 a an and x
(an a)
Case 2 is similar
End of Proof
Konstantin Busch - LSU 26
13
Complexity of Algorithms
Time complexity
Number of operations performed
Space complexity
Size of memory used
Linear-Search( x, a1 , a2 ,, an ) {
i 1
while( i n and x a i )
i
if ( i n) return i //item found
else return 0 //item not found
}
14
Time complexity
Comparisons
Binary-Search( x, a1 , a2 ,, an ) {
i 1 //left endpoint of search area
jn //right endpoint of search area
while(i j ) {
m (i j ) / 2
if ( x a m) i m 1 //item is in right half
else j m //item is in left half
}
if (x a i ) return i //item found
else return 0 //item not found
}
Konstantin Busch - LSU 30
15
Search 19
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
12 13 15 16 18 19 20 22
18 19 20 22
18 19
Konstantin Busch - LSU 31
Time complexity
n
Size of search list at iteration 1: 20
n
Size of search list at iteration 2:
21
n
Size of search list at iteration k:
2 k 1
16
n
Size of search list at iteration k:
2 k 1
m 1 log n
Total comparisons:
(1 log n) 2 1 (log n)
Comparisons
per iteration
17
Bubble sort algorithm
Bubble-Sort( a1 , a2 , , an ) {
for ( i 1 to n 1 ) {
for ( j 1 to n i )
if ( a j a j 1)
swap a j , a j 1
}
First iteration
2 2 2 2 5
3 3 3 5 2
4 4 5 3 3
1 5 4 4 4
5 1 1 1 1
Second iteration Last iteration
5 5 5 5 5
2 2 2 4 4
3 3 4 2 3
4 4 3 3 2
1 1 1 1 1
Konstantin Busch - LSU 36
18
Time complexity
Comparisons in iteration 1: n 1
Comparisons in iteration n 1 : 1
(n 1)n
Total: 1 2 (n 1) (n 2 )
2
Konstantin Busch - LSU 37
Tractable problems
Class P:
Problems with algorithms whose
time complexity is polynomial O ( n b )
19
Intractable problems
Class NP :
Solution can be verified in polynomial time
but no polynomial time algorithm is known
P NP ?
Konstantin Busch - LSU 39
Unsolvable problems
20
Integers and Algorithms
Binary expansion
Digits: 0,1
(1 01011111) 2
1 28 0 2 7 1 2 6 0 2 5
1 2 4 1 23 1 2 2 1 21 1 2 0
351
21
Hexadecimal expansion
(2 AE 0 B)16
2 16 4 10 163 14 16 2 0 16 11
175627
Octal expansion
Digits: 0,1,2, ,7
(245)8
2 82 4 8 5
165
22
Conversion between binary and hexadecimal
(1001 0101 1111) 2
( 4 5 3 7)8
Base b expansion( n) {
qn
k 0
While ( q 0 ) {
ak q mod b
q q / b
k k 1
}
return ( ak 1ak 2 a1a0 ) b
}
23
Binary expansion of 241 (11110001 ) 2
241 2 120 1 a0
120 2 60 0 a1
60 2 30 0 a2
30 2 15 0
27
15
7 23
1
1
3 2 1 1
1 20 1 a7
12345 8 1543 1 a0
1543 8 192 7 a1
192 8 24 0 a2
24 83 0 a3
3 80 3 a4
24
Binary_addition(a, b ) {
a ( an 1an 2 a1a0 ) 2
b (bn 1bn 2 b1b0 ) 2
c0 //carry bit
for j 0 to n 1 {
d (a j b j c) / 2 //auxilliary
s j a j b j c 2d //j sum bit
cd //carry bit
}
sn c //last sum bit
return ( sn sn 1 s1s0 ) 2
}
Konstantin Busch - LSU 49
Carry bit: 1 1 1
111 0 a
1 0 11 b
11 0 0 1
25
Binary_multiplication( a, b) {
a ( an 1an 2 a1a0 ) 2
b (bn 1bn 2 b1b0 ) 2
for j 0 to n 1 {
if ( b j 1 )
c j a 2 j //a shifted j positions
else
cj 0
}
p c0 c1 cn 1
return binary expansion of p
}
Konstantin Busch - LSU 51
1 1 0 a
1 0 1 b
1 1 0 c0
0 0 0 c1
1 1 0 c2
1 1 1 1 0
Time complexity of multiplication: O (n)
(counting shifts and bit additions) O(log 2 a)
Konstantin Busch - LSU 52
26
Integers and Division
Integers a, b ( a 0)
Examples: 3 | 12 12 3 4
3| 7
Konstantin Busch - LSU 53
a , b, c integers
if a|b then a | bc
a|b s b a s bc a (sc )
27
a , b, c integers
a|b s b a s
b c a (s t )
a|c t c a t
a , b, c integers
a|b s b a s
c a st
b|c t c b t
28
a, b, c, m, n integers
a|b a | mb
a | mb nc
a|c a | nc
aZ d Z
There are unique q, r Z such that:
a d q r
29
a d q r
q a div d r a mod d
a a
q r a d
d d
Examples: 101 11 9 2
9 101 div 11 2 101 mod 11
11 3( 4) 1
4 11 div 3 1 11 mod 3
Konstantin Busch - LSU 59
a
2d d 0 d 2d 3d a d a
1 d
d
d
a
d
30
Division_algorithm(a, d ) {
q0 r | a |
while ( r d ) {
r rd
q q 1
}
if ( a 0 and r 0 ) { //a is negative
r d r //adjust r
q (q 1) //adjust q
}
else if ( a 0 ) { q q }
return q (a div d ) , r ( a mod d )
}
Konstantin Busch - LSU 61
a 15 q
d 4
r
15 0
15 4 11 1
11 4 7 2
743 3
r 15 mod 4 3 q 15 div 4 3
Time complexity of division alg.: O(q log a )
There is a better algorithm: O (log a log d )
(based on binary search)
Konstantin Busch - LSU 62
31