Docstoc

Analysis of Algorithms_ 91.doc

Document Sample
Analysis of Algorithms_ 91.doc Powered By Docstoc
					UML CS                        Analysis of Algorithms 91.404                     Spring, 2001

                             Homework Set #1 & SOLUTIONS
                             Prepared by TA Wenhua (Michelle) Shi

      Assigned: Friday, 2/2                             Due: Friday, 2/9 (start of lecture)

               This assignment covers textbook material in Chapters1-2.
          Note: Partial credit for wrong answers is only given if work is shown.


1. (10 points) Let R be a problem. Suppose that the worst-case asymptotic time complexity of
   R is in O(n2) and also in (n lg n). Now, let A be an algorithm that solves R. Which of the
   following statements about A are consistent with this information about the asymptotic time
   complexity of R? (Note: The answer might include more than one of the choices.)
       A has worst-case time complexity O(n2).
       A has worst-case time complexity O(n3/2).
       A has worst-case time complexity O(n).
       A has worst-case time complexity ( n2).
       A has worst-case time complexity ( n3).
   Answer: Statements 1, 2, 4 and 5 are consistent.
       Note: For probem R, O(n2) can be tight or loose upper bound and (n lg n) can be
       tight or loose lower bound. An algorithm that solves R can be so efficient that it achieves
       its complexity as the same as the problem ( but can not be smaller), or it can be so
       inefficient that it is much more complex (say, exponential) than the problem.


2. (10 points) For (a) and (b), answer yes or no and briefly explain.
(a) If I prove that an algorithm takes O(n2) worst-case time, is it possible that it takes O(n) time
          on some inputs?
    Answer: Yes. Note that O(n2) worst-case time is an upper bound and we have not given a
    lower bound limit. Because (n) is smaller than (n2), it is possible that an algorithm
    satisfies both O(n2) worst-case time and O(n) time on some inputs.
(b) If I prove that an algorithm takes  (n) best-case time, is it possible that it takes (n lg n)
          time on all inputs?
    Answer: Yes. Note that  (n) best-case time can be a loose lower bound. If an algorithm
    takes (n lg n) time on all inputs, it is absolutely right that it is larger than  (n).


3. (20 points) Rank the following 5 functions by order of asymptotic growth. That is, find an
   arrangement g1, g2, g3, g4, g5 of the functions satisfying
      g1 = ( g2 ), g2 = ( g3 ), ..., g4 = ( g5 ). Justify your answer mathematically.
     6n4 + 5n3         2n + 3n         6n (lg2 n)               (1/4)n   999log n + 65,000
UML CS                        Analysis of Algorithms 91.404                        Spring, 2001
   Answer: (1/4)n < 999log n + 65,000            <    6n (lg2 n)   <   6n4 + 5n3     < 2n + 3n
      Justification: let’s ignore all coefficients and minor parts, since
             (1/4)n = (1/4)n )
             999log n + 65,000 = lg n )
             6n (lg2 n) = n (lg2 n) )
             6n4 + 5n3 = n4 )
             2n + 3n =  2n ) .
      Let g1 = (1/4)n , g2 = lg n, n0 = 2 and c =1,
           Then, 0 <= (1/4)n <= lg n, n>=2.
      Let g2 = lg n, g3 = n (lg2 n), n0 = 2 and c =1,
           Then, 0 <= lg n <= n (lg2 n), n>=2 since 1 <= n lg n n>=2.
      Let g3 = n (lg2 n), g4 = n4, n0 = 2 and c =1,
           Then, 0 <= n (lg2 n) <= n4, n>=2 since (lg2 n) <= n3, which is true n>=2.
      Let g4 = n4, g5 = 2n, n0 = 2 and c =1,
           Then, 0 <= n4 <= 2n, n>=24 since lg (n4) <= lg (2n)  4 lg n <= n lg 2
            4 lg n <= n , which is true n>=24.


4. (10 points) Find the smallest (positive) integer value for which 50n3 < 3n
   Answer: n=10 is the smallest integer value for which 50n3 < 3n.
   Note that we have :       n         50n3             3n
                              9        36,450           19,683
                              10       50,000           59,049


