You are on page 1of 73

Chapter 3 (Part 1):

The Fundamentals: Algorithms, the


Integers & Matrices

• Algorithms (Section 3.1)

• The Growth of Functions


(Section 3.2)

• Complexity
© of Algorithms
by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2011
© by Kenneth H. Rosen, Discrete Mathematics & its
Algorithms (3.1)
• Some Applications:

– Use of number theory to make message


secret

– Generate pseudorandom numbers

– Assign memory locations to computer files

– Internet security
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
2 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)

• Introduction

– Given a sequence of integers, find the


largest one

– Given a set, list all of his subsets

– Given a set of integers, put them in


increasing order

– Given a network, find the shortest path


© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504, 3
Algorithms (3.1) (cont.)

– Methodology:

• Construct a model that translates the problem into a


mathematical context

• Build a method that will solve the general problem


using the model

Ideally, we need a procedure that follows a sequence


of steps that leads to the desired answer. Such a
sequence is called an algorithm.

History: the term algorithm is a corruption of the name


Al-Khowarizmi (mathematician of the 9th century)
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
4 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)

– Definition:

An algorithm is a finite set of precise instructions


for performing a computation or for solving a
problem.

– Example: Describe an algorithm for finding the


largest value in a finite sequence of integers

Solution: We perform the following steps:

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


5 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)

1. Set the temporary maximum equal to the


first integer in the sequence
2. Compare the next integer in the sequence
to the temporary maximum, and if it is larger
that the temporary maximum, set the
temporary maximum equal to this integer
3. Repeat the previous step if there are more
integers in the sequence
4. Stop when there are no integers left in the
sequence. The temporary maximum at this
point is the largest integer in the sequence

Pseudocode:
CSE 504,© by Kenneth H. Rosen,intermediate step between
6 Seventh
Discrete Mathematics & its Applications, an 2011
Edition, Mc Graw-Hill,
Algorithms (3.1) (cont.)

Algorithm: Finding the maximum element in a


finite sequence

Procedure max(a1, a2, …, an: integer)


max := a1
For i := 2 to n
If max < ai then max := ai
{max is the largest element}

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


7 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)
– Properties of an algorithm:

• Input: an algorithm has input values from a specified


set
• Output: from each set of input values an algorithm
produces output values from a specified set. The output
values are the solution to the problem
• Definiteness: the steps of an algorithm must be defined
precisely
• Correctness: an algorithm should produce the correct
output values for each set of input values
• Finiteness: an algorithm should produce the desired
output after a finite (but perhaps large) number of steps
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504,
for input in the set 8
Algorithms (3.1) (cont.)

• Searching Algorithms

– Problem: “Locate an element x in a list of


distinct elements
a1, a2, …, an, or determine that it is not in the
list.”

We should provide as a solution to this


search problem the location of the term in the
list that equals x.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


9 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)

– The linear search

Algorithm: The linear search algorithm

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
CSE 504,© by is
Kenneth the subscript
H. Rosen, Discrete of 10the
Mathematics & its Applications, term
Seventh Edition, that
Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)

– The binary search

• Constraint: can be used when the list has terms


occurring in order of increasing size (words listed in
lexicographic order)

• Methodology: Compare the element to be located to


the middle term of the list

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


11 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)
• Example: Search 19 in the list
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
– split the list into 2 subsets with 8 terms each
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
– Compare 19 with the largest element of the first set
10< 19  search 19 in the second set
– Split the second subset into 2 smaller subsets
12 13 15 16 18 19 20 22
– Compare 19 with 16
16 < 19  search 19 in the second set
– Split the second subset as: 18 19 20 22
– Compare 19 > 19 is false  search 19 in 18 19
– Split the subset as : 18 19
– Since 18 < 19  search restricted to the second list
– Finally 19 is located at the 14th element of the original list
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
12 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)
Algorithm: the binary search algorithm
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 equal to x, or 0
if x is not found}
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
13 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)

• Sorting

– Goal:

“Order the elements of a list”. For example,


sorting the list
7, 2, 1, 4, 5, 9 produces the list 1, 2, 4, , 5, 7,
9. Similarly, sorting the list d, h, c, a, f
produces a, c, d, f, h.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


14 Seventh Edition, Mc Graw-Hill, 2011
– The Bubble sort
• Example: Sort the list 3, 2, 4, 1, 5 into increasing order
using the Bubble sort

Steps of the Bubble sort

3 2 2 2 2 2 2 2 1
2 3 3 3 3 3 1 1 2
4 4 4 1 1 1 3 3 3
1 1 1 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
1st pass 2nd pass 3rd pass 4th pass

= ordered = permute
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
15 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)

Algorithm: the Bubble sort

Procedure Bubblesort (a1, …, an)


for i := 1 to n-1 {count number of passes}
for j := 1 to n-i
if aj > aj+1 then interchange aj and aj+1
{a1, …, an is the increasing order}

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


16 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)

• Greedy algorithms

– Goal: Solving optimization problems. Find a


solution to the given problem that either
minimizes or maximizes the value of some
parameter

– Some examples that involves optimization:

• Find a route between 2 cities with smallest total


mileage
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
• Determine a way to encode 17
CSE 504, messages using the
Algorithms (3.1) (cont.)

– The change making problem

• Problem statement: Consider the problem of


making n cents change with quarters, dimes,
nickels and pennies, and using the least total
number of coins.

• For example, to make change for 67 cents, we


do the following:
1. Select a quarter, leaving 42 cents
2. Select a second quarter, leaving 17 cents
3. Select a dime, leaving 7 cents
4. Select a nickel, leaving 2 cents
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504, 5. Select a penny, leaving 1 cent 18
Algorithms (3.1) (cont.)

Algorithm: Greedy change making

Procedure change (c1, c2, …, cr: values of


denominations of coins where c1 > c2 >…> cr; n:
positive integer)
For i := 1 to r
while n  ci
begin
add a coin with value ci to the change
n := n-ci
end

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


19 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)

• Remark: if we have only quarters, dimes and


pennies  the change for 30 cents would be
made using 6 coins = 1 quarter + 5 pennies.

Whereas a better solution is equal to 3 coins = 3


dimes!

Therefore:

“The greedy algorithm selects the best choice at


each step, instead of considering all sequences of
steps that may lead to an optimal solution. The
greedy algorithm often leads to a solution!”
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
20 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)

• Lemma:

If is a positive integer, then n cents in change using


quarters, dimes, nickels and pennies using the
fewest coins possible has at most 2 dimes, at most 1
nickel, at most 4 pennies and cannot have 2 dimes
and 1 nickel. The amount of change in dimes, nickels
and pennies cannot exceed 24 cents.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


21 Seventh Edition, Mc Graw-Hill, 2011
Algorithms (3.1) (cont.)
• Proof of the lemma using contradiction:

If we had more than the number of coins specified,


then we will be able to replace them using fewer
coins that have the same value.

1. 3 dimes will be replaced by a quarter and 1 nickel


2. 2 nickels replaced by a dime
3. 5 pennies replaced by a nickel
4. 2 dimes and I nickel replaced by a quarter

Besides, we cannot have 2 dimes and 1 nickel  24 cents is


the most money we can have in dimes, nickels and
pennies when we make change using the fewest number
of coins for n cents.
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
22 Seventh Edition, Mc Graw-Hill, 2011
The Growth of Functions (Section
3.2)
• We quantify the concept that g grows at least
as fast as f.

• What really matters in comparing the


complexity of algorithms?

– We only care about the behavior for large


problems.
– Even bad algorithms can be used to solve small
problems.
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
– Ignore implementation details such
CSE 504, 23 as loop
The Growth of Functions (3.2) (cont.)

• The Big-O Notation


