1. [10] The text showed how to interchange the values of variables and , using the replacement notation, by setting , , . Show how the values of four variables can be rearranged to by a sequence of replacements. In other words, the new value of is to be the original value of , etc. Try to use the minimum number of replacements.
The sequence of replacements is as shown below.
2. [15] Prove that is always greater than at the beginning of step E1, except possibly the first time this step occurs.
We will prove that holds as a precondition to step E1, except possibly the first time this step occurs.
Proposition. holds as a precondition to step E1, except possibly the first time this step occurs.
Proof. Initially, before the first time step E1 occurs, either or .
First, we consider the case when . After E1, we have . If , the algorithm terminates by step E2, proving one variant of the first case. Otherwise, if , after the assignment of step E3, we have as a precondition to step E1, concluding the proof for the first case.
Second, we consider the case wehn . After E1, we have . If , then and the algorithm terminates by step E2, proving one variant of the second case. Otherwise, if , after the assignment of step E3, we have as a precondition to step E1, concluding the proof for the second case.
Therefore, having proved all possible cases, holds as a precondition to step E1, except possibly the first time this step occurs. □
3. [20] Change Algorithm E (for the sake of efficiency) so that all trivial replacement operations such as “” are avoided. Write this new algorithm in the style of Algorithm E, and call it Algorithm F.
Algorithm F (Efficient Euclid’s algorithm). Given two positive integers and , find their greatest common divisor, that is, the largest positive integer that evenly divides both and . For the sake of efficiency, all trivial replacement operations are avoided.
4. [16] What is the greatest common divisor of 2166 and 6099?
We will determine the greatest common divisor of 2166 and 6099 using iterations of Algorithm E.
1 | 2166 | 6099 | 2166 |
2 | 6099 | 2166 | 1767 |
3 | 2166 | 1767 | 399 |
4 | 1767 | 399 | 171 |
5 | 399 | 171 | 57 |
6 | 171 | 57 | 0 |
Therefore, the greatest common divisor of 2166 and 6099 is 57.
5. [12] Show that the “Procedure for Reading This Set of Books” that appears after the preface actually fails to be a genuine algorithm on at least three of our five counts! Also mention some differences in format between it and Algorithm E.
The “Procedure for Reading This Set of Books” that appears after the preface fails to be a genuine algorithm on at least three of five counts.
The procedure lacks finiteness, as it will not always terminate after a finite number of steps. In particular, when we reach step 18, we are to go back to step 3, causing us to repeat the procedure an infinite number of times.
The procedure lacks definiteness, as some steps are not precisely defined. As an example, consider step 10, which instructs us to report errors to the author.
For the procedure, the output is not clearly defined.
The procedure also lacks effectiveness, as some steps are not sufficiently basic and in principle can’t be done exactly and in a finite length of time. Consider steps 12 and 13 which instruct us to work on exercises in the book. In the case of exercises rated 50 or higher, research problems not yet solved satisfactorily, work on such exercises is clearly not bounded in time to be considered effective.
There are also differences in format. In particular: it is not headed by a label specifying it as an algorithm and with an identifying letter, such as Algorithm A; it is not accompanied by a brief description; step numbers are not preceded by the (missing) identifying letter, such as A1; each step is not introduced with a brief description in square brackets; parenthetical remarks do not appear to be limited to asserting invariant conditions; and the procedure is not terminated by a square symbol, such as .
6. [20] What is , the average number of times step E1 is performed when ?
We want to determine , the average number of times step E1 is performed when . It is sufficient to consider only those cases where . We count the steps for each case below.
1 | 1 | 5 | 1 |
2 | 5 | 1 | 0 |
1 | 2 | 5 | 2 |
2 | 5 | 2 | 1 |
3 | 2 | 1 | 0 |
1 | 3 | 5 | 3 |
2 | 5 | 3 | 2 |
3 | 3 | 2 | 1 |
4 | 2 | 1 | 0 |
1 | 4 | 5 | 4 |
2 | 5 | 4 | 1 |
3 | 4 | 1 | 0 |
1 | 5 | 5 | 0 |
The average number of steps is . That is, .
7. [M21] Suppose that is known and that is allowed to range over all positive integers; let be the average number of times that step E1 is executed in Algorithm E. Show that is well defined. Is in any way related to ?
Assume that the value of is known but is allowed to range over all positive integers. We want to know the average number of times, that step E1 of Algorithm E will be performed.
is well defined. For , the first iteration of the algorithm simply switches and (constituting one performance of step E1); thereafter, on average, additional steps are required. Otherwise, if , we only have a finite number of cases to consider for , which as , will contribute to the average.
Therefore, .
8. [M25] Give an “effective” formal algorithm for computing the greatest common divisor of positive integers and , by specifying , , , as in Eqs. (3). Let the input be represented by the string , that is, ’s followed by ’s. Try to make your solution as simple as possible. [Hint: Use Algorithm E, but instead of division in step E1, set , .]
First, we develop an algorithm that doesn’t rely on division to determine the greatest common divisor.
Algorithm G (Euclid’s algorithm without division). Given two positive integers and , find their greatest common divisor, without division.
Next, we express this algorithm formally, without any initial consideration of whether it is “effective” or not.
Let be the set of all ordered pairs ; all ordered triples ; all ordered quadruples , and ; and all singletons ; where and are positive integers and is a nonnegative integer. Let be the subset of all pairs , and let be the subset of all singletons . Let be defined as follows:
Lastly, we redefine this algorithm formally to ensure it is also “effective.”
Let be our alphabet, and . Given input , our algorithm will terminate with . Let , , , and be specified as follows:
Note | |||||
repeat if | |||||
(Note that here, denotes the empty string.)
9. [M30] Suppose that and are computational methods. For example, might stand for Algorithm E as in Eqs. (2) except that and are restricted in magnitude, and might stand for a computer program implementation of Algorithm E. (Thus might be the set of all states of the machine, i.e., all possible configurations of its memory and registers; might be the definition of single machine actions; and might be the set of initial states, each including the program that determines the greatest common divisor as well as the particular values of and .)
Formulate a set-theoretic definition for the concept “ is a representation of ” or “ simulates .” This is to mean intuitively that any computation sequence of is mimicked by , except that might take more steps in which to do the computation and it might retain more information in its states. (We thereby obtain a rigorous interpretation of the statement, “Program is an implementation of Algorithm .”)
Let and be computational methods. We say “ is a representation of ” (or alternatively, “ simulates ”) when the following properties hold:
This can be demonstrated as follows. Suppose where: ; ; for positive integers , , , and nonnegative integer ; and is defined as follows:
And suppose
where: ;
;
for positive
integers ,
,
,
, and nonnegative
integer ;
and
is defined as follows:
We then let so that , and . We additionally define:
and or otherwise. Observe that as required:
Case | |||||||
Hence, “ is a a representation of .”
________________________________________________________________________________
For an alternative approach to simulation, see R. W. Floyd and R. Beigel, The Language of Machines (Computer Science Press, 1994), Section 3.3.