← 回題庫

卷別

資料結構與演算法

申論

國立臺灣大學 2023 · 考試 · 第 15 題

考科演算法

(21 points) Given kk singly linked lists, each of which has nn nodes. The numbers in the nodes of the ii-th list are given by ai,1,ai,2,…,ai,na_{i,1}, a_{i,2}, \dots, a_{i,n}. Each of the kk lists has the numbers sorted in non-decreasing order, i.e., ai,1≤ai,2≤⋯≤ai,na_{i,1} \le a_{i,2} \le \dots \le a_{i,n}. Professor Q asks the students to develop an algorithm to merge these kk lists into one singly linked list sorted in non-decreasing order. Three students have come up with different algorithms. For each of these three algorithms, analyze and give their asymptotic worst-case running times with big-O notation and in terms of kk and nn (tight bound; drop lower-order terms and constant factors other than kk and nn).

  • Student A: Repeatedly iterate over the numbers in the head nodes of the kk lists, find the node MM with the smallest number, remove MM from its original list and append it to the tail of result list SS; until all kk lists are empty.
  • Student B: Create an empty min heap HH. Remove the kk head nodes and insert them into HH (each node also carries the index of its source list). Repeatedly remove the min node MM from HH, append it to SS, and (if its source list mm is not empty) remove the head of list mm and insert into HH; while HH is not empty.
  • Student C: Recursive divide-and-conquer MergeTwoGroupLists: if only one list, return it; otherwise split the mm input lists into two groups of ⌈m/2⌉\lceil m/2 \rceil and ⌊m/2⌋\lfloor m/2 \rfloor lists, recursively merge each group into one sorted list S1,S2S_1, S_2, then merge the two sorted lists S1,S2S_1, S_2 into one.

標準解析

AI 已複查