108 年 國立中正大學資訊工程學系碩士班《計算機概論》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題20 分

  1. (20pts) Quicksort is a basic sorting algorithm. (a) Please describe how it works. (b) Please analyze its time complexity in the worst case and in the average case.

登入後即可作答並保存紀錄。

這一題的完整詳解

Quicksort 是一種基於分治策略(Divide and Conquer)的高效排序演算法,通常可採用原地(in-place)實作。

(a) Quicksort 的工作原理

Quicksort 的執行流程可分為以下三個步驟:

  1. 分割(Divide / Partition):
    從數列中選擇一個元素作為基準點(Pivot)。常見的選擇方式包括取第一個元素、最後一個元素、隨機選取,或採用「三數取中法」(Median-of-Three)以降低遇到極端分割的機率。接著透過 Partition 程序重新排列數列,使 pivot 移動到其最終排序後的位置,並滿足:
    • pivot 左側的所有元素皆小於或等於 pivot;
    • pivot 右側的所有元素皆大於(或大於等於)pivot。
  2. 遞迴求解(Conquer):
    分別對 pivot 左側與右側的子數列遞迴執行 Quicksort,直到子數列長度為 00 或 11(即已自然有序)為止。
  3. 合併(Combine):
    由於分割與交換皆在原陣列中直接進行,當左右子數列各自排序完成時,整個數列即已完成排序,不需額外合併步驟。

以常見的 Lomuto Partition(取最右端元素 A[high] 作為 pivot)為例:

  • 維護指標 i,初始值為 low - 1,代表小於或等於 pivot 區域的右邊界。
  • 使用指標 j 從 low 遍歷至 high - 1:若 A[j] <= pivot,則將 i 加 11 並交換 A[i] 與 A[j]。
  • 遍歷結束後,將 A[i + 1] 與 A[high](即 pivot)交換,回傳 i + 1 作為 pivot 的最終索引。

(b) 時間複雜度分析

設輸入數列長度為 nn,單次 Partition 需掃描所有元素一次,時間複雜度為 Θ(n)\Theta(n)。

1. 最壞情況時間複雜度(Worst Case):O(n2)O(n^2)

  • 發生條件:每次 Partition 選到的 pivot 皆為當前子數列的最小值或最大值(例如輸入數列已為遞增或遞減排序,且固定選擇首元素或尾元素作為 pivot)。
  • 推導:每次分割只能將大小為 nn 的問題拆成大小為 00 與 n−1n-1 的兩個子問題,遞迴深度達到 nn 層。時間複雜度滿足遞迴式:
    T(n)=T(n−1)+T(0)+Θ(n)=T(n−1)+Θ(n)T(n) = T(n-1) + T(0) + \Theta(n) = T(n-1) + \Theta(n)
    展開後可得:
    T(n)=∑i=1nΘ(i)=Θ(n2)=O(n2)T(n) = \sum_{i=1}^{n} \Theta(i) = \Theta(n^2) = O(n^2)

2. 平均情況時間複雜度(Average Case):O(nlog⁡n)O(n \log n)

  • 假設條件:輸入數列為均勻隨機排列(或每次 Partition 皆隨機等機率選取 pivot),則 pivot 成為當前子數列中第 kk
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題20 分

  1. (20pts) TCP has multiple congestion-control strategies. The followings are two basic mechanisms. Please describe their concepts: (a) congestion window (b) slow start.

登入後即可作答並保存紀錄。

這一題的完整詳解

【答案】
(a) 擁塞窗口(Congestion Window, CWND):
由 TCP 發送端依據網路擁塞程度動態維護的擁塞控制變數,用以限制在未收到接收端確認應答(ACK)前所能發送之最大資料量。發送端實際的發送窗口大小同時受限於網路擁塞程度與接收端緩衝區容量,即 Send Window=min⁡(CWND,RWND)\text{Send Window} = \min(\text{CWND}, \text{RWND})。

(b) 慢啟動(Slow Start):
TCP 連線建立初期或發生逾時重傳(Timeout)後的擁塞控制起始機制。CWND 由極小值(通常為 1 個最大區段大小 MSS)開始,發送端每收到一個新的有效 ACK 便將 CWND 增加 1 MSS,使 CWND 在每個來回時間(RTT)內呈現指數級(翻倍)增長,藉此快速探測網路可用頻寬,直至達到慢啟動門檻(ssthresh)後轉入擁塞避免階段。

詳解