– Definition: Let f and g be functions from N to R.
Then g asymptotically dominates f, denoted f is O(g) or
'f is big-O of g,' or 'f is order g,' iff
k C n [n > k  |f(n)|  C |g(n)|]

– Note:

• Choose k
• Choose C; it may depend on your choice of k
• Once you choose k and C, you must prove the truth of
the implication (often by induction)
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
24 Seventh Edition, Mc Graw-Hill, 2011
The Growth of functions (3.2) (cont.)

An alternative for those with a calculus background:

– Definition:
f(n)
if lim  0 then f is o(g) (called little - o of g)
n g ( n )

– Theorem: If f is o(g) then f is O(g).


Proof: by definition of limit as n goes to infinity, f(n)/g(n)
gets arbitrarily small.
That is for any  >0, there must be an integer N such
that when
n > N, | f(n)/g(n) | <  .
Hence,
CSE 504,© bychoose
Kenneth H. Rosen, =  and
C Discrete k =& itsN.
Mathematics
25
Applications, Seventh Edition, Mc Graw-Hill, 2011
The Growth of functions (3.2) (cont.)

– It is usually easier to prove f is o(g)


• using the theory of limits
• using L'Hospital's rule
• using the properties of logarithms
etc.

– Example: 3n + 5 is O(n2)
3n of5limits, it's easy to show
Proof: Using the theory
lim 0 2
n n

Hence 2) and so it is O(n2).


