免費詳解 / 中山 / 軟體 / 103

103 中山資工所考古題解答|軟體逐題詳解

103 中山資工所軟體考古題的逐題詳解,共 10 題,免費公開。

逐題詳解

110%

A computer has six tape drives, with n processes competing for them. Each process may need two drives. For which values of n is the system deadlock free?

deadlock 的最壞情形:每個 process 各拿到 1 台、都在等第 2 台,而且沒有空的 drive

只要每個 process 先各發 1 台後仍剩 1 台,就有 process 湊滿 2 台跑完並釋放

\(n\cdot(2-1)\lt6\) → \(n\le5\)

\(n=1,2,3,4,5\)(\(n\ge6\) 時 6 個 process 各拿 1 台即 deadlock)

210%

Consider the two-dimensional array A: double A[][] = new double[200][200]; where each double occupies 8 bytes and A[0][0] is at location 200, in a paged system with pages of size 200 bytes. A small process is in page 0 (locations 0 to 199) for manipulating the matrix; thus every instruction fetch will be from page 0. For three page frames, how many page faults are generated by the following array initialization loops, using LRU replacement and assuming (1) page frame 0 has the process in it, (2) the other two are initially empty, and (3) the array is stored in memory column-major?

(a) (5%)  for (int i = 0; i < 200; i++)
              for (int j = 0; j < 200; j++)
                  A[i][j] = 0;

(b) (5%)  for (int j = 0; j < 200; j++)
              for (int i = 0; i < 200; i++)
                  A[i][j] = 0;

1 page \(=200\) B \(=25\) 個 double;column-major:一整個 column 200 個 double \(=1600\) B \(=8\) pages;全陣列 \(=1600\) pages

\(A[i][j]\) 的位址 \(=200+8(200j+i)\) → page \(=1+8j+\lfloor i/25\rfloor\)

指令每次都在 page 0,page 0 永遠是最近用過的,frame 0 不會被換掉;陣列只有 2 個 frame 可用

(a) \(j\) 在內層:相鄰兩次存取差 \(8\) 個 page,一輪 \(j=0\sim199\) 碰 200 個不同 page,2 個 frame 留不住,每次存取都 fault

\(200\times200=40000\) 次

(b) \(i\) 在內層:一個 page 內 25 個元素連續用完才換下一頁,每個 page 只 fault 一次

1600 次

310%

Assume a page reference string for a process with m frames (initially all empty). The page reference string has length n with p distinct page numbers occurring in it. For any page-replacement algorithms, (a) (5%) What is a lower bound on the number of page faults? (b) (5%) What is an upper bound on the number of page faults?

(a) frame 一開始全空,每個相異 page 第一次被用到一定 fault

下界 \(p\)

(b) 最壞每次參考都 fault

上界 \(n\)

410%

Given a UNIX i-node with ten direct blocks and three levels of indirect blocks (i.e., a single, a double, and a triple) and assuming that the sizes of a pointer and a block are, respectively, 8 bytes and 8 Kbytes, answer the following questions. (a) (5%) What would be the size of the smallest file allowed in bytes? (b) (5%) What would be the size of the largest file allowed in bytes?

(a) 檔案只要有內容,就至少配 1 個 data block(8 KB);檔案大小本身最小為 1 byte

1 byte

空檔案(0 byte)不佔 data block。

(b) 一個 index block 放 \(8192/8=1024=2^{10}\) 個 pointer

層data blocks
direct10
single indirect\(2^{10}=1024\)
double indirect\(2^{20}=1{,}048{,}576\)
triple indirect\(2^{30}=1{,}073{,}741{,}824\)
合計1,074,791,434

\(1{,}074{,}791{,}434\times8192\)

\((10+2^{10}+2^{20}+2^{30})\times8\text{ KB}=8{,}804{,}691{,}427{,}328\) bytes(約 8 TB)

510%