TCP(傳輸控制協定)透過端對端的擁塞控制(Congestion Control)機制動態調控發送速率,避免注入過多資料導致路由器緩衝區溢位或封包遺失。題幹所述兩項基本機制說明如下:

一、擁塞窗口(Congestion Window, CWND):

  1. 核心概念與目的:
    CWND 是 TCP 發送端內部維護的擁塞控制狀態變數(相對於接收端用來進行流量控制的接收窗口 RWND),其主要目的在於反映當前網路路徑的承載能力,防止發送端送出過量封包加劇網路壅塞。
  2. 運作與限制機制:
    • 發送端在任一時刻,已送出但尚未收到確認應答(ACK)的資料總量(in-flight data),不能超過發送窗口上限。
    • 實際限制發送端傳輸量的有效窗口由網路承載力(CWND)與接收端可用緩衝空間(RWND)共同決定: Send Window=min⁡(CWND,RWND)\text{Send Window} = \min(\text{CWND}, \text{RWND})
    • 動態調整:當網路順暢並持續收到 ACK 時,發送端會依演算法調增 CWND;當偵測到封包遺失(如逾時重傳或收到三個重複 ACK)時,發送端會大幅縮減 CWND,降低傳輸量以緩解擁塞。

二、慢啟動(Slow Start):

  1. 核心概念與目的:
    當 TCP 新連線剛建立,或因逾時重傳而重新啟動傳輸時,發送端尚不清楚當前網路的可用頻寬。慢啟動機制先以保守的初始速率開始,再透過每個 RTT 倍增的方式快速探測網路容量上限,兼顧起始安全性與頻寬探測效率。
  2. 運作過程:
    • 初始狀態:發送端將 CWND 初始化為較小值(標準教科書模型通常設為 1 MSS1\text{ MSS},Maximum Segment Size)。
    • 增長規則:在慢啟動階段,發送端每收到一個新的有效 ACK,CWND 便增加 1 MSS1\text{ MSS}(即 CWND←CWND+1 MSS\text{CWND} \leftarrow \text{CWND} + 1\text{ MSS})。
    • 指數倍增歷程:
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題10 分

  1. (10pts) Please design a recursive version of a function to calculate the factorial of a positive integer n, i.e. n!=1×2×3×...×n.

登入後即可作答並保存紀錄。

這一題的完整詳解

計算一個正整數 nn 的階乘 (n!n!),即 n!=1×2×3×⋯×nn! = 1 \times 2 \times 3 \times \dots \times n。此問題可以使用遞迴 (Recursion) 的方式來定義。

遞迴函數的設計需要考慮兩個關鍵要素:

  1. 基本情況 (Base Case):這是遞迴停止的條件,必須是可直接計算的。
  2. 遞迴步驟 (Recursive Step):將問題分解成一個或多個規模較小的相同問題,並將其結果組合起來。

對於階乘函數,其遞迴定義如下:

  • 基本情況:

    • 0!=10! = 1
    • 1!=11! = 1
      在實際應用中,通常會以 0!=10! = 1 或 1!=11! = 1 作為基本情況。如果題目限定為「正整數 nn」,則 1!=11! = 1 是最直接的基本情況。
  • 遞迴步驟:
    對於任何大於 1 的正整數 nn,其階乘可以表示為 n!=n×(n−1)!n! = n \times (n-1)!。
    這意味著,計算 n!n! 的問題可以轉化為計算 (n−1)!(n-1)! 的問題,然後將結果乘以 nn。

基於上述定義,我們可以設計一個遞迴函數。以下是用 C 語言風格的偽碼表示:

// 遞迴函數,計算正整數 n 的階乘
// 假設 n 為非負整數
long long factorial_recursive(int n) {
    // 基本情況 (Base Case)
    if (n == 0 || n == 1) {
        return 1;
    }
    // 遞迴步驟 (Recursive Step)
    else {
        // n! = n * (n-1)!
        return (long long)n * factorial_recursive(n - 1);
    }
}

