Docsity

Homework Set 3 Solutions - Algorithm Analysis and Design | CPSC 5115G, Assignments of Computer Science

Toggle sidebar
bg1
CPSC 5115 Algorithm Analysis and Design
Homework Set 3 Due Friday, November 16, 2007
Homework key with answers and comments.
1. Find the greatest common divisor and least common multiple of the
following two pairs of numbers. Use Euclid’s Algorithm for GCD.
a) 105 and 231
b) 13 and 17
ANSWER: The least common multiple of two integers is related to their greatest common
divisor by a simple formula: LCM(M, N) = (MN) / GCD(M,N). Apply Euclid’s Algorithm
to get the GCD (Greatest Common Divisor).
a) GCD(231, 105) = GCD (105, 21) 231 = 2105 + 21
GCD(105, 21) = GCD (21, 0) = 21 105 = 521
LCM(231, 105) = (231105) / 21 = 2315 = 1,155.
b) GCD(17, 13) = GCD(13, 4) 17 = 113 + 4
GCD(13, 4) = GCD(4, 1) 13 = 34 + 1
GCD(4, 1) = GCD(1, 0) = 1 4 = 41 + 0
LCM(17, 13) = (1713) / 1 = 221.
NOTE: Each of 13 and 17 are prime numbers, so the pair must be relatively prime.
It follows immediately that their greatest common divisor is 1 and that their
product is their least common multiple. I just wanted to be complete.
2. Construct the shift table from Horspool’s Algorithm for the following string
TCCTATTCTT. This 10–character string from problem 2, section 7.2.
ANSWER: First view the pattern as an array of characters: P[0, 9].
Index 0 1 2 3 4 5 6 7 8 9
Char T C C T A T T C T T
M = 10, M – 1 = 9, M – 2 = 8. For J = 0 to 8 Do Table[P[J]] = 9 – J
J = 0 9 – J = 9 P[0] = “T” Table[“T”] = 9
J = 1 9 – J = 8 P[1] = “C” Table[“C”] = 8
J = 2 9 – J = 7 P[2] = “C” Table[“C”] = 7
J = 3 9 – J = 6 P[3] = “T” Table[“T”] = 6
J = 4 9 – J = 5 P[4] = “A” Table[“A”] = 5 Table[“A”] = 5
J = 5 9 – J = 4 P[5] = “T” Table[“T”] = 4
J = 6 9 – J = 3 P[6] = “T” Table[“T”] = 3
J = 7 9 – J = 2 P[7] = “C” Table[“C”] = 2 Table[“C”] = 2
J = 8 9 – J = 1 P[8] = “T” Table[“T”] = 1 Table[“T”] = 1, but 0 is OK
All other characters have a shift value of 10, the length of the pattern.
pf3
pf4

Partial preview of the text

Download Homework Set 3 Solutions - Algorithm Analysis and Design | CPSC 5115G and more Assignments Computer Science in PDF only on Docsity!

CPSC 5115 Algorithm Analysis and Design

Homework Set 3 Due Friday, November 16, 2007