5. (25 points) Textbook, (1st edition) exercise 1.3-5 on p.15. Consider the problem of searching
       for a value v in a sequence that is already sorted. Since the sequence is sorted, we can
       compare with v the value at the midpoint of the sequence and eliminate half of the
       sequence from further consideration. Binary search is an algorithm that repeats this
       procedure, halving the size of the remaining portion of the sequence each time. Write
       pseudocode for binary search. Explain why your pseudocode is correct. Show that its
       worst-case running time is in (lg n).
UML CS                         Analysis of Algorithms 91.404                       Spring, 2001
   Answer:
  BinarySearch(A, v)
  1. left  1
  2. right  length[A]
  3. while left <= right
  4.     do     middle  (left + right)/2
  5.            if v = A[middle]   then     Return middle
  6.            else   if v < A[middle]
  7.                   then right  middle –1      //reduces to left half of the array
  8.            else
  9.                   left  middle +1     //reduces to right half of the array
  10. Return NIL


  Correctness: Since the pseudocode contains a while loop, we need to show that the loop
  terminates and that when it terminates the correct answer is produced. The values in the
  array appear in sorted order; this allows us to discard ½ the remaining possibilities each
  time through the loop. If v is somewhere in the array, then we will encounter it as the middle
  element in some iteration. This takes at most lg(n) iterations, because after throwing away
  ½ the elements lg(n) times we have only 1 element left to examine, and left=right=middle.
  So, if v is in the array we return out of the loop and stop. If v is not in the array, then after
  lg(n) iterations we won’t find it in that one remaining element. In this case, left or right will
  be adjusted so that left > right. This adjustment causes the while condition to fail so that
  the pseudocode terminates and returns NIL (i.e. failure).


  Running Time: We argue that the worst-case running time of binary search is lg n ). The
  correctness argument above shows that the loop executes at most lg(n) times. This gives
  us an upper bound of O(lgn). The case in which v is not in the array is a worst-case input
  for binary search because in this case the loop is not exited early and lg(n) iterations are
  required. This worst-case input provides a worst-case lower bound of (lgn). Together the
  matching lgn lower and upper bounds allow us to conclude that the worst-case running time
  of binary search is lg n ).
UML CS                            Analysis of Algorithms 91.404                           Spring, 2001


6. (25 points) This problem refers to the Mystery( ) pseudocode below:
  Mystery(A, n)
          for     i  1 to n
                  do for j  1 to n
                              do if A[i][j] equals 1
                                       then print "*"                without newline
                                       else print " "                without newline
                       print newline

 (a) Given the following inputs, what is the output of a call Mystery(A, n) ? Inputs: n=5 and 2-
     dimensional nxn array A is initialized as follows (row elements are listed in increasing
     column order):

     row 1: 0, 0, 1, 0, 0   row 2: 0, 1, 0, 1, 0 row 3: 0, 1, 1, 1, 0   row 4: 1, 0, 0, 0, 1   row 5: 1, 0, 0, 0, 1


     Answer:
                                                       *
                                                  *         *
                                                  *    *    *
                                             *                   *
                                             *                   *




 (b) Give a function f(n) that is an upper bound on the worst-case running time of Mystery.
     Justify your answer.

 Answer: The upper bound worst-cast running time is O(n2) since each element of array is
 checked at most once by two for loops that each iterate a maximum of n times.

 (c) Give an example of a worst-case input for array A for Mystery when n=5.

 Answer: All elements of array equal to 0. Its running time is n2). Actually, every example
 forces the running time to be proportional to n2, since there is no way to exit the for loops
 early.
UML CS                       Analysis of Algorithms 91.404               Spring, 2001
 (d) Can you conclude from your answers to (b) and (c) that Mystery’s worst-case running
    time is = (f(n))? Why or why not?

 Answer: Yes. From (b), we get an upper bound on the worst-case running time O(f(n)), and
 from (d), we get an lower bound on the worst-case running time f(n)). So, we can conclude
 that the worst case running time is (f(n)).

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:4
posted:6/1/2012
language:English
pages:5
zhaonedx zhaonedx http://
About