ps2

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

Problem Set 2

Design and Analysis of Algorithms, Fall 2024

Due: October 18 23:59:59 (UTC+8), mail to ucas-alg-ps@chaodong.me.

Problem 1
In this problem we will develop a divide-and-conquer algorithm for the following geometric task.

The Closest Pair Problem:


Input: A set of n points in the plane, {p1 = (x1 , y1 ), p2 = (x2 , y2 ), · · · , pnp
= (xn , yn )}.
Output: The closest pair of points. That is, the pair pi ̸= pj that minimizes (xi − xj )2 + (yi − yj )2 .

For simplicity, assume that n is a power of two, and that all the x-coordinates xi are distinct, as are the
y-coordinates. Here’s a high-level overview of the algorithm:
• Find a value x for which exactly half the points have xi < x, and half have xi > x. On this basis,
split the points into two groups, L and R.
• Recursively find the closest pair in L and in R. Say these pairs are pL , qL ∈ L and pR , qR ∈ R,
with distances dL and dR respectively. Let d be the smaller of these two distances.
• It remains to be seen whether there is a point in L and a point in R that are less than distance d
apart from each other. To this end, discard all points with xi < x − d or xi > x + d and sort the
remaining points by y-coordinate.
• Now, go through this sorted list, and for each point, compute its distance to the seven subsequent
points in the list. Let pM , qM be the closest pair found in this way.
• The answer is one of the three pairs {pL , qL }, {pR , qR }, {pM , qM }, whichever is closest.
(a) Argue that the algorithm is correct. (Hint: You may find the following property helpful: any square
of size d × d in the plane contains at most four points of L.)
(b) Write down the pseudocode for the algorithm and argue its running time is O(n log2 n).
(c) [Bonus Question] Can you improve the algorithm so that its running time is reduced to O(n log n)?

Problem 2
(a) Implement the H EAP U PDATE(i, val) operation in a binary max-heap containing n nodes, which
changes the value of the element at index i to val. Your implementation should have O(lg n) runtime.
(b) Show how to find the k-th largest element in a binary max-heap in O(k lg k) time. You should give
an overview of your algorithm and then provide pseudocode. (Hint: Using another heap might help.)

Problem 3
Suppose you are given k sorted linked lists, and the total number of elements in these lists is n. Devise
an O(n lg k)-time algorithm to merge these k sorted lists into one sorted list.

1
Bonus Problem
Prove that when all elements are distinct, the best-case running time of H EAP S ORT is Ω(n lg n). (Hence,
when all elements are distinct, the running time of H EAP S ORT is Θ(n lg n).)

Problem 4

StrangeSort(A[0, · · · , n − 1])
1: if (n == 2 and A[0] > A[1]) then
2: Swap(A[0], A[1]).
3: else if (n > 2) then
4: m ← ⌈2n/3⌉.
5: StrangeSort(A[0, · · · , m − 1]).
6: StrangeSort(A[n − m, · · · , n − 1]).
7: StrangeSort(A[0, · · · , m − 1]). ▷ Yes, this line is identical to Line 5.

(a) What is the running time of the algorithm? Prove your answer.
(b) Prove that the algorithm actually sorts its input.
(c) Is the algorithm still correct if we replace m ← ⌈2n/3⌉ with m ← ⌊2n/3⌋. Prove your answer.
(d) Prove that the number of swaps executed by the algorithm is at most n2 .


Problem 5
The QuickSort algorithm introduced in class contains two recursive calls to itself. Specifically, after
QuickSort calls Partition, it recursively sorts the left subarray and then it recursively sorts the
right subarray. However, the second recursive call in QuickSort is not really necessary; we can avoid
it by using an iterative control structure. In particular, consider the following version of quicksort:

TRQuickSort(A[1 · · · n], p, r)
1: while (p < r) do
2: q ← Partition(A, p, r).
3: TRQuickSort(A, p, q − 1).
4: p ← q + 1.

(a) Prove that TRQuickSort(A[1 · · · n], 1, n) correctly sorts the array A.

As we have discussed in class previously, compilers usually execute recursive procedures by using a
stack that contains pertinent information, including the parameter values, for each recursive call. The
information for the most recent call is at the top of the stack, and the information for the initial call is
at the bottom. Upon calling a procedure, its information is pushed onto the stack; when it terminates,
its information is popped. Since we assume that array parameters are represented by pointers, the infor-
mation for each procedure call on the stack requires O(1) stack space. The stack depth is the maximum
amount of stack space used at any time during a computation.
(b) Describe a scenario in which TRQuickSort’s stack depth is Θ(n) on an n-element input array.
(c) Modify the code for TRQuickSort so that the worst-case stack depth is Θ(lg n). Your modification
should maintain the O(n lg n) expected running time of the algorithm. You also need to explain why
your modification can guarantee Θ(lg n) worst-case stack depth.

2
Problem 6
(a) Develop an algorithm that, given n integers in the range 0 to k, preprocesses its input and then
answers any query about how many of the n integers fall into a range [a, b] in O(1) time. Here, a ∈ N
and b ∈ N. Your algorithm should use O(n + k) time for preprocessing.
(b) You are given an array of integers, where different integers may have different numbers of digits, but
the total number of digits over all the integers is n. Develop an algorithm to sort the array in O(n) time.

Problem 7
Eve thinks of an integer between 1 and 1, 000, 000. You are trying to determine this number by asking as
few yes/no questions as possible. How many yes/no questions are required to determine Eve’s number
in the worst case? Give both an upper bound (i.e., devise an algorithm) and a lower bound. You also
need to briefly explain why your upper bound and lower bound are correct.

Problem 8
The problem of merging two sorted lists arises frequently. We have seen a procedure for it as the
subroutine M ERGE in M ERGE S ORT. In this problem, we will prove a lower bound of 2n − 1 on the
worst-case number of comparisons required to merge two sorted lists, each containing n items. First we
will show a lower bound of 2n − o(n) comparisons by using a decision tree.
(a) Given 2n numbers, compute the number of possible ways to divide them into two sorted lists, each
with n numbers.
(b) Using a decision tree and your answer to part (a), show that any algorithm that correctly merges two
sorted lists must perform at least 2n − o(n) comparisons.
Now we will show a slightly tighter 2n − 1 bound.
(c) Show that if two elements are consecutive in the sorted order and from different lists, then they must
be compared.
(d) Use your answer to the previous part to show a lower bound of 2n − 1 comparisons for merging two
sorted lists.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy