03 the Fundamentals Algorithms Integers 1
03 the Fundamentals Algorithms Integers 1
The Fundamentals:
Algorithms
The Integers
Objectives
⚫ Algorithms
⚫ The Growth of Functions
⚫ Complexity of Algorithms
⚫ The Integers and Division
⚫ Primes and Greatest Common Divisors
⚫ Integers and Algorithms
3.1- Algorithms (Thuật toán)
Ex: Finding the Maximum Element in a Finite Sequence {8, 4, 11, 3, 10}.
ALGORITHM 2.
The Linear Search Algorithm.
(Xác định vị trí của một phần tử trong một bảng liệt kê theo thứ tự)
Example. List all the steps used to search for 9 in the sequence 2, 3, 4, 5, 6, 8,
9, 11 using a linear search. How many comparisons required to search for 9 in the
sequence.
ALGORITHM 3.
The Binary Search Algorithm.
EX. Use the insertion sort to put the elements of the list 3, 2, 4, 1, 5 in increasing order.
Greedy Algorithm
⚫ 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.
⚫ Selecting the best choice at each step, instead of
considering all sequences of steps that may lead to an
optimal solution.
⚫ 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- The Growth of Functions
(Độ phức tạp của thuật toán)
⚫ Big-O does not provide the lower bound for the size
of f(x)
⚫ Big-Ω, Big- θ were introduced by Donald Knuth in
the 1970s
⚫ Big-Ω provides the lower bound for the size of f(x)
⚫ Big-θ provides the upper bound and lower bound on
the size of f(x)
Big-Omega and Big-Theta Notation
3.3- Complexity of Algorithms
Corollary 2:
m : positive integer, a,b : integers
(a+b) mod m = ((a mod m) + (b mod m)) mod m
ab mod m = ((a mod m)(b mod m)) mod m
Proof: page 205
Applications of Congruences
Hashing Function: H(k) = k mod m
Using in searching data tin memory.
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
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
Greatest Common Divisors and
Least Common Multiples
Definition 4:
The integers a1,a2,a3,…,an are pairwise relatively
prime if gcd(ai,aj)=1 whenever 1 ≤ i<j ≤ n
Example:
7 10 11 17 23 are pairwise relatively prime
7 10 11 16 24 are not pairwise relatively prime
⚫ Representations of Integers
⚫ Algorithms for Integer Operations
⚫ Modular Exponentiation
⚫ Euclid Algorithm
Representations of Integers
Theorem 1:
Let b be a positive integer greater than 1. Then if n is a
positive integer, it can be expressed uniquely in the form
n= akbk + ak-1bk-1+ … + a1b + a0
Where k is a nonnegative integer, a0,a1,a2,…,ak are
nonnegative integers less than b and ak 0
Proof: page 219
Example: (245)8= 2.82 + 4.8+5 = 165
Common Bases Expansions: Binary, Octal, Decimal,
Hexadecimal
Finding expansion of an integer: Pages 219, 220, 221
Algorithm 1: Constructing Base b Expansions