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

Practice Problems: Divide and Conquer

This document contains 4 practice problems related to divide and conquer algorithms: 1. An O(log n) algorithm is described to find the minimum value of a monotonically decreasing function in constant time by evaluating the function at carefully chosen values. 2. O(n) algorithms are described for solving the left and right maximal partial sum problems, which are then used to develop an O(n log n) divide-and-conquer algorithm for the maximum partial sum problem. 3. An O(log n) algorithm is asked to be described for finding the largest element in a sorted array that has been circularly shifted. 4. Recurrence equations are asked to be defined and solved for the

Uploaded by

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

Practice Problems: Divide and Conquer

This document contains 4 practice problems related to divide and conquer algorithms: 1. An O(log n) algorithm is described to find the minimum value of a monotonically decreasing function in constant time by evaluating the function at carefully chosen values. 2. O(n) algorithms are described for solving the left and right maximal partial sum problems, which are then used to develop an O(n log n) divide-and-conquer algorithm for the maximum partial sum problem. 3. An O(log n) algorithm is asked to be described for finding the largest element in a sorted array that has been circularly shifted. 4. Recurrence equations are asked to be defined and solved for the

Uploaded by

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

Practice problems: Divide and conquer

1. (exam1 fall 2003) In this problem we consider a monotonously decreasing function f :


N → Z (that is, a function defined on the natural numbers taking integer values, such that
f (i) > f (i + 1)). Assuming we can evaluate f at any i in constant time, we want to find
n = min{i ∈ N |f (i) ≤ 0} (that is, we want to find the value where f becomes negative).

12 n

We can obviously solve the problem in O(n) time by evaluating f (1), f (2), f (3), . . . f (n).
Describe an O(log n) algorithm. (Hint: Evaluate f on O(log n) carefully chosen values ≤ n
and possibly at a couple of values between n and 2n - but remember that you do not know
n initially).
2. (exam1 fall 2003) The maximum partial sum problem (M P S) is defined as follows. Given
an array A[1..n] of integers, find values of i and j with 1 ≤ i ≤ j ≤ n such that
X
j
A[k]
k=i

is maximized.

Example: For the array [4,-5,6,7,8,-10,5], the solution to M P S is i = 3 and j = 5 (sum


21).

To help us design an efficient algorithm for the maximum partial sum problem, we consider
the left position ` maximal partial sum problem (LM P S` ). This problem consists of finding
value j with ` ≤ j ≤ n such that
X
j
A[k]
k=`
is maximized. Similarly, the right position r maximal partial sum problem (RM P Sr ), consists
of finding value i with 1 ≤ i ≤ r such that
X
r
A[k]
k=i

1
is maximized.

Example: For the array [4,-5,6,7,8,-10,5] the solution to e.g. LM P S4 is j = 5 (sum 15)
and the solution to RM P S7 is i = 3 (sum 16).

(a) Describe O(n) time algorithms for solving LM P S` and RM P Sr for given ` and r.
(b) Using an O(n) time algorithm for LM P S` , describe a simple O(n2 ) algorithm for solving
M P S.
(c) Using O(n) time algorithms for LM P S` and RM P Sr , describe an O(n log n) divide-
and-conquer algorithm for solving M P S.

3. Suppose you are given an array A[1..n] of sorted integers that has been circularly shifted
k positions to the right. For example, [35, 42, 5, 15, 27, 29] is a sorted array that has been
circularly shifted k = 2 positions, while [27, 29, 35, 42, 5, 15] has been shifted k = 4 positions.
We can obviously find the largest element in A in O(n) time. Describe an O(log n) algorithm.
4. In this problem we consider divide-and-conquer algorithms for building a heap H on n ele-
ments given in an array A. Recall that a heap is an (almost) perfectly balanced binary tree
where key(v) ≥ key(parent(v)) for all nodes v. We assume n = 2h − 1 for some constant h,
such that H is perfectly balanced (leaf level is “full”).
First consider the following algorithm SlowHeap(1, n) which constructs (a pointer to) H by
finding the minimal element x in A, making x the root in H, and recursively constructing
the two sub-heaps below x (each of size approximately n−1
2 ).

SlowHeap(i, j)
If i = j then return pointer to heap consisting of node containing A[i]
Find i ≤ l ≤ j such that x = A[l] is the minimum element in A[i . . . j]
Exchange A[l] and A[j]
P trleft = SlowHeap(i, b i+j−1
2 c)
i+j−1
P trright = SlowHeap(b 2 c + 1, j − 1)
Return pointer to heap consisting of root r containing x with child pointers
P trleft and P trright
End

a) Define and solve a recurrence equation for the running time of Slowheap.
Recall that given a tree H where the heap condition is satisfied except possibly at the
root r (that is, key[r] ≥ key[leftchild(r)] and/or key[r] ≥ key[rightchild(r)] and key[v] ≥
key[parent(v)] for all nodes v 6= r), we can make H into a heap by performing a Down-
Heapify operation on the root r (Down-Heapify on node v swaps element in v with ele-
ment in one of the children of v and continues down the tree until a leaf is reached or heap
order is reestablished).
Consider the following algorithm FastHeap(1, n) which constructs (a pointer to) H by plac-
ing an arbitrary element x from A (the last one) in the root of H, recursively constructing
the two sub-heaps below x, and finally performing a Down-Heapify operation on x to make
H a heap.

2
FastHeap(i, j)
P trleft = FastHeap(i, b i+j−1
2 c)
P trright = FastHeap(b i+j−1 2 c + 1, j − 1)
Let P tr be pointer to tree consisting of root r containing x = A[j] with child
pointers P trleft and P trright
Perform Down-Heapify on P tr
Return P tr
End

b) Define and solve a recurrence equation for the running time of Fastheap.

You might also like