0% found this document useful (0 votes)
180 views100 pages

Discrete Mathematics: Unit-I-Chapter-2 Algorithms-Integers-Matrices

The document discusses algorithms and their properties. It describes algorithms for finding the maximum value in a sequence, linear search, binary search, sorting algorithms like bubble sort and insertion sort, and greedy algorithms. Examples are provided for finding the maximum, linear search, binary search, bubble sort, insertion sort, and a greedy coin changing algorithm. Pseudocode is given for many of the algorithms discussed.

Uploaded by

Khaja Ahmed
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)
180 views100 pages

Discrete Mathematics: Unit-I-Chapter-2 Algorithms-Integers-Matrices

The document discusses algorithms and their properties. It describes algorithms for finding the maximum value in a sequence, linear search, binary search, sorting algorithms like bubble sort and insertion sort, and greedy algorithms. Examples are provided for finding the maximum, linear search, binary search, bubble sort, insertion sort, and a greedy coin changing algorithm. Pseudocode is given for many of the algorithms discussed.

Uploaded by

Khaja Ahmed
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/ 100

Discrete Mathematics

Unit-I-Chapter-2
Algorithms-Integers-Matrices
Algorithms
 In general, an algorithm just means a defined procedure for performing and completing
some sort of task (aka a sequence of steps)
 Algorithms are the foundation of computer programming.
 Example: Describe an algorithm for finding the maximum (largest) value in a finite
sequence of integers
 Perform the following steps
 Set up temporary maximum equal to the first integer in the sequence
 Compare the next integer in the sequence to the temporary maximum, and if it is larger than the
temporary maximum, set the temporary maximum equal to this
 Repeat the previous step if there are more integers in the sequence
 Stop when there are no integers left in the sequence. The temporary maximum at this point is the
largest integer in the sequence.

BE III Sem-DM - CSE - 2020-21 - MJCET 2


Pseudo code
Provide an intermediate step between English and real implementation using a particular
programming language
procedure max(a1, a2, …, an: integers)
max := a1
for i:=2 to n
if max < ai then max:=ai
{max is the largest element}
Let {ai} = 7, 12, 3, 15. Find its maximum…

Set max = a1 = 7.
 Look at next element: a2 = 12.
 Is max <a2 ? Yes, so change max to 12.
 Look at next element: a3 = 3.
 Is 12 < 3 ? No, leave max alone….
 Is 12 < 15 ? Yes, max = 15…
 We are done: return 15

BE III Sem-DM - CSE - 2020-21 - MJCET 3


Properties of Algorithm
 Input: input values from a specified set
 Output: for each set of input values, an algorithm produces output value from a
specified set
 Definiteness: steps must be defined precisely
 Correctness: should produce the correct output values for each set of input
values
 Finiteness: should produce the desired output after a finite number of steps
 Effectiveness: must be possible to perform each step exactly and in a finite
amount of time
 Generality: applicable for all problems of the desired form, not just a particular
set of input values

BE III Sem-DM - CSE - 2020-21 - MJCET 4


Searching algorithms
Locate an element x in a list of distinct elements, a1, a2, …, an, or
determine it is not in the list
Solution is the location of the term in the list that equals x, and is
0 if x is not in the list
Given a list L of n elements that are sorted into a definite order
(e.g., numeric, alphabetical),
And given a particular element x, Determine whether x appears in
the list, and if so, return its index (position) in the list.

BE III Sem-DM - CSE - 2020-21 - MJCET 5


Linear Search (Pseudo code)
procedure linear search(x:integer, a1, a2, …, an: distinct integers)
i := 1 {start at beginning of list}
while (i≤n and x≠ai) {not done and not found}
i:=i+1 {go to the next position}
if i < n then location:=n {it was found}
else location:=0 {it wasn’t found}
{location is the index of the term equal to x,or is 0 if x is not
found}

BE III Sem-DM - CSE - 2020-21 - MJCET 6


Exercise
Devise an algorithm that finds the sum of all the integers in a
list.

BE III Sem-DM - CSE - 2020-21 - MJCET 7


Binary Search
 Given a sorted list, by comparing the element to be located to the middle term of
the list.
 The list is split into two smaller sublists (of equal size or one has one fewer term)
 Continue by restricting the search to the appropriate sublist
 Ex: Search for 19 in the (sorted) list 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
 First split the list
1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
 Then compare 19 and the largest term in the first list, and determine to use the list
 Continue
12 13 15 16 18 19 20 22
18 19 20 22
19 (down to one term)
BE III Sem-DM - CSE - 2020-21 - MJCET 8
Binary Search (Pseudo Code)
procedure binary_search(x: integer, a1, a2, …, an: increasing integers)
i := 1 {left endpoint of search interval}
j := n {right endpoint of search interval}
while i < j {while interval has > 1 item}
begin
m := ⎣(i + j)/2⎦ {middle}
if x > am
then i := m + 1
else j := m
end
if x = ai then location := i else location := 0
return location {index or 0 if not found}
BE III Sem-DM - CSE - 2020-21 - MJCET 9
Binary Search Example
 Search for 19 in the list
 Index: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22
 Entering while loop: i = 1, j = 16
 m = ⎣(i+j)/2⎦ = ⎣(1+16)/2⎦ = ⎣8.5⎦ = 8, i = 9, j = 16
 m = ⎣(i+j)/2⎦ = ⎣(9+16)/2⎦ = ⎣12.5⎦ = 12, i = 13, j = 16
 m = ⎣(i+j)/2⎦ = ⎣(13+16)/2⎦ = ⎣14.5⎦ = 14, i = 13, j = 14
 m = ⎣(i+j)/2⎦ = ⎣(13+14)/2⎦ = ⎣13.5⎦ = 13, i = 14, j = 14
 Exit loop

BE III Sem-DM - CSE - 2020-21 - MJCET 10


Sorting Algorithms
Sorting is common in many applications.
E.g. spreadsheets and databases
We can search quickly when data is ordered
Sorting is also widely used as a subroutine in other data-processing
algorithms.
Two sorting algorithms :
Bubble sort
Insertion sort
However, these are not very efficient, and you should not use them on
large data sets!
BE III Sem-DM - CSE - 2020-21 - MJCET 11
Bubble Sort
Smaller elements “float” up to the top of the list, like bubbles in a
container of liquid, and the larger elements “sink” to the bottom.
Use the bubble sort to put 3, 2, 4,1, 5 into increasing order

BE III Sem-DM - CSE - 2020-21 - MJCET 12


