← 回題庫

資料結構與演算法(A)

國立臺灣大學 2022 · 考試 · 22 題 · 限時 100 分

剩餘 1:40:00已答 0/22

整卷連續作答,作答時答案與解析隱藏;按「交卷」後揭曉正解、標準解析並自動批改(申論/計算題不計分,供自行對照)。

  1. 第 1 題選擇

    For each of the following algorithms in the next five problems, what is the tightest asymptotic upper bound for its runtime complexity for nn numbers? Please use the following table of (A)-(E) choices when answering.

    (A)(B)(C)(D)(E)
    O(1)O(1)O(lg⁡n)O(\lg n)O(n)O(n)O(nlg⁡n)O(n\lg n)O(n2)O(n^2)

    (3 points) Selection sort: expected time?

  2. 第 2 題選擇

    For each of the following algorithms in the next five problems, what is the tightest asymptotic upper bound for its runtime complexity for nn numbers? Please use the following table of (A)-(E) choices when answering.

    (A)(B)(C)(D)(E)
    O(1)O(1)O(lg⁡n)O(\lg n)O(n)O(n)O(nlg⁡n)O(n\lg n)O(n2)O(n^2)

    (3 points) Merge sort: expected time?

  3. 第 3 題選擇

    For each of the following algorithms in the next five problems, what is the tightest asymptotic upper bound for its runtime complexity for nn numbers? Please use the following table of (A)-(E) choices when answering.

    (A)(B)(C)(D)(E)
    O(1)O(1)O(lg⁡n)O(\lg n)O(n)O(n)O(nlg⁡n)O(n\lg n)O(n2)O(n^2)

    (3 points) MAX_HEAPIFY for a max-heap: expected time?

  4. 第 4 題選擇

    For each of the following algorithms in the next five problems, what is the tightest asymptotic upper bound for its runtime complexity for nn numbers? Please use the following table of (A)-(E) choices when answering.

    (A)(B)(C)(D)(E)
    O(1)O(1)O(lg⁡n)O(\lg n)O(n)O(n)O(nlg⁡n)O(n\lg n)O(n2)O(n^2)

    (3 points) Quick sort: worst-case time?

  5. 第 5 題選擇

    For each of the following algorithms in the next five problems, what is the tightest asymptotic upper bound for its runtime complexity for nn numbers? Please use the following table of (A)-(E) choices when answering.

    (A)(B)(C)(D)(E)
    O(1)O(1)O(lg⁡n)O(\lg n)O(n)O(n)O(nlg⁡n)O(n\lg n)O(n2)O(n^2)

    (3 points) Bucket sort when there are Θ(n)\Theta(n) buckets: expected time?

  6. 第 6 題選擇

    (5 points) Which of the following is NOT an in-place sorting algorithm (in their standard implementations)?

  7. 第 7 題選擇

    (5 points) In a max-heap stored in an array, after these 6 numbers have been inserted in this exact sequence: 5,4,2,6,1,35, 4, 2, 6, 1, 3, what is the value of the node at index 2 (with the root being the node at index 1)?

  8. 第 8 題選擇

    (5 points) Following the previous problem, what is the value of the left child of the node with value 3?

  9. 第 9 題選擇

    (5 points) Consider an array of 8 elements being sorted using quicksort. It has just finished the first pass of partitioning and pivot swapping, thus changing the original array into the following array:

    [7,11,16,10,17,1,18,30][7, 11, 16, 10, 17, 1, 18, 30]

    How many elements could have been the pivot?

  10. 第 10 題選擇

    (5 points) Consider an integer array AA of size n>0n > 0 with elements A[1],…,A[n]A[1], \dots, A[n], where the first pp elements are positive and the other (n−p)(n - p) elements are negative. The following algorithm calculates pp correctly by calling COMPUTE-P(A,n)\text{COMPUTE-P}(A, n). What loop invariant is maintained for the while loop of the algorithm?

    COMPUTE-P(A, len)
    1  if A[1] < 0 return 0
    2  left = 1, right = len
    3  while left < right
    4      m = ceiling((left + right) / 2)
    5      if A[m] > 0
    6          left = m
    7      else
    8          right = m - 1
    9  return left
    
  11. 第 11 題選擇

    (5 points) Consider a string of length nn, e.g., cbaabcba. We want to add the smallest number of characters to the front of the string to make it a palindrome, e.g., abcbaabcba. This problem can be solved by first concatenating the string with its inverse with a delimiter #, e.g. cbaabcba#abcbaabc. Then, run the pre-processing routine of the Knuth-Morris-Pratt algorithm on the new string s0s1⋯s2ns_0 s_1 \cdots s_{2n} of length (2n+1)(2n + 1) to get a failure function fs(j)f_s(j), which computes the length of the longest prefix of the string s0s1…sjs_0 s_1 \dots s_j that is also a suffix. After running the pre-processing algorithm, how can we calculate the smallest number of characters that should be added to the front of the original string to make a palindrome?

  12. 第 12 題選擇

    (5 points) Which of the following statement has been proved to be true?

  13. 第 13 題選擇

    (5 points) Consider a hash table with f(n)f(n) entries with a hash function that can uniformly dispatch items to those entries. Which property below ensures that a successful search can be done in O(1)O(1)?

  14. 第 14 題選擇

    (5 points) Consider a red-black tree with nn internal nodes, where nn is even. At most how many of them can be a black node with one red child?

  15. 第 15 題選擇

    (5 points) In the red-black tree with 26 nodes that reaches the solution of the problem above, what is the maximum height of the tree? A tree with one node is assumed to have height 1.

  16. 第 16 題選擇

    (5 points) Dynamic programming can be used to solve the matrix-chain multiplication problem. Suppose we hope to compute the matrix product A1A2…A6A_1 A_2 \dots A_6 with the matrix dimensions as follows.

    matrixA1A_1A2A_2A3A_3A4A_4A5A_5A6A_6
    dimension30×3530\times3535×1535\times1515×515\times55×105\times1010×2010\times2020×2520\times25

    Let m[i,j]m[i, j] be the minimum number of scalar multiplications needed to compute the matrix AiAi+1…AjA_i A_{i+1} \dots A_j. What is m[2,5]m[2, 5]?

  17. 第 17 題選擇

    (5 points) What is the time complexity of finding the minimum number of scalar multiplications needed for A1A2…AnA_1 A_2 \dots A_n using dynamic programming?

  18. 第 18 題選擇

    (5 points) When using Dijkstra's algorithm to solve the single-source shortest problem for a graph having VV vertices and EE edges, which is INCORRECT?

  19. 第 19 題選擇

    (5 points) Consider the following undirected graph GG. Let TT be the minimum spanning tree (MST) of the graph. Which is INCORRECT?

    Vertices V1,…,V7V_1, \dots, V_7 with edges and weights:

    • (V1,V4)=1(V_1, V_4) = 1
    • (V1,V3)=3(V_1, V_3) = 3
    • (V2,V7)=2(V_2, V_7) = 2
    • (V1,V2)=8(V_1, V_2) = 8
    • (V2,V3)=14(V_2, V_3) = 14
    • (V1,V7)=16(V_1, V_7) = 16
    • (V4,V3)=20(V_4, V_3) = 20
    • (V4,V5)=21(V_4, V_5) = 21
    • (V1,V6)=25(V_1, V_6) = 25
    • (V5,V6)=28(V_5, V_6) = 28
    • (V6,V7)=15(V_6, V_7) = 15
    • (V1,V5)=36(V_1, V_5) = 36
  20. 第 20 題選擇

    (5 points) Given a graph G=(V,E)G = (V, E) with the adjacent matrix W=wijW = w_{ij} with WW an n×nn \times n matrix. The following algorithm can compute the All-Pairs Shortest Path of GG.

    FLOYD-WARSHALL'(W)
    1  D = W
    2  for k = 1, 2, ..., n
    3      for i = 1, 2, ..., n
    4          for j = 1, 2, ..., n
    5              d_ij = min(d_ij, d_ik + d_kj)
    

    What is the SPACE complexity of this algorithm?

  21. 第 21 題選擇

    (5 points) When using the Edmonds-Karp algorithm to find the maximum flow of a flow network G=(V,E)G = (V, E), which is CORRECT?

  22. 第 22 題選擇

    (5 points) Consider a fractional knapsack problem of 6 items. The ii-th item is worth viv_i dollars and weighs wiw_i pounds.

    item ii123456
    pounds wiw_i428558
    dollars viv_i38167920

    Suppose that at most W=20W = 20 pounds can be carried in the knapsack. The sequence of picking the items is