說明:

  • 函數 factorial_recursive 接受一個整數 n 作為輸入。
  • 當 n 為 0 或 1 時,函數直接返回 1,這就停止了遞迴。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題20 分

  1. (20pts) The following figure depicts a fully-connected neural network for recognizing handwritten digits (i.e. 0, 1, 2, ..., 9), which contains 1 input layer, 3 hidden layers, and 1 output layer.
    🖼️【此處有附圖,請對照原卷】
    The input layer has 256 nodes, each of which represents an 8-bit pixel of a 1616 grayscale image. Each of the 3 hidden layers contains 16 neurons, which computes a weighted sum of all nodes in its precedent layer, adds a bias value, and performs ReLU activation (i.e. the result remains the same if it has a positive value; otherwise the result becomes 0). In other words, the j-th neuron of the i-th layer: xi,jx_{i,j} computes max⁡(∑k=0N−1xi−1,k×Wi,j,k+bi,j,0)\max(\sum_{k=0}^{N-1} x_{i-1,k} \times W_{i,j,k} + b_{i,j}, 0), where N=256N=256 for i=1i=1 and N=16N=16 for i=2i=2 & 3.
    The output layer has 10 nodes, each of which represents a digit (i.e. 0, 1, 2, ..., 9). The j-th output node x4,jx_{4,j} computes ∑k=015x3,k×W4,j,k+b4,j\sum_{k=0}^{15} x_{3,k} \times W_{4,j,k} + b_{4,j} without ReLU. The output with the maximum value will be the inference result.
    (a) Write a C program to compute the 10 outputs (represented in IEEE single-precision floating-point format) from the 256 inputs (represented in unsigned char). Assume weights and biases are represented as single-precision floating-point numbers.
    (b) How many weights and biases are needed respectively to compute the outputs of the neural network shown above for one 16
    16 image?
🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 3 頁原卷第 4 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題旨在評量多層全連接類神經網路(Fully-Connected Neural Network / Multi-Layer Perceptron, MLP)的前向推論(Forward Propagation)運算邏輯、模型參數量計算,以及以 C 語言進行底層數值運算與型態轉換之能力。