Homework key with answers and comments.

  1. Find the greatest common divisor and least common multiple of the following two pairs of numbers. Use Euclid’s Algorithm for GCD. a) 105 and 231 b) 13 and 17 ANSWER: The least common multiple of two integers is related to their greatest common divisor by a simple formula: LCM(M, N) = (MN) / GCD(M,N). Apply Euclid’s Algorithm to get the GCD (Greatest Common Divisor). a) GCD(231, 105) = GCD (105, 21) 231 = 2105 + 21 GCD(105, 21) = GCD (21, 0) = 21 105 = 5 21 LCM(231, 105) = (231105) / 21 = 2315 = 1,155. b) GCD(17, 13) = GCD(13, 4) 17 = 113 + 4 GCD(13, 4) = GCD(4, 1) 13 = 34 + 1 GCD(4, 1) = GCD(1, 0) = 1 4 = 41 + 0 LCM(17, 13) = (1713) / 1 = 221. NOTE: Each of 13 and 17 are prime numbers, so the pair must be relatively prime. It follows immediately that their greatest common divisor is 1 and that their product is their least common multiple. I just wanted to be complete.
  2. Construct the shift table from Horspool’s Algorithm for the following string TCCTATTCTT. This 10–character string from problem 2, section 7.2. ANSWER: First view the pattern as an array of characters: P[0, 9]. Index 0 1 2 3 4 5 6 7 8 9 Char T C C T A T T C T T M = 10, M – 1 = 9, M – 2 = 8. For J = 0 to 8 Do Table[P[J]] = 9 – J J = 0 9 – J = 9 P[0] = “T” Table[“T”] = 9 J = 1 9 – J = 8 P[1] = “C” Table[“C”] = 8 J = 2 9 – J = 7 P[2] = “C” Table[“C”] = 7 J = 3 9 – J = 6 P[3] = “T” Table[“T”] = 6 J = 4 9 – J = 5 P[4] = “A” Table[“A”] = 5 Table[“A”] = 5 J = 5 9 – J = 4 P[5] = “T” Table[“T”] = 4 J = 6 9 – J = 3 P[6] = “T” Table[“T”] = 3 J = 7 9 – J = 2 P[7] = “C” Table[“C”] = 2 Table[“C”] = 2 J = 8 9 – J = 1 P[8] = “T” Table[“T”] = 1 Table[“T”] = 1, but 0 is OK All other characters have a shift value of 10, the length of the pattern.
  1. Problem 1 in Exercises 7.3 of Levitin’s textbook. This is a problem on open hashing. The problem begins as follows: “For the input 30, 20, 56, 75, 31, 19 and hash function h(K) = K mod 11. ANSWER: 30 mod 11 = 8 75 mod 11 = 9 20 mod 11 = 9 31 mod 11 = 9 56 mod 11 = 1 19 mod 11 = 8 a) Here is the open hash table for the input in the order stated. How many comparisons does it take to find each key? 1 comparison: 20, 30, 56 2 comparisons: 19, 75 3 comparisons: 31 b) The largest number of key comparisons for a successful search is 3. This occurs for the key value 31. c) The average is (31 + 22 + 13) / 6 = (3 + 4 + 3) / 6 = 10/6 = 5/31..

To compute the number of searches for each key, we annotate the table by placing every key below the slot to which it first hashed. Here is the table. 0 1 2 3 4 5 6 7 8 9 10 31 56 19 30 20 75 56 30 20 19 75 31 This shows the number of comparisons required for each key. 1 comparison: 20, 30, 56 Key = 19 6 comparisons (Try slots 8, 9, 10, 0, 1, and 2) Key = 75 2 comparisons (Try slots 9 and 10) Key = 31 3 comparisons (Try slots 9, 10, and 0) b) The largest number of key comparisons in a successful search is 6 for key = 19. c) The average number is (31 + 6 + 2 + 3) / 6 = 14 / 6 = 7 / 32.33.

  1. This is a variant of the puzzle called “Birthday Paradox”. How many people should be in a room so that the chances are better than even that two of them will have been born in the same month? NOTE: For 13 or more people in the room, it is necessary that two or more were born in the same month. ANSWER: We handle this by finding the probability that everybody in the room has a different birth month and noting when this drops below 50%. Imagine an empty room and one person at a time entering the room. 1 person in room Probability = 12/12 = 1. Second person enters the room. There are 11 “unclaimed months” 2 people in room Probability = 11/12  0. Third person enters the room. There are 10 “unclaimed months” 3 people in room Probability = (11/12)(10/12)  0. Fourth person enters the room. There are 9 “unclaimed months” 4 people in room Probability = (11/12)(10/12)(9/12)  0. Fifth person enters the room. There are 8 “unclaimed months” 5 people in room Probability = (11/12)(10/12)(9/12)(8/12)  0. The probability that 12 people, chosen at random, have distinct birth months is given by (12!) / (12^12 ). Factor out a single 12 to get (11!) / (12^11 ). 11! = 39,916,800 and 12^11  7.43 1011 , so the probability is about 5.37 10 –5.