0% found this document useful (0 votes)
133 views55 pages

The Fundamentals: Algorithms The Integers

The document discusses algorithms and their complexity, including algorithms for finding the maximum element, linear search, binary search, and sorting methods like bubble sort and insertion sort. It also covers the fundamentals of integers and modular arithmetic, including division, primes, and greatest common divisors. Key concepts are defined such as algorithms, Big-O notation, and time complexity analysis of algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views55 pages

The Fundamentals: Algorithms The Integers

The document discusses algorithms and their complexity, including algorithms for finding the maximum element, linear search, binary search, and sorting methods like bubble sort and insertion sort. It also covers the fundamentals of integers and modular arithmetic, including division, primes, and greatest common divisors. Key concepts are defined such as algorithms, Big-O notation, and time complexity analysis of algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Chapter 3

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

An algorithm is a finite set of precise instructions for


performing a computation or for solving a
problem.

Specifying an algorithm: natural language/ pseudocode


Finding the Maximum Element in a
Finite Sequence
Q1. find the maximum of the list 1, 6, 9, 8 
Finding the Maximum Element in a
Finite Sequence

Procedure max (a1,a2,a3,…,an: integers)


max:=a1
for i:=2 to n
if max < ai then max:= ai
The Linear Search
Q2. Search for 8 in the sequence 1, 4, 6, 7, 8, 10 
The Linear Search

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
{location is the subscript of the term that equals x, or is 0 if
x is not found}
The Binary Search
Q2. Search for 8 in the increasing sequence 1, 4, 6, 7, 8, 10 
The Binary Search
procedure binary search ( x: integer, a1,a2,…,an : increasing
integers)
i:=1 { i is left endpoint of search interval}
j:=n { j is right endpoint of search interval}
while i<j
begin
m:= (i+j)/2
if x>am then i:=m+1
else j:= m
end
if x=ai then location := i
else location:= 0
{location is the subscript of the term that equals x, or is 0 if x is not
found}
Sorting
 Putting elements into a list in which the elements are in
increasing order.
 There are some sorting algorithms:
– Bubble sort
– Insertion sort
– Selection sort
– Binary insertion sort
– Shaker sort
– Merge sort and quick sort (section 4.4)
– Tournament sort (section 10.2)
Bubble Sort
Q3. Sorting the numbers of 3, 2, 4, 1, 5 in a increasing sequence
Bubble Sort

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}
Insertion Sort
Q3. Sorting the numbers of 7, 2, 4, 1, 5, 3 in a increasing sequence
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 6 7 8 9 12 11
a: 1 2 3 5 6 7 8 9 12 11
ai:= m
end
{a1,a2,…,an are sorted} It is usually not the most efficient
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.
Properties of an algorithm

 Input
 Output
 Definiteness
 Correctness
 Effectiveness
 Generality
3.2. The Growth of Functions
3.2.1-Big-O Notation
Definition:
Let f and g be functions from R to R. 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)


Q1. Determine whether each of these functions is:
a. f(x) = 5.logx is O(x) b. f(x) = (x^3 + 2x)/(2x + 1) is O(x^2)
c. f(x) = x^2 + x^4 is O(x^3) d. f(x) = 2^x is O(x^2)
 

Q2. Give as good a big-O estimate as possible for each of these functions. 
a. (n^2 + 8)(n + 1) b. (n logn + n^2)(n^3 + 2) 
3.3- Complexity of Algorithms

 Computational complexity = Time complexity + space complexity.

 Time complexity can be expressed in terms of the number of


operations used by the algorithm.
– Average-case complexity
– Worst-case complexity

 Space complexity will not be considered.


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
If max < ai then max:= ai 2 2
3 2 2(n-1) +1 = 2n-1
… 2 comparisions
Time Complexity is θ(n) n 2
n+1 1, max< ai is omitted
Understanding the Complexity of
Algorithms
Complexity Terminology Problem class
Θ(1) Constant complexity Tractable (dễ), class P
Θ(log n) Logarithmic complexity Class P
Θ(n) Linear complexity Class P
Θ(n logn) n log n complexity Class P
Θ(nb), b  1, integer Polynominal complexity Intractable, class NP

Θ(bn), b>1 Exponential complexity


Θ(n!) Factorial complexity

NP : Non-deterministic Polynomial time


3.4- The Integers and Division
Definition: If a and b are integers with a  0, we say that a divides
b if there is an integer c such that b=ac.

When a divides b, we say that:


a is a factor of b b is a multiple of a
Notation: a|b : a divides b ałb : a does not divide b

Example:
3ł7 because 7/3 is not an integer
3|12 because 12/3=4, remainder=0

Corollary 1:
a|b ^ a|c → a|(mb+nc), m, n are integers
The Division Algorithm
Theorem 2: Division Algorithm: Let a be an integer and d a positive
integer. Then there are unique integers q and r, with 0 ≤ r<d, such
that a=dq+r
d: divisor , r: remainder, q: quotient
Note: r can not be negative

Definition: a=dq+r
a: dividend d: divisor
q: quotient r : remainder,
q= a div d r = a mod d

Example:
101 is divided by 11: 101 = 11.9 + 2  q=9, r=2
-11 is divided by 3: 3(-4)+ 1  q=-4, r=1

No OK : -11 is divided by 3 : 3(-3)-2  q=-3, r = -2


Q1. Does 17 divide each of these numbers? 
a. 68 b. 84 c. 357 d. 1001

Q2. What are the quotient and remainder when


a. 19 is divided by 7 b. −111 is divided by 11 c. 789 is divided by 23
Modular Arithmetic

Definition: a, b: integers, m: positive integer.


a is called congruent to b modulo m if m|a-b

Notations:
a ≡ b (mod m), a is congruent to b modulo m
a  b (mod m), a is not congruent to b mod m

Examples:
15 is congruent to 6 modulo 3 because 3|15-6
15 is not congruent to 7 modulo 3 because 3 ł15-7
Q3. Suppose that a and b are integers, a ≡ 4 (mod 13), and b ≡ 9 (mod 13). Find the integer c
with 0 ≤ c ≤ 12 such that 
a. c ≡ 9a (mod 13) b. c ≡ 11+2b (mod 13). c. c ≡ a + 2b (mod 13). 
Modular Arithmetic

Theorem 3
a,b: integers, m: positive integer
a ≡ b (mod m) ↔ a mod m = b mod m

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…

Theorem 5
m: positive integer
a ≡ b (mod m) ^ c ≡ d (mod m)
a+c ≡ b+d (mod m) ^ ac ≡ bd (mod m)

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
Q4. Evaluate these quantities. 
a. 13 mod 3 b. −97 mod 11 c. 155 mod 19 d. −221 mod 23

 
Q5. Find a div m and a mod m when 
a. a = −111, m = 19. b. a = −999, m = 101. 

Q6. Decide whether each of these integers is congruent to 5 modulo 17. 


a. 80 b. 103 c. −29 d. −122 
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
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

Q7. Suppose pseudo-random numbers are produced by using: xn+1 = (5xn + 11) mod 13.
If x3= 1, find x4 and x2. 
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

Sender: (encoding)
Message: “ABC” , k=3
Using f(p) = (p+3) mod 26 // 26 characters
ABC  0 1 2  3 4 5  DEF

Receiver: (decoding)
DEF  3 4 5
Using f-1(p) = (p-3) mod 26
3 4 5  0 1 2  ABC
3.5- Primes and Greatest Common
Divisors
Definition 1:
A positive integer p greater than 1 is called prime if the only
positive factors are 1 and p
A positive integer that is greater than 1 and is not prime is
called composite

Examples:
Primes: 2 3 5 7 11
Composites: 4 6 8 9

Finding very large primes: tests for supercomputers


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
Primes…

Theorem 2: If n is a composite, then n has a


prime divisor less than or equal to √n

Theorem 3: There are infinite many primes


Greatest Common Divisors and
Least Common Multiples
Definition 2:
Let a, b be integers, not both zero. The largest integer
d such that d|a and d|b is called the greatest common
divisor of a and b.

Notation: gcd(a,b)

Example: gcd(24,36)=?
Greatest Common Divisors and
Least Common Multiples
Definition 3:
The integers a, b are relatively prime if their greatest
common divisor is 1.

Which positive integers less than 12 are relatively prime to 12? 

Which positive integers less than 30 are relatively prime to 30? 


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
Greatest Common Divisors and
Least Common Multiples
Definition 5:
The least common multiple of the positive integer a and b
is the smallest integer that is divisible by both a and b
Notation: lcm(a,b)

Example:
lcm(12,36) lcm(7,11)
Greatest Common Divisors and
Least Common Multiples

Theorem 5:
Let a, b be positive integers then
ab= gcd(a,b). lcm(a,b)

Example: gcd(8,12) = 4 lcm(8,12)=24  8.12 = 4.24


3.6- Integers and Algorithms

 Representations of Integers
 Algorithms for Integer Operations
 Modular Exponentiation
 Euclid Algorithm
Representations of Integers
Representations of Integers
Algorithm 2: Adding of Integers

Complexity: O (n)
Algorithm 3: Multiplying Integers

Complexity: O (n2)
Algorithm 4: Computing div and mod
Algorithm 4: Computing div and mod
procedure division ( a: integer; d: positive integer)
q:=0
r:= |a|
while r ≥ d {quotient= number of times of successive subtractions}
begin
r:= r-d
q := q+1
end
If a<0 and r>0 then {updating remainder when a<0}
begin
r:= d-r
q := -(q+1)
end
{ q = a div d is the quotient, r=a mod d is the remainder}
Algorithm 5: Modular Exponentiation
Algorithm 5: Modular Exponentiation

{ bn mod m = ? . Ex: 3644 mod 645 = 36 }


procedure mod_ex ( b: integer, n=(ak-1ak-2…a1a0)2, m: positive integer)
x:=1
power := b mod m
for i:=0 to k-1
begin
if ai=1 then x:= (x.power) mod m
power := (power.power) mod m
end
{ x equals bn mod m }

Corollary 2: ab mod m = ((a mod m)(b mod m)) mod m


bn mod m = result of successive factori mod m
The Euclidean Algorithm
The Euclidean Algorithm
Lemma: Proof: page 228
Let a= bq+r, where a, b, q, r are integers. Then gcd(a,b) = gcd(b,r)
Example: 287 = 91.3 + 14 gcd(287,91) =gcd(91,14)= 7

procedure gcd(a,b: positive integer)


x:=a
y:=b
while y  0
begin
r := x mod y
x:=y
y:= r
end {gcd(a,b) is x}
Summary
 Algorithms
 The Growth of Functions
 Complexity of Algorithms
 The Integers and Division
 Primes and Greatest Common Divisors
 Integers and Algorithms
Thanks

You might also like