Chapter3 Algorithm
Chapter3 Algorithm
•Algorithms
2
Algorithms
•What is an algorithm?
3
Algorithms
• Properties of algorithms:
4
Algorithm Examples
•We will use a pseudo code to specify algorithms, which slightly reminds us of
Basic and Pascal.
5
Algorithm Examples
•Another example: a linear search algorithm, that is, an algorithm that linearly
searches a sequence for a particular element.
6
Algorithm Examples
•If the terms in a sequence are ordered, a binary search algorithm is more
efficient than linear search.
•The binary search algorithm iteratively restricts the relevant search interval
until it closes in on the position of the element to be located.
7
Algorithm Examples
search interval
a c d f g h j l m o p r s u v x z
center element
8
Algorithm Examples
search interval
a c d f g h j l m o p r s u v x z
center element
9
Algorithm Examples
search interval
a c d f g h j l m o p r s u v x z
center element
10
Algorithm Examples
search interval
a c d f g h j l m o p r s u v x z
center element
11
Algorithm Examples
search interval
a c d f g h j l m o p r s u v x z
center element
found !
12
Algorithm Examples
procedure binary_search(x: integer; a1, a2, …, an: integers)
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 zero if x is not found}
13
Complexity
•In general, we are not so much interested in the time and space
complexity for small inputs.
14
Complexity
•For example, let us assume two algorithms A and B that solve the
same class of problems.
15
Complexity
•This means that algorithm B cannot be used for large inputs, while
algorithm A is still feasible.
•The growth of time and space complexity with increasing input size
n is a suitable measure for the comparison of algorithms.
16
Complexity
• Comparison: time complexity of algorithms A and B
17
The Growth of Functions
The growth of functions is usually described using the big-O notation.
|f(x)| C|g(x)|
whenever x > k.
18
The Growth of Functions
When we analyze the growth of complexity functions, f(x) and g(x) are
always positive.
If we want to show that f(x) is O(g(x)), we only need to find one pair
(C, k) (which is never unique).
19
The Growth of Functions
The idea behind the big-O notation is to establish an upper boundary
for the growth of a function f(x) for large x.
20
The Growth of Functions
•Example:
Show that f(x) = x2 + 2x + 1 is O(x2).
x2 + 2x + 1 x2 + 2x2 + x2
x2 + 2x + 1 4x2
f(x) is O(x2).
21
The Growth of Functions
Yes. x3 grows faster than x2, so x3 grows also faster than f(x).
Therefore, we always have to find the smallest simple function g(x) for which
f(x) is O(g(x)).
22
The Growth of Functions
“Popular” functions g(n) are
n log n, 1, 2n, n2, n!, n, n3, log n
• 1
• log n
• n
• n log n
• n2
• n3
• n4
.
.
• 2n
• n!
23
The Growth of Functions
24
Useful Rules for Big-O
•For any polynomial f(x) = anxn + an-1xn-1 + … + a0, where a0, a1, …, an are
n n-1 0 0 1 n
real numbers,
f(x) is O(xn).
25
Complexity Examples
•What does the following algorithm compute?
procedure who_knows(a1, a2, …, an: integers)
m := 0
for i := 1 to n-1
for j := i + 1 to n
if |ai – aj| > m then m := |ai – aj|
{m is the maximum difference between any two numbers in the input sequence}
Comparisons: n-1 + n-2 + n-3 + … + 1
= (n – 1)n/2 = 0.5n2 – 0.5n
26
Complexity Examples
•Another algorithm solving the same problem:
27
Let us get into…
•Number Theory
28
Introduction to Number Theory
• divisibility,
• greatest common divisors,
• least common multiples, and
• modular arithmetic
29
Division
•If a and b are integers with a 0, we say that
a divides b if there is an integer c so that b = ac.
•The notation
a |a |b b means that a divides b.
30
Divisibility Theorems
•For integers a, b, and c it is true that
• if a | b and a | c, then a | (b + c)
• Example: 3 | 6 and 3 | 9, so 3 | 15.
• if a | b and b | c, then a | c
• Example: 4 | 8 and 8 | 24, so 4 | 24.
31
Primes
•A positive integer p greater than 1 is called prime if the only positive factors of
p are 1 and p.
•Note: 1 is not a prime
•A positive integer that is greater than 1 and is not prime is called composite.
•Every positive integer can be written uniquely as the product of primes, where
the prime factors are written in order of increasing size.
32
Primes
•Examples:
15 = 35
48 = 22223 = 2 3
4
17 = 17
100 = 2
2255 = 2 5
2
512 = 222222222 = 2
9
515 = 5103
28 = 227
33
Primes
•If n is a composite integer, then n has a prime divisor less than or equal √n .
•This is easy to see: if n is a composite integer, it must have at least two prime
divisors. Let the largest two be p1 and p2. Then p1p2 <= n.
•p1 and p2 cannot both be greater than √n , because then p1p2 > n.
34
The 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.
35
The Division Algorithm
•Example:
• 17 is the dividend,
• 5 is the divisor,
• 3 is called the quotient, and
• 2 is called the remainder.
36
The Division Algorithm
•Another example:
37
Greatest Common Divisors
•Let a and 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.
38
Greatest Common Divisors
•Using prime factorizations:
•Example:
a = 60 = 22 31 51
b = 54 = 21 33 50
gcd(a, b) = 21 31 50 = 6
39
Relatively Prime Integers
•Definition:
•Examples:
40
Relatively Prime Integers
•Definition:
•The integers a1, a2, …, an are pairwise relatively prime if gcd(ai, aj) = 1
whenever 1 i < j n.
•Examples:
41
Least Common Multiples
•Definition:
•The least common multiple of the positive integers a and b is the smallest
positive integer that is divisible by both a and b.
•Examples:
lcm(3, 7) = 21
lcm(4, 6) = 12
lcm(5, 10) = 10
42
Least Common Multiples
•Using prime factorizations:
•Example:
a = 60 = 22 31 51
b = 54 = 21 33 50
lcm(a, b) = 22 33 51 = 4275 = 540
43
GCD and LCM
a = 60 = 22 31 51
b = 54 = 21 33 50
44
Modular Arithmetic
•Let a be an integer and m be a positive integer.
We denote by a mod m the remainder when a is divided by m.
•Examples:
9 mod 4 = 1
9 mod 3 = 0
9 mod 10 = 9
-13 mod 4 = 3
45
Congruences
•Let a and b be integers and m be a positive integer. We say that a is
congruent to b modulo m if
m divides a – b.
46
Congruences
•Examples:
•Is it true that 46 68 (mod 11) ?
•Yes, because 11 | (46 – 68).
•Is it true that 46 68 (mod 22)?
•Yes, because 22 | (46 – 68).
•For which integers z is it true that z 12 (mod 10)?
•It is true for any z{…,-28, -18, -8, 2, 12, 22, 32, …}
•Theorem: Let m be a positive integer. The integers a and b are
congruent modulo m if and only if there is an integer k such that
a = b + km.
47
Congruences
•Theorem: Let m be a positive integer.
If a b (mod m) and c d (mod m), then
a + c b + d (mod m) and ac bd (mod m).
•Proof:
•We know that a b (mod m) and c d (mod m) implies that there are
integers s and t with
b = a + sm and d = c + tm.
•Therefore,
•b + d = (a + sm) + (c + tm) = (a + c) + m(s + t) and
•bd = (a + sm)(c + tm) = ac + m(at + cs + stm).
•Hence, a + c b + d (mod m) and ac bd (mod m).
48
Congruences
•Theorem: Let m be a positive integer. a b (mod m) iff a mod m = b mod
m.
•Proof:
•Let a = mq1 + r1, and b = mq2 + r2.
Only if part: a mod m = b mod m r1 = r2, therefore
a – b = m(q1 – q2), and a b (mod m).
If part: a b (mod m) implies
a – b = mq
mq1 + r1 – (mq2 + r2) = mq
r1 – r2 = m(q – q1 + q2).
Since 0 r1, r2 m, 0 |r1 - r2| m. The only multiple in that range is 0.
Therefore r1 = r2, and a mod m = b mod m.
49
The Euclidean Algorithm
•The Euclidean Algorithm finds the greatest common divisor of two
integers a and b.
Therefore, any divisor (including their gcd) of 287 and 91 must also be
a divisor of 287 - 913 = 14.
50
The Euclidean Algorithm
•In the next step, we divide 91 by 14:
91 = 146 + 7
So we divide 14 by 7:
14 = 72 + 0
51
The Euclidean Algorithm
•In pseudocode, the algorithm can be implemented as follows:
procedure gcd(a, b: positive integers)
x := a
y := b
while y 0
begin
r := x mod y
x := y
y := r
end {x is gcd(a, b)}
52
Representations of Integers
•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 = a bk
k
+ ak-1bk-1 + … + a1b1 + a0b0,
•where k is a nonnegative integer,
•a0, a1, …, ak are nonnegative integers less than b,
•and ak 0.
53
Representations of Integers
54
Representations of Integers
•How can we construct the base b expansion of an integer n?
•First, divide n by b to obtain a quotient q0 and remainder a0, that is,
•n = bq0 + a0, where 0 a0 < b.
•The remainder a0 is the rightmost digit in the base b expansion of n.
•Next, divide q0 by b to obtain:
•q0 = bq1 + a1, where 0 a1 < b.
•a1 is the second digit from the right in the base b expansion of n.
Continue this process until you obtain a quotient equal to zero.
55
Representations of Integers
•Example:
What is the base 8 expansion of (12345) ?
10
1543 = 8192 + 7
192 = 824 + 0
24 = 83 + 0
3 = 8 0 + 3
56
Representations of Integers
procedure base_b_expansion(n, b: positive integers)
q := n
k := 0
while q 0
begin
ak := q mod b
q := q/b
k := k + 1
end
{the base b expansion of n is (ak-1 … a1a0) }
b
57
Addition of Integers
•How do we (humans) add two integers?
1 11 carry
•Example: 7583
+ 4932
1 2 51 5
1 1 carry
Binary expansions: (1011)2
+ (1010)2
(101 01 )2
58
Addition of Integers
Let a = (a a …a a ) , b = (b b …b b ) .
n-1 n-2 1 0 2 n-1 n-2 1 0 2
•where s1 is the next bit in the binary expansion of a + b, and c1 is the carry.
59
Addition of Integers (cont.)
60
Addition of Integers
•Example:
Add a = (1110) and b = (1011) .
2 2
a + b + c = 1 + 1 + 0 = 12 + 0, so c = 1 and s = 0.
1 1 0 1 1
a + b + c = 1 + 0 + 1 = 12 + 0, so c = 1 and s = 0.
2 2 1 2 2
a + b + c = 1 + 1 + 1 = 12 + 1, so c = 1 and s = 1.
3 3 2 3 3
•s4 = c3 = 1.
•Therefore, s = a + b = (11001)2.
61
Addition of Integers
procedure add(a, b: positive integers)
c := 0
for j := 0 to n-1
begin
d := (a + b + c)/2
j j
s := a + b + c – 2d
j j j
c := d
end
s := c
n
{the binary expansion of the sum is (s s …s s ) }
n n-1 1 0 2
62