Bubble Sort Pseudo Code
procedure bubble_sort(a1, a2, …, an: real numbers, n ≥ 2)
for i := 1 to n – 1 {iterate n – 1 passes}
for j := 1 to n – i
if aj > aj+1 then interchange aj and aj+1
{an-i+1, …, an is sorted and ≤ a1, …, an-i}
{a1, a2, …, an is sorted}

BE III Sem-DM - CSE - 2020-21 - MJCET 13


Insertion Sort
English description of algorithm:
 Start with the second element, for each item in the input list:
 “Insert” it into the correct place in the sorted output list generated so far. Like
so:
 Find the location where the new item should be inserted using linear or
binary search.
 Then, shift the items from that position onwards up by one position.
 Put the new item in the remaining hole.

BE III Sem-DM - CSE - 2020-21 - MJCET 14


Insertion Sort
Start with 2nd term
 Larger than 1st term, insert after 1st term
 Smaller than 1st term, insert before 1st term
At this moment, first 2 terms in the list are in correct positions
For 3rd term
 Compare with all the elements in the list
 Find the first element in the list that is not less than this element
 For j-th term
Compare with the elements in the list
Find the first element in the list that is not less than this element

BE III Sem-DM - CSE - 2020-21 - MJCET 15


Insertion Sort Algorithm
Use the insertion sort to put 3, 2, 4, 1, 5 into increasing order
 Insert the 2nd element 2 in the right position:
 3 > 2 ⇒ put 2 in front of 3. ⇒ 2, 3, 4, 1, 5

 Insert the 3rd element 4 in the right position:


 4 > 2 ⇒ do nothing. Move to the next comparison.
 4 > 3 ⇒ do nothing. Done. ⇒ 2, 3, 4, 1, 5
 Insert the 4th element 1 in the right position:
 2 > 1 ⇒ put 1 in front of 2. ⇒ 1, 2, 3, 4, 5

 Insert the 5th element 5 in the right position:


 5 > 1 ⇒ do nothing. Move to the next comparison.
 5 > 2 ⇒ do nothing. Move to the next comparison.
 5 > 3 ⇒ do nothing. Move to the next comparison.
 5 > 4 ⇒ do nothing. Done. ⇒ 1, 2, 3, 4, 5

BE III Sem-DM - CSE - 2020-21 - MJCET 16


Insertion Sort Algorithm
procedure insertion_sort(a1, a2, …, an: real numbers, n ≥ 2)
for i := 2 to n
begin
m := ai {the element to be inserted}
j := 1
while aj < m {look for the index of the hole with j }
j := j + 1
{now a1, …, aj-1 < m ≤ aj, …, ai }
{the hole is at j; j ≤ i, i.e. possibly j = i }
for k := j + 1 to i
ak := ak+1
ai := m
{a1, a2, …, ai are sorted in increasing order}
end {a1, a2, …, an are sorted in increasing order}
BE III Sem-DM - CSE - 2020-21 - MJCET 17
Greedy algorithm
Many algorithms are designed to solve optimization problems
Greedy algorithm:
Simple and naive
Select the best choice at each step, instead of considering all
sequences of steps
Once find a feasible solution
Either prove the solution is optimal or show a counterexample that
the solution is non-optimal

BE III Sem-DM - CSE - 2020-21 - MJCET 18


Greedy algorithm Example
Given n cents change with quarters (25), dimes(10), nickels(5) and
paise(1), and use the least total number of coins
Say, 67 paise
Greedy algorithm
First select a quarter (leaving 42 paise)
Second select a quarter (leaving 17 paise)
Select a dime (leaving 7 paise)
Select a nickel (leaving 2paise)
Select a paisa (leaving 1 paisa)
Select a paisa

BE III Sem-DM - CSE - 2020-21 - MJCET 19


Greedy change-making algorithm
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 then
add a coin with value ci to the change
n:=n- ci
end

BE III Sem-DM - CSE - 2020-21 - MJCET 20


Greedy change-making algorithm Example
Change of 30 cents
If we use only quarters, dimes, and paise (no nickels)
Using greedy algorithm:
6 coins: 1 quarter, 5 paise
Could use only 3 coins (3 dimes)

BE III Sem-DM - CSE - 2020-21 - MJCET 21


Growth of Functions
Analysis of an algorithm
 Derive estimates for the time and space needed to execute the algorithm.
Complexity of an algorithm
 Amounts of time and space required to execute the algorithm
 function of the input: difficult to obtain an explicit formula
 instead of dealing with the input, function of the size of the input
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.
 Ignore implementation details such as loop counter increment, etc.
BE III Sem-DM - CSE - 2020-21 - MJCET 22
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 f(x) is O(g(x))
if there are constants C and k such that
|f(x)| ≤ C |g(x)| whenever x > k
Read as f(x) is big-oh of g(x)
 “Beyond some point k, function f is at most a constant C times g (i.e.,
proportional to g).” :f is bounded from above by g

BE III Sem-DM - CSE - 2020-21 - MJCET 23


Big-O Notation
 Used extensively to estimate the number of operations an algorithm uses as its inputs
grows
 Determine whether it is practical to use a particular algorithm to solve a problem as the
size of the input increases
 Can compare with two algorithms and determine which is more efficient
 For instance, one algorithm uses 100n2+17n+4 operations and the other uses n3 operations
 Can figure out which is more efficient with big-O notation
 The first one is more efficient when n is large even though it uses more operations for
smaller values of n, e.g., n=10
 When big-O notation is used, f(x) is O(g(x)) is chosen to be as small as possible

BE III Sem-DM - CSE - 2020-21 - MJCET 24


Big-Omega Notation & Big-Theta notation
 Big-Omega :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 f(x) is 𝛺(g(x)) if there are positive constants C and k such that
|f(x)|≥C|g(x)| when x>k
 Read as f(x) is big-Omega of g(x)
 f(x) is Ω(g(x)) if and only if g(x) is O(f(x))
 Big-Theta : 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 𝛳(g(x)) if f(x) is O(g(x)) and f(x) is 𝛺g(x))

BE III Sem-DM - CSE - 2020-21 - MJCET 25


Complexity of Algorithms
An algorithm must always produce the correct answer, and should be
efficient.
How can the efficiency of an algorithm be analyzed?
The algorithmic complexity of a computation is, most generally, a measure
of how difficult it is to perform the computation.
That is, it measures some aspect of the cost of computation (in a general
sense of “cost”).
 Amount of resources required to do a computation.
Some of the most common complexity measures:
 “Time” complexity: # of operations or steps required
 “Space” complexity: # of memory bits required
BE III Sem-DM - CSE - 2020-21 - MJCET 26
Worst-, Average- and Best-Case Complexity
 A worst-case complexity measure estimates the time required for the most
time consuming input of each size.
 An average-case complexity measure estimates the average time required
