�Discrete Mathematics (MAD101)
Chapter 3:
The Integers & Algorithms
1.1 Algorithm
1.1 Algorithm
(Pseudocode)
Procedure max (a1,a2,a3,…,an: integers):
max:=a1
for i:=2 to n
if max < ai then max:= ai
return max {max is the largest element}
Example: How many comparisons needed? How many assignments needed in worst case?
a) 5, 2,1, 3, 0
b) 1, 2, 3, 5
1.1 Algorithm
Linear Search (tìm tuyến tính)
Procedure linear search (x: integer, a1,a2,…,an :distinct integers)
i:=1
while i ≤ n and x ≠ ai
i:=i+1
if i ≤ n then location:= i
else location:=0
return location {location is the subscript of the term that equals x, or is 0 if x is not found}
Example: x= 5
a= 7,8,2,5,6
1.1 Algorithm
Binary Search (tìm nhị phân)
procedure binary search ( x:integer, a1,a2,…,an : increasing integers)
i:=1 { i is left endpoint of search interval} input: x=5
j:=n { j is right endpoint of search interval} a=1,4,6,7,8,9
while i<j ouput: ?
m:= ⎣(i+j)/2⎦
if x>am then i:=m+1
else j:= m
if x=ai then location := i
else location:=0
return location {location is the subscript of the term that equals x, or is 0 if x is not found}
1.1 Algorithm
Bubble Sort (sắp xếp nổi bọt)
procedure bubble sort (a1,a2,…,an :real numbers with n ≥ 2)
for i:= 1 to n-1
for j:=1 to n- i
if aj > aj+1 then interchange aj and aj+1
{a1,a2,…,an are sorted}
Example: 3, 2, 4, 1, 5
1.1 Algorithm
Bubble Sort (sắp xếp nổi bọt)
1.1 Algorithm
Insertion Sort (sắp xếp chèn)
procedure insertion sort (a1,a2,…,an :real numbers with n ≥ 2)
for j:= 2 to n { j: position of the examined element }
begin
{ finding out the right position of aj }
i:=1
while i<j and aj > ai
i:= i+1
m:=aj { save aj }
{ moving j-i elements backward }
for k:=0 to j-i-1 aj-k:=aj-k-1
{move aj to the position i}
ai:= m
end
{a1,a2,…,an are sorted}
a: 1 2 3 5 7 4 6 (n= 7)
j=6
i=3
m=4
a: 1 2 3 4 6
a: 1 2 3 5 6
a: 1 2 3 4 5 7 6
5 7
5 7
1.1 Algorithm
Example:
1.1 Algorithm
Greedy Change-Making Algorithm. (thuật đổi tiền)
Procedure change(c₁,c₂,...,c_r: values of denominations (mệnh giá) of coins, where c₁ > c₂ > ··· > c_r ; n: a positive integer (số tiền cần đổi))
for j := 1 to r
dⱼ := 0 // dⱼ counts the coins of denomination cⱼ used
while n ≥ cⱼ
dⱼ := dⱼ + 1 // add a coin of denomination cⱼ
n := n−cⱼ
// d_i is the number of coins of denomination c_i in the change for i = 1,2,...,r
Example: a) n=135, c= 20, 10, 5
b) n=135, c= 20,10,1
c) n= 135, c=20, 10, 7
1.1 Algorithm
Q1: Consider the algorithm “Finding Max”. How many comparisons are done when the output is a= 1, 2, 6, 3, 7, 5 ?
Q2: Given the sequence: 5 7 1 3 2 4. What is the result obtained when the second loop of the Bubble Sort is executed ?
Q3. Consider the algorithm “Linear Search”. How many comparisons are done in the worst case for an input string of length n ?
1.2 The Growth of Functions
Big- O Notion
| f(x) | ≤ C| g(x) | , for all x > k.
Notation: f(x)= O(g(x))
Remark: f(x) is O(g(x)) if and only if the quotient f(x)/g(x) is bounded for x > k !
| f(x)/g(x) | = | x /x² | = | 1/x | < 1, for all x > 1.
So f(x)=O(g(x)) (here C= 1 and k= 1 )
1.2 The Growth of Functions
Big- O Notion
b) g(x) is not O(g(x)). Indeed, consider the quotient
| g(x)/f(x) | = | x²/x | = | x | , which is not bounded when x is increasing to infinity !
| f(x)/g(x) | = | (x² + 2x +1) /x² | = | 1+ 2/x + 1/ x² |
We see that | 1+ 2/x + 1/ x² | ≤ 1 + 2+ 1 = 4 , for all x >1 . So
| f(x)/g(x) | < 4 ⇔ | (x² + 2x +1) | < 4x², for all x > 1,
which means that x² + 2x +1 is O(x²).
(see the picture below)
1.2 The Growth of Functions
Big- O Notion
1.2 The Growth of Functions
Big- O Notation
which implies that log(n)/n is bounded when n is large, so log(n)=O(n) !
1.2 The Growth of Functions
Big- O Notation
The Big-O notation classifies the order of functions by its growth (mức độ tăng trưởng) :
1 log(n) n nlog(n) n² aⁿ n!
In this list, each function is “smaller” than the succeeding function (when n is large)
(you can easily verify by computing the limit of each quotient )
when n > 1, we can estimate: n⁴ +n < n⁴ + n⁴ = 2n⁴
⇒ log(n⁴ +n) < log(2n⁴) = log(2) + log(n⁴) = 4log(n)+log(2)
Since 4log(n)+log(2) is O(log(n)), we can conclude that log(n⁴ +n) is also O(log(n)!
1.2 The Growth of Functions
Big- O Notation
1.2 The Growth of Functions
Big- O Notation
is a polynomial of degree n (aₙ ≠ 0). Then log(f(x)) is O(log(x))
n, , 2ⁿ, log(n¹º), nlog(n), n²
1.2 The Growth of Functions
Big- 𝛀 and Big- 𝛉 Notation
|f(x)| ≥ C|g(x)|, for all x > k
C₁|g(x)| ≤ |f(x)| ≤ C₂|g(x)|, for all x > k
x² is not 𝛳(x), since x² is not O(x) !
1.2 The Growth of Functions
Big- 𝛀 and Big- 𝛉 Notation
b) log(n⁴ +n) is 𝛳(log(n)). Indeed, we have seen that log(n⁴ +n) is O(log(n)). To prove that log(n⁴ +n) is 𝛺(log(n)), we evaluate as follows :
n⁴ +n > n⁴ , for all n >0, so log(n⁴ +n) > log(n⁴)=4log(n)
which implies that log(n⁴ +n) is 𝛺(log(n)), hence log(n⁴ +n) is 𝛳(log(n)) !
For example, x is O(x²), so x² is 𝛺(x) !
1.2 The Growth of Functions
1.2 The Growth of Functions
1.2 The Growth of Functions
1.2 The Growth of Functions
Find complexity (big O) of this algorithm: