CSCI 256
Design and analysis of algorithms
Solutions to Assignment 5

Due Monday, 3/5/2001


Only turn in problems from the second section.

Practice problems:

  1. An array T contains n elements. Rather than finding the ith smallest element, we want to find all of the i smallest elements for some i <= n. Find an efficient algorithm to solve the problem.

    Solution: Use the select algorithm with worst-case time O(n) to find the ith smallest element. Because of the way that works, all elements to the left of it would be smaller than i. Sort those (efficiently - e.g., mergesort or heapsort) for O(i lg i) and a total complexity of O(n + i lg i). If i is small then it will be O(n), but if it is large (e.g., i = n), then it will be O(n lg n).

  2. Let T[1..n] be a sorted array of distinct integers, some of which may be negative. Give an algorithm that can find an index i such that 1 <= i <= n and T[i] = i, given such an element exists. Your algorithm should take time O(lg n) in the worst case.

    Solution: Use the ideas of binary search:

       Fixed(T,i,j) // Find i <= k <= j s.t. T[k] = k
         if j > i return "no solution" // don't worry about type problems
         k = (i+j)/2
         if T[k] = k then 
           return k
         elseif T[k] < k then 
           return Fixed(T,k+1,j)
         else
           return Fixed(T,i,k-1)
    
    To get started, call Fixed(T,1,n).

    Why does this work? Suppose T[k] = m< k, then because elements of T are distinct integers, and they are sorted, T[k-1] <= m-1 < k-1. Thus no subscript less than k could possibly work. Therefore we can narrow the search to T[k+1..j]. Similarly if T[k] > k, we know that nothing greater than k can work. Therefore we can narrow the solution to T[i..k-1].

    The complexity is exactly the same as binary search: T(n) = T(n/2) + 1, with solution Theta(lg n).

Problems to be turned in:

  1. Let T[1..n] be an array of n integers. An integer is a majority element in T if it appears strictly more than n/2 times in T. Give an algorithm to decide whether an array T[1..n] has a majority element, and, if so, find it. Your algorithm must be O(n).

    Solution: Suppose x is a majority element. Therefore it must also be the median element. If it were the smallest and there are more than n/2 copies, then if the list were sorted, x certainly must fill all the slots from 1 to floor(n/2)+1 slots (and possibly more). Similarly if it were the largest element and the list were sorted, it would fill at least the slots from ceiling(n/2)-1 to n. Of course if it were in between the largest and smallest it would also fill the median slot. Thus, no matter what, if there is a majority element, it would be the median element of the list.

    Now, of course, we don't want to sort the list. Instead use the select algorithm to find the median element, x, in guaranteed time O(n). That element is the only possible candidate for majority element, so sweep through the entire list counting up the number of x's. If more than n/2 then it is majority, and if not, there is no majority element.

  2. A celebrity is defined to be someone who is known by everyone, but who does not know anyone. Let n be the number of people in a group. Find a linear time (in n) algorithm to identify the celebrity or discover that there is no celebrity. The only operation allowed is to ask someone if they know the person the asker is pointing to. Hint: Think inductively. How can you use the answer for groups of n-1 to solve the problem for groups of size n? Consider doing some (a constant amount) of work before deciding out whom to exclude from the group.

    Solution: The key to solving this is to eliminate a possible celebrity before making the recursive call. Ask person A if they know person B. If the answer is yes, then A must not be a celebrity. If the answer is no, then B must not be a celebrity. Call the person we now know is not a celebrity, G, for groupie.

    Run the algorithm to find out if the group of n-1 people different from G has a celebrity, C. Clearly C is the only one of the n who might possibly be a celebrity, as we already know the groupie is a non-celebrity and no one else in the group of n-1 can be a celebrity (after all, they know G - disqualifying themselves from being celebrities). Ask G if she knows C and C if she knows G. If the answer to the first is yes and the second is no, then C is a genuine celebrity (everyone knows her and she knows no one). Any other set of answers show that C is not a celebrity and thus there are no celebrities in the group.

    The complexity of the algorithm is given by T(1) = 0, T(2) = 2, and T(n) = 1 + T(n-1) + 2 for n > 2. It is easy to show that the solution is T(n) = 3n - 4 for n > 1, which is in Theta(n).

  3. You have probably all run into the towers of hanoi problem. If not, it starts out with n disks of different sizes arranged on a peg in decreasing order of sizes (i.e., smallest on top). There are two other empty pegs. The goal is to move all the disks, one at a time, from the first peg to another chosen peg obeying the following rules for moves: Disks are moved from the top of one peg to another. A disk can be moved to a peg only if it is smaller than all other disks on that peg.

    1. Design an algorithm (by induction of course) to find a minimal sequence of moves that solves the towers of hanoi puzzle for n disks.

      Solution: Not surprisingly, the algorithm is recursive:

         Hanoi(start,end,middle,n) // algo to move n disks from pegs start to end 
                                   // using middle peg to help, following rules
           if n = 1 then
              move a disk from start to end
           else
              Hanoi(start,middle,end,n-1)
              move a disk from start to end
              Hanoi(middle,end,start,n-1)

    2. How many moves are used in your algorithm? Construct a recurrence relation for the number of moves, and solve it.

      Solution: T(1) = 1 and T(n) = 2T(n-1) + 1 for n > 1. We can solve this by iterated substitution:

         T(n) = 2T(n-1) + 1
              = 2(2T(n-2)+1) + 1 = 22T(n-2) + 2 + 1
              = 22(2T(n-3)+1) + 2 + 1 = 23T(n-3) + 22 + 2 + 1
              = 2kT(n-k)  + (2k-1 + ... + 2 + 1)
      Thus if k = n-1 we get
         T(n) = 2n-1T(1) + (2n-2 + ... + 	2 + 1) 
              = 2n-1T(1) + (2n-1 - 1)
              = 2n - 1

      Thus the algorithm is Theta(2n).
    3. Prove (by induction) that the number of moves in part b is optimal. That is, prove that there cannot exist any other algorithm that uses less moves. Hint: The key is to think about under which circumstances that largest disk can be moved.

      Solution: We prove by induction that we cannot get by with less than 2n - 1 moves for n disks.

      n=1: Clearly 1 = 21 - 1 move is necessary as otherwise the disk isn't moved.

      n>1: Suppose n-1 disks take at least 2n-1 - 1 moves. To successfully move n disks, clearly must move the bottom (nth) disk. To be able to do this, you must clear the other n-1 disks off of it (you can only move 1 disk at a time). Moreover there must be an empty peg available for the biggest disk to move to. That peg must be empty as the biggest disk may not rest on any other disk. Thus the n-1 disks must all be on some other peg. By induction, it takes at least 2n-1 moves to get those other disks onto that peg.

      Eventually must move the largest disk to its final destination (though there may be other moves first), so that takes at least one more move. Again, to do that, all the other n-1 must be stacked on a different peg. After moving the nth to it resting place, must eventually get those other n-1 disks from the peg they were on before moving the nth, to be stacked on top of the nth. Moving those n-1 disks will again take at least 2n - 1, by induction.

      Thus we require 2n-1 - 1 to clear off the nth, at least one more to move the nth to its destination, and at least 2n-1 - 1 more to get the n-1 back on top of the nth (though of course there may also be extra moves). Thus we have at least 2*2n-1 - 1 - 1 + 1 = 2n - 1 moves necessary to solve the puzzle. Thus our algorithm above is optimal.


    Back to:

  4. CS256 home page
  5. Kim Bruce's home page
  6. CS Department home page
  7. kim@cs.williams.edu