MA/CS 109 Third CS Homework Discussion Exercises You should form groups to analyze these problems during your assigned discussion periods, and hand in the solutions together with the homework problems below. You may collaborate on the discussion exercises problems with your group and hand in common solutions, but you MUST do the “homework exercises” by yourself. Problem 1 For this problem, I would like you to write an algorithm as detailed as you can make it (within reason) which specifies how to make change for an amount of money less than 1 dollar. Specifically: The Change Problem Input: A number N between 1 and 99 (inclusive), representing how many cents change is due. Output: Four numbers (Q,D,N,P) representing Q = how many quarters, D = how many dimes, N = how many nickels, and P = how many pennies, such that 1. The sum Q+D+N+P is as small as possible (give as few coins back as possible); and 2. N = 25Q + 10D + 5N + P (the change correctly amounts to what is due). Solution: We will use two operators in our algorithm: we indicate by “A/B” the result of dividing A by B and throwing away the fractional remainder (this is called “integer division” and is the whole number of times that B divides into A), e.g., 13/5 = 2 and 12/4 = 3; secondly, let A % B = remainder after dividing A by B a whole number of times, e.g., 13 % 5 = 3 and 12 % 4 = 0. Input: A number C between 0 and 99 inclusive representing the number of cents change due; Output: Four numbers (Q,D,N,P) representing the smallest number of coins to return as change. Step 1: Let Q = C/25; reset C to C % 25 and to go step 2; Step 2: Let D = C/10; reset C to C % 10 and to go step 3; Step 3: Let N = C/5 and let P = C % 5 and go to step 4; Step 4: Output (Q,D,N,P) Example: C = 97 cents => 3 Quarters, 2 Dimes, and 2 Pennies. The Partition Problem is as follows:
Input: A set S of integers of size N >= 2.
Output: A subset S’ of S such that the sum of the numbers in S’ is equal to the
sum of the numbers in S – S’; or report that “no such subset exists.”
Example 1: S = {1, 8, 5, 2 }
Output: S’ = { 8 }, S–S’ = {1, 5, 2}
Example 2: S = { 1, 5, 7, 3, 2 }
Output: S’ = {1, 5, 3 },
S–S’ = {7, 2}
Example 3: S = { 2, 4, 3, 6 }
Output: No such subset exists.
Here is a (very dumb) algorithm for this problem, based on a simple encoding of subsets using binary numbers: if S = { s1, s2, ...., sn } with N elements, then we can use N bits b1b2....bn to indicate whether each individual element is in the subset S’ or not: make bi ==1 if and only if si is in S’. For example, if S = { 1, 2, 3 }, then the subset {2, 3} would be represented by 011, and the whole set by 111. Essentially, we can think of 0 as “No” and 1 as “Yes” in answering the following question for each subset: Algorithm for the Partition Problem Input: A set of integers S = { s1, s2, ...., sn } with N elements. Step One: Let B = 0000...01 (an N-‐bit number equal to 1); Step Two: Let S’ = the subset of S indicated by B, and check if sum(S’) = sum(S—S’); if it is, print out the set S’; otherwise go to Step Three; Step Three: Add 1 to the binary number B; if the result is all 1s (the largest number representable in N bits), stop and print out “No such set exists”; otherwise, go back to Step Two with this new value of B. Problem 2 Now answer the following questions: (a) Run this algorithm for S = { 2, 3, 4 } and show all work (the subsets, the sums, and the result); Solution: Here are the subsets for S; we will show the binary number encoding the subset, then subsets of S on the left side of the partition “|” and the sums on the right side: 000 -‐ | 2, 3, 4 0 | 9 001 4 | 2, 3 4 | 5 010 3 | 2, 4 3 | 6 011 3, 4 | 2 7 | 2 100 2 | 3, 4 2 | 7 101 2, 4 | 3 6 | 3 110 2, 3 | 4 5 | 4 111 2, 3, 4 | -‐ 9 | 0 The result is “No such set exists”. (b) We did not check the empty set (B = all 0s) and the set S itself (B = all 1s) as solutions; why is this not necessary? Solution: Assuming all numbers are larger than 0, then it is impossible to have no numbers (0) equal to the sum of some numbers; if you allow negatives and 0, then you need to check only one of these possibilities. Since I did not specify “only numbers larger than 0” I will accept any answer to this problem. (c) If you have an input set with N elements, how many subsets will you have to check in the worst case? Solution: There are 2N binary numbers with N bits, each corresponding to a subset of S; if you do not check the first and last, then you get 2N – 2. (d) Suppose you could check 1,000,000 subsets a second on a computer and you decide to try to solve a problem with 100 numbers. How long would it take in the worst case? [Hint: express your answer very approximately using the “Short Scale” from http://en.wikipedia.org/wiki/Names_of_large_numbers.] Solution: Using Google, 2100 = about 1.3 * 1030 and if we divide by 106, we get 1.3 * 1024 seconds. There are about 60*60*24*365.25 = 31,557,600 seconds in a year, and dividing we get about 4.1 * 1016 years or about 41 quadrillion years. This is about 3 million times longer than the universe has been in existence! (e) (Optional: Just think about it) This is an exceedingly dumb algorithm: it just enumerates every possible solution, no matter how silly, and checks each one. Can you think of a better way to do it? Solution: In half a century, no one has come up with a smarter way of doing this problem….. Homework Exercises These should be completed by yourself. Problem 3 (ECC - Error-Correcting Codes)
This problem is based on the lecture slides posted over the weekend on the topic
of Friday’s lecture (www.cs.bu.edu/fac/snyder/cs109/).
(a) Encode the following four-bit message using the ECC: 1101.
Solution: 1101100
(b) Suppose you receive the following (corrupted) message which has been
encoding using the ECC, what is the corrected message? 1010101.
Solution: 1000101
(b) Supposing you wanted to send a message consisting of 75 bits, using a
version of the ECC, but with additional parity bits. How many parity bits
would you need for 75 message bits?
Solution: You would need 7 bits, since then you can encode 27 = 128 possibilities,
or 120 message bits plus 7 parity bits.
Problem 4
The ECC we examined can correct any one bit error, and hence can also detect
one-bit errors. But what happens with two bit errors? In this problem we will
explore how the ECC deals with this.
(a) Try several examples of two bit errors using the example from lecture:
0011100 (correct), e.g., decode 1111100, 0000100, 1011110, 0101100, all
with two bit errors. What happens? Is it possible to detect two bit errors or
does this get confused with something else? Describe what you think is
happening.
Solution: Any bit pattern in the ECC that is not correct will be interpreted as a
one-bit error that can be corrected. Therefore it can not detect two-bit errors.
(b) Suppose we use the ECC method and then the error-detecting code, that is,
we encode a 4-bit string using the ECC method (for a total of 7 bits), and
then we add one more parity bit, which makes the parity for all 8 bits even.
Try several 1-bit errors, and several 2-bit errors, and describe how this
solves the problem: the resulting code can correct all 1-bit errors, and
detect (but not correct) all 2 bits errors. Explain what conditions would
indicate a 1-bit error (and its location) and what conditions would indicate
a 2-bit error.
Solution: Any 1 or 2 bit error will be interpreted by the ECC as a 1-bit error to be
corrected; any odd number of errors will be interpreted by the error-detecting
code, and any even number of errors will go undetected. Therefore:
No errors: Both codes will agree there is no error;
One-bit error: The ECC and the error-detecting code will agree there has been an
error, and the ECC will correct it (correctly!);
Two-bit error: The ECC will interpret this as a one-bit error and try to correct, but
the error-detecting code will report that there has been no error.
Therefore, then the two codes agree, go with their decision, and allow the
correction of the 1-bit error; when they disagree, assume there has occurred a 2-bit
error, and simply detect the error and ignore the correction.
Problem 5
Consider the following simple algorithm for sorting a list of N numbers into
ascending order:
Step One: Scan from the left for the first adjacent pair of numbers which
are out of order (the left one is larger than the right one); if no such pair
exists, then stop, else go to step two.
Step Two: Swap the two numbers in the list and go back to step one.
Example (out of order pairs found are underlined, after swapping shown in red): 42715 -‐> 24715 -‐> 24175 -‐> 21475 -‐> 12475 -‐> 12357 done! Suppose our measure of the complexity of this algorithm is the number of times we must swap two numbers. (a) Sort the list 42917, show every step, and count the number of swaps. Solution: (each line after the first is a swap) 42917 24917 24197 21497 12497 12479 Five swaps! (b) What is minimum number of swaps possible for any input list of size N? Give an example of such a “best case” list with 5 elements. Solution: The minimum is 0, which occurs when the list is already sorted, e.g., 12345. (c) What kind of input list would cause the maximum number of swaps? Play around with a few examples of 2, 3, 4, and 5-‐element lists, and give “worst-‐
case” examples (they are very similar) and the number of swaps required for each of these four lengths. Solution: The worst case is when the list is sorted in the reverse order, e.g., 21 12 (1 swap) 321 231 213 123 (3 swaps) 4321 3421 3241 2341 2314 2134 1234 (6 swaps) 54321 45321 43521 34521 34251 32451 23451 23415 23145 21345 12335 (10 swaps) The number of swaps is in fact N2/2 – N/2, which is the same we discovered in lecture for the “iterative sorting” algorithm.
© Copyright 2026 Paperzz