Presentation is loading. Please wait.

Presentation is loading. Please wait.

© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 3 (Part 3): The Fundamentals: Algorithms, the.

Similar presentations


Presentation on theme: "© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 3 (Part 3): The Fundamentals: Algorithms, the."— Presentation transcript:

1 © by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 3 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices Integers & Algorithms (Section 3.6)

2 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 2 Integers & Algorithms (3.6) Introduction –Algorithm is a set a procedures that handle arithmetic operations –Use binary representations to enter the world of computer arithmetic –Good illustration of the complexity of an algorithm –Role of the Euclidean algorithm –Base b expansion and modular exponentiation, important in cryptography

3 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 3 Integers & Algorithms (3.6) (cont.) 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 = a k b k + a k-1 b k-1 +…+ a 1 b + a 0, where k is a nonnegative integer, a 0, a 1, …,a k are nonnegative integers less than b, and a k  0. The representation of n given in theorem 1 is known as the base b expansion of n.

4 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 4 Integers & Algorithms (3.6) (cont.) –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*2 8 + 0*2 7 + 1*2 6 + 0*2 5 + 1*2 4 + 1*2 3 + 1*2 2 + 1*2 1 + 1*2 0 = 351

5 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 5 Integers & Algorithms (3.6) (cont.) –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*16 4 + 10*16 3 + 14*16 2 + 0*16 1 + 11*16 0 = (175627) 10 Each hexadecimal digit can be represented using 4 bits. (1110 0101) 2 = (E5) 16 (E 16 = (1110) 2 ; 5 16 = (0101) 2 ) –Bytes: bit strings of length 8 represented by two hexadecimal digits.

6 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 6 Integers & Algorithms (3.6) (cont.) 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

7 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 7 Integers & Algorithms (3.6) (cont.) Example: Find the hexadecimal expansion of (11 1110 1011 1100) 2 and the binary expansion of (A8D) 16. Solution: 1.Form blocks of 4 digits and add zeros at the start of the leftmost block if necessary. They are: 0011, 1110, 1011 and 1100. 2.Convert each binary block into decimal (0011) 2 = 3 16 ; (1110) 2 = E 16 ; (1011) 2 = B 16 ; (1100) 2 = C 16 Therefore, (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: A 16 = (1010) 2, 8 16 = (1000) 2, D 16 = (1101) 2

8 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 8 Integers & Algorithms (3.6) (cont.) 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 = (a n-1 a n-2 … a 1 a 0 ) 2, b = (b n-1 b n-2 … b 1 b 0 ) 2 a + b? (sum of two n-bits numbers) First: a 0 + b 0 = c 0 * 2 + s 0 where s 0 = rightmost bit in the binary expansion of a + b c 0 = carry = 0 or 1. Second: a 1 + b 1 + c 0 = c 1 * 2 + s 1 where s 1 = next bit of the sum expansion c 1 = carry … a + b = (s n s n-1 s n-2 … s 1 s 0 ) 2

9 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 9 Integers & Algorithms (3.6) (cont.) Example: Add a = (1110) 2 and b = (1011) 2 Solution: a 0 + b 0 = 0 + 1 = 0 * 2 + 1  c 0 = 0, s 0 = 1 a 1 + b 1 + c 0 = 1 + 1 + 0 = 1 * 2 + 0  c 1 = 1, s 1 = 0 a 2 + b 2 + c 1 = 1 + 0 + 1 = 1 * 2 + 0  c 2 = 1, s 2 = 0 a 3 + b 3 + c 2 = 1 + 1 + 1 = 1 * 2 + 1  c 3 = 1, s 3 = 1  s 4 = c 3 = 1  a + b = (1 1001) 2 11 1110= a 1011= b 1 001= c

10 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 10 Integers & Algorithms (3.6) (cont.) –Goal 2: Compute the product since b = b n-1 b n-2 …b 1 b 0 then: ab = a (b 0 2 0 + b 1 2 1 + … + b n-1 2 n-1 ) = a (b 0 2 0 ) + a(b 1 2 1 ) + … + a(b n-1 2 n-1 ) Using this latter equation, we can compute a * b. ab j = a if b j = 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.

11 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 11 Integers & Algorithms (3.6) (cont.) –Example: Find the product of a = (110) 2 and b = (101) 2 Solution: ab 0 2 0 = (110) 2 * 1 * 2 0 = (110) 2 ab 1 2 1 = (110) 2 * 0 * 2 1 = (0000) 2 (4 zeros-shift) ab 2 2 2 = (110) 2 * 1 * 2 2 = (11000) 2 add 2-zero bits

12 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 12 Integers & Algorithms (3.6) (cont.) To find the product, we need to perform: (110) 2 + (0000) 2 + (11000) 2 ab = (11110) 2 Other exercise: check for example: 7 * 5 = 35 is true in binary expansion. 11000 00000 00110 11110

13 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 13 Integers & Algorithms (3.6) (cont.) The Euclidean algorithm –Goal: Compute the great common divisor (gcd) of two integers avoiding the prime factorization of each number which is time-consuming.

14 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 14 Integers & Algorithms (3.6) (cont.) –Illustration: Find gcd(91, 287) 1.Divide 287 (the larger) by 91 (the smaller) 287 = 91 * 3 + 14 Any divisor of 91 and 287 must be a divisor of 287 – 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) 2.Next divide 91 by 14 to obtain: 91 = 14 * 6 + 7 Using the same reasoning, our next step is to: 3.Divide 14 by 7: 14 = 7 * 2 since 7/14  gcd(14,7) = 7. But since gcd(287,91) = gcd(91,14) = gcd (14,7) = 7, we have solved the problem

15 CSE 504, Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices 15 Integers & Algorithms (3.6) (cont.) 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 + 248 414 = 248 * 1 + 166 248 = 166 * 1 + 82 166 = 82 * 2 + 2 82 = 2 * 41 Therefore: gcd(414,662) = 2 (since 2 is the last nonzero remainder !)


Download ppt "© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 3 (Part 3): The Fundamentals: Algorithms, the."

Similar presentations


Ads by Google