©3n + 5 H.is o(n
CSE 504, by Kenneth Rosen, Discrete
26
Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
The Growth of functions (3.2) (cont.)

– Also note that O(g) is a set called a

complexity class.

– It contains all the functions which g dominates.

f is O(g) means f  O(g).

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


27 Seventh Edition, Mc Graw-Hill, 2011
The Growth of functions (3.2) (cont.)
• Properties of Big-O
– f is O(g) iff O(f)  O(g)
– If f is O(g) and g is O(f) then O(f) = O(g)
– The set O(g) is closed under addition :
If f is O(g) and h is O(g) then f + h is O(g)
– The set O(g) is closed under multiplication by a scalar a (real number):
If f is O(g) then a*f is O(g)
that is,
O(g) is a vector space.
(The proof is in the book).
Also, as you would expect,
– if f is O(g) and g is O(h), then f is O(h).
In particular
O( f )  O(g)  O(h)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504, 28
The Growth of functions (3.2) (cont.)

– Theorem:

If f1 is O(g1) and f2 is O(g2) then

• f1f2 is O(g1g2) (1)

• f1 + f2 is O(max{ g1, g2}) (2)

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


29 Seventh Edition, Mc Graw-Hill, 2011
The Growth of functions (3.2) (cont.)

– Proof of (2): There is a k1 and C1 such that


1. f1(n) < C1g1(n)
when n > k1.
There is a k2 and C2 such that
2. f2(n) < C2g2(n)
when n > k2.
We must find a k3 and C3 such that
3. f1(n)f2(n) < C3g1(n)g2(n)
when n > k3.
We use the inequality
if 0 < a < b and 0 < c < d then ac < bd
to conclude that
f1(n)f2(n) < C1C2g1(n)g2(n)
as long as k > max{k1, k2} so that both inequalities 1 and
2. hold at the same time.
Therefore, choose
C3 = C1C2 and k3 = max{k1, k2} Q.E.D.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


30 Seventh Edition, Mc Graw-Hill, 2011
The Growth of functions (3.2) (cont.)

• Important Complexity Classes

O(1)  O(log n)  O(n)  O(n log n)  O(n2)


 O(nj)  O(cn)  O(n!)

where j>2 and c>1.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


31 Seventh Edition, Mc Graw-Hill, 2011
The Growth of functions (3.2) (cont.)

– Example:

Find the complexity class of the function


(nn!+ 3n+2 + 3n100 )(nn + n2n )

Solution:
This means to simplify the expression.
Throw out stuff which you know doesn't grow as
fast.
We are using the property that if f is O(g) then f+g
is
O(g).
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
32 Seventh Edition, Mc Graw-Hill, 2011
The Growth of functions (3.2) (cont.)

– Solution (cont.)

• Eliminate the 3n100 term since n! grows much


faster.
• Eliminate the 3n+2 term since it also doesn't grow
as fast as the n! term.
Now simplify the second term:
Which grows faster, the nn or the n2n?
• Take the log (base 2) of both.
Since the log is an increasing function whatever
conclusion we draw about the logs will also apply
to the original functions (why?).
• Compare
CSE 504, n log
© by Kenneth H. Rosen, nMathematics
Discrete or log& n + n. 33 Seventh Edition, Mc Graw-Hill, 2011
its Applications,
The Growth of functions (3.2) (cont.)

– If a flop takes a nanosecond, how big can a


problem be solved (the value of n) in

• a minute?
• a day?
• a year?

for the complexity class O( n n! nn).

– Note: We often want to compare algorithms


in the same complexity class

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


34 Seventh Edition, Mc Graw-Hill, 2011
The Growth of functions (3.2) (cont.)

– Example:

Suppose
Algorithm 1 has complexity n2 - n + 1
Algorithm 2 has complexity n2/2 + 3n +2

Then both are O(n2) but Algorithm 2 has a smaller leading


coefficient and will be faster for large problems.

Hence we write
Algorithm 1 has complexity n2 + O(n)
Algorithm 2 has complexity n2/2 + O(n)

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


35 Seventh Edition, Mc Graw-Hill, 2011
Complexity of Algorithms (3.3)

• Time Complexity: Determine the


approximate number of operations required
to solve a problem of size n.

• Space Complexity: Determine the


approximate memory required to solve a
problem of size n.
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
36 Seventh Edition, Mc Graw-Hill, 2011
Complexity of Algorithms (3.3) (cont.)

• Time Complexity

– Use the Big-O notation


– Ignore house keeping
– Count the expensive operations only

– Basic operations:

• searching algorithms - key comparisons


• sorting algorithms - list component comparisons
• numerical algorithms
504,© by Kenneth - floating point ops. (flops) - 2011
CSE H. Rosen, Discrete Mathematics
37
& its Applications, Seventh Edition, Mc Graw-Hill,
Complexity of Algorithms (3.3) (cont.)

• Worst Case: maximum number of


operations

• Average Case: mean number of


operations assuming an input probability
distribution

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


38 Seventh Edition, Mc Graw-Hill, 2011
Complexity of Algorithms (3.3) (cont.)
• Examples:
– Multiply an n x n matrix A by a scalar c to produce
the matrix B:
procedure (n, c, A, B)
for i from 1 to n do
for j from 1 to n do
B(i, j) = cA(i, j)
end do
end do

Analysis (worst case):


Count the number of floating point multiplications.
n2 elements requires n2 multiplications.
time complexity is
O(n2) or quadratic complexity.
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504, 39
Complexity of Algorithms (3.3) (cont.)

– Multiply an n x n upper triangular matrix A


A(i, j) = 0 if i > j
by a scalar c to produce the (upper triangular)
matrix B.

procedure (n, c, A, B)
/* A (and B) are upper triangular */
for i from 1 to n do
for j from i to n do
B(i, j) = cA(i, j)
end do
end do

Analysis (worst case):


© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
Count
CSE 504, the number of floating point 40 multiplications.
Complexity of Algorithms (3.3) (cont.)
The maximum number of non-zero elements in an
n x n upper triangular matrix
=1+2+3+4+...+n
or
• remove the diagonal elements (n) from the total (n2)
• divide by 2
• add back the diagonal elements to get

(n2 - n)/2 + n = n2/2 + n/2


which is
n2/2 + O(n).

Quadratic
CSE 504,© bycomplexity but
Kenneth H. Rosen, Discrete the& itsleading
Mathematics
41 coefficient
Applications, Seventh Edition, Mc Graw-Hill, is
2011
Complexity of Algorithms (3.3) (cont.)

– Bubble sort: L is a list of elements to be


sorted.

• We assume nothing about the initial order


• The list is in ascending order upon completion.

Analysis (worst case):


Count the number of list comparisons required.

Method: If the jth element of L is larger than the (j +


1)st, swap them.

Note: this is not an efficient implementation of the


algorithm
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
42 Seventh Edition, Mc Graw-Hill, 2011
Complexity of Algorithms (3.3) (cont.)

procedure bubble (n, L)


/*
- L is a list of n elements
- swap is an intermediate swap
location
*/
for i from n - 1 to 1 by -1 do
for j from 1 to i do
if L(j) > L(j + 1) do
swap = L(j + 1)
L(j + 1) = L (j)
L(j) = swap
end do
end do
end do
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504, 43
Complexity of Algorithms (3.3) (cont.)
• Bubble the largest element to the 'top' by starting at the
bottom - swap elements until the largest in the top
position.
• Bubble the second largest to the position below the top.
• Continue until the list is sorted.
n-1 comparison on the first pass
n-2 comparisons on the second pass
.
.
.
1 comparison on the last pass

Total:
(n - 1)+ (n - 2) + . . . . + 1 = O(n2) or quadratic complexity
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
44 Seventh Edition, Mc Graw-Hill, 2011
Complexity of Algorithms (3.3) (cont.)

– An algorithm to determine if a function f from


A to B is an injection:

Input: a table with two columns:


• Left column contains the elements of A.
• Right column contains the images of the elements
in the left column.

Analysis (worst case):


Count comparisons of elements of B.

Recall that two elements of column 1 cannot have


the same images in column 2.
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
45 Seventh Edition, Mc Graw-Hill, 2011
Complexity of Algorithms (3.3) (cont.)
One solution:

• Sort the right column


Worst case complexity (using Bubble sort)
O(n2)

• Compare adjacent elements to see if they agree


Worst case complexity
O(n)

Total:
O(n2) + O(n) = O(n2)
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
46 Seventh Edition, Mc Graw-Hill, 2011
Chapter 3 (Part 2):
The Fundamentals: Algorithms, the
Integers & Matrices

• The Integers and Division (Section


3.4)

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2011
© by Kenneth H. Rosen, Discrete Mathematics & its
• Introduction

– Review basics concepts of number theory

– Divisibility, greatest common divisors,


modular arithmetic

– Computer arithmetic using binary expansions

– Application to computer arithmetic,


cryptology, secret messages
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504, 48
• Division

– Definition 1:

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 and that b is multiple of a. The
notation a | b denotes that a divides b. We
write a b when a does not divide b
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
49 Seventh Edition, Mc Graw-Hill, 2011
– Example :

4 | 12 4 18

3 7, since 7 | 3 is not an integer.


– Example: Let n and d be positive integers.
How many positive integers not exceeding n
are divisible by d?

Solution: They are of the form {dk}, where k is


a positive integer.
0 < dk  n  0 < k  n/p
 There are n/p positive integers not
exceeding n that are divisible by d.
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
50 Seventh Edition, Mc Graw-Hill, 2011
– Theorem 1:

Let a, b and c be integers. Then


1. If a|b and a|c, then a | (b + c);
2. If a|b, then a|bc c  Z;
3. If a|b and b|c, then a|c

Proof: Suppose a|b and a|c   s  Z,  t 


Z such that: b = as and c = at. Therefore:
b + c = as + at = a (s + t); which means
that
CSE 504,©a 51 part 1 of the
by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
| (b + c). This establishes
Corollary 1: if a, b and c are integers such that a|b
and a|c, then a | mb +nc whenever m and n are
integers.

Proof: Part 2 of theorem 1 shows that: a | mb and


a | nc whenever m and n are integers. Using part
1 of the theorem  a | mb + nc.
Q.E.D.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


52 Seventh Edition, Mc Graw-Hill, 2011
• Primes

– Definition 2:

A positive integer p greater than 1 is called


prime if the only positive factors of p are 1 and
p. A positive integer that is greater than 1 and is
not prime is called composite.

– Example: 7 is prime, 9 is composite.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


53 Seventh Edition, Mc Graw-Hill, 2011
– Theorem 2:

The Fundamental Theorem of Arithmetic (FTA)


Every positive integer greater than 1 can be
written in a unique way as a prime or as the
product of two or more primes where the prime
factors are written in a nondecreasing size

Example: 100 = 2.2.5.5 = 22.52


641 = 641
999 = 3.3.3.37 = 33.37
1024 = 2.2.2.2.2.2.2.2.2.2 = 210
Large numbers are used for secret messages in
cryptology.
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
54 Seventh Edition, Mc Graw-Hill, 2011
– Theorem 3:

If n is a composite integer, then n has a


prime divisor less than or equal to n.

Proof: if n is composite, n has a factor such


that:
1 < a < n and b  Z such that: n = ab,
where both a and b are positive integers
greater than 1. Therefore
a  n or b  n, since otherwise
ab > n . n = n  n has positive divisor 
n. This divisor is either prime
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
CSE 504, 55 or using the
Example: Prove that the integer 101 is prime.

Solution: Using the contrapositive form of


theorem 3, states that :
“n has not a prime number divisor  n  n
prime”
The only primes not exceeding 101 are {2,
3, 5, 7}, since these numbers do not divide
101  101 is prime!

Q.E.D.
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
56 Seventh Edition, Mc Graw-Hill, 2011
– Theorem 4:

There are infinitely many primes.

Proof: Let’s assume there are only finitely


many primes p1, p2, …, pn. Let Q = p1p2 … pn
+ 1. Using FTA, Q is prime or Q can be
written as the product of two or more primes.
However, none of the primes pj divides Q,
(since pj|Q  pj | (Q – p1p2 … pn) = 1 
impossible since pj prime) if none of the
primes pj divides Q  Q is prime. Q  Pj
contradiction, because
© by Kenneth H. Rosen, Discrete Mathematics & itswe assumed that we
CSE 504, 57
Applications, Seventh Edition, Mc Graw-Hill, 2011
Remark: the largest prime known has been an
integer of the form 2p –1, where p is also prime
(Mersenne primes.)

Example: 22 – 1 = 3, 23 –1 =7, 25 –1 =31 are


Mersenne primes, whereas 211 – 1 = 2047 is not
a Mersenne prime since 2047 = 23 . 89

Remark: the largest number known so far (year


2001) is
213,466,917 – 1 (over four million digits!!!)
Visit GIMPS (Great Internet Mersenne Search)
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
58 Seventh Edition, Mc Graw-Hill, 2011
– Theorem 5:

The Prime Number Theorem


The ratio of the number of primes not
exceeding x and (x/lnx) approaches 1 as x
grows without bound.
(Conjectured by Legendre & Gauss)

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


59 Seventh Edition, Mc Graw-Hill, 2011
• The Division Algorithm

– Theorem 6:

The Division Algorithm


Let a be an integer and d a positive integer.
Then !(q, r)  Z2; 0  r < d: a = dq +r.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


60 Seventh Edition, Mc Graw-Hill, 2011
– Definition 3:

d is called the divisor, a the dividend, q the


quotient and r the remainder.
q = a div d, r = a mod d.

– Example: 101 = 11.9 + 2


Quotient Remainder
= 101 div 11 = 2 = 101 mod 11

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


61 Seventh Edition, Mc Graw-Hill, 2011
• Greatest Common Divisors & Least
Common Multiples
– Definition 4

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. It is
denoted gcd (a, b).

Example: gcd (24, 36)


Div (24) = {1,2,3,4,6,8,12,24}
Div (36) = {1,2,3,4,6,8,9,12,18,36}
Com(24,36) = = {1,2,3,4,6,12}
gcd(24,36) = 12
CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,
62 Seventh Edition, Mc Graw-Hill, 2011
– Definition 5:

The integers a and b are relatively prime (rp)


if gcd(a, b) =1.

Example: 17 and 22 are rp since gcd(17,22)


= 1.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


63 Seventh Edition, Mc Graw-Hill, 2011
– Definition 7:

The least common multiple (lcm) of the positive


integers a and b is the smallest positive integer that
is divisible by both a and b.

lcm ( a , b )  p1max( a1 ,b1 ) p2max( a 2 ,b2 ) ... pnmax( an ,bn )


where max(x,y) denotes the maximum of x and y.

Example : What is the least common multiple of:


233572 and 2433?
Solution: lcm(233572 ,2433) = 2 max(3,4). 3max(5,3).
7max(2,0)
= 2H.4Rosen,
CSE 504,© by Kenneth 357Discrete
2 Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
64
– Theorem 7:

Let a and b be positive integers. Then


ab = gcd(a,b).lcm(a.b).

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


65 Seventh Edition, Mc Graw-Hill, 2011
• Modular Arithmetic
– Definition 8:

Let (a, b)  Z2,, m  Z+ then a is a congruent to


b modulo m if m divides a –b.
Notation: a  b (mod m).

– Theorem 8

Let a and b be integers, and let m be a positive


integer. Then a  b (mod m) if and only if
a mod m = b mod m.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


66 Seventh Edition, Mc Graw-Hill, 2011
– Example: 17  5 (mod 6)
24  14 (mod 6)?
Since: 6|(17 – 5) = 12  17  5 (mod 6)
6 does not divide 10
 24 is not congruent to 14 (mod 6)
– Theorem 9:

Let m be a positive integer. The integers a and b


are congruent modulo m if and only if
k  Z; a = b + km

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


67 Seventh Edition, Mc Graw-Hill, 2011
• Applications of Congruences

1. Hashing Functions

2. Pseudorandom Numbers

3. Cryptology (Caesar Cepher)

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


68 Seventh Edition, Mc Graw-Hill, 2011
1. Hashing Functions

Assignment of memory location to a student


record
h(k) = k mod m
Key: social security # # of available
memory location

Example: h (064212848) = 064212848 mod 111 =


14 when m = 111

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


69 Seventh Edition, Mc Graw-Hill, 2011
2. Pseudorandom Numbers

• Needed for computer simulation

• Linear congruential method :


xn+1 = (axn + c) mod m

• Put them between 0 and 1 as: yn = xn/m

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


70 Seventh Edition, Mc Graw-Hill, 2011
3. Cryptology (Caesar Cepher)

a) Encryption:

