東京大学 工学系研究科 電気系工学専攻 2024年8月実施 問題4 情報工学II
Author
adj-matrix, 祭音Myyura
Description
I.
Problem summary
the official sample paper, Problem 4, pp. 21–23
Gates have two inputs unless shown otherwise.
- For the supplied circuit, , give its truth table and a minimal sum-of-products expression.
- A full adder takes and outputs sum bit and carry . Find minimal sum-of-products expressions. Implement the sum block using XOR gates and the carry block using NAND gates.
- Draw a circuit adding and . Only the two low result bits are output; use full adders and gates as needed.
- Draw the corresponding subtractor , using modulo .
- Draw a combined circuit : input selects addition and selects subtraction. Reduce both the number of full adders and the number of gates.
The top-left circuit in the following drawing gives the connections for Question (1); the other panels show implementations requested in (2).
II.
Consider the following algorithm , which overwrites an input array of distinct non-negative integers to sort its components in ascending order:
- for or , do nothing.
- for , do nothing if , and overwrite as , otherwise.
- for , select an element of by using a function as . Let be an array consisting of all the elements in being smaller than , and be an array consisting of all the elements in being greater than . Overwrite as . Then, recursively apply to the subarrays and in as and , respectively.
Answer the following questions.
(1) Fig. 8 exemplifies the operation of for an array when and the order of the elements in and inherits their order in . Draw the operation of for by following the diagram in Fig. 8.
(2) Suppose that an array contains each integer from 1 to 9, once each. Also, assume that and the order of the elements in and inherits their order in . Obtain the array that maximizes the number of recursive calls of and has the largest value evaluated as a 9-digit integer. Also obtain the array that minimizes the number of recursive calls of and has the largest value evaluated as a 9-digit integer. Note that the value of evaluated as a 9-digit integer is 123456789.
(3) Assume that . For an array with length , obtain the order of worst-case time complexity by supplementing the intermediate steps of its derivation. In the derivation, evaluate the order of the time complexity in terms of comparisons, while ignoring the complexity of the other operations.
(4) Program 1 is an implementation of the algorithm in the C programming language. Describe the codes that should be in the blanks, [A], [B], and [C].
(5) Suppose that is a function that returns an element selected from uniformly and randomly. Answer the order of the average time complexity of as a function of . In addition, describe its reason in detail.
Fig. 8
(6,9,3,5,1,7)
|
(3,5,1) -- 6 - (9,7)
| | |
(1) - 3 - (5) | (7,9)
|____|____| | |
| | |
(1,3,5) -- 6 - (7,9)
|
(1,3,5,6,7,9)
/* Program1 */
void swap(int X[], int i, int j){
int tmp = X[i]; X[i] = X[j]; X[j] = tmp;
}
int partition (int X[], int left, int right) {
int pivot = X[left];
int i = right;
for (int j = right; j >= left+1; j--) {
if (X[j] >= pivot) {
[A]
i--;
}
}
[B]
return i;
}
void f(int X[], int left, int right) {
if (left < right) {
int pivotpos = partition(X, left, right);
[C]
}
}
int main (void) {
int X[10] = {9, 6, 1, 7, 2, 3, 4, 5, 0, 8};
f(X, 0, sizeof(X) / sizeof(X[0]) - 1);
return 0;
}
题目描述
第二部分定义递归排序算法 。输入是由 个互不相同的非负整数组成的数组 : 时不操作; 时按升序交换; 时由 选枢轴,把小于 的元素组成 、大于 的元素组成 ,将数组改写为 ,再分别递归排序 。
- 当 ,且划分后各元素保持原相对次序时,仿照图 8,完整画出 的递归划分与最终排序过程。
- 恰含整数 至 各一次,仍取首元素为枢轴并保持相对次序。求使递归调用次数最多、且作为九位整数时数值最大的数组 ;再求使递归调用次数最少且九位整数值最大的数组 。
- 当 时,以元素比较次数为准,补出递推推导的中间步骤并求长度为 的数组在最坏情况下的时间复杂度阶。
- 补全给定 C 程序中
partition的 [A]、[B] 和递归函数f的 [C],使程序实现上述划分与排序。 - 若 从 中均匀随机选取枢轴,给出 的平均时间复杂度阶,并详细说明随机划分、递归树高度和每层工作量如何导出该结论。
Kai
I.
(1)
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 0 |
Expanding and applying the consensus theorem gives
Both two-literal implicants are essential: the input requires the first and requires the second.
(2)
The sum is odd parity: , requiring two XOR gates. A NAND-only implementation of the carry is shown above. With , its intermediate nets are
This uses six two-input NAND gates. A three-input NAND, if permitted, could directly combine instead.
(3), (4), (5)
For , the low full adder receives ; its carry feeds the high full adder . For , invert both bits and set the initial carry to . In both circuits, discard the final carry and output only .
For , form for , and set the initial carry to . The resulting two-full-adder circuit uses two XOR gates:
There is a trade-off if full adders and individual gates are counted separately. Since the high carry is unused, another realization is
Use two XOR gates and one AND gate to form , then one full adder with inputs , taking only its sum. This uses one full adder and three gates. Replacing that full adder by two XOR gates gives a realization with no full adders and five gates. These constructions give upper bounds on component counts. Comparing the two designs requires a cost convention for full adders versus gates.
II.
(1)
(6,8,4,5,2,3,1,9,7)
|
(4,5,2,3,1) -- 6 -- (8,9,7)
| | |
(2,3,1) --- 4 - (5) | (7) - 8 - (9)
| | | | |____|____|
(1) - 2 - (3) | | | |
|____|____| | | | (7,8,9)
| | | | |
(1,2,3) | | | |
|_______|____| | |
| | |
(1,2,3,4,5) | |
|_______|_______|
|
(1,2,3,4,5,6,7,8,9)
(2)
The worst case is when the partition is as unbalanced as possible.
Since we want the digits at the start of the array to be as large as possible.
In addition, each integer from 1 to 9 is one each.
Therefore, the worst case is 987654321, i.e. .
The following diagram is one minimum-call case, although it is not the largest such array: (from bottom to top)
(1,2,3,4,5,6,7,8,9)
_______|_________
| | |
(1,2,3,4) 5 (6,7,8,9)
_____|____ | _____|____
| | | | | | |
(1,2) - 3 - (4) | (6,7) - 8 - (9)
| | |
(3,4,2,1) - 5 --- (8,9,7,6)
|
(5,8,9,7,6,3,4,2,1)
Let be the minimum number of invocations on an input of length . Then
This gives , , and . Lexicographic maximization subject to these optimal splits chooses pivots , then , then , and places the larger child elements first while preserving each child's order. Hence
i.e. 968735421.
(Note to readers: The C code provided in the question is unstable; it will swap the Pivot to the middle and swap the elements that were originally at the end to the beginning)
(3)
In the worst case, the time complexity
Since is constant, therefore
i.e. .
(4)
- A:
swap(X, i, j); - B:
swap(X, left, i); - C:
f(X, left, pivotpos-1); f(X, pivotpos+1, right);
(5)
The average comparison count is .
Order the keys by rank. A pair with ranks is compared exactly when the first pivot chosen from the interval is one of its two endpoints, which has probability . The special case of two keys also makes exactly one comparison. Therefore
where . Equivalently, the expectation satisfies the recurrence with partition cost and uniformly distributed pivot rank.