CS 432, Algorithms
Problems Set #1, Fall 2005


To turn in:

Turn in solutions to these problems:

Important: Put the answer to each question on a separate sheet of paper (or one problem on front and another on the back). This is to make grading easier for us.

Note: Soon there will be a programming problem based on Problem B3. This will be optional, but if you want to start thinking about coding up the solutions to that, feel free to get started.

Collaboration Rules for This Assignment:

You may talk about the problems with other students in the course and TAs, or you can work alone. If you talk with or collaborate with other students, you must list their names at the first page of your paper. (Failure to do so violates the collaboration policy for CS432.) If you work alone, write a pledge that you worked alone without collaboration on the assignment.

If you collobarate with other students, a group of three students (and no more) may agree to turn in a group write-up of the solutions. List all group members on the paper, and pledge that all members made a reasonable contribution to working out the solutions. However, any of the group members may choose to turn in an individual write-up of the solutions. If you choose to do this, you cannot not simply copy all or part of another group member's write-up. (We will investigate this if we observe it.) You should still list your collaborators on the first page of your paper.

The goal of collaboration among students is simple. you can help each other learn. If what you do for another student simply lets them turn in the assignment without helping them learn in the process, this is not acceptable.


Problem B1: General Problem Solving

Rotate a vector or array of n elements left by i positions. In other words, x[1]...x[i] x[i+1] ... x[n] becomes x[i+1]...x[n] x[1]...x[i]. So if i=3 then "abcdefg" becomes "defgabc". Another way of saying this is that AB become BA where A is the subsequence of the first i elements and B is the remaining elements.

The obvious solution is use a temporary scratch vector of proportional to n to build the solution, but this has space complexity BigTheta(n). Do it with just a constant number of extra variables, i.e. in BigTheta(1).

(One real-life example of this problem is cut-and-paste in a word processor. You chose a block of bytes, cut them, and then paste them somewhere else. So the block of bytes that looks like this ABCD becomes ACBD when you cut A and paste it between C and D.)


Problem B2: General Problem Solving

You're given a large dictionary of English words (n words). Find an efficient way to find all sets of anagrams (i.e. words that are composed of the same letters). To describe the efficiency of your solution, you may assume all words have size m (if you need to know this). If you need it, you can assume you have an efficient sort algorithm, i.e one that's BigTheta(n lg n).


Problem B3: General Problem Solving and Divide and Conquer

Given an input vector X of n floating point values (pos. and neg.), a contiguous subvector X[i]...X[j] is the elements stored between positions i and j in X. Each contiguous subvector has a sum associated with it that is calculated by adding up all elements in the subvector.

Your problem: find and print the maximum sum of any contiguous subvector X[i]…X[j] . You do not have to remember the start and end positions of the subvector (though it would be easy to add this.)

If all the elements are positive, then the answer clearly is the entire vector. But if the elements are a mixture of positive and negative values, then an algorithm is required to find the sum of the "maximum subvector". (Note: if all values are negative, we'll treat this as a special case where the best vector is the "empty" subvector, with sum zero.

Example: If our vector contains these values:

31 -41 59 26 -53 58 97 -93 -23 84

then the subvector with the max sum is found in the range from i=2 to j=6 (where indexing starts at zero):

59 26 -53 58 97

There are some obvious solutions. Think of those first. What are there time-complexities?

(A) But have you considered a recursive divide and conquer approach? The approach needed is similar to the closest-pair problem in that that you find solutions for each half of the input, but you must also find a possible solution that "spans" the dividing line between the two halves. Give the recurrence relation and the order-class for this algorithm's complexity.

(B) Can you think of an approach that does not require you to read all the data into a vector or array, but instead lets you read each item in the input stream once without saving it? (This would be an example of an on-line algorithm.) Find an algorithm that works this way and argue that it has linear time-complexity.


Problem O1 (From Baase textbook, p. 67, problem 1.50.)

You have a set of n gold coins that should have the exact same weight, but you know that exactly one of them is fake and has a different weight.You also have a balance scale, which allows you to compare two piles of coins to see which pile is lighter or if they're equal. You can assume that the fake coin is lighter than a real one.

Describe an algorithm for finding the fake coin by doing the fewest weighings. Give a formula for the number of weighings. Does your algorithm have to change if you don't know in advance whether there's a fake coin or not?


Problem O2

(A) We've seen an algorithm that finds the maximum element in a sequence in n-1 operations by scanning through the sequence from the 2nd to the last item, updating the current maximum if needed. But, those of you who are sports fans know that tournaments or "play-offs" find the best team (or player) by organizing "comparisons" (i.e. games) between pairs of teams, with the winner advancing to play some other winner in another round. The winner of the final round is the best (or maximum) of the original set.

Question: Assuming that the number of teams n is some power of 2 (say 2k), show the number of comparisons needed in such a play-off to find the best team. (Hint: it will help if you draw out a tournament tree and think about the math associated with binary trees.)

(B) If we want to find the 2nd best team (or say the 2nd largest item), then we can implement this by doing two "scans" through a list of items. The first finds the maximum, which could then be moved to the first position. The second scan skips the first item (the max) and finds the next largest. Convince yourself that you understand this approach, and that it does 2n-3 comparisons.

Question: Describe an algorithm that uses the "play-off" approach listed above that finds the second largest in fewer operations. Give the formula for the number of comparisons your algorithm uses. (The key to this approach is to reduce the number of possible candidates for the 2nd largest item. It might be that it could require more complicated processing to store and remember things, but for this question we're just trying to reduce the number of comparisons.)


Problem O3

Let a and b be real numbers > 1. If a < b show that an belongs to LittleOh(bn), that is an grows more slowly than bn.


Problem O4

Prove lg n belongs to LittleOh(nk) for any k > 0. This of course means that the log function grows more slowly than any polynomial (even those with fractional powers, e.g. 0.00001).



Problem O5

Prove nk belongs to LittleOh(an) for any k > 0 and a > 1. You're thus proving that all polynomials (no matter how large their degree) grow more slowly than any exponential function an when a>1.

Hint: you may need the following derivative, and you may need to use it more than once:
     the derivative of

Also, you can assume that k is a whole number for this proof.


Problem O6

Draw the decision tree for the binary search algorithm with n=17.


Problem O7

Write a divide and conquere algorithm to find both the maximum and the minimum values in an array. See the slides "Recurrences, Divide and Conquer", slide 31, for more details. (We talked about this in class, BTW.) Also, give the recurrence relation for the wost-case number of comparisons and solve this equation. How does this compare to the more obvious solution of just linearly search the array looking for both the largest and smallest element?



Problem R1: Recurrence Relations:

Can you use a theorem to find the order class for these? Or, can you successfully use the iteration method? Assume T(1) = 1 when you want to solve these with the iteration method.

  1. T(n) = T(n/2) + lg n
  2. T(n) = T(n/2) + n
  3. T(n) = 2T(n/2) + n (kind of like mergesort)
  4. T(n) = 2T(n/2) + n lg n
  5. T(n) = T(n-1) + 1 (like selection sort)
  6. T(n) = T(n/2) + 1 (like binary search)
  7. T(n) = 2T(n-1) + 1 (like Towers of Hanoi)


Problems from Algorithms textbook, Section 2.3, pp. 51-54
(Solutions for problems marked "S" begin on page 656)