CS – 560 Algorithms and their Analysis

Document Sample
CS – 560 Algorithms and their Analysis Powered By Docstoc
					                           CS – 560 Algorithms and their Analysis
                                 Assignment #3* (40 points)
                                      Due Nov 2, 2009

1. (3+2+2 points) The following parts are independent:
   (a) What is the running time of the Heapsort for presorted input?
   (b) Are there particularly good inputs (that would produce low complexity) for
       Heapsort? (inputs can be ordered, reverse ordered, random, etc., etc.)
   (c) Are there particularly good inputs for Mergesort?

2. (6 points) An array A of size n has values that are uniformly distributed in the interval
   {x, y} where x and y are given, i.e. every element of A is greater than x and less than
   y. Uniform distribution means that the probability that a particular element of A is in
   a sub-interval x1>x and y1<y is proportional to the length of the sub-interval, i.e. its
   probability is equal to (y1-x1)/(y-x). Propose an algorithm to sort the array A in (n )

3. (3+4 points) Parts (a) and (b) are independent.
   (a) Suppose you are given a list consisting of (n  lg n ) sorted elements followed by
       (lg n) randomly ordered elements. Give an efficient algorithm to sort the entire
       list and determine the complexity of your algorithm.
   (b) Suppose that in Quicksort the pivot is the average of all keys. Will this guarantee
       balance partitioning and therefore (n lg n ) complexity? Explain.

4. (2 + 8 points) The following parts (a) and (b) are independent, but both refer to
inversion in a sequence.
(a) Suppose we exchange elements a[i] and a[j] (j>i) which were originally out of order.
For example if a= [2,8,3,7,1,5,6] and if we exchange second (i=2) and sixth (j=6)
elements, we have [2,5,3,7,1,8,6] and now 5 and 8 are ordered and therefore the array has
fewer inversions, i.e. several inversions are removed. In terms of i and j, what is the
maximum number of inversions that can be removed?
(b) This problem arises in ranking which has many applications especially, in a web
technique called collaborative filtering. Here they try to match your preference (for
books, movies, restaurants) with those of other people on the internet. A main problem in
collaborative ranking is comparing two rankings. You rank a list of n movies, say as {1,
2, …, n} and another person ranks them as {a1, a2, …, an}. For example, if there are
four movies, your ranking would be {1,2,3,4} and other person’s ranking could be {a1,
a2, a3, a4}={2,3,1,4}, i.e. the movie your ranked as 1 the other person ranked as 2, etc. A
natural way to quantify similarity between two rankings is to count the number of
inversions in the sequence. Given a list {a1, a2,…, an}, propose an algorithm in pseudo-
code to count the number of inversions with O(n lg n ) complexity. (Hint: Recursively

 The grade will be based mainly on the completeness and neatness of your solutions, and the reasoning
presented in support of your answers. Please type your answers or write very clearly.
divide the list into two sub-lists, count inversions in each sub-list and combine the sub-

5. (3+7 points) A bank has issued two separate sets of account access cards, set A and set
B. Each set consists of n cards and for every card in the set A there is a corresponding
card in the set B that belong to the same account, and we will refer to these two cards as
the matching pair. Each card is a small plastic object containing a magnetic strip with
some encrypted number that corresponds to a unique account in the bank. It is required
to find all matching pairs. There is a card reader machine such that when two cards, one
from set A and one from set B, are inserted in the machine one of its three light indicators
turns on; green if the pair matches, red if the account number on A is larger than B, and
yellow if the number on B is higher than that of A. However, the card reader cannot
compare two cards belonging to the same set.
   (a) Describe an algorithm in clearly defined step that uses (n 2 ) tests to find the
       matching pairs.
   (b) Describe a more efficient algorithm with an average case complexity of.
       O(n lg n ) tests to find the matching pairs. Prove that your algorithm satisfies the
       desired complexity.

Jun Wang Jun Wang Dr
About Some of Those documents come from internet for research purpose,if you have the copyrights of one of them,tell me by mail you!