# Homework 3 Optional Problems

Prove that the worst-case expected running time of every randomized comparison-based sorting algorithm is . (Here the worst-case is over inputs, and the expectation is over the random coin flips made by the algorithm.)?

**ANSWER:** TBD

Suppose we modify the deterministic linear-time selection algorithm by grouping the elements into groups of 7, rather than groups of 5. (Use the “median-of-medians” as the pivot, as before.) Does the algorithm still run in time? What if we use groups of 3?

**ANSWER:** The median for odd number of elements is the middle element, for even number it’s the first of 2 middle elements. For example, the median of 5 elements is the 3rd element, and it’s also the 3rd for 6 elements. Therefore, the median is the element for all 1. Consider the following array:

```
x x x x x x
x x x x x x x
x x x X x x x
x x x x x x x
x x x x x x
```

At least half of the groups contribute at least elements that are greater than , except for the one group that has fewer than elements if does not divide exactly, and the one group containing itself. Discounting these two groups, it follows that the number of elements greater than is at least . It follows from the definition of ceiling that this expression . Similarly, at least elements are smaller than . Thus, in the worst case, the size of the subarray in the recursive SELECT call is .

Therefore, the recurrence relation is:

This recurrence is one of the following generic form:

The solution to the above is given by:

Since

Similarly, for , the number of elements greater than is at least:

Therefore, the recurrence relation is

Since

For , the number of elements greater than is at least:

Therefore, the recurrence relation is

Since

Given an array of n distinct (but unsorted) elements with such that , a weighted median is an element for which the total weight of all elements with value less than is at most , and also the total weight of elements with value larger than is at most . Observe that there are at most two weighted medians. Show how to compute all weighted medians in worst-case time.

**ANSWER:** The weighted-median algorithm works as follows. If , we just return the brute-force solution. Otherwise, we proceed as follows (the pseudocode handles the special case of two medians, this description only applies to finding the lower/only weighted median).
We find the actual median of the n elements and then partition around it. Then compute the total weights of the two halves. If the weights of the two halves are each , then the weighted median is . Otherwise, the weighted (lower) median should be in the half with total weight exceeding . The total weight of the “lighter” half is added to the weight of , and the search continues with the heavier half.

```
WEIGHTED-MEDIAN(x, w, n)
if n == 1
return x[1]
else if n == 2
// lower and upper weighted medians
if w[1] == w[2]
return {x[1], x[2]}
else if w[1] < w[2]
return {x[2]}
else
return {x[1]}
else
// using randomized linear-time selection
find the median x[k] of X = {x[1], x[2], ..., x[n]}
// using the O(n) partition algorithm from QuickSort
partition the set X around x[k]
// using linear scan
compute W[L] = sum(x[i] < x[k]) w[i]
and W[R] = sum(x[i] > x[k]) w[i]
// x[k] = lower weighted median
if W[L] < W/2 and W[R] == W/2 // (1)
return {x[k], x[k + 1]}
// x[k] = upper weighted median
else if W[L] == W/2 and W[R] < W/2 // (2)
return {x[k - 1], x[k]}
// x[k] = weighted median
else if W[L] ≤ W/2 and W[R] ≤ W/2 // (3)
return {x[k]}
// by definition of weighted median,
// it must be on the heavier side
else if W[L] > W/2 // (4)
w[k] = w[k] + W[R]
X' = {x[i] ∈ X: x[i] ≤ x[k]}
return WEIGHTED-MEDIAN(X')
else // (5)
w[k] = w[k] + W[L]
X' = {x[i] ∈ X: x[i] ≥ x[k]}
return WEIGHTED-MEDIAN(X')
```

Example 1:

```
x = {1, 2, 3, 4, 5}, w = {.15, .1, .2, .3, .25}, n = 5
W = 1, W/2 = .5
Iteration 1:
k = 3, x[3] = 3, w[3] = .2, W[L] = .25, W[R] = .55
Case 5
recurse with x = {3, 4, 5}, w = {.45, .3, .25}, n = 3
Iteration 2:
k = 2, x[2] = 4, w[2] = .3, W[L] = .45, W[R] = .25
Case 3
return {4}
```

Example 2:

```
x = {1, 2, 3, 4}, w = {.49, .01, .25, .25}, n = 4
W = 1, W/2 = .5
Iteration 1:
k = 2, x[2] = 2, w[2] = .01, W[L] = .49, W[R] = .50
Case 1
return {2, 3}
```

We showed in an optional video lecture that every undirected graph has only polynomially (in the number n of vertices) different minimum cuts. Is this also true for directed graphs? Prove it or give a counterexample.

**ANSWER:** TBD

For a parameter , an -minimum cut is one for which the number of crossing edges is at most times that of a minimum cut. How many -minimum cuts can an undirected graph have, as a function of and the number of vertices? Prove the best upper bound that you can.

**ANSWER:** . Proof TBD.