07 Divide and Conquer
07 Divide and Conquer
Invented by Tony Hoare in the late 1950’s, the Quicksort algorithm was a breakthrough in sorting
methods. Variants of Hoare’s original algorithm are still a sorting method of choice today. In this worksheet,
you will gain experience with the divide and conquer algorithmic design approach, as well as the analysis
of recurrence relations, that led Hoare to this breakthrough. You will apply this algorithm in the next
worksheet to the problem of finding the median of a list of values.
2. T (n) = T (n/2) + 1
3. T (n) = T (n/4) + n
∗
Copyright Notice: UBC retains the rights to this document. You may not distribute this document without permission.
1
√
4. T (n) = 3T (n/9) + n log2 n
√
5. T (n) = nT (n/3) + n2
2
2 Quicksort Runtime Analysis
Here is a basic version of Quicksort. We will assume that the array A[1..n] to be sorted has n distinct
elements.
1. Suppose that QuickSort happens to always select the ⌈ n4 ⌉-th smallest element as its pivot. Give a
recurrence relation for the running time of QuickSort.
2. Using the recurrence, draw a recursion tree for QuickSort. Label each node by the number of elements
in the array at that node’s call (the root is labeled n) and the amount of time taken by that node
but not its children. Also, label the total work (time) for each "level" of calls. Show the root at level
0 and the next two levels below the root, and also the node at the leftmost and rightmost branches
of level i.
3
3. Find the following two quantities.
(a) The number of levels in the tree down to the shallowest leaf. Hint: Is the shallowest leaf on the
leftmost side of the tree, the rightmost side, or somewhere else? If you’ve already described the
problem size of the leftmost and rightmost nodes at level i as a function of i, then set that equal
to the problem size you expect at the leaves and solve for i.
(b) The number of levels in the tree down to the deepest leaf.
4. Use the work from the previous parts to find asymptotic upper and lower bounds for the solution of
your recurrence.
5. Now, we will relax our assumption that Quicksort always selects the ⌈ n4 ⌉-th smallest element as its
pivot. Instead, consider a weaker assumption that the rank of the pivot is always in the range between
⌈ n4 ⌉ and ⌊ 3n
4 ⌋ (the rank of an element is k if the element is the kth smallest in the array). What can
you say about the running time of Quicksort in this case?
4
3 The Stock Market, dividends and risks
A firm of financial analysts has hired you to select (efficiently) the stocks that they should recommend to
their clients. For each company, they have a pair of values (x, y) where
• y is a real number between -10 and 10 quantifying the risk of investing in the company (with -10
being high risk, and +10 being low risk).
So each company is represented by the point (x, y) in the plane. We will assume without loss of generality
that no two companies are mapped to the same point. Given two such points, we will say that a point
q = (q.x, q.y) dominates the point p = (p.x, p.y) if q.x ≥ p.x and q.y ≥ p.y. That is, q lies to the right of
and above p, as illustrated in picture on the left.
Clearly, a point p that is dominated by a point q is of no interest, since q is both worth more and less
risky than p. Hence, to help the firm decide which stocks to recommend, you want to only return companies
that correspond to maximal points: those that no other point dominates. These are the points p3 , p4 and
p6 in the figure on the right.
1. Describe an algorithm that takes as input a set P of points, and a point q, and returns all points of
P that q does not dominate. Hint: this is simple, so don’t think too hard about it.
5
2. In order to solve this problem using a divide-and-conquer algorithm, we need to divide it into two or
more subproblems. How many subproblems should we use, and is how is the input divided between
these subproblems?
3. If a point is maximal in the last one of the subproblems, is it automatically maximal in P ? Why or
why not?
4. If a point is maximal in the first of the subproblems, is it automatically maximal in P ? Why or why
not?
5. Suppose that a point p in the first subproblem is dominated by one or more points in the other sub-
problem(s) (assuming this is possible). Name one point in the other subproblem(s) that is guaranteed
to dominate x.
6
6. Using the results of the previous steps, write pseudo-code for an efficient algorithm MaximalPoints
that takes a input a set P of points, and return a set of all maximal points of P .
4 Bonus Problem
Can you extend the analysis of Quicksort to show that if the pivot is chosen randomly and uniformly from
among the elements, then the expected running time is Θ(n log n)? Note that we cannot assume that a
randomly chosen pivot is always in the range between ⌈ n4 ⌉ and ⌊ 3n
4 ⌋.