Homework problem H

Problem H

Assigned Oct 7, due Oct 16.

This problem is loosely inspired by the topic of order statistics.

Partition problem: Given a set S of n distinct positive integers, determine if there exists a partition of the set into two parts A and B in such a way that the sum of the numbers in A equals the sum of the numbers in B.

Definition: The sets A and B form a partition of S if S = A ∪ B and ∅ = A ∩ B.

An algorithm for the problem:

bool partition(Ptr b, Ptr e) 
/* [b..e) references a range of positive integers.
 * Return true iff a partition of the values in range [b..e) exists
 * solving the partition problem.
 */
{	integer S = 0;
	for(Ptr i = b; i < e; ++i) S += *i;
	if (S is odd) return false;
	else return SubsetSum(b, e, S/2);
}

bool SubsetSum(Ptr b, Ptr e, integer T) 
/* [b..e) references a range of positive integers.
 * T is a nonnegative integer.
 * Return true iff a subset from the range [b..e) exists whose sum is exactly T.
 */
{	size_t n = e - b;
        if (n == 0) return T == 0;
	if (T < *b) return SubsetSum(b+1, e, T); // *b can't be used.
	// else solution not using *b or solution using *b to be checked.
	return SubsetSum(b+1, e, T) 
	    || SubsetSum(b+1, e, T - *b);
}
Correction Oct 9: recursive calls not to "targettedSubsetSum"
Second Correction Oct 9: On part b of the exercise below, Ω(2n) rather than Ω(n2n). Also note that you can prove O(2n) for part a.

Exercise:

  1. (9 points) Show that the worst case run time of partition(b,e) is O(n2n), where n = e - b.
  2. (0.9 points) Show that the worst case runtime of partition is Ω(2n).

    Hint: Consider the data set 2, 4, ..., 2n.

  3. (0.1 points) Can you do better than the given algorithm?

Continuing a trend, some relatively harder parts are assigned few points. A perfectly respectable score on this problem is 9 points. The somewhat harder part b is given for those with the time and interest to take on the challenge. In the larger sense, it is worth the effort. However from the point of view of grades, I don't want to penalize those for whom it is a stumper or who just don't have the time. You are advised not to work on part c too much. On the other hand, if you do significantly better than the given algorithm, you could get famous.