核心概念與公式如下:

  1. 全連接層前向推論(Forward Propagation):
    設前一層輸出向量為 xi−1∈RM\mathbf{x}_{i-1} \in \mathbb{R}^{M},當前第 ii 層之權重矩陣為 Wi∈RN×M\mathbf{W}_i \in \mathbb{R}^{N \times M}、偏差向量為 bi∈RN\mathbf{b}_i \in \mathbb{R}^{N}。則該層第 jj 個神經元在激勵前的加權總和(仿射轉換)為: zi,j=∑k=0M−1xi−1,k⋅Wi,j,k+bi,jz_{i,j} = \sum_{k=0}^{M-1} x_{i-1,k} \cdot W_{i,j,k} + b_{i,j}
  2. ReLU 激勵函數(Rectified Linear Unit): xi,j=max⁡(zi,j,0)={zi,j,if zi,j>00,otherwisex_{i,j} = \max(z_{i,j}, 0) = \begin{cases} z_{i,j}, & \text{if } z_{i,j} > 0 \\ 0, & \text{otherwise} \end{cases}
  3. 參數量計算法則:
    若相鄰兩層的節點數分別為 NinN_{\text{in}} 與 NoutN_{\text{out}},則該層全連接所包含的:
    • 權重數量(Weights)為 Nin×NoutN_{\text{in}} \times N_{\text{out}}。
    • 偏差值數量(Biases)為 NoutN_{\text{out}}。

解題方法

依原卷附圖所示,該網路包含 1 個輸入層(256 個節點)、3 個隱藏層(每層各 16 個神經元)、1 個輸出層(10 個節點),層與層之間均為全連接架構。

(a) C 語言程式設計

依題目規範:

  • 輸入資料:256 個元素之 unsigned char 陣列(8-bit 灰階像素,值域為 0∼2550 \sim 255)。
  • 輸出資料:10 個元素之單精度浮點數陣列(IEEE 754 single-precision float,對應數字 0∼90 \sim 9)。
  • 權重與偏差值:均為 float。
  • 運算規則:第 1 至第 3 隱藏層計算後需經過 ReLU 激勵;第 4 層(輸出層)僅計算加權和加偏差,不經過 ReLU。
#include <stdio.h>

#define INPUT_SIZE   256
#define HIDDEN_SIZE  16
#define OUTPUT_SIZE  10

/* 
 * forward_inference:
 * 依據題目定義執行 4 層神經網路前向推論
 */
void forward_inference(
    const unsigned char input[INPUT_SIZE],
    const float W1[HIDDEN_SIZE][INPUT_SIZE], const float b1[HIDDEN_SIZE],
    const float W2[HIDDEN_SIZE][HIDDEN_SIZE], const float b2[HIDDEN_SIZE],
    const float W3[HIDDEN_SIZE][HIDDEN_SIZE], const float b3[HIDDEN_SIZE],
    const float W4[OUTPUT_SIZE][HIDDEN_SIZE], const float b4[OUTPUT_SIZE],
    float output[OUTPUT_SIZE]
) {
    float h1[HIDDEN_SIZE];
    float h2[HIDDEN_SIZE];
    float h3[HIDDEN_SIZE];

    /* Layer 1: Input (256) -> Hidden 1 (16) with ReLU */
    for (int j = 0; j < HIDDEN_SIZE; j++) {
        float sum = b1[j];
        for (int k = 0; k < INPUT_SIZE; k++) {
            sum += (float)input[k] * W1[j][k];
        }
        h1[j] = (sum > 0.0f) ? sum : 0.0f;
    }

    /* Layer 2: Hidden 1 (16) -> Hidden 2 (16) with ReLU */
    for (int j = 0; j < HIDDEN_SIZE; j++) {
        float sum = b2[j];
        for (int k = 0; k < HIDDEN_SIZE; k++) {
            sum += h1[k] * W2[j][k];
        }
        h2[j] = (sum > 0.0f) ? sum : 0.0f;
    }

    /* Layer 3: Hidden 2 (16) -> Hidden 3 (16) with ReLU */
    for (int j = 0; j < HIDDEN_SIZE; j++) {
        float sum = b3[j];
        for (int k = 0; k < HIDDEN_SIZE; k++) {
            sum += h2[k] * W3[j][k];
        }
        h3[j] = (sum > 0.0f) ? sum : 0.0f;
    }

    /* Layer 4: Hidden 3 (16) -> Output (10) without ReLU */
    for (int j = 0; j < OUTPUT_SIZE; j++) {
        float sum = b4[j];
        for (int k = 0; k < HIDDEN_SIZE; k++) {
            sum += h3[k] * W4[j][k];
        }
        output[j] = sum; /* 線性輸出 */
    }
}
  • 時間複雜度:
    總乘加運算次數等於總權重數量: O(256×16+16×16+16×16+16×10)=O(4768)=O(1)O(256 \times 16 + 16 \times 16 + 16 \times 16 + 16 \times 10) = O(4768) = O(1) 網路結構維度固定,執行時間為常數時間。
  • 空間複雜度:
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題30 分

  1. (30%) Due to cost issues, a practical system usually implements only a fraction of required memory on chip and manages on-chip and off-chip data movements with the cache mechanism.
    (a) Assume there is only a 1KByte SRAM on chip for the weights in Problem 4 (assume biases are handled independently), and a design team decides to implement a direct-mapped cache to manage this 1Kbyte on-chip SRAM (all weights are stored off chip). Assume one cache block stores 32-byte data. What are the on-chip storage required in addition to the 1Kbyte SRAM to implement the direct-mapped weight cache? (Hint: cache tag ...)
    (b) What is the miss rate of the weight cache in (a)? What is the type of cache miss (i.e. compulsory, conflict, or capacity)?
    (c) Describe an effective method to improve the weight memory organization in (a) under the same cost constraint.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查計算機組織與神經網路硬體加速器中快取記憶體(Cache Memory)之架構設計、額外硬體開銷(Overhead)計算、快取缺失(Cache Misses)分析及最佳化策略:

  1. Direct-Mapped Cache 額外儲存開銷計算:
    • 總容量:1 KB=1024 Bytes1\text{ KB} = 1024\text{ Bytes}。
    • 區塊大小(Block size):32 Bytes=25 Bytes32\text{ Bytes} = 2^5\text{ Bytes},故 Offset 需要 5 bits5\text{ bits}。
    • 區塊數量(Number of blocks / lines):102432=32 blocks=25\frac{1024}{32} = 32\text{ blocks} = 2^5,故 Index 需要 5 bits5\text{ bits}。
    • 假設標準 32-bit 主記憶體位址架構:Tag 寬度為 32−5−5=22 bits32 - 5 - 5 = 22\text{ bits}。
    • 額外儲存開銷包括每個 Block 的 Tag 及 Valid bit(神經網路推論時權重通常為唯讀 Read-only,不需 Dirty bit): Overhead=32×(22 bits (Tag)+1 bit (Valid))=32×23 bits=736 bits=92 Bytes\text{Overhead} = 32 \times (22\text{ bits (Tag)} + 1\text{ bit (Valid)}) = 32 \times 23\text{ bits} = 736\text{ bits} = 92\text{ Bytes}
  2. Miss Rate 與 Miss 類型分析:
    • 神經網路推論(Inference)中,全連接層或卷積層之權重矩陣通常依序線性讀取(Sequential access)。
    • 每個 32-byte 快取區塊可容納 8 個單精度 32-bit(4 Bytes)浮點數權重。受惠於良好的空間局部性 (Spatial locality),每連續讀取 8 個權重,僅第 1 個權重發生 Cold miss,其餘 7 次皆為 Cache hit。故理論 Miss rate 為 1/8=12.5%1/8 = 12.5\%。
    • 權重資料量若超出 1KB 快取容量,第一次讀取發生 Compulsory miss(冷啟動缺失);若發生循環重複使用(如卷積層),容量不足則伴隨 Capacity miss(容量缺失)。
  3. 記憶體架構改善方案:
    • 採用雙緩衝暫存器/乒乓記憶體 (Double Buffering / Scratchpad Memory, SPM) 結合 DMA 異步預取(Prefetching),徹底消除 Tag/Valid 的硬體開銷與快取查詢延遲。

解題方法

(a) Direct-mapped Cache 額外需要的 on-chip 儲存容量計算

  1. 位址欄位切割(以標準 32-bit 位址為基準):
    • 快取總容量:C=1 KB=1024 BytesC = 1\text{ KB} = 1024\text{ Bytes}
    • 區塊大小:B=32 BytesB = 32\text{ Bytes}
    • Byte Offset:log⁡2(32)=5 bits\log_2(32) = 5\text{ bits}
    • Index 欄位:快取區塊數(Cache lines)N=1024 B32 B=32N = \frac{1024\text{ B}}{32\text{ B}} = 32。Index 寬度為 log⁡2(32)=5 bits\log_2(32) = 5\text{ bits}。
    • Tag 欄位:Tag=32−Index−Offset=32−5−5=22 bits\text{Tag} = 32 - \text{Index} - \text{Offset} = 32 - 5 - 5 = 22\text{ bits}。
  2. 每個 Cache Line 之額外控制位元:
    • Tag 儲存:22 bits22\text{ bits}
    • Valid bit:1 bit1\text{ bit}(用於指示該區塊資料是否有效)
    • (註:神經網路推論階段權重為固定常數唯讀資料,不需進行寫入回寫操作,因此不需 Dirty bit)。
    • 每個區塊額外開銷:22+1=23 bits22 + 1 = 23\text{ bits}。
  3. 總額外儲存容量 (Total Overhead): Total Bits=32×23 bits=736 bits\text{Total Bits} = 32 \times 23\text{ bits} = 736\text{ bits} 換算為位元組(Bytes): 736 bits8 bits/Byte=92 Bytes\frac{736\text{ bits}}{8\text{ bits/Byte}} = 92\text{ Bytes} 故除了 1 KB 資料 SRAM 之外,尚需額外 736 bits(或 92 Bytes) 的 on-chip 儲存空間。

(b) Weight Cache 之 Miss Rate 與 Miss 類型分析

  1. Miss Rate(缺失率)分析:
    • 神經網路推論運算具有高度的空間局部性 (Spatial locality)。權重資料在記憶體中連續儲存,推論時由 PE(處理單元)依序讀取。
    • 假設權重採用標準 32-bit 浮點數(Single-precision float, 4 Bytes)或 16-bit 浮點數(FP16/BF16, 2 Bytes):
      • 若為 4-byte 浮點數:一個 32-byte 區塊包含 324=8\frac{32}{4} = 8 個連續權重。第 1 次存取發生 Miss 將整個 32-byte 載入,後續 7 次存取皆 Hit。因此平均 Miss Rate 為: Miss Rate=18=12.5%\text{Miss Rate} = \frac{1}{8} = 12.5\%
      • 若為 2-byte FP16:一個區塊可裝 16 個權重,Miss Rate 為 116=6.25%\frac{1}{16} = 6.25\%;若為 1-byte INT8,則 Miss Rate 為 132=3.125%\frac{1}{32} = 3.125\%。
  2. Miss 類型判定:
    • Compulsory miss (強制性/冷啟動缺失):各層權重首次被載入快取時必然發生的缺失。
    • Capacity miss (容量缺失):神經網路模型總權重量遠大於 1 KB,當卷積核權重需要跨特徵圖多次重複利用(Reuse),但因 1KB 容量不足導致舊權重被擠出快取,後續重複使用時引發之缺失。

(c) 相同成本限制下改善 Weight Memory Organization 之有效方法

在晶片面積與預算受限(同為 1KB SRAM 成本)下,最佳替代與改善方案為:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題