Upload
hyatt-lambert
View
52
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Enough Mathematical Appetizers!. Let us look at something more interesting: Algorithms. Algorithms. What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a problem. - PowerPoint PPT Presentation
Citation preview
Discrete Structures 1
Enough Mathematical Enough Mathematical Appetizers! Appetizers!
Let us look at something more interesting:Let us look at something more interesting:
AlgorithmsAlgorithms
Discrete Structures 2
Algorithms Algorithms What is an algorithm?What is an algorithm?
An algorithm is a finite set of precise An algorithm is a finite set of precise instructions for performing a computation or instructions for performing a computation or for solving a problem.for solving a problem.
This is a rather vague definition. You will get to This is a rather vague definition. You will get to know a more precise and mathematically know a more precise and mathematically useful definition when you attend CMSC441. useful definition when you attend CMSC441.
But this one is good enough for now…But this one is good enough for now…
Discrete Structures 3
Algorithms Algorithms Properties of algorithms:Properties of algorithms:
• InputInput from a specified set, from a specified set,• OutputOutput from a specified set (solution), from a specified set (solution),• DefinitenessDefiniteness of every step in the computation, of every step in the computation,• CorrectnessCorrectness of output for every possible input, of output for every possible input,• FinitenessFiniteness of the number of calculation steps, of the number of calculation steps,• EffectivenessEffectiveness of each calculation step and of each calculation step and• GeneralityGenerality for a class of problems. for a class of problems.
Discrete Structures 4
Algorithm ExamplesAlgorithm ExamplesWe will use a pseudocode to specify We will use a pseudocode to specify algorithms.algorithms.Example:Example: an algorithm that finds the an algorithm that finds the maximum element in a finite sequencemaximum element in a finite sequence
procedureprocedure max(a max(a11, a, a22, …, a, …, ann: integers): integers)max := amax := a11forfor i := 2 i := 2 toto n n
ifif max < a max < aii thenthen max := a max := aii{max is the largest element}{max is the largest element}
Discrete Structures 5
Algorithm ExamplesAlgorithm ExamplesAnother example:Another example: a linear search algorithm, a linear search algorithm, that is, an algorithm that linearly searches a that is, an algorithm that linearly searches a sequence for a particular element.sequence for a particular element.procedureprocedure linear_search(x: integer; a linear_search(x: integer; a11, a, a22, …, , …, aann: : integers) integers)i := 1i := 1while while (i (i n and x n and x a aii))
i := i + 1i := i + 1ifif i i n n thenthen location := i location := ielseelse location := 0 location := 0{location is the subscript of the term that {location is the subscript of the term that equals x, or is zero if x is not found}equals x, or is zero if x is not found}
Discrete Structures 6
Algorithm ExamplesAlgorithm Examples
If the terms in a sequence are ordered, a If the terms in a sequence are ordered, a binary search algorithm is more efficient than binary search algorithm is more efficient than linear search.linear search.
The binary search algorithm iteratively The binary search algorithm iteratively restricts the relevant search interval until it restricts the relevant search interval until it closes in on the position of the element to be closes in on the position of the element to be located.located.
Discrete Structures 7
Algorithm ExamplesAlgorithm Examples
a c d f g h j l m o p r s u v x za c d f g h j l m o p r s u v x z
binary search for the letter ‘j’binary search for the letter ‘j’
center center elementelement
search search intervalinterval
Discrete Structures 8
Algorithm ExamplesAlgorithm Examples
a c d f g h j l m a c d f g h j l m o p r s u v x zo p r s u v x z
binary search for the letter ‘j’binary search for the letter ‘j’
center center elementelement
search search intervalinterval
Discrete Structures 9
Algorithm ExamplesAlgorithm Examples
a c d f ga c d f g h j l m h j l m o p r s u v x zo p r s u v x z
binary search for the letter ‘j’binary search for the letter ‘j’
center center elementelement
search search intervalinterval
Discrete Structures 10
Algorithm ExamplesAlgorithm Examples
a c d f ga c d f g h j h j l ml m o p r s u v x zo p r s u v x z
binary search for the letter ‘j’binary search for the letter ‘j’
center center elementelement
search search intervalinterval
Discrete Structures 11
Algorithm ExamplesAlgorithm Examples
a c d f ga c d f g h h j j l ml m o p r s u v x zo p r s u v x z
binary search for the letter ‘j’binary search for the letter ‘j’
center center elementelement
search search intervalinterval
found !found !
Discrete Structures 12
Algorithm ExamplesAlgorithm Examplesprocedureprocedure binary_search(x: integer; a binary_search(x: integer; a11, a, a22, …, , …, aann: : integers) integers)i := 1 i := 1 {i is left endpoint of search interval}{i is left endpoint of search interval}j := n j := n {j is right endpoint of search interval}{j is right endpoint of search interval} while while (i < j)(i < j)beginbegin
m := m := (i + j)/2(i + j)/2ifif x > a x > amm thenthen i := m + 1 i := m + 1 {increase the i} {increase the i} elseelse j := m j := m {decrease the j} {decrease the j}
endendifif x = a x = aii thenthen location := i location := ielseelse location := 0 location := 0{location is the subscript of the term that equals {location is the subscript of the term that equals x, or is zero if x is not found}x, or is zero if x is not found}
Discrete Structures 13
ComplexityComplexity
In general, we are not so much interested in In general, we are not so much interested in the time and space complexity for small the time and space complexity for small inputs.inputs.
For example, while the difference in time For example, while the difference in time complexity between linear and binary search complexity between linear and binary search is meaningless for a sequence with n = 10, it is meaningless for a sequence with n = 10, it is gigantic for n = 2is gigantic for n = 23030..
Discrete Structures 14
ComplexityComplexityFor example, let us assume two algorithms A For example, let us assume two algorithms A and B that solve the same class of problems.and B that solve the same class of problems.The time complexity of A is 5,000n, the one The time complexity of A is 5,000n, the one for B is for B is 1.11.1nn for an input with n elements. for an input with n elements.For n = 10, A requires 50,000 steps, but B For n = 10, A requires 50,000 steps, but B only 3, so B seems to be superior to A.only 3, so B seems to be superior to A.For n = 1000, however, A requires 5,000,000 For n = 1000, however, A requires 5,000,000 steps, while B requires 2.5steps, while B requires 2.510104141 steps. steps.
Discrete Structures 15
ComplexityComplexityThis means that algorithm B cannot be used This means that algorithm B cannot be used for large inputs, while algorithm A is still for large inputs, while algorithm A is still feasible.feasible.
So what is important is the So what is important is the growthgrowth of the of the complexity functions.complexity functions.
The growth of time and space complexity with The growth of time and space complexity with increasing input size n is a suitable measure increasing input size n is a suitable measure for the comparison of algorithms. for the comparison of algorithms.
Discrete Structures 16
ComplexityComplexityComparison:Comparison: time complexity of algorithms A and B time complexity of algorithms A and B
Algorithm AAlgorithm A Algorithm BAlgorithm BInput SizeInput Sizenn1010
1001001,0001,000
1,000,0001,000,000
5,000n5,000n50,00050,000500,000500,000
5,000,0005,000,00051051099
1.11.1nn33
2.5102.510414113,78113,781
4.8104.8104139241392
Discrete Structures 17
The Growth of FunctionsThe Growth of FunctionsThe growth of functions is usually described The growth of functions is usually described using the using the big-O notationbig-O notation..
Definition:Definition: Let f and g be functions from the Let f and g be functions from the integers or the real numbers to the real integers or the real numbers to the real numbers.numbers.We say that f(x) is O(g(x)) if there are We say that f(x) is O(g(x)) if there are constants C and k such thatconstants C and k such that|f(x)| |f(x)| C|g(x)| C|g(x)|whenever x > k.whenever x > k.
Discrete Structures 18
The Growth of FunctionsThe Growth of FunctionsWhen we analyze the growth of When we analyze the growth of complexity complexity functionsfunctions, f(x) and g(x) are always positive. , f(x) and g(x) are always positive. Therefore, we can simplify the big-O Therefore, we can simplify the big-O requirement torequirement tof(x) f(x) C Cg(x) whenever x > k.g(x) whenever x > k.
If we want to show that f(x) is O(g(x)), we only If we want to show that f(x) is O(g(x)), we only need to find need to find oneone pair (C, k) (which is never pair (C, k) (which is never unique).unique).
Discrete Structures 19
The Growth of FunctionsThe Growth of FunctionsThe idea behind the big-O notation is to The idea behind the big-O notation is to establish an establish an upper boundaryupper boundary for the growth for the growth of a function f(x) for of a function f(x) for large large x.x.This boundary is specified by a function g(x) This boundary is specified by a function g(x) that is usually much that is usually much simplersimpler than f(x). than f(x).We accept the constant C in the requirementWe accept the constant C in the requirementf(x) f(x) C Cg(x) whenever x > k,g(x) whenever x > k,because because C does not grow with x.C does not grow with x.We are only interested in large x, so it is OK ifWe are only interested in large x, so it is OK iff(x) > Cf(x) > Cg(x) for x g(x) for x k. k.
Discrete Structures 20
The Growth of FunctionsThe Growth of FunctionsExample:Example:Show that f(x) = xShow that f(x) = x22 + 2x + 1 is O(x + 2x + 1 is O(x22).).
For x > 1 we have:For x > 1 we have:xx22 + 2x + 1 + 2x + 1 x x22 + 2x + 2x22 + x + x22
xx22 + 2x + 1 + 2x + 1 4x 4x22
Therefore, for C = 4 and k = 1:Therefore, for C = 4 and k = 1:f(x) f(x) Cx Cx22 whenever x > k. whenever x > k.
f(x) is O(xf(x) is O(x22).).
Discrete Structures 21
The Growth of FunctionsThe Growth of Functions
Question: If f(x) is O(xQuestion: If f(x) is O(x22), is it also O(x), is it also O(x33)?)?
Yes.Yes. x x33 grows faster than x grows faster than x22, so x, so x33 grows also grows also faster than f(x).faster than f(x).
Therefore, we always have to find the Therefore, we always have to find the smallestsmallest simple function g(x) for which f(x) is simple function g(x) for which f(x) is O(g(x)). O(g(x)).
Discrete Structures 22
The Growth of FunctionsThe Growth of Functions““Popular” functions g(n) arePopular” functions g(n) aren log n, 1, 2n log n, 1, 2nn, n, n22, n!, n, n, n!, n, n33, log n, log n
Listed from slowest to fastest growth:Listed from slowest to fastest growth:• 11• log nlog n• nn• n log nn log n• nn22
• nn33
• 22nn
• n!n!
Discrete Structures 23
The Growth of FunctionsThe Growth of Functions
A problem that can be solved with polynomial A problem that can be solved with polynomial worst-case complexity is called worst-case complexity is called tractabletractable..
Problems of higher complexity are called Problems of higher complexity are called intractable.intractable.
Problems that no algorithm can solve are called Problems that no algorithm can solve are called unsolvableunsolvable..
You will find out more about this in Algorithms You will find out more about this in Algorithms Course.Course.
Discrete Structures 24
Useful Rules for Big-OUseful Rules for Big-OFor any For any polynomialpolynomial f(x) = a f(x) = annxxnn + a + an-1n-1xxn-1n-1 + … + + … + aa00, where a, where a00, a, a11, …, a, …, ann are real numbers, are real numbers,f(x) is O(xf(x) is O(xnn).).
If fIf f11(x) is O(g(x) is O(g11(x)) and f(x)) and f22(x) is O(g(x) is O(g22(x)), then (x)), then (f(f11 + f + f22)(x) is O(max(g)(x) is O(max(g11(x), g(x), g22(x)))(x)))
If fIf f11(x) is O(g(x)) and f(x) is O(g(x)) and f22(x) is O(g(x)), then(x) is O(g(x)), then(f(f11 + f + f22)(x) is O(g(x)).)(x) is O(g(x)).
If fIf f11(x) is O(g(x) is O(g11(x)) and f(x)) and f22(x) is O(g(x) is O(g22(x)), then (x)), then (f(f11ff22)(x) is O(g)(x) is O(g11(x) g(x) g22(x)).(x)).
Discrete Structures 25
Complexity ExamplesComplexity ExamplesWhat does the following algorithm compute?What does the following algorithm compute?procedureprocedure who_knows(a who_knows(a11, a, a22, …, a, …, ann: integers): integers)m := 0m := 0forfor i := 1 to n-1 i := 1 to n-1
forfor j := i + 1 to n j := i + 1 to nifif |a |aii – a – ajj| > m | > m thenthen m := |a m := |aii – a – ajj||
{m is the maximum difference between any two {m is the maximum difference between any two numbers in the input sequence}numbers in the input sequence}Comparisons: n-1 + n-2 + n-3 + … + 1Comparisons: n-1 + n-2 + n-3 + … + 1 = (n – 1)n/2 = 0.5n= (n – 1)n/2 = 0.5n22 – 0.5n – 0.5n
Time complexity is O(nTime complexity is O(n22).).
Discrete Structures 26
Complexity ExamplesComplexity ExamplesAnother algorithm solving the same problem:Another algorithm solving the same problem:procedureprocedure max_diff(a max_diff(a11, a, a22, …, a, …, ann: integers): integers)min := a1min := a1max := a1max := a1forfor i := 2 to n i := 2 to n
ifif a aii < min < min thenthen min := a min := aiielseelse if a if aii > max > max thenthen max := a max := aii
m := max - minm := max - minComparisons: 2n - 2Comparisons: 2n - 2Time complexity is O(n).Time complexity is O(n).
Discrete Structures 28
Introduction to Number TheoryIntroduction to Number Theory
Number theory is about Number theory is about integersintegers and their and their properties.properties.
We will start with the basic principles ofWe will start with the basic principles of• divisibility,divisibility,• greatest common divisors,greatest common divisors,• least common multiples, andleast common multiples, and• modular arithmeticmodular arithmetic
and look at some relevant algorithms. and look at some relevant algorithms.
Discrete Structures 29
DivisionDivisionIf a and b are integers with a If a and b are integers with a 0, we say that 0, we say that a a dividesdivides b if there is an integer c so that b = b if there is an integer c so that b = ac.ac.
When a divides b we say that a is a When a divides b we say that a is a factorfactor of b of b and that b is a and that b is a multiplemultiple of a. of a.
The notation The notation a | ba | b means that a divides b. means that a divides b.
We write We write a a χχ b b when a does not divide b when a does not divide b(see book for correct symbol).(see book for correct symbol).
Discrete Structures 30
Divisibility TheoremsDivisibility TheoremsFor integers a, b, and c it is true thatFor integers a, b, and c it is true that• if a | b and a | c, then a | (b + c)if a | b and a | c, then a | (b + c) Example:Example: 3 | 6 and 3 | 9, so 3 | 15. 3 | 6 and 3 | 9, so 3 | 15.• if a | b, then a | bc for all integers cif a | b, then a | bc for all integers c Example:Example: 5 | 10, so 5 | 20, 5 | 30, 5 | 40, … 5 | 10, so 5 | 20, 5 | 30, 5 | 40, …• if a | b and b | c, then a | cif a | b and b | c, then a | c Example:Example: 4 | 8 and 8 | 24, so 4 | 24. 4 | 8 and 8 | 24, so 4 | 24.
Discrete Structures 31
PrimesPrimesA positive integer p greater than 1 is called prime A positive integer p greater than 1 is called prime if the only positive factors of p are 1 and p.if the only positive factors of p are 1 and p.Note: 1 is not primeNote: 1 is not primeNote: 2 is the only even primeNote: 2 is the only even primeA positive integer that is greater than 1 and is A positive integer that is greater than 1 and is not prime is called composite.not prime is called composite.The fundamental theorem of arithmetic:The fundamental theorem of arithmetic:Every positive integer can be written Every positive integer can be written uniquelyuniquely as the as the product of primesproduct of primes, where the prime , where the prime factors are written in order of increasing size.factors are written in order of increasing size.
Discrete Structures 32
PrimesPrimesExamples:Examples:
3·53·548 =48 =17 =17 =100 100 ==512 512 ==515 515 ==28 =28 =
15 =15 =2·2·2·2·3 = 22·2·2·2·3 = 244·3·317172·2·5·5 = 22·2·5·5 = 222·5·522
2·2·2·2·2·2·2·2·2 = 22·2·2·2·2·2·2·2·2 = 299
5·1035·1032·2·72·2·7
Discrete Structures 33
PrimesPrimesIf n is a composite integer, then n has a prime If n is a composite integer, then n has a prime divisor less than or equal .divisor less than or equal .
This is easy to see: if n is a composite integer, it This is easy to see: if n is a composite integer, it must have at least two prime divisors. Let the must have at least two prime divisors. Let the largest two be plargest two be p11 and p and p22. Then p. Then p11pp22 <= n. <= n.
pp11 and p and p22 cannot both be greater than cannot both be greater than , because then p, because then p11pp22 > n. > n.
n
n
Discrete Structures 34
The Division AlgorithmThe Division AlgorithmLet Let aa be an integer and be an integer and dd a positive integer. a positive integer.Then there are unique integers Then there are unique integers qq and and rr, with , with 0 0 r < d r < d, such that , such that a = dq + ra = dq + r..
In the above equation, In the above equation, • dd is called the divisor, is called the divisor, • aa is called the dividend, is called the dividend, • qq is called the quotient, and is called the quotient, and • rr is called the remainder. is called the remainder.
Discrete Structures 35
The Division AlgorithmThe Division AlgorithmExample:Example:
When we divide 17 by 5, we haveWhen we divide 17 by 5, we have
17 = 517 = 53 + 2.3 + 2.
• 17 is the dividend,17 is the dividend,• 5 is the divisor,5 is the divisor,• 3 is called the quotient, and3 is called the quotient, and• 2 is called the remainder.2 is called the remainder.
Discrete Structures 36
The Division AlgorithmThe Division AlgorithmAnother example:Another example:What happens when we divide -11 by 3 ?What happens when we divide -11 by 3 ?Note that the remainder cannot be negative.Note that the remainder cannot be negative.-11 = 3-11 = 3(-4) + 1.(-4) + 1.
• -11 is the dividend,-11 is the dividend,• 3 is the divisor,3 is the divisor,• -4 is called the quotient, and-4 is called the quotient, and• 1 is called the remainder.1 is called the remainder.
Discrete Structures 37
Greatest Common DivisorsGreatest Common DivisorsLet a and b be integers, not both zero.Let a and b be integers, not both zero.The largest integer d such that d | a and d | b is The largest integer d such that d | a and d | b is called the called the greatest common divisorgreatest common divisor of a and b. of a and b.The greatest common divisor of a and b is The greatest common divisor of a and b is denoted by gcd(a, b).denoted by gcd(a, b).Example 1:Example 1: What is gcd(48, 72) ? What is gcd(48, 72) ?The positive common divisors of 48 and 72 are The positive common divisors of 48 and 72 are 1, 2, 3, 4, 6, 8, 12, 16, and 24, so gcd(48, 72) = 1, 2, 3, 4, 6, 8, 12, 16, and 24, so gcd(48, 72) = 24. 24. Example 2:Example 2: What is gcd(19, 72) ? What is gcd(19, 72) ?The only positive common divisor of 19 and 72 isThe only positive common divisor of 19 and 72 is1, so gcd(19, 72) = 1. 1, so gcd(19, 72) = 1.
Discrete Structures 38
Greatest Common DivisorsGreatest Common DivisorsUsing prime factorizations:Using prime factorizations:
a = pa = p11aa1 1 p p22
aa2 2 … p… pnnaan n , b = p, b = p11
bb1 1 p p22bb2 2 … p… pnn
bbn n ,,where pwhere p11 < p < p22 < … < p < … < pnn and a and aii, b, bii NN for 1 for 1 i i n n
gcd(a, b) = pgcd(a, b) = p11min(amin(a11, b, b1 1 )) p p22
min(amin(a22, b, b2 2 )) … p… pnnmin(amin(ann, b, bn n ))
Example:Example:a = 60 a = 60 = =
2222 3 311 5 511 b = 54 b = 54 = =
2211 3 333 5 500 gcd(a, b) gcd(a, b) = =
2211 3 311 5 50 0 = 6 = 6
Discrete Structures 39
Relatively Prime IntegersRelatively Prime IntegersDefinition:Definition:Two integers a and b are Two integers a and b are relatively primerelatively prime if if gcd(a, b) = 1.gcd(a, b) = 1.
Examples:Examples:Are 15 and 28 relatively prime?Are 15 and 28 relatively prime?Yes, gcd(15, 28) = 1.Yes, gcd(15, 28) = 1.Are 55 and 28 relatively prime?Are 55 and 28 relatively prime?Yes, gcd(55, 28) = 1.Yes, gcd(55, 28) = 1.Are 35 and 28 relatively prime?Are 35 and 28 relatively prime?No, gcd(35, 28) = 7.No, gcd(35, 28) = 7.
Discrete Structures 40
Relatively Prime IntegersRelatively Prime IntegersDefinition:Definition:The integers aThe integers a11, a, a22, …, a, …, ann are are pairwise pairwise relatively primerelatively prime if gcd(a if gcd(aii, a, ajj) = 1 whenever 1 ) = 1 whenever 1 i < j i < j n. n.Examples:Examples:Are 15, 17, and 27 pairwise relatively prime?Are 15, 17, and 27 pairwise relatively prime?No, because gcd(15, 27) = 3.No, because gcd(15, 27) = 3.Are 15, 17, and 28 pairwise relatively prime?Are 15, 17, and 28 pairwise relatively prime?Yes, because gcd(15, 17) = 1, gcd(15, 28) = 1 Yes, because gcd(15, 17) = 1, gcd(15, 28) = 1 and gcd(17, 28) = 1.and gcd(17, 28) = 1.
Discrete Structures 41
Least Common MultiplesLeast Common MultiplesDefinition:Definition:The The least common multipleleast common multiple of the positive of the positive integers a and b is the smallest positive integers a and b is the smallest positive integer that is divisible by both a and b.integer that is divisible by both a and b.We denote the least common multiple of a and We denote the least common multiple of a and b by lcm(a, b).b by lcm(a, b).Examples:Examples:lcm(3, 7) lcm(3, 7) ==
2121lcm(4, 6) lcm(4, 6) ==
1212lcm(5, 10) lcm(5, 10) ==
1010
Discrete Structures 42
Least Common MultiplesLeast Common MultiplesUsing prime factorizations:Using prime factorizations:
a = pa = p11aa1 1 p p22
aa2 2 … p… pnnaan n , b = p, b = p11
bb1 1 p p22bb2 2 … p… pnn
bbn n ,,where pwhere p11 < p < p22 < … < p < … < pnn and a and aii, b, bii NN for 1 for 1 i i n n
lcm(a, b) = plcm(a, b) = p11max(amax(a11, b, b1 1 )) p p22
max(amax(a22, b, b2 2 )) … p… pnnmax(amax(ann, b, bn n ))
Example:Example:a = 60 a = 60 = =
2222 3 311 5 511 b = 54 b = 54 = =
2211 3 333 5 500 lcm(a, b) lcm(a, b) = =
2222 3 333 5 51 1 = 4275 = 540 = 4275 = 540
Discrete Structures 43
GCD and LCMGCD and LCM
a = 60 a = 60 = =
2222 3 311 5 511
b = 54 b = 54 = =
2211 3 333 5 500
lcm(a, b) lcm(a, b) = =
2222 3 333 5 51 1 = 540 = 540gcd(a, b) gcd(a, b) = =
2211 3 311 5 50 0 = 6 = 6
Theorem: ab Theorem: ab ==
gcd(a,b)lcm(a,gcd(a,b)lcm(a,b)b)
Discrete Structures 44
Modular ArithmeticModular ArithmeticLet a be an integer and m be a positive integer.Let a be an integer and m be a positive integer.We denote by We denote by a mod ma mod m the remainder when a the remainder when a is divided by m.is divided by m.
Examples:Examples:
9 mod 4 9 mod 4 ==
119 mod 3 9 mod 3 ==
009 mod 10 9 mod 10 ==
99-13 mod 4 -13 mod 4 ==
33
Discrete Structures 45
CongruencesCongruencesLet a and b be integers and m be a positive Let a and b be integers and m be a positive integer. We say that integer. We say that a is congruent to b a is congruent to b modulo mmodulo m if if m divides a – b.m divides a – b.
We use the notation We use the notation a a b (mod m) b (mod m) to indicate to indicate that a is congruent to b modulo m.that a is congruent to b modulo m.
In other words:In other words:a a b (mod m) if and only if b (mod m) if and only if a mod m = b mod a mod m = b mod mm. .
Discrete Structures 46
CongruencesCongruencesExamples:Examples:Is it true that 46 Is it true that 46 68 (mod 11) ? 68 (mod 11) ?Yes, because 11 | (46 – 68).Yes, because 11 | (46 – 68).Is it true that 46 Is it true that 46 68 (mod 22)? 68 (mod 22)?Yes, because 22 | (46 – 68).Yes, because 22 | (46 – 68).For which integers z is it true that z For which integers z is it true that z 12 (mod 10)? 12 (mod 10)? z-12 | 10 or z-12 = 10x for some integer xz-12 | 10 or z-12 = 10x for some integer xIt is true for any zIt is true for any z{…,-28, -18, -8, 2, 12, 22, 32, {…,-28, -18, -8, 2, 12, 22, 32, …}…}
Theorem:Theorem: Let m be a positive integer. The integers Let m be a positive integer. The integers a and b are congruent modulo m if and only if there a and b are congruent modulo m if and only if there is an integer k such that a = b + km.is an integer k such that a = b + km.
Discrete Structures 47
CongruencesCongruencesTheorem:Theorem: Let m be a positive integer. Let m be a positive integer. If a If a b (mod m) and c b (mod m) and c d (mod m), then d (mod m), then a + c a + c b + d (mod m) and ac b + d (mod m) and ac bd (mod m). bd (mod m).Proof:Proof: We know that a We know that a b (mod m) and c b (mod m) and c d (mod m) d (mod m) implies that there are integers s and t with implies that there are integers s and t with b = a + sm and d = c + tm. b = a + sm and d = c + tm. Therefore,Therefore,b + d = (a + sm) + (c + tm) = (a + c) + m(s + t) b + d = (a + sm) + (c + tm) = (a + c) + m(s + t) andandbd = (a + sm)(c + tm) = ac + m(at + cs + stm).bd = (a + sm)(c + tm) = ac + m(at + cs + stm).Hence, a + c Hence, a + c b + d (mod m) and ac b + d (mod m) and ac bd (mod bd (mod m).m).
Discrete Structures 48
CongruencesCongruencesTheorem:Theorem: Let m be a positive integer. a Let m be a positive integer. a b (mod b (mod m) iff a mod m = b mod m.m) iff a mod m = b mod m.Proof:Proof: Let a = mq1 + r1, and b = mq2 + r2.Let a = mq1 + r1, and b = mq2 + r2.Only if part:Only if part: a mod m = b mod m a mod m = b mod m r1 = r2, therefore r1 = r2, therefore
a – b = m(q1 – q2), and a a – b = m(q1 – q2), and a b (mod m). b (mod m).If part:If part: a a b (mod m) implies b (mod m) implies a – b = mqa – b = mq mq1 + r1 – (mq2 + r2) = mqmq1 + r1 – (mq2 + r2) = mq r1 – r2 = m(q – q1 + q2).r1 – r2 = m(q – q1 + q2).Since 0 Since 0 r1, r2 r1, r2 m, 0 m, 0 |r1 - r2| |r1 - r2| m. The only m. The only multiple in that range is 0. multiple in that range is 0. Therefore r1 = r2, and a mod m = b mod m. Therefore r1 = r2, and a mod m = b mod m.
Discrete Structures 49
The Euclidean Algorithm The Euclidean Algorithm The The Euclidean AlgorithmEuclidean Algorithm finds the finds the greatest greatest common divisorcommon divisor of two integers a and b. of two integers a and b. For example, if we want to find gcd(287, 91), For example, if we want to find gcd(287, 91), we we dividedivide 287 by 91: 287 by 91:287 = 91287 = 913 + 143 + 14We know that for integers a, b and c,We know that for integers a, b and c,if a | b and a | c, then a | (b + c).if a | b and a | c, then a | (b + c).Therefore, any divisor (including their gcd) of Therefore, any divisor (including their gcd) of 287 and 91 must also be a divisor of 287 - 91287 and 91 must also be a divisor of 287 - 913 3 = 14.= 14.Consequently, gcd(287, 91) = gcd(14, 91).Consequently, gcd(287, 91) = gcd(14, 91).
Discrete Structures 50
The Euclidean Algorithm The Euclidean Algorithm In the next step, we divide 91 by 14:In the next step, we divide 91 by 14:91 = 1491 = 146 + 76 + 7This means that gcd(14, 91) = gcd(14, 7).This means that gcd(14, 91) = gcd(14, 7).
So we divide 14 by 7:So we divide 14 by 7:14 = 714 = 72 + 02 + 0We find that 7 | 14, and thus gcd(14, 7) = 7.We find that 7 | 14, and thus gcd(14, 7) = 7.
Therefore, gcd(287, 91) = 7.Therefore, gcd(287, 91) = 7.
Discrete Structures 51
The Euclidean Algorithm The Euclidean Algorithm In In pseudocodepseudocode, the algorithm can be , the algorithm can be implemented as follows: implemented as follows:
procedureprocedure gcd(a, b: positive integers) gcd(a, b: positive integers)x := ax := ay := by := bwhilewhile y y 0 0beginbegin
r := x r := x modmod y yx := yx := yy := ry := r
endend {x is gcd(a, b)}{x is gcd(a, b)}
Discrete Structures 52
Representations of IntegersRepresentations of IntegersLet b be a positive integer greater than 1.Let b be a positive integer greater than 1.Then if n is a positive integer, it can be Then if n is a positive integer, it can be expressed expressed uniquelyuniquely in the form: in the form:
n = an = akkbbkk + a + ak-1k-1bbk-1k-1 + … + a + … + a11b + ab + a00,,
where k is a nonnegative integer,where k is a nonnegative integer,aa00, a, a11, …, a, …, akk are nonnegative integers less than b, are nonnegative integers less than b,and aand akk 0. 0.
Example for b=10:Example for b=10:859 = 8859 = 8101022 + 5 + 5101011 + 9 + 9101000
Discrete Structures 53
Representations of IntegersRepresentations of Integers
Example for b=2 (binary expansion):Example for b=2 (binary expansion):(10110)(10110)22 = 1 = 12244 + 1 + 12222 + 1 + 12211 = (22) = (22)1010
Example for b=16 (hexadecimal Example for b=16 (hexadecimal expansion):expansion):(we use letters A to F to indicate numbers 10 to (we use letters A to F to indicate numbers 10 to 15)15)(3A0F)(3A0F)1616 = 3 = 3161633 + 10 + 10161622 + 15 + 15161600 = (14863) = (14863)1010
Discrete Structures 54
Representations of IntegersRepresentations of IntegersHow can we construct the base b expansion of an How can we construct the base b expansion of an integer n?integer n?First, divide n by b to obtain a quotient qFirst, divide n by b to obtain a quotient q00 and and remainder aremainder a00, that is,, that is,n = bqn = bq00 + a + a00, where 0 , where 0 a a00 < b. < b.The remainder aThe remainder a00 is the rightmost digit in the base is the rightmost digit in the base b expansion of n.b expansion of n.Next, divide qNext, divide q00 by b to obtain: by b to obtain:qq00 = bq = bq11 + a + a11, where 0 , where 0 a a11 < b. < b.aa11 is the second digit from the right in the base b is the second digit from the right in the base b expansion of n. Continue this process until you expansion of n. Continue this process until you obtain a quotient equal to zero.obtain a quotient equal to zero.
Discrete Structures 55
Representations of IntegersRepresentations of IntegersExample:Example: What is the base 8 expansion of (12345)What is the base 8 expansion of (12345)10 10 ??
First, divide 12345 by 8:First, divide 12345 by 8:12345 = 812345 = 81543 + 11543 + 11543 = 81543 = 8192 + 7192 + 7192 = 8192 = 824 + 024 + 024 = 824 = 83 + 03 + 03 = 83 = 80 + 30 + 3The result is: (12345)The result is: (12345)1010 = (30071) = (30071)88..
Discrete Structures 56
Representations of IntegersRepresentations of Integersprocedure procedure base_b_expansion(n, b: positive base_b_expansion(n, b: positive integers)integers)q := nq := nk := 0k := 0whilewhile q q 0 0beginbegin
aakk := q mod b := q mod bq := q := q/bq/bk := k + 1k := k + 1
endend {the base b expansion of n is (a{the base b expansion of n is (ak-1k-1 … a … a11aa00))b b }}
Discrete Structures 57
Addition of IntegersAddition of IntegersHow do we (humans) add two integers?How do we (humans) add two integers?
Example: Example: 75837583 + + 49324932
5511552211
111111 carrycarry
Binary expansions: Binary expansions: (1011)(1011)22 + + (1010)(1010)22
1100
carrycarry11
1100
11
11(( ))22
Discrete Structures 58
Addition of IntegersAddition of IntegersLet a = (aLet a = (an-1n-1aan-2n-2…a…a11aa00))22, b = (b, b = (bn-1n-1bbn-2n-2…b…b11bb00))2.2.
How can we How can we algorithmically algorithmically add these two binary add these two binary numbers?numbers?First, add their rightmost bits:First, add their rightmost bits:aa00 + b + b00 = c = c002 + s2 + s00,,where swhere s00 is the is the rightmost bitrightmost bit in the binary in the binary expansion of a + b, and cexpansion of a + b, and c00 is the is the carrycarry..Then, add the next pair of bits and the carry:Then, add the next pair of bits and the carry:aa11 + b + b1 1 + c+ c00 = c = c112 + s2 + s11,,where swhere s11 is the is the next bitnext bit in the binary expansion of in the binary expansion of a + b, and ca + b, and c11 is the carry. is the carry.
Discrete Structures 59
Addition of IntegersAddition of Integers
Continue this process until you obtain cContinue this process until you obtain cn-1n-1..
The leading bit of the sum is sThe leading bit of the sum is snn = c = cn-1n-1..
The result is:The result is:a + b = (sa + b = (snnssn-1n-1…s…s11ss00))22
Thus in each step, the new carry cThus in each step, the new carry cjj is the quotient is the quotient of the division of aof the division of ajj + b + bj j + c+ cj-1j-1 by 2 and the partial by 2 and the partial result sresult sjj is the remainder of that division. is the remainder of that division.
Discrete Structures 60
Addition of IntegersAddition of IntegersExample:Example:Add a = (1110)Add a = (1110)22 and b = (1011) and b = (1011)22..
aa00 + b + b00 = 0 + 1 = 0 = 0 + 1 = 02 + 1, so that c2 + 1, so that c00 = 0 and s = 0 and s00 = 1. = 1.aa11 + b + b1 1 + c+ c00 = 1 + 1 + 0 = 1 = 1 + 1 + 0 = 12 + 0, so c2 + 0, so c11 = 1 and s = 1 and s11 = 0. = 0.aa22 + b + b2 2 + c+ c11 = 1 + 0 + 1 = 1 = 1 + 0 + 1 = 12 + 0, so c2 + 0, so c22 = 1 and s = 1 and s22 = 0. = 0.aa33 + b + b3 3 + c+ c22 = 1 + 1 + 1 = 1 = 1 + 1 + 1 = 12 + 1, so c2 + 1, so c33 = 1 and s = 1 and s33 = 1. = 1.ss44 = c = c33 = 1. = 1.
Therefore, s = a + b = (11001)Therefore, s = a + b = (11001)22..
Discrete Structures 61
Addition of IntegersAddition of Integersprocedure procedure add(a, b: positive integers)add(a, b: positive integers)c := 0c := 0for j := 0 to n-1for j := 0 to n-1beginbegin
d := d := (a(ajj + b + bjj + c)/2 + c)/2ssjj := a := ajj + b + bjj + c – 2d + c – 2dc := dc := d
endendssnn := c := c{the binary expansion of the sum is (s{the binary expansion of the sum is (snnssn-1n-1……ss11ss00))22}}
And we shouldn’t miss And we shouldn’t miss something about matrices…something about matrices…
Discrete Structures 62
Discrete Structures 63
Definition 1Definition 1 A A matrixmatrix is a rectangular array of is a rectangular array of
numbers. A matrix with m rows and n numbers. A matrix with m rows and n columns is called an m columns is called an m xx n matrix. The n matrix. The plural of matrix is matrices. A matrix with plural of matrix is matrices. A matrix with the same number of rows as columns is the same number of rows as columns is called called squaresquare. Two matrices are . Two matrices are equalequal if if they have the same number of rows and they have the same number of rows and the same number of columns and the the same number of columns and the corresponding entries in every position corresponding entries in every position are equal.are equal.
Example:Example: The matrix is a 3 The matrix is a 3 XX 2 2 matrix.matrix.
31
0 21 1
Discrete Structures 64
Definition 2Definition 2
Let Let
The ith The ith rowrow of A is the 1 of A is the 1 xx n matrix [a n matrix [ai1i1, a, ai2i2, …, a, …, ainin]. ]. The jth The jth columncolumn of A is the n of A is the n xx 1 matrix 1 matrix
The (i, j)th The (i, j)th elementelement or or entryentry of A is the element a of A is the element aijij, that is, , that is,
the number in the ith row and jth column of A. A the number in the ith row and jth column of A. A convenient shorthand notation for expressing the matrix A convenient shorthand notation for expressing the matrix A is to write A = [ais to write A = [aijij], which indicates that A is the matrix with ], which indicates that A is the matrix with its (i, j)th element equal to aits (i, j)th element equal to aijij..
.
a...aa
a...aaa...aa
A
nn2nn1
n22221
n11211
a
a
a
nj
j2
j1
Discrete Structures 65
Matrix ArithmeticMatrix ArithmeticDefinition 3Definition 3
Let A = [aLet A = [aijij] and B = [b] and B = [bijij] be m ] be m xx n n matrices. The matrices. The sum of A and Bsum of A and B, denoted by , denoted by A + B, is the m A + B, is the m xx n matrix that has a n matrix that has aijij + b + bijij as its (i, j)th element. In other words, A + as its (i, j)th element. In other words, A + B = [aB = [aijij + b + bijij].].
Example:Example:
5 2 23 -1 -324 -4
21 10 3 -1 14 - 3
04 33 - 2210 - 1
Discrete Structures 66
Definition 4Definition 4
Let A be an m Let A be an m xx k matrix and B be a k k matrix and B be a k xx n n matrix. The matrix. The product of A and Bproduct of A and B, denoted , denoted by AB, is the m by AB, is the m xx n matrix with its (i, j)th n matrix with its (i, j)th entry equal to the sum of the products of entry equal to the sum of the products of the corresponding elements from the ith the corresponding elements from the ith row of A and the jth column of B. In other row of A and the jth column of B. In other words, if AB = [cwords, if AB = [cijij], then], then
CCijij = a = ai1i1bb1j 1j + a+ ai2i2bb2j 2j + … + a+ … + aikikbbkjkj..
Discrete Structures 67
Example: Let
Find AB if it is defined.
Solution: Since A is a 4 x 3 matrix and B is a 3 x 2 matrix, the product AB is defined and is a 4 x 2 matrix. To find the elements of AB, the corresponding elements of the rows of A and the columns of B are first multiplied and then these products are added. For instance, the element in the (3, 1)th position of AB is the sum of the products of the corresponding elements of the third row of A and the first column of B; namely 3 * 2 + 1 * 1 + 0 * 3 = 7. When all the elements of AB are computed, we see that
!!! Matrix multiplication is not commutative.
0 31 14 2
B and
20 20 1 31 1 240 1
A
2813 79 8414
AB
Discrete Structures 68
Example:Example: Let Let
Does AB = BA?Does AB = BA?
Solution:Solution: We find that We find that
Hence, AB BA.Hence, AB BA.
1 11 2
B and 1 21 1
A
23
4 3BA and
5 3 23
AB
Discrete Structures 69
Matrix chain multiplicationMatrix chain multiplication
Problem:Problem: How should the matrix-chain A How should the matrix-chain A11AA22……AAnn be computed using the fewest be computed using the fewest multiplication of integers, where Amultiplication of integers, where A11AA22…A…Ann are are mm1 1 xx m m22, m, m22 xx m m33, …, m, …, mnn xx m m n+1n+1 matrices matrices respectively and each has integers as respectively and each has integers as entries?entries?
Discrete Structures 70
Example:Example: A A11 = 30 = 30 xx 20 (30 rows and 20 columns) 20 (30 rows and 20 columns) A A22 = 20 = 20 xx 40 40 A A33 = 40 = 40 xx 10 10
Solution:Solution: 2 possibilities to compute A 2 possibilities to compute A11AA22AA331)1) AA1 1 (A(A22AA33))2)2) (A(A11AA22)A)A33
1) First A1) First A22AA33 requires 20 * 40 * 10 = 8000 multiplications requires 20 * 40 * 10 = 8000 multiplications A A11(A(A22AA33) requires 30 * 20 * 10 = 6000 multiplications) requires 30 * 20 * 10 = 6000 multiplicationsTotal: 14000 multiplications.Total: 14000 multiplications.
2) First A2) First A11AA22 requires 30 * 20 * 40 = 24000 multiplications requires 30 * 20 * 40 = 24000 multiplications (A (A11AA22)A)A3 3 requires 30 * 40 * 10 = 12000requires 30 * 40 * 10 = 12000Total: 36000 multiplications. Total: 36000 multiplications.
(1) is more efficient! (1) is more efficient!
Discrete Structures 71
Transposes and power matricesTransposes and power matrices
Definition 5Definition 5
The The identity matrix of order nidentity matrix of order n is the n is the n xx n n matrix matrix IIn n = [= [ijij], where ], where ijij = 1 if i = j and = 1 if i = j and ijij = 0 if i = 0 if i j. Hence j. Hence .
1 ...0 0
0 ... 10 0 ...0 1
In
IA ;A*...*A*AA n0
times r
r
Discrete Structures 72
Definition 6Definition 6
Let A = [aLet A = [aijij] be an m ] be an m xx n matrix. The n matrix. The transposetranspose of A, denoted A of A, denoted Att, is the n , is the n xx m m matrix obtained by interchanging the matrix obtained by interchanging the rows and the columns of A. In other rows and the columns of A. In other words, if Awords, if Att = [b = [bijij], then b], then bijij = a = aijij for for i = 1, 2, …, n and j = 1, 2, …, m.i = 1, 2, …, n and j = 1, 2, …, m.
Example:Example:
The transpose of the matrix is The transpose of the matrix is . .
65 4
3 21
6 35 24 1
Discrete Structures 73
Definition 7Definition 7
A square matrix A is called A square matrix A is called symmetricsymmetric if A if A = A= Att. Thus A = [a. Thus A = [aijij] is symmetric if a] is symmetric if aijij = a = ajiji for all i and j with 1 i n and 1 j n.for all i and j with 1 i n and 1 j n.
Example:Example: The matrix is The matrix is symmetric.symmetric.
0 10 10 10 1 1
Discrete Structures 74
Zero-one matricesZero-one matrices
– It is a matrix with entries that are 0 or 1. It is a matrix with entries that are 0 or 1. They represent discrete structures using They represent discrete structures using Boolean arithmetic.Boolean arithmetic.
– We define the following Boolean We define the following Boolean operations:operations:
otherwise0 1b or 1b if 1
bb
otherwise0 1bb if 1
bb
2121
2121
Discrete Structures 75
Definition 8Definition 8
Let A = [aLet A = [aijij] and B = [b] and B = [bijij] be m ] be m x x n zero-n zero-one matrices. Then the one matrices. Then the joinjoin of A and B is of A and B is the zero-one matrix with (i, j)th entry athe zero-one matrix with (i, j)th entry aijij bbijij. The join of A and B is denoted A B. . The join of A and B is denoted A B. The The meetmeet of A and B is the zero-one of A and B is the zero-one matrix with (i, j)th entry amatrix with (i, j)th entry aij ij b bijij. The meet . The meet of A and B is denoted by A B.of A and B is denoted by A B.
Discrete Structures 76
Example:Example: Find the join and meet of the zero- Find the join and meet of the zero-one matricesone matrices
Solution:Solution: We find that the joint of A and B is:We find that the joint of A and B is:
The meet of A and B is:The meet of A and B is:
.0 1 10 10
B ,0 10 10 1
A
.0 1 11 1 1
00 11 1001 100 1
BA
.0 10 00 0
00 11 1001 100 1
BA
Discrete Structures 77
Definition 9Definition 9
Let A = [aLet A = [aijij] be an m ] be an m xx k zero-one matrix k zero-one matrix and and B = [bB = [bijij] be a k ] be a k xx n zero-one matrix. Then n zero-one matrix. Then the the Boolean productBoolean product of A and B, denoted of A and B, denoted by A B, is the m by A B, is the m xx n matrix with (i, j)th n matrix with (i, j)th entry [centry [cijij] where] whereccijij = (a = (ai1i1 b b1j1j) (a) (ai2i2 b b2j2j) … (a) … (aikik b bkjkj).).
Discrete Structures 78
Example:Example: Find the Boolean product of A and Find the Boolean product of A and B, whereB, where
Solution:Solution:
.1 10 0 1 1
B ,0 110 0 1
A
0 1 11 10 0 1 1
000 10 110 100 0000 10 1
)10()01( )10()11( 0)(01)(1)11()00( )11()10( 0)(11)(0)10()01( )10()11( 0)(01)(1
BA
Discrete Structures 79
Algorithm The Boolean ProductAlgorithm The Boolean Product
procedureprocedure Boolean productBoolean product (A,B: zero-one (A,B: zero-one matrices)matrices)
forfor i := 1 i := 1 toto m mforfor j := 1 j := 1 toto n nbeginbegin
ccijij := 0 := 0
forfor q := 1 q := 1 toto k kccijij := c := cijij (a (aiqiq b bqjqj))
endend{C = [c{C = [cijij] is the Boolean product of A and B}] is the Boolean product of A and B}
Discrete Structures 80
Definition 10Definition 10Let A be a square zero-one matrix and let Let A be a square zero-one matrix and let r be a positive integer. The rth r be a positive integer. The rth Boolean Boolean powerpower of A is the Boolean product of r of A is the Boolean product of r factors of A. The rth Boolean product of A factors of A. The rth Boolean product of A is denoted by Ais denoted by A[r][r]. Hence. Hence
(This is well defined since the Boolean (This is well defined since the Boolean product of matrices is associative.) We product of matrices is associative.) We also define Aalso define A[0][0] to be I to be Inn..
.A...AAAAtimes r
]r[
Discrete Structures 81
Example: Let . Find A[n] for all positive integers n.
Solution: We find that
We also find that
Additional computation shows that
The reader can now see that A[n] = A[5] for all positive integers n with n 5.
0 1 100 110 0
A
.10 110 0 0 1 1
AAA ]2[
.1 1 110 11 1 1
AAA ,1 1 10 1 110 1
AAA ]3[[4]]2[]3[
.1 1 11 1 11 1 1
A ]5[