跳到主要内容

東京大学 工学系研究科 電気系工学専攻 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.

  1. For the supplied circuit, o=(x+y)(y+z)o=(\overline x+\overline y)(y+z), give its truth table and a minimal sum-of-products expression.
  2. A full adder takes (x,y,i)(x,y,i) and outputs sum bit zz and carry cc. Find minimal sum-of-products expressions. Implement the sum block A1A_1 using XOR gates and the carry block A2A_2 using NAND gates.
  3. Draw a circuit SS adding X=x1x0X=x_1x_0 and Y=y1y0Y=y_1y_0. Only the two low result bits (z0,z1)(z_0,z_1) are output; use full adders and gates as needed.
  4. Draw the corresponding subtractor DD, using XY=X+(y1y0+01)X-Y=X+(\overline{y_1}\,\overline{y_0}+01) modulo 44.
  5. Draw a combined circuit SDSD: input w=0w=0 selects addition and w=1w=1 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).

Given logic circuit and full-adder implementations

II.

Consider the following algorithm f(X)f(X), which overwrites an input array X=(x1,x2,,xn)X = (x_1, x_2, \cdots, x_n) of nn distinct non-negative integers to sort its components in ascending order:

  1. for n=0n = 0 or n=1n = 1, do nothing.
  2. for n=2n = 2, do nothing if x1<x2x_1 < x_2, and overwrite XX as X=(x2,x1)X = (x_2, x_1), otherwise.
  3. for n>2n > 2, select an element pp of XX by using a function h(X)h(X) as p=h(X)p = h(X). Let X1X_1 be an array consisting of all the elements in XX being smaller than pp, and X2X_2 be an array consisting of all the elements in XX being greater than pp. Overwrite XX as X=(X1,p,X2)X = (X_1, p, X_2). Then, recursively apply ff to the subarrays X1X_1 and X2X_2 in XX as f(X1)f(X_1) and f(X2)f(X_2), respectively.

Answer the following questions.

(1) Fig. 8 exemplifies the operation of ff for an array X=(6,9,3,5,1,7)X = (6,9,3,5,1,7) when h(X)=x1h(X) = x_1 and the order of the elements in X1X_1 and X2X_2 inherits their order in XX. Draw the operation of ff for X=(6,8,4,5,2,3,1,9,7)X = (6,8,4,5,2,3,1,9,7) by following the diagram in Fig. 8.

(2) Suppose that an array XX contains each integer from 1 to 9, once each. Also, assume that h(X)=x1h(X) = x_1 and the order of the elements in X1X_1 and X2X_2 inherits their order in XX. Obtain the array XmaxX_{\max} that maximizes the number of recursive calls of f(X)f(X) and has the largest value evaluated as a 9-digit integer. Also obtain the array XminX_{\min} that minimizes the number of recursive calls of f(X)f(X) and has the largest value evaluated as a 9-digit integer. Note that the value of X=(1,2,3,4,5,6,7,8,9)X = (1,2,3,4,5,6,7,8,9) evaluated as a 9-digit integer is 123456789.

(3) Assume that h(X)=x1h(X) = x_1. For an array XX with length nn, 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 f(X)f(X) in the C programming language. Describe the codes that should be in the blanks, [A], [B], and [C].

(5) Suppose that h(X)h(X) is a function that returns an element pp selected from XX uniformly and randomly. Answer the order of the average time complexity of f(X)f(X) as a function of nn. 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;
}

题目描述

第二部分定义递归排序算法 f(X)f(X)。输入是由 nn 个互不相同的非负整数组成的数组 X=(x1,,xn)X=(x_1,\ldots,x_n)n1n\le1 时不操作;n=2n=2 时按升序交换;n>2n>2 时由 p=h(X)p=h(X) 选枢轴,把小于 pp 的元素组成 X1X_1、大于 pp 的元素组成 X2X_2,将数组改写为 (X1,p,X2)(X_1,p,X_2),再分别递归排序 X1,X2X_1,X_2

  1. h(X)=x1h(X)=x_1,且划分后各元素保持原相对次序时,仿照图 8,完整画出 X=(6,8,4,5,2,3,1,9,7)X=(6,8,4,5,2,3,1,9,7) 的递归划分与最终排序过程。
  2. XX 恰含整数 1199 各一次,仍取首元素为枢轴并保持相对次序。求使递归调用次数最多、且作为九位整数时数值最大的数组 XmaxX_{\max};再求使递归调用次数最少且九位整数值最大的数组 XminX_{\min}
  3. h(X)=x1h(X)=x_1 时,以元素比较次数为准,补出递推推导的中间步骤并求长度为 nn 的数组在最坏情况下的时间复杂度阶。
  4. 补全给定 C 程序中 partition 的 [A]、[B] 和递归函数 f 的 [C],使程序实现上述划分与排序。
  5. h(X)h(X)XX 中均匀随机选取枢轴,给出 f(X)f(X) 的平均时间复杂度阶,并详细说明随机划分、递归树高度和每层工作量如何导出该结论。

