Chapter 3-The Fundamentals - Algorithms - Integers
Chapter 3-The Fundamentals - Algorithms - Integers
Chapter 3
The Fundamentals:
Algorithms and the Integers
Objectives
l Algorithms
l The Growth of Functions
l Complexity of Algorithms
l The Integers and Division
l Primes and Greatest Common Divisors
l Integers and Algorithms
3.1- Algorithms
An algorithm is a finite set of precise instructions
for performing a computation or for solving a
problem.
Specifying an algorithm: natural language/ pseudocode
Properties of an algorithm
l Input: An alg. has input values from a specified set
l Output: From each set of IV an alg. produces OV from a
specified set. The OV are the solution to the problem
l Definiteness: The steps of an alg. must be defined precisely
l Correctness: produce the correct OV for each set of IV
l Finiteness: produce the desired OV after a finite number of
steps for any IV in the set.
l Effectiveness: perform each step of an alg. exactly and in a
finite amount of time.
l Generality: be applicable for all problems of the desired form,
not just for a particular set of input values.
Sorting
l Putting elements into a list in which the elements are in
increasing order.
l There are some sorting algorithms
l Bubble sort
l Insertion sort
l Selection sort (exercise p. 178)
l Binary insertion sort (exercise p. 179)
l Shaker sort (exercise p.259)
l Merge sort and quick sort (section 4.4)
l Tournament sort (10.2)
Bubble Sort
Insertion Sort
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 a: 1 2 3 6 7 8 5 9 12 11
while aj > ai i:= i+1 j= 7
m:=aj { save aj } i=4
{ moving j-i elements backward } m=5
for k:=0 to j-i-1 aj-k:=aj-k-1 a: 1 2 3 6 7 8 5 9 12 11
{move aj to the position i} a: 1 2 3 6 7 5 8 9 12 11
ai:= m a: 1 2 3 5 6 7 8 9 12 11
end
{a1,a2,…,an are sorted} It is usually not the most efficient
Greedy Algorithm
l They are usually used to solve optimization problems: Finding out a
solution to the given problem that either minimizes or maximizes the
value of some parameter.
l Selecting the best choice at each step, instead of considering all
sequences of steps that may lead to an optimal solution.
l Some problems:
– Finding a route between two cities with smallest total mileage
(number of miles that a person passed).
– Determining a way to encode messages using the fewest bits
possible.
– Finding a set of fiber links between network nodes using the least
amount of fiber.
3.2.1-Big-O Notation
Definition:
Let f and g be functions from the set of
integers or the set of real numbers to the
set of real numbers. We say that f(x) is
O(g(x)) if there are constants C and k such
that |f(x)| ≤ C|g(x)| whenever x>k
Example: Show that f(x)=x2 + 2x +1 is O(x2)
l Examine with x>1 è x2 >x
l è f(x)=x2 + 2x +1 < x2 + 2x2 + x2
l è f(x) < 4x2
l è Let g(x)= x2
l è C=4, k=1, |f(x)| ≤ C|g(x)|
l èf(x) is O(x2)
Big-O: Theorem 1
Let f(x)=anxn + an-1xn-1+…+a1x+a0, where
a0,a1,…,an are real number, then f(x) is O(xn)
If x>1
|f(x)| =| anxn + an-1xn-1+…+a1x+a0|
≤ | anxn |+|an-1xn-1|+…+|a1x|+|a0| { triangle inequality }
≤ xn (| an |+|an-1xn-1/xn|+…+|a1x/xn|+|a0/xn|)
≤ xn (| an |+|an-1/x|+…+|a1/xn-1|+|a0/xn|)
≤ xn (| an |+|an-1|+…+|a1|+|a0|)
Let C= | an |+|an-1|+…+|a1|+|a0|
|f(x)| ≤ Cxn
è f(x) = O (xn)
The Growth of
Combinations
of Functions
Big-O : Theorems
Theorem 2:
f1(x)=O(g1(x)) ^ f2(x)=O(g2(x))
→ (f1+f2)(x) = O(max(|g1(x)|,|g2(x)|))
Theorem 3:
f1(x)=O(g1(x)) ^ f2(x)=O(g2(x))
→ (f1f2)(x) = O(g1g2(x)))
Corollary 1:
f1(x)=O(g (x)) ^ f2(x)=O(g (x)) → (f1+f2)(x) = O(g(x))
Demo 1
Describe the time complexity of the algorithm for finding the
largest element in a set:
Procedure max ( a1,a2,…,an : integers)
max:= a1 i Number of
comparisons
for i:=2 to n
2 2
If max < ai then max:= ai 3 2 2(n-1) +1 = 2n-1
… 2 comparisions
Time Complexity is θ(n) n 2
n+1 1, max< ai is omitted
Demo 2
Describe the average-case time complexity of the linear-search
algorithm :
Procedure linear search (x: integer, a1,a2,…,an :distinct integers)
i:=1
i Number of comparisons done
while (i ≤ n and x ¹ ai) i:=i+1
if i ≤ n then location:= i 1 2
else location:=0 2 4
…
Avg-Complexity= [(3+5+7+…+(2n+1) ]/n n 2n
= [2(1+2+3+…+n) +n]/n n+1 1, x ¹ ai is omitted
= [2n(n+1)/2]/n + 1
=[n(n+1)]/n + 1
See demonstrations about the worst-
= n+1 + 1 = n+2
case complexity: Examples 5,6 pages
= θ(n) 195, 196
Modular Arithmetic
l
Modular Arithmetic
Theorem 3
a,b: integers, m: positive integer
a ≡ b (mod m) ↔ (a mod m) = (b mod m)
Modular Arithmetic…
Theorem 4
a,b: integers, m: positive integer
a and b are congruent modulo m if and only if there is
an integer k such that a = b + km
Modular Arithmetic…
l
Applications of Congruences
Hashing Function: H(k) = k mod m
Using in assigning memory locations to computer files.
k: data searched, m : memory block
Examples:
H(064212848) mod 111= 14
H(037149212) mod 111= 65
Collision: H(k1) = H(k2). For example, H(107405723) = 14
Applications of Congruences
Pseudo-random Numbers xn+1=(axn+c) mod m
a: multiplier, c: increment, x0: seed
with 2 ≤ a<m, 0 ≤ c<m, 0 ≤ x0<m
Examples:
m=9 è random numbers: 0..8
a= 7, c=4, x0=3
Result: Page 207
Applications of Congruences
Cryptography: letter 1 → letter 2
Examples: shift cipher with k f(p) =(p+k) mod 26
à f-1(p)=(p-k) mod 26
Applications of Congruences
What is the secret message produced from the message
“MEET YOU IN THE PARK” using
the Caesar cipher (k=3)?
MEET Y O U I N T HE P A R K
12 4 4 19 24 14 20 8 13 19 7 4 15 0 17 10
Sender: (encoding)
Using f(p) = (p+3) mod 26 // 26 characters
15 7 7 22 1 17 23 11 16 22 10 7 18 3 20 13.
PHHW BR X L Q W K H SD U N
Receiver: (decoding) Using f-1(p) = (p-3) mod 26
15 7 7 22 1 17 23 11 16 22 10 7 18 3 20 13
12 4 4 19 24 14 20 8 13 19 7 4 15 0 17 10
GV. Nguyễn Quốc Thanh Discrete Mathematics
ĐẠI HỌC FPT CẦN THƠ
Primes…
Theorem 1- The fundamental theorem of
arithmetic:
Every positive integer greater than 1 can be written
uniquely as a prime or as the product of two or more
primes where the prime factors are written in order of
nondecreasing size
Examples:
Primes: 37
Composite: 100 = 2.2.5.5 = 2252
999 = 3.3.3.37 = 3337
Primes…
Converting a number to prime factors
Examples: 7007
Try it to 2,3,5 : 7007 can not divided by 2,3,5
7007 : 7
1001 : 7
143: 11
13: 13
0
è 7007 = 72.11.13
Primes…
Theorem 2: If n is a composite, then n has a
prime divisor less than or equal to √ ̅n
Proof:
n is a composite è n = ab in which a or b is a prime
If (a> √ ̅n ^ b> √ ̅n) à ab>n è false
è Prime divisor of n less than or equal to √ ̅n
Primes…
Theorem 3: There are infinite many primes
Proof: page 212
Theorem 4: The prime number theorem:
The ratio of the number of primes not exceeding x and
x/ln x approaches 1 and grows without bound (i.e. the
number of primes not exceeding x can be
approximated by x/ln x).
Example:
x= 101000 à The odds that an integer near 101000 is prime are
approximately 1/ln 101000 ~ 1/2300
Example:
gcd(3,7)=1 è 3,7 are relatively prime
gcd (17,22)=1 è 17,22 are relatively prime
gcd(17,34) = 17 è 17, 34 are not relatively prime
Representations of Integers
l
Summary
l Algorithms
l The Growth of Functions
l Complexity of Algorithms
l The Integers and Division
l Primes and Greatest Common Divisors
l Integers and Algorithms
Thanks