problemset5Calgary
problemset5Calgary
In this set you’re asked to design and analyze a divide and conquer algorithm for some given
problems. For each problem provide:
b. An analysis of the worst-case run-time of your algorithm. Your analysis should contain the
following:
i. Express the run-time of your algorithm as a recurrence relation. Explain why the recurrence
is correct.
ii. Guess a tight asymptotic bound for the run-time. For this you may use the Master Theorem,
the iteration method or the substitution method.
iii. Prove this bound. You’ll probably have to prove the upper and lower bound separately.
You do not have to prove in as much detail for this set as you did in the second set. For
example, it is sufficient to state that 3n2 + n lg n ∈ Θ(n2 ) without having to prove this fact.
iv. You’ll be asked to compare the asymptotic run-time of your algorithm with other algorithms
for the same problem. In the comparison use either o, ω or Θ.
i. To prove that your algorithm terminates, you will have to show that the terminating of your
algorithm will eventually be reached and that the recursion will unwind.
ii. To prove that your algorithm returns a correct result you will most likely do a proof by
induction where
i. The base case is the terminating condition of the algorithm.
ii. The inductive hypothesis assumes that the recursive call returns a correct result.
iii. The inductive step shows that the algorithm correctly uses the result(s) of the recursive
call(s).
1. Problem: Searching
• Input: Two binary search trees (BSTs) T1 and T2 . Note that these trees are not
necessarily balanced trees.
• Output: A binary search tree T3 such that
(a) All nodes in T3 are either in T1 or T2 ,
(b) All nodes in T1 are in T3 , and
(c) All nodes in T2 are in T3 .
The resulting tree also does not have to be balanced, but it must be a binary search
tree.
• Input Size: The size of the input is the number of nodes in T1 and the number of
nodes in T2 .
When designing your algorithm you may assume that all keys in T1 and T2 are unique and
that the following functions are available.
• The function Root(T ) returns the root node of the tree in Θ(1) time.
• The function Left(x) returns the node that is the root of the left subtree of tree node
x in Θ(1) time.
• The function Right(x) returns the node that is the root of the right sub-tree of the tree
node x in Θ(1) time.
• The function Key(x) returns the key that is stored at the tree node x in Θ(1) time.
When analyzing your divide and conquer algorithm, express the recurrence in terms of the
height of the binary trees T1 and T2 . Once you have a tight bound in terms of the height of
tree, then use this to give a worst case asymptotic run-time for a balanced binary tree and
for an unbalanced binary tree.
A very simple algorithm for this problem is taking all the nodes in T1 and T2 and creating a
new binary tree from scratch. It is possible to create a tree with n nodes in Θ(n) time.
Compare the following three:
A simple algorithm for this problem would merge the two lists and return the ith element
from the result. Compare your divide and conquer algorithm with this simple algorithm.
Your divide and conquer algorithm should be more efficient than this simple algorithm.