Kai

I.

(1)

xxyyzzoo
0000
0011
0101
0111
1000
1011
1100
1110

Expanding and applying the consensus theorem gives

C=xy+xz+yz=xy+yz.C=\overline xy+\overline xz+\overline yz =\boxed{\overline xy+\overline yz}.

Both two-literal implicants are essential: the input 010010 requires the first and 101101 requires the second.

(2)

z=xyi+xyi+xyi+xyi,c=xy+xi+yi.z=\boxed{\overline x\,\overline y i+\overline x y\overline i+x\overline y\,\overline i+xyi}, \qquad c=\boxed{xy+xi+yi}.

The sum is odd parity: z=(xy)iz=(x\oplus y)\oplus i, requiring two XOR gates. A NAND-only implementation of the carry is shown above. With N(a,b)=abN(a,b)=\overline{ab}, its intermediate nets are

n1=N(x,y),n2=N(x,i),n3=N(y,i),n4=N(n1,n2),n5=N(n4,n4),c=N(n5,n3).n_1=N(x,y),\quad n_2=N(x,i),\quad n_3=N(y,i),\quad n_4=N(n_1,n_2),\quad n_5=N(n_4,n_4),\quad c=N(n_5,n_3).

This uses six two-input NAND gates. A three-input NAND, if permitted, could directly combine n1,n2,n3n_1,n_2,n_3 instead.

(3), (4), (5)

For SS, the low full adder receives (x0,y0,0)(x_0,y_0,0); its carry feeds the high full adder (x1,y1,c0)(x_1,y_1,c_0). For DD, invert both yy bits and set the initial carry to 11. In both circuits, discard the final carry and output only (z0,z1)(z_0,z_1).

For SDSD, form yk=ykwy'_k=y_k\oplus w for k=0,1k=0,1, and set the initial carry to ww. The resulting two-full-adder circuit uses two XOR gates:

Z=[X+(Yxor(3w))+w]mod4.Z=\bigl[X+(Y\mathbin{\mathrm{xor}}(3w))+w\bigr]\bmod4.

Two-bit adder, subtractor, and selectable adder-subtractor

There is a trade-off if full adders and individual gates are counted separately. Since the high carry is unused, another realization is

z0=x0y0,b=y0(x0w),z1=x1y1b.z_0=x_0\oplus y_0,\qquad b=y_0(x_0\oplus w),\qquad z_1=x_1\oplus y_1\oplus b.

Use two XOR gates and one AND gate to form z0,bz_0,b, then one full adder with inputs (x1,y1,b)(x_1,y_1,b), 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. XmaxX_{\max}.

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 C(n)C(n) be the minimum number of invocations on an input of length nn. Then

C(0)=C(1)=C(2)=1,C(n)=1+min0k<n{C(k)+C(n1k)}.C(0)=C(1)=C(2)=1,\qquad C(n)=1+\min_{0\le k<n}\{C(k)+C(n-1-k)\}.

This gives C(3)=C(4)=C(5)=3C(3)=C(4)=C(5)=3, C(6)=C(7)=C(8)=5C(6)=C(7)=C(8)=5, and C(9)=7C(9)=7. Lexicographic maximization subject to these optimal splits chooses pivots 99, then 66, then 33, and places the larger child elements first while preserving each child's order. Hence

Xmin=(9,6,8,7,3,5,4,2,1),X_{\min}=(9,6,8,7,3,5,4,2,1),

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 T(n)=T(n1)+T(0)+O(n)T(n) = T(n-1) + T(0) + O(n)

Since T(0)T(0) is constant, therefore T(n)=T(n1)+cn=cn+c(n1)+c(n2)++c(1)cn(n+1)2T(n) = T(n-1) + cn = cn + c(n-1) + c(n-2) + \dots + c(1) \approx c \frac{n(n+1)}{2}

i.e. T(n)=Θ(n2)T(n) = \Theta(n^2).

(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 Θ(nlogn)\Theta(n\log n).

Order the keys by rank. A pair with ranks i<ji<j is compared exactly when the first pivot chosen from the interval {i,,j}\{i,\ldots,j\} is one of its two endpoints, which has probability 2/(ji+1)2/(j-i+1). The special case of two keys also makes exactly one comparison. Therefore

E[Cn]=1i<jn2ji+1=2d=1n1ndd+1=2(n+1)Hn4n=Θ(nlogn),\mathbb E[C_n]=\sum_{1\le i<j\le n}\frac{2}{j-i+1} =2\sum_{d=1}^{n-1}\frac{n-d}{d+1} =2(n+1)H_n-4n=\Theta(n\log n),

where Hn=k=1n1/kH_n=\sum_{k=1}^n1/k. Equivalently, the expectation satisfies the recurrence with partition cost n1n-1 and uniformly distributed pivot rank.