for input of each size.
 A best-case complexity measure estimates the least time consuming input
of each size.
 Most algorithms have different complexities for inputs of different sizes.
 E.g. searching a long list typically takes more time than searching a short one.
 Therefore, complexity is usually expressed as a function of the input size.
 This function usually gives the complexity for the worst-case input of any given
length.
BE III Sem-DM - CSE - 2020-21 - MJCET 27
Max algorithm time complexity
 procedure max(a1, a2, …, an: integers)
max := a1 t1
for i:=2 to n t2
if max < ai then max:=ai t3
{max is the largest element} t4
 First, what is an expression for the exact total worst-case time? (Not its order of growth.)
 t1: once
 t2: n – 1 + 1 times
 t3 : (comparisons): n – 1 times
 t4: once
 Worst-case time complexity:
 t (n) =t1+ t2 +t3+t4= 1 +(n- 1+ 1) +(n- 1)+ 1=2n+1
 There are t (n) =t2 +t3=(n-1+1)+(n-1)=2n-1 comparisons, the time complexity is 𝛳(n) measured in
terms of the number of comparisons
BE III Sem-DM - CSE - 2020-21 - MJCET 28
Linear Search Time Complexity
In terms of the number of comparisons
procedure linear_search (x: integer,a1, a2, …, an: distinct
integers)
i := 1
while (i ≤ n ∧ x ≠ ai) t11 & t12
i := i + 1
if i ≤ n then location := i t2
else location := 0
return location
29
Linear Search Time Complexity Analysis
Worst case time complexity:
t(n) =t11+ t12 +t2=(n+1)+n+1=2n+2= Θ(n)
Best case:
 t(n) =t11+ t12 +t2=1+1+1= Θ(1)
Average case, if item is present:
t(n)=3+5+7+..(2n+1)/n = 2(1+2+…+n)+n/n = 2[n(n+1)/2]/n +1
= n+2= Θ(n)

30
Binary Search Time Complexity
procedure binary_search (x:integer, a1, a2, …, an: distinct integers,
sorted smallest to largest)
i := 1
j := n
while i < j begin t1
m := ⎣(i + j)/2⎦
if x > am then i := m + 1 else j := m t2
end
if x = ai then location := i else location := 0 t3
return location
31
Binary Search Time Complexity Analysis
Suppose that n is a power of 2, i.e., ∃k: n = 2k.
Original range from i = 1 to j = n contains n items.
Each iteration: Size j - i + 1 of range is cut in half.
Size decreases as 2k, 2k-1, 2k-2,…
Loop terminates when size of range is 1 = 20 (i = j).
Therefore, the number of iterations is: k = log2n
t (n)= t1+t2+t3=(k+1)+k+1=2k+2=2log2n +2= Θ(log2n)

32
Bubble Sort Time Complexity Analysis
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 is in increasing order}
Worst-case complexity in terms of the number of comparisons: Θ(n2)

33
Insertion Sort Time Complexity Analysis
procedure insertion_sort (a1, a2, …, an: real numbers; n ≥ 2)
for j := 2 to n
begin
i := 1
while aj > ai
i := i + 1
m := aj
for k := 0 to j – i – 1
aj-k := aj-k-1
ai := m
end {a1, a2, …, an are sorted in increasing order}
Worst-case complexity in terms of the number of comparisons: Θ(n2)
34
Common Terminology for the Complexity of Algorithms

35
The Integers and Division
Number theory: It is the branch of mathematics which involves
integers and their properties
Number theory is vital in many important algorithms today (hash
functions, cryptography, digital signatures,…).
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 a multiple
of a
The notation a | b denotes a divides b. We write a ∤ b when a does not
divide b
BE III Sem-DM - CSE - 2020-21 - MJCET 36
Theorem and corollary
Theorem: Let a, b, and c be integers, then
If a | b and a | c, then a | (b+c)
If a | b, then a | bc for all integers c
If a | b and b | c, then a | c
Corollary: If a, b, and c are integers such that a | b and a | c,
then a | mb+nc whenever m and n are integers

BE III Sem-DM - CSE - 2020-21 - MJCET 37


Primes
Prime: A positive integer p greater than 1 if the only positive
factors of p are 1 and p
An integer p > 1 is prime iff the only positive factors of p are 1
and p itself.
Some primes: 2, 3, 5, 7, 11, 13,...
Non-prime integers greater than 1 are called composite, because
they can be composed by multiplying two integers greater than 1.

BE III Sem-DM - CSE - 2020-21 - MJCET 38


Fundamental theorem of arithmetic
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 when the prime factors are
written in order of non-decreasing size
Prime factorizations of integers: Ex
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
BE III Sem-DM - CSE - 2020-21 - MJCET 39
Prime Numbers Theorem
Theorem: If n is a composite integer, then n has a prime divisor less
than or equal to √n
Proof: As n is composite, n has a factor a with 1<a<n, and thus
n=ab
We show that a≤ √n or b ≤ √n (by contraposition)
Thus n has a divisor not exceeding √n
This divisor is either prime or by the fundamental theorem of
arithmetic, has a prime divisor, and thus n has a prime divisor b less
than or equal to √n
BE III Sem-DM - CSE - 2020-21 - MJCET 40
Prime Numbers Theorem
Contra positive of previous Theorem: An integer is prime if it is not
divisible by any prime less than or equal to its square root
Example: Show that 101 is prime
 Primes not exceeding √101 : 2, 3, 5, 7
 101 is not divisible by any of 2, 3, 5, or 7
 Therefore, 101 is a prime
Exercise: Are these integers prime? 107, 93

BE III Sem-DM - CSE - 2020-21 - MJCET 41


Procedure for prime factorization
Begin by dividing n by successive primes, starting with 2
If n has a prime factor, we would find a prime factor not exceeding √n
If no prime factor is found, then n is prime
Otherwise, if a prime factor p is found, continue by factoring n/p
Note that n/p has no prime factors less than p
If n/p has no prime factor greater than or equal to p and not exceeding its square root, then it is prime
Otherwise, if it has a prime factor q, continue by factoring n/(pq)
Continue until factorization has been reduced to a prime
Find the prime factorization of 7007 (√ 7007=83.7 )
Perform division of 7007 by successive primes, beginning with 2. None of the primes 2,3, and5 divides 7007.
However 7 divides 7007, with 7007 / 7 = 1001 (7007 = 7 ・ 1001)
Perform division of 1001 by successive primes, beginning with 7
7 also divides 1001, since 1001 / 7 = 143 (7007 = 7 ・ 7 ・ 143)
Perform division of 143 by successive primes, beginning with 7 (7 does not divide 143,11 divides 143)
143 / 11 = 13 (7007 = 7 ・ 7 ・ 11 ・ 13). Since 13 is prime, procedure is completed = (72 ・ 11 ・ 13)
Find the prime factorization of 729, 10!