• Making messages secrets by shifting each


letter three letters forward in the alphabet
BE XA

• Mathematical expression:
f(p) = (p + 3) mod 26 0  p  25

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


71 Seventh Edition, Mc Graw-Hill, 2011
– Example: What is the secret message produced
from the message “Meet you in the park”
Solution:
1. Replace letters with numbers:
meet = 12 4 4 19
you = 24 14 20
in = 8 1 3
the = 19 7 4
park = 15 0 17 10
2. Replace each of these numbers p by f(p) = (p + 3) mod
26
meet = 15 7 7 22
you = 1 17 23
in = 11 16
the = 22 10 7
park = 18 3 20 13
3. Translate
CSE 504,©back into letters: “PHHW BRX 72 LQ WKH
by Kenneth H. Rosen, Discrete Mathematics & its Applications, Seventh Edition, Mc Graw-Hill, 2011
b) Decryption (Deciphering)

f(p) = (p + k) mod 26 (shift cepher)


 f -1(p) = (p – k) mod 26

Caesar’s method and shift cipher are very


vulnerable and thus have low level of security
(reason frequency of occurrence of letters in the
message)
 Replace letters with blocks of letters.

CSE 504,© by Kenneth H. Rosen, Discrete Mathematics & its Applications,


73 Seventh Edition, Mc Graw-Hill, 2011

You might also like