390 likes | 580 Views
Lecture 8. 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)
E N D
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
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.
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. 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) = 243572
Theorem 7: Let a and b be positive integers. Then ab = gcd(a,b).lcm(a.b).
Integers & Algorithms • Representations of integers • Theorem 1 Let b be a positive integer grater 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 nonnegative integer, a0, a1, …,ak are nonnegative integers less than b, and ak 0. The representation of n given in theorem 1 is known as the base b expansion of n.
Integers & Algorithms • Example: What is the decimal expansion of the integer that has (1 0101 1111)2 as its binary expansion? Solution: (1 0101 1111)2 = 1*28 + 0*27 + 1*26 + 0*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20 = 351
Integers & Algorithms • Hexadecimal expansions = base 16 expansion 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F The letters A through F represent the digits corresponding to the number 10 through 15. • Example: What is the decimal expansion of the hexadecimal expansion of (2AE0B)16? Solution: (2AE0B)16 = 2*164 + 10*163 + 14*162 + 0*161 + 11*160 = (175627)10 Each hexadecimal digit can be represented using 4 bits. (1110 0101)2 = (E5)16 (E16 = (1110)2 ; 516 = (0101)2) • Bytes: bit strings of length 8 represented by two hexadecimal digits.
Integers & Algorithms • Representations of integers (cont.) • Base conversion Example: Find the base 8 (or octal) expansion of (12345)10 Solution: First, divide 12345 by 8.12345 = 8 * 1543 + 1 1543 = 8 * 192 + 7 192 = 8 * 24 + 0 24 = 8 * 3 +0 3 = 8 * 0 + 3 (12345)10 = (30071)8
Integers & Algorithms Example: Find the hexadecimal expansion of (11 1110 1011 1100)2 and the binary expansion of (A8D)16. Solution: • Form blocks of 4 digits and add zeros at the start of the leftmost block if necessary. They are: 0011, 1110, 1011 and 1100. • Convert each binary block into decimal(0011)2 = 316; (1110)2 = E16; (1011)2 = B16; (1100)2 = C16Therefore, (11 1110 1011 1100)2 = (3EBC)16.The conversion of (A8D)16 into binary notation requires replacing each hexadecimal digit by a block of 4 binary digits. They are: A16 = (1010)2, 816 = (1000)2, D16 = (1101)2
Integers & Algorithms • Algorithms for integer operations • Goal 1: Compute the sum of two integers expressed with their binary expansions. The complexity will be based on the number of bits used.a = (an-1an-2 … a1a0)2, b = (bn-1bn-2 … b1b0)2 a + b? (sum of two n-bits numbers) First: a0 + b0 = c0* 2 + s0 where s0 = rightmost bit in the binary expansion of a + b c0 = carry = 0 or 1.Second: a1 + b1 + c0 = c1* 2 + s1 where s1 = next bit of the sum expansion c1 = carry … a + b = (snsn-1sn-2 … s1s0)2
Integers & Algorithms Example: Add a = (1110)2 and b = (1011)2 Solution: a0 + b0 = 0 + 1 = 0 * 2 + 1 c0 = 0, s0 = 1 a1 + b1 + c0 = 1 + 1 + 0 = 1 * 2 + 0 c1 = 1, s1 = 0 a2 + b2 + c1 = 1 + 0 + 1 = 1 * 2 + 0 c2 = 1, s2 = 0 a3 + b3 + c2 = 1 + 1 + 1 = 1 * 2 + 1 c3 = 1, s3 = 1 s4 = c3 = 1 a + b = (1 1001)2
Integers & Algorithms • Goal 2: Compute the product since b = bn-1bn-2…b1b0 then:ab = a (b020 + b121 + … + bn-12n-1) = a (b020) + a(b121) + … + a(bn-12n-1) Using this latter equation, we can compute a * b.abj = a if bj = 1 and 0 otherwise. Each time we multiply a term by 2, we shift its binary expansion one place to the left and add a zero at the tail end of the expansion.
Integers & Algorithms • Example: Find the product of a = (110)2 and b = (101)2 Solution:ab020 = (110)2 * 1 * 20 = (110)2 ab121 = (110)2 * 0 * 21 = (0000)2 (4 zeros-shift) ab222 = (110)2 * 1 * 22 = (11000)2 add 2-zero bits
Integers & Algorithms To find the product, we need to perform:(110)2 + (0000)2 + (11000)2ab = (11110)2Other exercise: check for example: 7 * 5 = 35 is true in binary expansion.
Integers & Algorithms • The Euclidean algorithm • Goal: Compute the great common divisor (gcd) of two integers avoiding the prime factorization of each number which is time-consuming.
Integers & Algorithms • Illustration: Find gcd(91, 287) • Divide 287 (the larger) by 91 (the smaller)287 = 91 * 3 + 14 • Any divisor of 91 and 287 must be a divisor of287 – 91 * 3 =14 (Theorem 1, part 1 & 2) • Any divisor of 91 and 14 divides 287 = 91 * 3 + 14 • gcd(91,287) = gcd(91,14) • Next divide 91 by 14 to obtain: 91 = 14 * 6 + 7Using the same reasoning, our next step is to: • Divide 14 by 7: 14 = 7 * 2since 7/14 gcd(14,7) = 7. But since gcd(287,91) = gcd(91,14) = gcd (14,7) = 7, we have solved the problem
Integers & Algorithms Lemma 1: Let a = bq + r, where a, b, q and r are integers. Then we can write: gcd(a,b) = gcd(b,r) • Example: Determine the gcd of 414 and 662 using the Euclidean algorithm. Solution: 662 = 414 * 1 + 248414 = 248 * 1 + 166 248 = 166 * 1 + 82166 = 82 * 2 + 282 = 2 * 41Therefore: gcd(414,662) = 2 (since 2 is the last nonzero remainder !) CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices
Definition 1 A matrix is a rectangular array of numbers. A matrix with m rows and n columns is called an m x n matrix. The plural of matrix is matrices. A matrix with the same number of rows as columns is called square. Two matrices are equal if they have the same number of rows and the same number of columns and the corresponding entries in every position are equal. • Example: The matrix is a 3X 2 matrix. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Definition 2 Let The ithrow of A is the 1 x n matrix [ai1, ai2, …, ain]. The jthcolumn of A is the n x 1 matrix The (i, j)thelement or entry of A is the element aij, that is, the number in the ith row and jth column of A. A convenient shorthand notation for expressing the matrix A is to write A = [aij], which indicates that A is the matrix with its (i, j)th element equal to aij.
Matrix Arithmetic • Definition 3 Let A = [aij] and B = [bij] be m x n matrices. The sum of A and B, denoted by A + B, is the m x n matrix that has aij + bij as its (i, j)th element. In other words, A + B = [aij + bij]. • Example: CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Definition 4 Let A be an m x k matrix and B be a k x n matrix. The product of A and B, denoted by AB, is the m x n matrix with its (i, j)th entry equal to the sum of the products of the corresponding elements from the ith row of A and the jth column of B. In other words, if AB = [cij], then Cij = ai1b1j + ai2b2j + … + aikbkj. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
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.
Example: Let Does AB = BA?Solution: We find that Hence, AB BA. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Matrix chain multiplication • Problem: How should the matrix-chain A1A2…An be computed using the fewest multiplication of integers, where A1A2…An are m1 x m2, m2x m3, …, mnx m n+1 matrices respectively and each has integers as entries? CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Example: A1 = 30 x 20 (30 rows and 20 columns) A2 = 20 x 40 A3 = 40 x 10 Solution: 2 possibilities to compute A1A2A3 • A1 (A2A3) • (A1A2)A3 1) First A2A3 requires 20 * 40 * 10 = 8000 multiplications A1(A2A3) requires 30 * 20 * 10 = 6000 multiplicationsTotal: 14000 multiplications. 2) First A1A2 requires 30 * 20 * 40 = 24000 multiplications (A1A2)A3 requires 30 * 40 * 10 = 12000Total: 36000 multiplications. (1) is more efficient! CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Transposes and power matrices • Definition 5 The identity matrix of order n is the n x n matrix In = [ij], where ij = 1 if i = j andij = 0 if i j. Hence CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Definition 6Let A = [aij] be an m x n matrix. The transpose of A, denoted At, is the n x m matrix obtained by interchanging the rows and the columns of A. In other words, if At = [bij], then bij = aij for i = 1, 2, …, n and j = 1, 2, …, m. • Example:The transpose of the matrix is . CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Definition 7 A square matrix A is called symmetric if A = At. Thus A = [aij] is symmetric if aij = aji for all i and j with 1 i n and 1 j n. • Example: The matrix is symmetric. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Zero-one matrices • It is a matrix with entries that are 0 or 1. They represent discrete structures using Boolean arithmetic. • We define the following Boolean operations: CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Definition 8 Let A = [aij] and B = [bij] be m x n zero-one matrices. Then the join of A and B is the zero-one matrix with (i, j)th entry aij bij. The join of A and B is denoted A B. The meet of A and B is the zero-one matrix with (i, j)th entry aij bij. The meet of A and B is denoted by A B. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Example: Find the join and meet of the zero-one matricesSolution: We find that the joint of A and B is:The meet of A and B is: CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Definition 9 Let A = [aij] be an m x k zero-one matrix and B = [bij] be a k x n zero-one matrix. Then the Boolean product of A and B, denoted by A B, is the m x n matrix with (i, j)th entry [cij] where cij = (ai1 b1j) (ai2 b2j) … (aik bkj). CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Example: Find the Boolean product of A and B, where Solution: CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Algorithm The Boolean Product procedureBoolean product (A,B: zero-one matrices) for i := 1 to mfor j := 1 to n begin cij := 0 for q := 1 to k cij := cij (aiq bqj) end {C = [cij] is the Boolean product of A and B} CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
Definition 10 Let A be a square zero-one matrix and let r be a positive integer. The rth Boolean power of A is the Boolean product of r factors of A. The rth Boolean product of A is denoted by A[r]. Hence (This is well defined since the Boolean product of matrices is associative.) We also define A[0] to be In. CSE 504, Chapter 2 (Part 4): The Fundamentals: Algorithms, the Integers & Matrices
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.