Professional Documents
Culture Documents
Fall 2018 Midterm Examination: Questions 1 2 3 4 5 6 7 Total Points 10 10 10 10 10 25 25 100 Score
Fall 2018 Midterm Examination: Questions 1 2 3 4 5 6 7 Total Points 10 10 10 10 10 25 25 100 Score
Instructions
Questions 1 2 3 4 5 6 7 Total
Points 10 10 10 10 10 25 25 100
Score
(COMP3711)[2018](f)midterm~=97lpo^_79477.pdf downloaded by khcheungaq from http://petergao.net/ustpastpaper/down.php?course=COMP3711&id=15 at 2020-10-06 10:43:21. Academic use within HKUST only.
We have two algorithms, A and B. Let TA (n) and TB (n) denote the time
complexities of algorithm A and B respectively, with respect to the input
size n. Below there are 8 different cases of time complexities for each
algorithm. Complete the last column of the following table with “A”, “B”,
or “U”, where:
Answer: 1) A, 2) U, 3) U, 4) B, 5) B
Grading Scheme: 2 points for each
2
(COMP3711)[2018](f)midterm~=97lpo^_79477.pdf downloaded by khcheungaq from http://petergao.net/ustpastpaper/down.php?course=COMP3711&id=15 at 2020-10-06 10:43:21. Academic use within HKUST only.
(5) Which of the above algorithms uses the least amount of memory
(working space)?
Answer: (1) MS and QS, (2) IS and MS, (3) IS, (4) MS and HS, (5) IS and HS
Grading Scheme: 2 points for each
3
(COMP3711)[2018](f)midterm~=97lpo^_79477.pdf downloaded by khcheungaq from http://petergao.net/ustpastpaper/down.php?course=COMP3711&id=15 at 2020-10-06 10:43:21. Academic use within HKUST only.
Answer: This problem is the same as the birthday paradox, by viewing the n items
as people and the m lockers as days. Thus, the expected number of pairs
is n2 /m.
4
(COMP3711)[2018](f)midterm~=97lpo^_79477.pdf downloaded by khcheungaq from http://petergao.net/ustpastpaper/down.php?course=COMP3711&id=15 at 2020-10-06 10:43:21. Academic use within HKUST only.
3 14 7 31 15 8 10 34 32 17 18 9 12 19
(b) Show the new array after inserting 4 in the original array.
Answer:
(a)
7 14 8 31 15 9 10 34 32 17 18 19 12
(b)
3 14 4 31 15 8 7 34 32 17 18 9 12 19 10
5
(COMP3711)[2018](f)midterm~=97lpo^_79477.pdf downloaded by khcheungaq from http://petergao.net/ustpastpaper/down.php?course=COMP3711&id=15 at 2020-10-06 10:43:21. Academic use within HKUST only.
Answer: You generate the same array C[1 . . . k] as in counting sort, i.e. the element
C[i] is the total number of elements smaller or equal to i. The answer to
the query is: C[y] − C[x].
6
(COMP3711)[2018](f)midterm~=97lpo^_79477.pdf downloaded by khcheungaq from http://petergao.net/ustpastpaper/down.php?course=COMP3711&id=15 at 2020-10-06 10:43:21. Academic use within HKUST only.
Answer: 1. Find the median of the array and partition it into two sub-arrays: O(n)
2. Recursively solve two subproblems until we have k blocks.
So T (n) = 2T (n/2) + cn. The base case is T (n/k) = 1. So, T (n) =
O(n log k).
7
(COMP3711)[2018](f)midterm~=97lpo^_79477.pdf downloaded by khcheungaq from http://petergao.net/ustpastpaper/down.php?course=COMP3711&id=15 at 2020-10-06 10:43:21. Academic use within HKUST only.
delete from the heap with the two smallest values |Li and |Lj |.
create a node of size |Li | + |Lj | whose children are |Li | and |Lj |.
insert the value |Li | + |Lj | into the heap.