0% found this document useful (0 votes)
24 views

2023 Algorithm Midterm

The document outlines an exam for an algorithms course. It contains 6 questions testing various algorithm and data structure concepts, including graph theory, heaps, sorting algorithms, asymptotic analysis, recursion trees, maximum subarrays, stable matchings, and an optimal bus route problem.

Uploaded by

lintinghao93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

2023 Algorithm Midterm

The document outlines an exam for an algorithms course. It contains 6 questions testing various algorithm and data structure concepts, including graph theory, heaps, sorting algorithms, asymptotic analysis, recursion trees, maximum subarrays, stable matchings, and an optimal bus route problem.

Uploaded by

lintinghao93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Algorithm

Spring 2023, Midterm Exam, April 13


5:00 – 8:00 pm

1. (30%) Please answer the following questions.

(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.

• f (n) is asymptotically upper bounded by g(n), denoted by f (n) = O(g(n)),


iff there exists positive constants c and n0 such that f (n) ≤ c · g(n) for all n > n0 .

• f (n) is asymptotically lower bounded by g(n), denoted by f (n) = Ω(g(n)),


iff there exists positive constants c and n0 such that f (n) ≥ c · g(n) for all n > n0 .

• f (n) is asymptotically equivalent to g(n), denoted by f (n) = Θ(g(n)),


iff there exists positive constants c1 , c2 and n0 such that c1 · g(n) ≤ f (n) ≤ c2 · g(n)
whenever n > n0 .

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.

(d) Show that f (n) = n3 + 3n − 2 6= Ω(n4 ).

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.

–ƒ„އ ƒ– Š‹‰”‘„އ (穩定配對問題)


5. (15%) Given an ordering of preferences for each element, the stable matching problem is to find
a stable matching Given ntwo
 Goal. between men
equally n women,
andsized find a "suitable"
sets of elements. matching.
A matching is stable when no two
unmatched partners prefer each other over their current partners.
依喜好程度對於異性排序
 每個男生由高至低列出志願序列
(a) Is (周,昆), (錦,劉), (梁,蔡) a stable matching?
 每個女生由高至低列出志願序列
(b) Describe an algorithm that solves the stable matching problem, and analyze its asymptotic
worst-case running time.
favorite least favorite favorite least favorite

1st 2nd 3rd 1st 2nd 3rd

周杰倫 蔡依林 昆凌 劉嘉玲 蔡依林 錦榮 周杰倫 梁朝偉

錦榮 蔡依林 昆凌 劉嘉玲 昆凌 周杰倫 錦榮 梁朝偉

梁朝偉 昆凌 蔡依林 劉嘉玲 劉嘉玲 周杰倫 錦榮 梁朝偉

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:

• All the passengers board the bus at point 0.


• Passenger i specifies a range [si , ti ], where 0 ≤ si ≤ ti ≤ m. The passenger is satisfied as
long as the bus stops at some location between si and ti (including the boundary).

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).

You might also like