BE III Sem-DM - CSE - 2020-21 - MJCET 42


The Division algorithm
Let a be integer and d be a positive integer. Then there are unique
integers q and r with 0 ≤ r < d, such that a=dq+r
In the equality, d is the divisor, a is dividend, q is the quotient, r is
the remainder
We can find q and r by: q = ⎣a/d⎦, r = a - dq
q = a div d, r = a mod d

43
The Division algorithm
Find the Quotient and remainder when
 i)101 is divided by 11 ii) -11 is divided by 3
101 = 11·9 + 2 (dividend: 101, divisor: 11)
 101 div 11 = 9 101 mod 11 = 2
 –11 = 3·(–4) + 1 or –11 = 3·(–3) – 2 ? (dividend: –11, divisor: 3)
 –11 div 3 = –4 –11 mod 3 = 1
(quotient: –4, remainder: 1)
Note that the remainder must not be negative.
Find the Quotient and remainder when
 i)19 is divided by 7 ii) -111 is divided by 11
44
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 (GCD) of a and b, often
denoted as gcd(a,b).Ex: gcd(24,36)=12.
The integers a and b are called relative prime or coprime if their GCD is 1
Ex: gcd(10, 17)=1, gcd(10, 21)=1, gcd(10,24)=2
 The integers a1, a2, …, an are pairwise relatively prime if gcd(ai, aj)=1 whenever
1≤i<j≤n
 Ex: 10,17,21 are pairwise relatively prime. Since gcd(10,17)=1, gcd(10,21)=1 and
gcd(17,21)=1
 Check whether the following integers are pairwise relatively prime
 i)11,15,19 ii)14,15,21
45
GCD Shortcut
If the prime factorizations are written as
a=p1a p2a …pna and b=p1b p2b …pnb , then the GCD is given by:
1 2 n 1 2 n

gcd(a ,b )=p1min(a ,b ) p2min(a ,b )… pnmin(a ,b )


1 1 2 2 n n

Ex: gcd(120,500)
 Prime factorization of 120= 23.3.5 and 500=22.53
 gcd(120,500)=2min(3,2)3min(1,0)5min(1,3) =223051 =20
Ex: gcd(84,96)
 84 = 22·3·7 96 = 25·3
 gcd(84,96) = 2min(2,5)3min(1,1)7min(1,0) = 22·31·70 = 12.
Find the GCD of 37.53.73, 211.35.59
46
Least Common Multiple
Least common multiple of the positive integers a and b is the
smallest positive integer that is divisible by both a and b, denoted as
lcm(a,b). E.g. lcm(6,10) = 30
Example: lcm(24,36) = ?
 Positive multiples of 24: 24, 48, 72, 96, 120, 144,…
 Positive multiples of 36: 36, 72, 108, 144,…
 Positive common multiples: 72, 144,…
 The smallest one of these is 72.

47
LCM Shortcut
If the prime factorizations are written as
a=p1a p2a …pna and b=p1b p2b …pnb , then the LCM is given by
1 2 n 1 2 n

lcm(a ,b )=p1max(a ,b ) p2max(a ,b )… pnmax(a ,b )


1 1 2 2 n n

Example: lcm(84,96)
 a = 84 = 2·2·3·7 = 22·31·71
 b = 96 = 2·2·2·2·2·3 = 25·31·70
 lcm(84,96) = 2max(2,5)·3max(1,1)·7max(1,0) = 25·31·71 =32·3·7= 672
Find the LCM of 37.53.73, 211.35.59

48
GCD and LCM
Theorem: Let a and b be positive integers. Then
ab = gcd(a,b) × lcm(a,b)
Example
a = 84 = 22·31·71
b = 96 = 25·31·70
ab = (22·31·71) ・ (25·31·70) = 22 ・ 31 ・ 70 ・ 25 ・ 31 ・ 71
= 2min(2,5) ・ 3min(1,1) ・ 7min(1,0) ・ 2max(2,5) ・ 3max(1,1) ・ 7max(1,0)
= gcd(a,b) × lcm(a,b)

49
Modular arithmetic
If a and b are integers and m is a positive integer, then a is congruent to b
modulo m if m divides a-b
We use the notation a≡b (mod m) to indicate that a is congruent to b modulo
m
 Note: this is a different use of “≡” than the meaning “equivalent” or “is defined
as” used before in logic.
If a and b are not congruent modulo m, we write a ≢b (mod m)
Ex: Determine whether 17 is congruent to 5 modulo 6, and whether 24
is congruent to14 modulo 6
Since 6 divides 17-5=12, we say 17≡5 (mod 6)
24-14=10 is not divisible by 6, and thus 24≢14 (mod 6)
50
Useful Congruence Theorems
Let a and b be integers, m be a positive integer.
Then a≡b (mod m) if and only if a mod m = b mod m
Let m be a positive integer. The integer a and b are congruent
modulo m if and only if there is an integer k such that a=b+km
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)
7 ≡ 2 (mod 5) and 11 ≡ 1 (mod 5), so
18=7+11 ≡ 2+1=3 (mod 5)
77=7∙11 ≡2∙1=2(mod 5)
51
Applications of Congruences
Hashing Functions: We want to quickly store and retrieve records in memory locations.
A hashing function takes a data item to be stored or retrieved and computes the first
choice for a location for the item.
h(k) = k mod m
A hashing function h assigns memory location h(k) to the record that has k as its key.
h(064212848) = 064212848 mod 111 = 14
h(037149212) = 037149212 mod 111 = 65
h(107405723) = 107405723 mod 111 = 14 ⇒ collision!
Find the first unoccupied memory location after the occupied memory.
 In this case, assign memory location 15.
If collision occurs infrequently, and if when one does occur it is resolved quickly, then
hashing provides a very fast method of storing and retrieving data.

52
Applications of Congruences
Pseudorandom Numbers: Numbers that are generated deterministically, but that
appear random for all practical purposes.
The most commonly used procedure for generating pseudorandom numbers is the
linear congruential method (uses the mod operator)
Requires four natural numbers:
The modulus m, multiplier a, increment c, and seed x0.
 where 2≤a<m, 0 ≤c<m, and 0≤x0<m
Generates a sequence of pseudorandom numbers {xn} with 0 ≤ xn < m for all n, by
xn+1=(axn+c) mod m
Tends to work best when a, c, m are prime, or at least relatively prime.
If c = 0, the method is called a pure multiplicative generator.

