資料結構›Ch4 鏈結串列
第 5 題/共 22 題
◀ DS 5/22
5. Linked List、K路合併、複雜度分析
#DS-04-005中Linked ListK路合併複雜度分析

(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}, \ldots, a_{i,n}, as shown in the figure below. 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 \cdots \le a_{i,n}, where ii is the index of the list. Professor Q asks the students to develop an algorithm to merge these k lists into one singly linked list sorted in non-decreasing order. Three students have come up with different algorithms, which are given below in pseudo code.

Student A: L1. Create an empty singly linked list SS to collect the result. L2. DO L3. Iterate over the numbers in the head nodes of the kk lists, and find the node with the smallest number, denoted as node MM. L4. Remove node MM from the original list, and insert it into the result list SS from the tail. L5. UNTIL all original kk lists are empty L6. Return the result list SS, which contains all nodes from the original kk lists and sorted in non-decreasing order.

Student B: L1. Create an empty singly linked list SS to collect the result. L2. Create an empty min heap HH. L3. Remove the kk head nodes from the kk lists and insert them into HH. Each node in HH also contains the index number ii of the list where it is from. L4. DO L5. Remove the min node MM from the heap. Insert this node into SS from the tail. Take note of the index number stored in MM, denoted as mm. L6. If list mm is not empty, remove the head node from list mm and insert it into HH. L7. WHILE HH is not empty L8. Return the result list SS, which contains all nodes from the original kk lists and sorted in non-decreasing order.

Student C: Call the recursive divide-and-conquer function MergeTwoGroupLists (kk sorted linked lists). The return value of the function is a list containing all nodes from the original kk lists and sorted in non-decreasing order.

L1. Function MergeTwoGroupLists (mm sorted linked lists) L2. If m==1m==1 then return the only list from the input. L3. Divide the input mm lists into two groups of lists, with ⌈m/2⌉\lceil m/2 \rceil and ⌊m/2⌋\lfloor m/2 \rfloor lists, respectively. L4. Recursively call MergeTwoGroupLists for each of these two groups of lists, merging the first group of the lists into one sorted list, S1S_1, and the second group of the lists into one sorted list, S2S_2. L5. Merge the two sorted lists S1S_1 and S2S_2 into one sorted list SS. L6. Return SS.

For each of these three algorithms, analyze and give their asymptotic worst-case running times with the big-O notation and in terms of kk and nn.

Note:

  • Lower-order terms and constant factors (excluding kk and nn) should be removed in the big-O notation in the answer.
  • The bound in the answer needs to be tight.
  • Only the final result will be graded and only fully correct answers will be given points. Please clearly mark your final result in the answer sheet.
📄 台大112
跳轉到第題
▤完整推導請見《WH 資工筆記 · 資料結構》Ch4 鏈結串列
本章題號 · 1–20 / 22