Exer4 W23

You might also like

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

CMPUT 204

Exercise #4 Department of Computing Science


University of Alberta

This Exercise Set consists of two groups of questions. The first group, Problems 1-9, are to be used
for Quiz 4, i.e., these problems or their variants will be selected for the quiz. The additional questions of
Problems 10-12 are for practice. Working around them can further build your skills in algorithmic problem
solving.

Problem 1. CLRS Exercise 6.5-2, Problem 6.1 (p165-167), Exercises 7.2-3, 7.2-4 (p178).

Problem 2. BST, AVL, and Hash Table

a. Show the result after each operation below, starting on an NIL BST: Insert(16), Insert(4),
Insert(3), Insert(9), Insert(1), Insert(44), Insert(29), Delete(1), Delete(4), Insert(34).

b. Draw an AVL tree of height 4 that contains the minimum number of nodes.

c. Delete a leaf node (any leaf node) from the AVL tree you showed above so that the resulting tree
violates the AVL-property. Then, apply tree-rotation to turn it to an AVL tree.

d. Perform a right rotation around the root node h to fix it.

e. Exercises 11.2-2 and 11.2-3 (CLRS p261)

Problem 3. Give an algorithm using the techniques and data structures you have seen in the course so
far, for the following problem: the input is an array A of size n of integers, find the k largest elements of
A and return them. Your algorithm must run in O(n + k log n). Explain your algorithm and analyze its
running time. What is the largest value of k for which your algorithm is still linear in n?

Problem 4. Consider the following very simple and elegant(?) sorting algorithm:
SomeSort (A, b, e)
if e = b + 1 then
if A[b] > A[e] then
exchange A[b] and A[e]
end if
else if e > b + 1 then
p ←− ⌊ e−b+1
3 ⌋
SomeSort (A, b, e − p)
SomeSort (A, b + p, e)
SomeSort (A, b, e − p)
end if

1
a. Explain why SomeSort correctly sorts its input array A, given that n = e − b + 1 is the size of A.
b. Find a recurrence for the worst-case running time of SomeSort. Give a tight (i.e. Θ) asymptotic
bound for the worse-case running time of SomeSort (Hint: for simplicity assume that n = 3k for some
constant k).
c. By comparing SomeSort with insertion sort, merge sort, heap-sort, and quicksort argue if this simple
algorithm is efficient.

Problem 5. A d-ary heap is like a binary heap, except that non-leaf nodes have d children instead of 2
children (with one possible exception for the last leaf).
a. Explain how to implement a d-ary heap in an array and implement the corresponding Parent and
Child operation as we had for the binary heap.
b. What is the height of a d-ary heap of n elements in terms of n and d?
c. Write a version of Max-Heapify for a d-ary max-heap and analyze its running time.
d. Write a version of Build-Heap for a d-ary max-heap and analyze its running time.
e. Give an efficient implementation of Extract-Max in a d-ary heap and analyze its running time.
f. Give an efficient implementation of Increase-Key(A, i, k), which first sets A[i] ←− max(A[i], k) and
then updates the d-ary heap appropriately. Analyze the running time.
g. Write a version of Insert in a d-ary heap and analyze its running time.

Problem 6.
a. Sorting lower bound can be argued by proof-by-contradiction. Suppose that there exists an algorithm
making < log(n!) comparisons for any instance. Show that this leads to a contradiction.
b. Suppose we are given a sorted list of n numbers, X = x1 ≤ x2 ≤ · · · ≤ xn , and we are asked to check
whether or not there are any duplicates in the list. We are limited to algorithms which compare pairs of
list elements (with a procedure Compare(i, j) which returns <, > or =, depending on the values of xi and
xj ). Assume the algorithm returns either Distinct (if there are no duplicates) or “i-th element equals
j-th element if xi = xj . Note that if there are multiple ties, any one can be reported. Prove a good lower
bound (i.e., the largest lower bound possible) for the number of calls to Compare to solve this problem.

Problem 7. Propose an efficient algorithm for the following problem. Given a BST T and a value v,
your algorithm should return the number of elements in T which are ≤ v. Prove the correctness of your
algorithm, and justify its runtime. (Hint: you may add a field size that keeps track of the number of nodes
in T .)

Problem 8. Recall that in a binary tree each node has at most 2 children. Suppose T is a binary tree
with |T | = n (i.e. has n nodes). For every node v we use Lv and Rv to denote the left subtree and right
subtree of v, respectively. We say the subtree rooted at v is nearly balanced if 12 ≤ |Rv |/|Lv | ≤ 2, i.e. the
sizes of the two subtrees are within a factor two of each other.
a. Suppose that the root of T is nearly balanced. What is the maximum height of T in terms of n?
Explain why.

2
b. We say the whole tree is nearly balanced if for every node v of T , the subtree rooted at v is nearly
balanced. Denote h as the height of T . Show that if T is nearly balanced then h ∈ O(log n) by first showing
that h ≤ ⌈log 3 n⌉.
2

Problem 9. An evil king has a cellar containing n bottles of expensive wine and his guards have just
caught a spy trying to poison the king’s wine. Fortunately, the guards caught the spy after he succeeded in
poisoning only one bottle. Unfortunately, they don’t know which one. To make matters worse, the poison
the spy used was very deadly — just on drop diluted even a billion to one will still kill someone. Even so,
the poison works slowly — it takes a full month for the person to die. Design an algorithm that allows the
evil king to determine exactly which one of his wine bottles was poisoned in just one month’s time while
expending at most O(log n) of his wine tasters. Prove your claims.

Extra Exercises for Practice

Problem 10. Given a heap A containing n keys and an element x. Propose an efficient algorithm for
reporting (say printing) all the keys in A that are greater than or equal to x (note that x is not necessarily
in A). The running time of your algorithm should be O(k) where k is the number of elements reported by
the algorithm.

Problem 11. Consider a modification of the selection-sort algorithm we saw in the first set of exercises
where instead of finding the smallest element in the unsorted part of the array and swapping it to be in its
proper place, we instead perform a sequence of swaps to move the smallest element to its proper location,
as shown below. Show that this algorithm, which is known as bubble-sort, runs in Θ(n2 ) time.
BubbleSort (A)
** Return array A of n sorted in nondecreasing order.
for i ←− 1 to n − 1 do
for j ←− n to i + 1 do
if A[j − 1] > A[j] then
Swap A[j − 1] and A[j]
end if
end for
end for
return A

Problem 12. Suppose you are given a set of small boxes, numbered 1 to n, identical in every aspect
except that each of the first i contains a pearl whereas the remaining n − i are empty. You can have two
magic wands that can each test if a box is empty or not in a single touch, except that a wand disappears if
you test it on a box that is empty. Show that, without knowing the value of i, you can use the two wands

to determine all the boxes containing pearls using no more than 2 n wand touches.

You might also like