53
Applications of Congruences
Pseudorandom Numbers Example: Let m=9, a=7, c=4, x0=3. Then the sequence of
pseudorandom numbers generated are
 x1=7x0+4 mod 9=(21+4) mod 9=25 mod 9 = 7
 x2=7x1+4 mod 9=(49+4) mod 9=53 mod 9 = 8
 x3=7x2+4 mod 9=(56+4) mod 9=60 mod 9 = 6 xn+1=(axn+c) mod m
 x4=7x3+4 mod 9=(42+4) mod 9=46 mod 9 = 1
 x5=7x4+4 mod 9=(7+4) mod 9=11 mod 9 = 2
 x6=7x5+4 mod 9=(14+4) mod 9=18 mod 9 = 0
 x7=7x6+4 mod 9=(0+4) mod 9=4 mod 9 = 4
 x8=7x7+4 mod 9=(28+4) mod 9=32 mod 9 =5
 x9=7x8+4 mod 9=(35+4) mod 9=11 mod 9 = 3
A sequence of 3, 7, 8, 6, 1, 2, 0, 4, 5, 3, 7, 8, 6, 1, 2, 0, 4, 5, 3 , …
Contains 9 different numbers before repeating
54
Applications of Congruences
Cryptology: The study of secret messages
Encryption is the process of making a message secret. Decryption is the
process of determining the original message from the encrypted message.
Some simple early codes include Caesar’s cipher:
Assign an integer from 0 to 25 to each letter based on its position in the alphabet.
Caesar's encryption method: f(p) = (p + 3) mod 26
“MEET YOU IN THE PARK”
 12 4 419 24 14 20 8 13 19 7 4 15 0 17 10
 15 7 7 22 1 17 23 11 16 22 10 7 18 3 20 13
“PHHW BRX LQ WKH SDUN”
Caesar's decryption method: f –1(p) = (p – 3) mod 26

55
Applications of Congruences
Cryptology: Caesar's encryption method does not provide a high
level of security
A slightly better approach: f(p) = (ap + b) mod 26
 Example:
 What letter replaces the letter K when the function f(p)= (7p + 3) mod 26
is used for encryption?
 10 represents K
 f(10) = (7×10 + 3) mod 26 = 73 mod 26 = 21
 21 represents V
 Therefore, K is replaced by V in the encrypted message
56
Integers and Algorithms
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=akbk+ak-1bk-1+…..+a1b+a0, where k is a non negative integer less than b, and ak!=0.
This representation is called the base b expansion of n
The base b expansion of n is denoted by (akak-1…..a1a0)b
 For instance, (245)8=2·82+4·8+5=165
 Particular Bases of Interest
 Base b = 10 (decimal): 10 digits: 0,1,2,3,4,5,6,7,8,9.
 Base b = 2 (binary): 2 digits: 0,1. (“Bits”=“binary digits.”)
 Base b = 8 (octal): 8 digits: 0,1,2,3,4,5,6,7.
 Base b = 16 (hexadecimal): 16 digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

57
Base Conversions
Example 1: Decimal expansion of the integer with binary expansion
(101011111)2?
 (101011111)2
= 1 ・ 28 + 0 ・ 27 + 1 ・ 26 + 0 ・ 25 + 1 ・ 24 + 1 ・ 23 + 1 ・ 22 +
1・2+1
= (351)10
Example 2: Decimal expansion of the integer with hexadecimal
expansion (2AE0B)16?
 (2AE0B)16 = 2 ・ 164 + 10 ・ 163 + 14 ・ 162 + 0 ・ 16 + 11
= (175627)10
58
Converting to Base b
To convert any integer n to any base b > 1:
To find the value of the rightmost (lowestorder) digit, simply compute n mod
b.
Now, replace n with the quotient.
Repeat above two steps to find subsequent digits, until n is gone (= 0).
 Example: Find the base 8, i.e. octal, expansion of (12345) 10
 12345 = 8 ・ 1543 + 1
 1543 = 8 ・ 192 + 7
 192 = 8 ・ 24 + 0
 24 = 8 ・ 3 + 0
 3=8・0+3
 Therefore, (12345)10 = (30071)8

59
Binary<-> Hexadecimal
 Hexadecimal expansion of (11 1110 1011 1100)2
∴ (11 1110 1011 1100)2 = (3EBC)16
 Binary expansion of (A8D)16
(A)16 = (1010)2, (8)16 = (1000)2, (D)16 = (1101)2
∴ (A8D)16 = (1010 1000 1101)2

60
Addition of Binary Numbers
procedure add(an−1…a0, bn-1…b0: binary representations of non-negative integers a, b)
carry := 0
for j := 0 to n−1
begin
d := ⎣(aj+bj+carry )/2 ⎦
sj := aj+bj+carry-2d
carry := d
end
sn := carry
return sn … s0: binary representation of integers

61
Addition of Binary Numbers
 Ex: Add a=(1110)2 and b= (1011)2
Sol: a0+b0= 0+1=0·2+1, so that c0=0 and s0=1. Then since
a1+b1+c0=1+1+0=1·2+0, it follows that c1=1 and s1=0. Continuing
a2+b2+c1=1+0+1=1·2+0, so that c2=1 and s2=0. Finally, since
a3+b3+c3=1+1+1=1·2+1, it follows that c3=1 and s3=1. This means that s4=c3=1.
∴ s=a+b=(11001)2

62
Multiplication of Binary Numbers
 ab = a(b0 ・ 20 + b1 ・ 21 + ・・・ + bn-1 ・ 2n-1)
= a(b0 ・ 20 ) + a(b1 ・ 21 ) + ・・・ + a(bn-1 ・ 2n-1)
procedure multiply(an−1…a0, bn−1…b0: binary representations of positive integers a,b)
for j := 0 to n−1
begin
if bj = 1 then cj:= a shifted j places
else cj:=0
end
{c0,c1,…cn-1 are the partial products}
p=0
for j := 0 to n−1
p:=p+cj
{p is the value of ab}
63
Multiplication of Binary Numbers
 ab = a(b0 ・ 20 + b1 ・ 21 + ・・・ + bn-1 ・ 2n-1)
= a(b0 ・ 20 ) + a(b1 ・ 21 ) + ・・・ + a(bn-1 ・ 2n-1)
Ex: Find the product of a=(110)2 and b=(101) 2
 Note that
 ab0.20=(110)2.1.20=(110)2
 ab1.21=(110)2.0.21=(0000)2
 and ab2.22=(110)2.1.22=(11000)2
 To find the product, add (110)2, (0000)2 and (11000)2
 Carrying out these additions shows that ab= (11110)2
110 a
x 101 b
110
000
110
111 10

