T4-PP1: Problem 2 on page 66 (Ch.2) of [SG]. -- SOLUTION SKETCH
When n=0 (empty list), Algorithm 2.10 will fail in Step 3,
when it tries to set the value of "largest so far" to A1.
Note that when n=0,
A1 is undefined (has no value).
To fix this problem, insert an if-statement between Steps 2 and 3 as follows:
if (n=0) then stop else Step 3. ....
T4-PP1: Problem 3 on page 66 (Ch.2) of [SG]. -- DIY
T4-PP2(a): Probs 16 on p76 (Ch2) of [SG]. --- SOLUTION SKETCH
(a) If n ≤ 2, then the test would be true, so the loop
would be executed. In fact, the test would never become
false. Thus, the algorithm would either loop forever, or
generate an error when referring to an invalid A[i] value
(when i is very large and it "overshoots" the valid range of
the array A).
If n > 2, then the test would be false the first time through,
so the loop would be skippped and A[1] would be reported as
the "largest value". [Of course, in this case, the algorithm
would be wrong since it reports the wrong value most of the time.]
(b) The loop will exit when i=n, and so the algorithm would
find the largest of the first n-1 elements. It would not look at
the last element (A[n]).
(c) If n=2, the loop would execute once, comparing A[1] and A[2].
Then the loop would exit on the next pass, return the larger of
the first two values. For any other value of n, the loop would be
skipped, reporting A[1] as the largest value.
T4-PP2(b): Probs 17 on p76 (Ch2) of [SG]. --- SOLUTION SKETCH
(a) The algorithm would still find the largest element in
the list, but if the largest were not unique then the algorithm
would find the last occurrence of the largest element in the list.
(b) The algorithm would find the smallest element in the list.
Note: The relational operators in the condition are very important, and care must be taken
to choose the correct one, since changing them can drastically change
the output of the algorithm.
|
So, try it out.) |
Optional FUN: For fun, I add one tricky algorithm that is a bit of a brain twister (esp. the version on the left). To help explain it, I added in an equivalent, but simpler version on the right:
|
|
Question:
Can you now try a cyclic shift of n elements:
a[1] <-- a[2] <-- a[3] <-- ... <-- a[n-1] <-- a[n] <-- a[1] (back to a[1])?
Algorithm ReverseArray(A,n); begin k <-- 1; while (k <= (n div 2)) do swap (A[k], A[n+1-k]); k <-- k + 1; endwhile end; |
Algorithm Repeated-Doubling(n): 1. begin 3. k <-- 1; 2. Count <--0; 4. while (k < n) do 6. k <-- k * 2; // double it 5. Count <-- Count + 1; 7. EndWhile 8. answer <-- Count; 9. end; |
Algorithm Repeated-Halving(n): 1. begin 3. k <-- n; 2. Count <--0; 4. while (k > 0) do 6. k <-- k div 2; // half it 5. Count <-- Count + 1; 7. EndWhile 8. answer <-- Count; 9. end; |
(a) [RH: Repeated Halving] Start with a number n.
Repeatedly "divide by two
(and throw away the remainder)" until we reach 0.
How many steps will you take?
(b) [RD: Repeated Doubling] Start with the number 1.
Repeatedly multiply by two until we get a number greater
than or equal to n.
How many steps will you take?
|
|
|
Algorithm Count-List(D, n, X); (* Counts the #times X appears in array D[1..n] *) begin Count <-- 0; k <-- 1; while (k <= n) do if D[k] = X then Count <-- Count + 1 endif k <-- k + 1; endwhile return Count end; |
Algorithm Find-Min(X,n); (* Assumes given list is stored in X[1..n] *) 1. begin 2. MinSoFar <-- X[1]; 3. Location <-- 1; k <-- 2; 5. while (k <= n) do 6. if (X[k] < MinSoFar) then 7. MinSoFar <-- X[k]; 8. Location <-- k; 9. endif 10. k <-- k + 1; 11. endwhile 12. return MinSoFar, Location 13.end. |
(a) Here, we can make effective use of the high-level primitive defined in T4-Q3 Find-Min(X,n). Instead of giving the whole algorithm, I will discuss the ideas behind several different methods.
One idea is to first find the smallest, then
"eliminate" it from consideration.
But, how to "algorithmically" "eliminate" the smallest from consideration?
Another idea is to find smallest and 2nd-smallest simultaneously.
(Most of you did this!)
Be careful how you initialize Min-sf and Min2-sf.
Also, think about how you update them: (2 comparisons for each element)