A disk has 10000 cylinders, each with 10 tracks of 512 blocks. A seek takes 1 ms per cylinder moved. If no attempt is made to put the blocks of a file close to each other, two blocks that are logically consecutive will require an average seek, which takes 6 ms. If, however, the operating system makes an attempt to cluster related blocks, the mean interblock distance can be reduced to 2 cylinders and the seek time reduced to 200 μs. Assuming that the rotational latency is 3 ms and the transfer time is 10 μs per block, answer the following questions. (a) (5%) How long does it take to read a 200 block randomly placed file? (b) (5%) How long does it take to read a 200 block clustered file?

每個 block:seek + rotational latency + transfer

(a) \(200\times(6+3+0.01)=200\times9.01\)

1802 ms

(b) \(200\times(0.2+3+0.01)=200\times3.21\)

642 ms

seek 快 30 倍,整體只快約 2.8 倍;rotational latency 成為主要時間。

610%

Red-Black Tree. (1) (4%) A red-black tree is a binary search tree where every node is either red or black. What are the four properties of a red-black tree? (2) (3%) Suppose that a red-black tree contains n internal nodes. What is its maximum height? What is the time to search one value? What is the time to find the maximum value? (3) (3%) Do you think whether a red-black tree is balanced or not? Please give your reason.

(1) ① root 是黑的

② 每個 leaf(NIL)是黑的

③ 紅色 node 的兩個 child 都是黑的(不會有兩個紅 node 相連)

④ 從任一 node 到它底下所有 leaf 的路徑,黑色 node 數相同(black-height)

(2) 由 ③④:最長路徑(紅黑交替)\(\le\) 2 倍最短路徑(全黑)

最大高度 \(\le2\log_2(n+1)\);search \(O(\log n)\);找最大值(一路往右)\(O(\log n)\)

(3) 算 balanced:不像 AVL 要求左右高度差 \(\le1\),但最長路徑不超過最短路徑的 2 倍,高度為 \(O(\log n)\),所有操作時間隨高度成 \(O(\log n)\)

710%

Tree Search & Expression Tree. (1) (6%) Suppose that each internal node of a tree has at most three children. Given the BFS sequence “iceodlmfhjbgna” and the DFS sequence “icdbglemnofhaj” of the tree, please draw that tree. (2) (4%) Given a postfix expression “8 3 2 + - 4 9 3 / + * 3 * 7 +”, please translate it into an infix expression and give the calculation result.

(1) 兩個序列都由 i 開頭 → root 是 i

BFS:i | c e o | d l m f h j | b g n a

DFS:i | c d b g l | e m n | o f h a j

DFS 把 i 的子樹切成三段,段首為 c、e、o → i 的 child 是 c、e、o(BFS 第 2 層)

BFS 第 3 層 d l m f h j 依序屬於 c、e、o 的 child:c 的段 \(\{d,b,g,l\}\) 含 d、l;e 的段 \(\{m,n\}\) 含 m;o 的段 \(\{f,h,a,j\}\) 含 f、h、j → c:d、l;e:m;o:f、h、j

BFS 第 4 層 b g n a:d 的子樹有 b、g;m 有 n;h 有 a

iceodlmfhjbgna

檢查 DFS:i, c, d, b, g, l, e, m, n, o, f, h, a, j ✓;BFS:i | c e o | d l m f h j | b g n a ✓

(2) 用 stack 由左到右

8 3 2 + → 8, (3+2);- → (8−(3+2))

4 9 3 / → 4, (9/3);+ → (4+(9/3));* → ((8−(3+2))×(4+(9/3)))

3 * → (…×3);7 + → ((…×3)+7)

\(\big((8-(3+2))\times(4+9/3)\big)\times3+7=3\times7\times3+7=70\)

810%

Treap. (1) (5%) Given the following treap, please show the final result after inserting a node (D:9). (2) (5%) Following (1), please show the final result after inserting a node (F:2).

A:10B:7C:25E:23G:4H:5I:73K:65

treap:key 是 BST(字母序)、priority 是 min-heap;先照 BST 放到 leaf,再 rotation 把 priority 小的往上提

(1) D:9:G → B → E → C,D 掛在 C:25 右邊