64
Modular Exponentiation
 Problem: Given large integers b (base), n (exponent), and m (modulus), efficiently compute bn mod m.
 Note that bn itself may be completely infeasible to compute and store directly.
 E.g. if n is a 1,000-bit number, then bn itself will have far more digits than there are atoms in the
universe!
 Yet, this is a type of calculation that is commonly required in modern cryptographic algorithms!
 Note that: bnn=(b k 1 ) k-1x(b
2k-1 n 2k-2)n x… x(b20)n
k-2 0
    
b n
 b k 1 2  n1 2 n0
 We can compute b to various powers of 2 by repeated squaring.
 Then multiply them into the partial product, or not, depending on whether the corresponding ni bit is 1.
 Crucially, we can do the mod m operations as we go along, because of the various identity laws of
modular arithmetic.
 All the numbers stay small.
 To compute bn , first find the values of b mod m, b2 mod m, …,b2k-1 mod m
 Next multiplies those terms with b2j mod m where aj=1, finding the remainder of the product when
divided by m after each multiplication.

65
Modular Exponentiation
 To compute 311
 11=(1011)2 ,So 311=38 32 31 .
 First compute 32=9,
 and then 34=92=81,
 and 38=(34)2=(81)2=6561,
 So 311=6561*9*3=177147

66
Modular Exponentiation
procedure modular exponentiation (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}

67
Modular Exponentiation
 Compute 3644 mod 645
 First note that 644=(1010000100)2
 At the beginning, x=1, power=3 mod 645 = 3
 i=0, a0=0, x=1, power=32 mod 645=9
 i=1, a1=0, x=1, power=92 mod 645=81
 i=2, a2=1, x=1*81 mod 645=81, power=812 mod 645=6561 mod 645=111
 i=3, a3=0, x=81, power=1112 mod 645=12321 mod 645=66
 i=4, a4=0, x=81, power=662 mod 645=4356 mod 645=486
 i=5, a5=0, x=81, power=4862 mod 645=236196 mod 645=126
 i=6, a6=0, x=81, power=1262 mod 645=15876 mod 645=396
 i=7, a7=1, x=(81*396) mod 645=471, power=3962 mod 645=156816 mod 645=81
 i=8, a8=0, x=471, power=812 mod 645=6561mod 645=111
 i=9, a9=1, x=(471*111) mod 645=36

 3644 mod 645=36


 Use Modular Exponentiation to find 32003 mod 99
68
Euclidean algorithm
 Need more efficient prime factorization algorithm
 Example: Find gcd(91,287)
 287=91 ∙ 3 +14
 Any divisor of 287 and 91 must be a divisor of 287- 91 ∙ 3 =14
 Any divisor of 91 and 14 must also be a divisor of 287= 91 ∙ 3 +14
 Hence, the gcd(91,287)=gcd(91,14)
 Next divide 91 by 14, 91= 14 ∙ 6+7
 Any divisor of 91 and 14 also divides 91- 14 ∙ 6=7 and any divisor of 14 and 7
divides 91, i.e., gcd(91,14)=gcd(14,7)
 14= 7 ∙ 2, gcd(14,7)=7, and thus gcd(287,91)=gcd(91,14)=gcd(14,7)=7

69
Euclidean algorithm
 Lemma: Let a=bq+r, where a, b, q, and r are integers. Then gcd(a,b)=gcd(b,r)
 Suppose a and b are positive integers, a≥b. Let r0=a and r1=b, we successively
apply the division algorithm
r0  r1 q 1  r 2 , 0  r 2  r1
r1  r 2 q 2  r3 , 0  r3  r 2
...
rn  2  rn  1 q n  1  rn , 0  rn  rn  1
rn  1  rn q n
gcd( a , b )  gcd( r0 , r1 )  gcd( r1 , r 2 )    gcd( r n  2 , r n  1 )
 gcd( r n  1 , r n )  gcd( r n , 0 )  r n
 Hence, the gcd is the last nonzero remainder in the sequence of divisions

70
Euclidean algorithm
 procedure gcd(a, b: positive integers)
x:=a
y:=b
while (y≠0)
begin
r:=x mod y
x:=y
y:=r
end {gcd(a,b) is x}

71
Euclidean algorithm
 Find the GCD of 414 and 662 using the Euclidean algorithm
662=414 ∙ 1+248
414=248 ∙ 1+166
248=166 ∙ 1+82
166=82 ∙ 2 + 2
82=2 ∙ 41
gcd(414,662)=2 (the last nonzero remainder)
 Use Euclidean algorithm to find the
i) gcd(123,277) ii) gcd(1001,1331)

72
Applications of Number Theory
 Miscellaneous Results
 Theorem 1: If a and b are positive integers, then there exist integers s and t such
that gcd(a,b)=sa + tb
 ∀a,b ∈ Z+: ∃s,t ∈Z: gcd(a,b) = sa + tb
 Lemma 1: If a, b and c are positive integers such that gcd(a,b)=1 and a|bc, then a|c.
 ∀a,b,c ∈ Z+: gcd(a,b)=1 ∧ a | bc → a|c
 Lemma 2: If p is a prime and p|a1a2… an where each ai is an integer, then p| ai for
some i.
 If p is prime and p|a1a2… an (integers ai) then ∃i: p| ai.
 Theorem 2: Let m be a positive integer and let a, b and c be integers.
 If ac ≡ bc (mod m) and gcd(c,m)=1, then a ≡ b (mod m).

73
Theorem 1: Example
 Theorem 1: If a and b are positive integers, then there exist integers s and t such
that gcd(a,b)=sa + tb
 ∀a,b ∈ Z+: ∃s,t ∈Z: gcd(a,b) = sa + tb
 Example: Express gcd(252, 198) = 18 as a linear combination of 252 and 198.
 252 = 1 ⋅ 198 + 54
198 = 3 ⋅ 54 + 36
54 = 1 ⋅ 36 + 18
36 = 2 ⋅ 18
 18 = 54 – 1 ⋅ 36 = 54 – 1 ⋅ (198 – 3 ⋅ 54) = 4 ⋅ 54 – 1 ⋅ 198
= 4 ⋅ (252 – 1 ⋅ 198) – 1 ⋅ 198
= 4 ⋅ 252 – 5 ⋅ 198
 Therefore, gcd(252, 198) = 18 = 4 ⋅ 252 – 5 ⋅ 198
74
Theorem 2: Example
 Theorem 2: Let m be a positive integer and let a, b and c be integers.
 If ac ≡ bc (mod m) and gcd(c,m)=1, then a ≡ b (mod m).
 Examples
 20 ≡ 8 (mod 3) i.e. 5 ⋅ 4 ≡ 2 ⋅ 4 (mod 3)
