CS510 ProblemSet 3 Divide N Conquer
CS510 ProblemSet 3 Divide N Conquer
CS510 ProblemSet 3 Divide N Conquer
Problem 1. Given a sorted array of distinct integers A[1...n], you want to find out
whether there is an index i for which A[i] = i. Give a divide and conquer algorithm that
runs in O(log n) time to find out an index i if it exists.
What are the running times of each algorithm and which would you choose and why?
Problem 3. Assume that n = 2k for some positive integer k. Using induction prove that
if T (n) is given as follows, then T (n) = n log n.
2 if n = 2
T (n) = n
2T 2 + n if n > 2
n(n+1)
Show, by induction, that T (n) = 2
Problem 7. Given a binary string S of type{1m 0n }, devise an algorithm that finds the
number of zeroes in O(log k) time. Let m + n = k
Problem 8. The Tower of Hanoi is a classic puzzle invented by the French Mathematician
Edouard Lucas in 1883. In this puzzle there are 8 disks, initially stacked in decreasing
order of size one of three pegs. We have to move all of the disks to another peg while
following these two rules
Design an algorithm to solve this problem and analyze the running time of your algorithm.
Hint : Think how you can solve the problem if there were only 2 disks instead of 8 and
see if you can build from that.
Problem 9. [K-way Merge] Suppose you have k sorted arrays A1 , A2 , ...Ak each with
n elements. You want to combine them into a single sorted array of size kn. One way
to do this would be to use the merge operation we discussed in class. First merge arrays
A1 , A2 then merge the result with A3 and so on
Algorithm 1 :
function fib(n)
if n == 0 or n == 1 then
return 1
else
return fib(n-1) + fib(n-2)
• A lot of computation seems to be repeated over and over in this algorithm. For
example f ib(4) would compute f ib(3) and f ib(2) and then the called f ib(3) would
also compute f ib(2). Is there a way we could avoid computing the same thing over
and over again while still using a recursive approach?
It turns out that the worst case input for this algorithm is p = f ib(n), q = f ib(n − 1).
Analyze the running time of the algorithm for this input.
Hint : Recall the identity f ib(n) = f ib(n − 1) + f ib(n − 2). Also note that f ib(n) ≤
2f ib(n − 1)
Problem 12. Given the following recurrence relations, find the running time (in big-O
notation) using the recursion tree method, substitution method and the Master Theorem.
•
8T n + Θ(n2 )
if n ≥ 2
T (n) = 2
1 else
•
3T n + n log n
if n ≥ 2
T (n) = 4
1
else
•
√
2T n + n
if n ≥ 2
T (n) = 4
1 else
Problem 13. Flavius Josephus was a famous historian of the first century. During the
Jewish-Roman war, he was captured along with 40 other jewish rebels by the Romans.
The rebels preferred suicide to capture and so decided that they would form a circle and
kill every third person in the circle until no one was left. Josephus and one of his friend
didn’t want suicide so Josephus quickly calculated where the two should stand in the
circle so that they would be the last one’s left.
• Come up with a more general solution where instead of 41, there are n people in
the circle.
Problem 14. Suppose that you somehow know that the number of inversions in an array
of size N is 10 where 10 N . Which of the following sorting algorithms should you
use to sort this array completely: Merge Sort, Selection Sort, Insertion Sort. Give an
appropriate reason for your choice.
Problem 15. Suppose you are given an array X of m integers, devise an efficient algo-
rithm to find the sum of the total number of inversions in all sub-arrays of X of length
l. Hint: you should find the number of inversions in each of the (m − l + 1) sub-arrays
of length l and simply add them.
Problem 17. A bank confiscates n bank cards suspecting them to be involved in fraud.
Each card corresponds to a unique account but an account can have many cards cor-
responding to it. Therefore, two bank cards are equivalent if they belong to the same
account. The bank has a testing machine for finding out if two cards are equivalent.
They want to know that among the collection of n cards, are there more than n/2 cards
that are equivalent. The only operation that the machine can do is to select two cards
and tests them for equivalence. You are required to come up with a solution to this using
only O(n log n) invocations of the testing machine.
Problem 18. Prove the correctness of the following algorithm for incrementing natural
numbers. Analyze the algorithm to find out how many times is line 3 executed in the
worst case.
Problem 19. Prove the correctness of the following recursive algorithm for the multipli-
cation of two natural numbers is correct, ∀ integer constants ≥ 2. Analyze this algorithm
to find out how many times line 5 executed in the worst case.
Problem 20. Consider an n − node complete binary tree T , where n = 2d − 1 for some
d. Each node v of T is labeled with a real number xv . You may assume that the real
numbers labeling the nodes are all distinct. A node v of T is a local minimum if the label
xv is less than the label xw for all nodes w that are joined to v by an edge.
You are given such a complete binary tree T , but the labeling is only specified in the
following implicit way: for each node v,you can determine the value xv by probing the
node v. Show how to find a local minimum of T using only O(log n) probes to the nodes
of T .
Problem 21. An investment company has stock prices for n consecutive days. That is
for each day, they have a specific stock price. They want to know on which day they
Problem 22. Suppose you are given an array A with n entries, with each entry holding a
distinct number. You are told that the sequence of values A[1], A[2], ..., A[n] is unimodal.
For some index p between 1 and n, the values in the array entries increase up to position
p in A and then decrease the remainder of the way until position n. (So if you were
to draw a plot with the array position j on the x-axis and the value of the entry A[j]
on the y-axis, the plotted points would rise until x-value p, where they’d achieve their
maximum, and then fall from there on.) You’d like to find the "peak entry" p without
having to read the entire array in fact, by reading as few entries of A as possible. Show
how to find the entry p by reading at most O(log n) entries of A.