9 \(\lt\) 25 → 對 C 左旋,D 升上去(C 成 D 的左 child)

9 \(\lt\) 23 → D 是 E 的左 child,對 E 右旋,D 再升(E 成 D 的右 child)

9 \(\gt\) 7,heap 條件成立,停止

A:10B:7C:25D:9E:23G:4H:5I:73K:65

(2) F:2:G → B → D → E,F 掛在 E:23 右邊;2 比路徑上的 priority 都小,一路往上提

對 E 左旋(F 升,E 成 F 的左 child)

對 D 左旋(F 升,D 成 F 的左 child,F 原左子樹 E 轉成 D 的右 child)

對 B 左旋(F 升,B 成 F 的左 child,D 轉成 B 的右 child)

對 G 右旋(F 升為 root,G 成 F 的右 child,G 的左子樹變空)

A:10B:7C:25D:9E:23F:2G:4H:5I:73K:65

檢查:中序仍為 A B C D E F G H I K;每個 parent 的 priority 都不大於 child。

910%

Hash Functions. (1) (4%) What are the two major characteristics of hash functions to make them be widely adopted in authentication applications? (2) (4%) What is a hash collision? How can you solve this problem by chaining? (3) (2%) What is a perfect hashing?

(1) ① one-way(preimage resistance):由 hash 值無法反推原訊息,存密碼或驗證時不洩漏原文

② collision resistance:找不到兩個不同訊息有相同 hash 值,別人無法拿另一份訊息冒充

另外基本條件:可接受任意長度輸入、輸出固定長度、計算容易。

(2) collision:兩個不同的 key 經 hash function 得到同一個位址

chaining:每個位址(bucket)掛一條 linked list,hash 到該位址的 key 都串在這條 list 上;查詢先算位址,再沿 list 逐一比對,平均 \(O(1+\alpha)\)(\(\alpha=n/m\) 為 load factor)

(3) perfect hashing:對固定的 key 集合,hash function 使所有 key 完全沒有 collision,查詢 worst case \(O(1)\);常見作法為兩層表,第二層對落在同一 bucket 的 \(n_i\) 個 key 用大小 \(n_i^2\) 的表

1010%

Heaps. (1) (2%) What is a binomial heap? (2) (4%) Please explain the two binomial-heap properties. (3) (2%) Suppose that two binomial heap H1 and H2 have n1 and n2 nodes, respectively. Then, what is the worst-case running time to unite H1 and H2? (4) (2%) What is a Fibonacci heap?

(1) binomial heap:一組 binomial tree 組成的 heap,每棵都是 min-heap order,且每種 degree 最多一棵。\(B_0\) 是單一 node,\(B_k\) 由兩棵 \(B_{k-1}\) 連成(一棵掛在另一棵 root 下),有 \(2^k\) 個 node

(2) ① 每棵 binomial tree 都滿足 min-heap order:node 的 key \(\ge\) parent 的 key,最小值在某棵樹的 root

② 對每個 \(k\ge0\),root degree 為 \(k\) 的 binomial tree 至多一棵:\(n\) 個 node 的 heap 有沒有 \(B_k\) 由 \(n\) 的二進位第 \(k\) 位決定,樹的棵數 \(\le\lfloor\log_2n\rfloor+1\)

(3) union 如同二進位加法:合併兩條 root list,遇到同 degree 的兩棵就連成 degree 加 1 的樹(進位)

\(O(\log n)\),\(n=n_1+n_2\)

(4) Fibonacci heap:一組 min-heap order 的樹,結構限制比 binomial heap 鬆,root 串在 circular doubly linked list 並有 min 指標,用 lazy 方式把整理延到 extract-min;insert、union、decrease-key 為 amortized \(O(1)\),extract-min、delete 為 amortized \(O(\log n)\)

109–115 的詳解也是這樣寫的。

國立中山大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 236 頁,逐題推導。

購買 · NT$ 850 看中山其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

題目有錯、詳解看不懂,都歡迎來信

想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。

william.chou.123123@gmail.com