2023 Algorithm Midterm
2023 Algorithm Midterm
(a) Show that for each even number n greater than 4, there exists a 5-regular graph G(V, E)
with n nodes.
(b) Illustrate the operation of Max-Heap-Insert(A, 3) on the heap A = h13, 12, 9, 10, 12, 8, 7, 4, 5, 6, 2, 1i.
(c) Use the master method to give a tight asymptotic bound on the running time of the
recurrence T (n) = 4T (n/2) + n4 .
(d) Show that an n-element heap has height blg nc.
(e) Show that the running time of Quicksort is Θ(n2 ) when the array A contains distinct
elements and is sorted in decreasing order.
(f) Which of them are in-place sorting algorithms? a : Insertion Sort b : Merge Sort
c : Quicksort d : Heapsort
2. (20%) Let f (n) and g(n) be real-valued functions. The asymptotic notations, O, Ω, and Θ, are
defined as follows.
Please answer the following questions according to the definitions given above.
(a) Finding upper bound for the function f (n) = n3 + 3n − 2 by using the O notation.
Justify your answer by demonstrating the constants.
(b) Finding lower bound for the function f (n) = n3 + 3n − 2 by using the Ω notation.
Justify your answer by demonstrating the constants.
(c) Finding tight bound for the function f (n) = n3 + 3n − 2 by using the Θ notation.
Justify your answer by demonstrating the constants.
3. (15%) Use a recursion tree to determine a good asymptotic upper bound on the recurrence
T (n) = 4T (bn/4c) + 2n, where T (1) = 2, T (2) = 5 and T (3) = 8. Use the substitution method
to verify your answer.
1
4. (15%) Given an array A = hx1 , x2 , . . . , xn i of numbers, the maximum-subarray problem is to
find two indices i and j such that A = hxi , x2 , . . . , xj i has the greatest sum of any contiguous
subarray of array A.
(a) Illustrate the operation of Find-Maximum-Subarray on the array A = h−3, 5, −3, 4, −2, 2, 4, −3i.
(b) Describe an algorithm that solves the maximum-subarray problem in O(n log n) time.
6. (15%) Famous
4 ride-sharing company NTOU has created a new “shared-bus” service. The bus
serves a path of m miles, indexed by [0, m]. The main differences between this service and
traditional bus services are:
There are no fixed bus stops. Instead, the stops will be decided after gathering the user infor-
mation. As an engineer at NTOU, you need to solve the following problem: given the number of
passengers n, and each passenger’s preference [si , ti ], design an algorithm that finds the smallest
number of stops in order to satisfy all customers.
(a) Design the algorithm and analyze its running time. Your algorithm should run in O(n log n)
time.
(b) Prove the correctness of the algorithm you designed in (a).