ps1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Problem Set 1

Design and Analysis of Algorithms, Fall 2024

Due: September 27 23:59:59 (UTC+8), mail to [email protected].

Problem 1
Recall the I NSERTION S ORT algorithm we discussed in class. We have argued why it always terminates
on any input, we have also informally argued why it always returns the correct answer on any input. In
this exercise, you are asked to formally prove that the I NSERTION S ORT algorithm always correctly sorts
the input. (Hint: You can prove the correctness of loop invariant we discussed in class via induction. But
in the inductive step, you may need to come up with another loop invariant to show the effectiveness of
the while loop. That is, to prove the inductive step, you need to state and prove another loop invariant.)

Problem 2
Sort the following functions from asymptotically smallest to asymptotically largest, indicating ties if
there are any. You do not need to prove your answer. To simplify notation, write f (n) ≪ g(n) to denote
f (n) ∈ o(g(n)) and f (n) = g(n) to denote f (n) ∈ Θ(g(n)).1
∗ √
lg(lg∗ n) 2lg n ( 3)lg n n2 n! (lg n)!
n
(10/9)n n3 lg2 n lg(n!) 22 n1/ lg n
ln ln n lg∗ n n · 2n nlg lg n ln n √ 1
2lg n (lg√n)lg n en 4lg n (n + 1)! lg lg n
n+1
lg∗ (lg n) 2 2 lg n n 2n n lg n 22

Problem 3
Design a M IN S TACK data structure that can store comparable elements and supports stack operations
PUSH (x), POP (), as well as the MIN () operation, which returns the minimum value currently stored in
the data structure. All operations should run in O(1) time in your implementation. (You may assume
there exists a S TACK data structure which supports PUSH and POP, and both operations run in Θ(1)
time.) You should give a brief overview of your M IN S TACK data structure, then provide pseudocode for
each of the three operations. You should also discuss the space complexity of your implementation.

Problem 4
You are given an infix expression in which each operator is in {+, ×, (, )} and each operand is a single
digit positive integer. Write an algorithm to evaluate its value. (For example, given “(3 + 6) × 5”,
your algorithm should output “45”.) You should give a brief overview of your algorithm, then provide
pseudocode, and finally discuss its time complexity. (To get full credit, your algorithm should have O(n)
time complexity.)
1
Read Section 3.2 of CLRS for the definition of function lg∗ n. Also, in this exercise, lg(n) means log2 (n).

1
Problem 5
(a) Use a recursion tree to justify the solution to the recurrence T (n) = T (αn) + T ((1 − α)n) + Θ(n)
is Θ(n lg n), where α is a constant in the range 0 < α < 1. Notice that you must justify both T (n) =
O(n lg n) and T (n) = Ω(n lg n).
(b) Give asymptotic upper and lower bounds for T (n) in each of the following recurrences. You may
assume that T (n) is constant for sufficiently small n. You should make your bounds as tight as possible.
You do not need to prove your answer.
• T (n) = 3T (n/3 − 2) + n/2.

• T (n) = 4T (n/2) + n2 n.
• T (n) = T (n − 2) + lg n.
√ √
• T (n) = nT ( n) + n.
• T (n) = T (n/2) + T (n/4) + n.

Problem 6
(a) Recall the M ERGE S ORT algorithm we discussed in class. Suppose you are given an input array A
containing n integers, implement M ERGE S ORT to sort the integers in A. You need to give the complete
pseudocode of your implementation, including the M ERGE procedure. You should also analyze the time
complexity and the space complexity of your implementation. (For full credit, your implementation
should have O(n log n) runtime, and use O(n) space beside input.)
(b) Redo part (a), except that this time your input is a (singly) linked-list. In particular, you are given
the head pointer and the size of the list. (For full credit, your implementation should have O(n log n)
runtime, and use O(log n) space beside input.)

Problem 7
(a) Devise a variant of Karatsuba’s algorithm that squares any n-digit number in O(nlg 3 ) time, by
reducing to squaring three ⌈n/2⌉-digit numbers. (Hint: notice that xy = (x2 + y 2 − (x − y)2 )/2.)
(b) Professor F. Lake claims that it is asymptotically faster to square an n-bit integer than to multiply
two n-bit integers. Should you believe him? Prove your answer.

Problem 8
An array A[1 · · · n] is said to have a majority element if more than half of its entries are the same. Given
an array, the task is to design an algorithm to tell whether the array has a majority element, and, if so, to
find that element. The elements of the array are not necessarily from some ordered domain, and so there
can be no comparisons of the form “is A[i] > A[j]?”. (Think of the array elements as JPEG images,
say.) However, you can answer questions of the form “is A[i] = A[j]?” in constant time.
(a) Devise an algorithm to solve this problem in O(n lg n) time. You should give a brief overview of
your algorithm and provide pseudocode. You should also briefly argue the correctness of your algorithm.
(Hint: use divide and conquer.)
(b) [Bonus Question] Devise an algorithm to solve this problem in O(n) time. You should give a brief
overview of your algorithm and provide pseudocode. You should also briefly argue the correctness of
your algorithm.

You might also like