Since gcd(4, 3) = 1, 5 ≡ 2 (mod 3)
 14 ≡ 8 (mod 6) i.e. 7 ⋅ 2 ≡4 ⋅2(mod 6) but 7 ≡ 4 (mod 6) (As gcd(2,6) ≠ 1)

75
Linear Congruences
 A congruence of the form ax ≡ b (mod m) is called a linear congruence. (m∈Z+,
a,b∈Z,and x: variable)
 To solve the congruence is to find the x’s that satisfy it.
 An inverse of a, modulo m is any integer a-1 such that a-1a ≡ 1 (mod m).
 If we can find such an a-1, notice that we can then solve ax ≡ b (mod m) by multiplying
through by it, giving a-1ax ≡ a-1b (mod m), thus
1 ・ x ≡ a-1b (mod m), thus x ≡ a-1b (mod m).

76
Theorem 3
 Theorem 3: If gcd(a,m)=1 (i.e. a and m are relatively prime) and m > 1,
then a has an inverse a-1 unique modulo m.
 Proof: By theorem 1, since gcd(a,m)=1, ∃s,t: sa + tm = 1, so sa + tm ≡ 1 (mod
m).
 Since tm ≡ 0 (mod m), it follows that sa ≡ 1 (mod m).
 Thus s is an inverse of a (mod m).
 Find an inverse of 3 modulo 7
 Since gcd(3, 7) = 1, by Theorem 3 there exists an inverse of 3 modulo 7.
 7 = 2 ・ 3 + 1 by Euclidean algo.
 From the above equation, –2 ・ 3 + 1 ・ 7 = 1
 Therefore, –2 is an inverse of 3 modulo 7
 (Note that every integer congruent to –2 modulo 7 is also an inverse of 3, such as 5, –9, 12, and so on.)
 Find an inverse of i) 4 modulo 9 ii) 19 modulo 141

77
Example
 What are the solutions of the linear congruence 3x ≡ 4 (mod 7)?
 –2 is an inverse of 3 modulo 7 (previous slide)
 Multiply both side by –2: –2 ・ 3x ≡ –2 ・ 4 (mod 7)
 –6 ・ x ≡ x ≡ –8 ≡ 6 (mod 7)
 Therefore, the solutions to the congruence are the integers x such that x ≡ 6 (mod 7), i.e. 6, 13, 20,
27,… and –1, –8, –15,…
 e.g. 3 ・ 13 = 39 ≡ 4 (mod 7)
 What are the solutions of the linear congruence 4x ≡ 5(mod 9)?

78
Chinese Remainder Theorem
 Theorem: (Chinese remainder theorem)Let m1,…,mn > 0 be pairwise relatively prime
and ai ,…,an arbitrary integers.
Then the equations system x ≡ ai (mod mi) (for i=1,..,n) has a unique solution modulo m =
m1 m2 ・・・ mn.
 Proof:
Let Mk = m/mk. (Thus gcd(mk , Mk)=1.)
So by Theorem 3, ∃yk=Mk such that Mk yk≡1 (mod mk).
Now let x = Σi aiyiMi = a1M1y1 + a2M2 y2+ ・・・ + anMnyn.
Since mj|Mk for j≠k, Mj≡0 (mod mk), so
x≡ ak Mk yk ≡ak (mod mk). Thus, the congruences hold.

79
Chinese Remainder Theorem
 Ex: There are certain things whose numbers is unknown. When divided by 3, the remainder is 2; when
divided by 5, the remainder is 3; and when divided by 7, the remainder is 2. What will be the number of
things?
 This can be translated as x ≡ 2(mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7).
 Soln: Here k=3, m1=3,m2=5,m3=7
 a1=2, a2=3,a3=2. To solve the system of congruences.
 m=m1⋅m2⋅m3=3⋅5⋅7=105.
 M1=m/m1=105/3=35, M2=m/m2=105/5=21, M3=m/m3=105/7=15
 M1=35 modulo 3, M2=21 modulo 5, M3=15 modulo 7
 As Mkyk=1(mod mk)=> Mk=y-1 (mod mk)
 y1=M1-1 mod m1, y2= M2-1 mod m2, y3= M3-1 mod m3
 y1=2, as 2 is an inverse of M1= 35 modulo 3, since 35 ≡ 2 (mod 3);
 y2=1, as 1 is an inverse of M2= 21 modulo 5, since 21 ≡ 1(mod 5); and
 y3=1, as 1 is an inverse of M3= 15 modulo 7, since 15 ≡ 1(mod 7).
 The solution to this system are those x such that
 x ≡ a1M1y1+a2M2y2+a3M3y3=2⋅35⋅2+3⋅21⋅1+2⋅15⋅1=233=23 (mod 105)
 23 is the smallest positive integer that is a simultaneous solution.
80
Chinese Remainder Theorem
 Example: Which integer leaves a remainder of 1 when divided by 2 and also remainder of 1 when divided by 3.

81
Pseudoprimes & Carmichael Numbers
 Ancient Chinese mathematicians noticed that whenever n is prime, 2n−1≡1 (mod n).
 Some also claimed that the converse was true.
 However, it turns out that the converse is not true!
 If 2n−1≡1 (mod n), it doesn’t follow that n is prime.
 For example, 341=11·31, but 2340≡1 (mod 341).
 Composites n with this property are called pseudoprimes.
 More generally, if bn−1≡1 (mod n) and n is composite, then n is called a pseudoprime to the
base b.
 Carmichael Numbers : These are sort of the “ultimate pseudoprimes.”
 A Carmichael number is a composite integer n such that bn−1≡1 (mod n) for all b relatively prime to n.
 The smallest few are 561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841, 29341.

82
Fermat’s Little Theorem
 Fermat generalized the ancient observation that 2 p−1≡1 (mod p) for primes p to the
following more general theorem:
 Theorem: (Fermat’s Little Theorem.)
 If p is prime and a is an integer not divisible by p, then ap−1≡1 (mod p).
 Furthermore, for every integer a we have ap ≡ a (mod p).
 Example (Exponentiation MOD a Prime)
 Find 2301 mod 5:
 By FLT, 24 ≡ 1 (mod 5). Hence,
 2300 = (24)75 ≡ 1 (mod 5).
 Therefore, 2301=(2300) ・ 2 ≡ 1 ・ 2 (mod 5)≡2 (mod 5)

 Exercise: Compute the following using Fermat’s Little Theorem


a) 52003 mod 7 b) 52003 mod 11 c) 52003 mod 13

83
Public Key Cryptography
 In private key cryptosystems, the same secret “key” string is used to both encode and
decode messages.
 This raises the problem of how to securely communicate the key strings.
 In public key cryptosystems, there are two complementary keys instead.
 One key decrypts the messages that the other one encrypts.
 This means that one key (the public key) can be made public, while the other (the private
key) can be kept secret from everyone.
 Messages to the owner can be encrypted by anyone using the public key, but can only be
decrypted by the owner using the private key.
 Or, the owner can encrypt a message with their private key, and then anyone can decrypt it, and
know that only the owner could have encrypted it. This is the basis of digital signature systems.
 The most famous public-key cryptosystem is RSA.
 It is based entirely on number theory

84
Public Key Cryptography
Rivest-Shamir-Adleman (RSA)
 Choose a pair p, q of large random prime numbers with about the same number of
bits
 Let n = pq
 Choose exponent e that is relatively prime to (p−1)(q−1) and 1 < e <(p−1)(q−1)
 Compute d, the inverse of e modulo (p−1)(q−1).
 The public key consists of: n, and e.
 The private key consists of: n, and d.

85
Public Key Cryptography
RSA Encryption
 To encrypt a message encoded as an integer:
 Translate each letter into an integer and group them to form larger integers, each representing a
block of letters. Each block is encrypted using the mapping
C = Me mod n.
 Example: RSA encryption of the message STOP with p = 43, q = 59, and e = 13
 n = 43 x 59 = 2537
 gcd(e, (p–1)(q–1)) = gcd(13, 42 ・ 58) = 1
 STOP -> 1819 1415
 C = Me mod n.= M13 mod 2537
 181913 mod 2537 = 2081; 141513 mod 2537 = 2182
 Encrypted message: 2081 2182

86
Public Key Cryptography
RSA Decryption
 To decrypt the encoded message C,
 Compute M = Cd mod n
 Recall that d is an inverse of e modulo (p−1)(q−1).
 Ex: RSA decryption of the message 0981 0461 encrypted with p = 43, q = 59, and e = 13
 n = 43 x 59 = 2537; d =e-1 modulo((p-1)(q-1))
 ed=1 mod((p-1)(q-1)) =>13d= 1 modulo (42 ・ 58) => 13d= 1 modulo 2436=> d= 937
 0981937 mod 2537 = 0704
 0461937 mod 2537 = 1115
 Decrypted message: 0704 1115
 Translation back to English letters: HELP

87
Matrices
 A matrix is a rectangular array of objects (usually numbers).
 An m × n (“m by n”) matrix has exactly m horizontal rows, and n vertical columns.
 Plural of matrix = matrices
 An n × n matrix is called a square matrix
 Row and Column Order
 The rows in a matrix are usually indexed 1 to m from top to bottom.
 The columns are usually indexed 1 to n from left to right.
 Elements are indexed by row, then by column.

88
Matrix Equality
 Two matrices A and B are considered equal iff they have the same number of rows, the
same number of columns, and all their corresponding elements are equal.

 Matrix Sums: The sum A + B of two matrices A, B (which must have the same number of rows, and
the same number of columns) is the matrix (also with the same shape) given by adding corresponding
elements of A and B. ->A + B = [aij + bij ]

89
Matrix Products
 For an m × k matrix A and a k × n matrix B, the product AB is the m × n matrix:

 I.e., the element of AB indexed (i, j) is given by the vector dot product of the i-th row of A
and the j-th column of B.
 Ex:

90
Matrix Product Example

 Because A is a 2×3 matrix and B is a 2×2 matrix, the product AB is not defined.
 Matrix multiplication is not commutative!
 A: m × n matrix and B: r × s matrix
 AB is defined when n = r
 BA is defined when s = m
 When both AB and BA are defined, generally they are not the same size unless m = n = r = s
 If both AB and BA are defined and are the same size, then A and B must be square and of the
same size
 Even when A and B are both n × n matrices, AB and BA are not necessarily equal

91
Matrix Multiplication Algorithm
procedure matmul(matrices A: m × k, B: k × n)
for i := 1 to m
for j := 1 to n
begin
cij := 0
for q := 1 to k
cij := cij + aiqbqj
end
{C = [cij] is the product of A and B}

92
Identity Matrices
 The identity matrix of order n is the n x n matrix, In, is the rank-n square matrix with 1’s
along the upper-left to lower-right diagonal, and 0’s everywhere else.

93
Matrix Inverses
 For some (but not all) square matrices A, there exists a unique multiplicative inverse A−1 of
A, a matrix such that A−1 A = In.
 If the inverse exists, it is unique, and A−1 A = A A−1 .
 Powers of Matrices:
 If A is an n × n square matrix and p ≥ 0, then:
 Ap = AAA···A (and A0 = In)
p times

94
Matrix Transposition
 If A = [aij] is an m × n matrix, the transpose of A (often written At or AT) is the n × m matrix
given by
At = B = [bij] = [aji] (1 ≤ i ≤ n,1 ≤ j ≤ m)

95
Symmetric Matrices
 A square matrix A is symmetric iff A = At. I.e., ∀i, j ≤ n: aij = aji .
 Which of the below matrices is symmetric?

96
Zero-One Matrices
 Useful for representing other structures.
 E.g., relations, directed graphs
 All elements of a zero-one matrix are either 0 or 1.
 E.g., representing False & True respectively.
 The join of A, B (both m × n zero-one matrices):
 A ∨ B = [aij ∨ bij]
 The meet of A, B:
 A ∧ B = [aij ∧ bij] = [aij bij]

97
Boolean Products
 Let A = [aij] be an m × k zero-one matrix and B = [bij] be a k × n zero-one matrix,
 The Boolean product of A and B is like normal matrix multiplication, but using ∨ instead
of +, and ∧ instead of × in the row-column “vector dot product”:

 Find the Boolean product of A and B, where

98
Boolean Powers
 For a square zero-one matrix A, and any k ≥ 0, the k-th Boolean power of A is simply the
Boolean product of k copies of A.
 A[k] = A⊙A ⊙ ・・ ⊙ A
k times
 A[0] = In

99
Exercises 22 11 0 4
 Find AB if A= 33
and B=
22
0

11
4

33

 Find a matrix A such that A=


22 33 33 0
0

11 4
4 11 22

 In which order should the matrices A1,A2 and A3 --where A1 is 30 x 20, A2 is 20 x 40


and A3 is 40 x 10, all with integer entries– be multiplied to use the least number of
multiplications of integers?
 Let A= 1 0and 1 B= 0 1 1
Find 1 1 0 1 0 1
a)A ∨ B 0 0 1 1 0 1
b) A ∧ B
c) A ⊙ B

100

You might also like