Practice Quiz 1 Solutions: Introduction To Algorithms

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Introduction to Algorithms October 6,2005

Massachusetts Institute of Technology 6.046J/18.410J


Professors Erik D. Demaine and Charles E. Leiserson Handout 11

Practice Quiz 1 Solutions

Problem -1. Recurrences


Solve the following recurrences by giving tight -notation bounds. You do not need to justify your
answers, but any justification that you provide will help when assigning partial credit.

(a)                       

Solution: Master method does not apply directly, but we have         

. Now apply case 3 of master method to get


!


                    

. Therefore, we have
  . Lower bound is obvious.

        *  

   

(b)
!

              

Solution: Master method does not apply directly. But is much smaller than ,
!

 

therefore ignore the lower order term and guess that the answer is        .
Check by substitution.

(c)         /   1 3 4 

Solution: By Case 1 of the Master Method, we have      

 7  : < > ?
 .

(d)
!

           1 3 

Solution: By Case 3 of the Master Method, we have       1 3   .

(e)        /   1 3 4 

Solution: By Case 2 of the Master Method, we have      1 3


>

  .

(f)
!

    A      

>

Solution: By Case 2 of the Master Method, we have      

>
1 C 3   .
Handout 11: Practice Quiz 1 Solutions 2

(g)

        

Solution: By Case 3 of the Master Method, we have      


 .

(h)

          

Solution: . This is

 

 

   

 

                           

 . For the upper bound, note that


        "    , where "     "         ,
which is clearly . $      

Problem -2. True or False


Circle T or F for each of the following statements, and briefly explain why. The better your
argument, the higher your grade, but be brief. No points will be given even for a correct solution
if no justification is presented.


(a) T F For all asymptotically positive     ,                   .

Solution: True. Clearly, is                  . Let             . For


any , for all for some . Hence, ,


! # %     !         )  )      $      

whence . Thus,             $                         .

(b) T F The worst-case running time and expected running time are equal to within con-
stant factors for any randomized algorithm.

Solution: False. Randomized quicksort has worst-case running time of  




and expected running time of .      

(b) T F The collection of hash functions is universal, where the three +  - .


0

.
0

1 1

hash functions map the universe of keys into the range


0 5 0 7 0 : 0 0

- 3 - % 

according to the following table:


; ; ; ;

.   .   .  

3  %

% 

% % %

  %
Handout 11: Practice Quiz 1 Solutions 3

Solution: True. A hash family that maps a universe of keys into slots is 

universal if for each pair of distinct keys , the number of hash functions 

 

 for which is exactly . In this problem, and


 


   


        

 . Therefore, for any pair of the four distinct keys, exactly hash function
  

should make them collide. By consulting the table above, we have:

only for mapping into slot


        

only for mapping into slot


        

only for mapping into slot


         

 


   

 only for 

mapping into slot

 


   

 only for  mapping into slot 

only for mapping into slot


       

Problem -3. Short Answers


Give brief, but complete, answers to the following questions.

(a) Argue that any comparison based sorting algorithm can be made to be stable, without
affecting the running time by more than a constant factor.

Solution: To make a comparison based sorting algorithm stable, we just tag all
elements with their original positions in the array. Now, if , then we

  

compare and , to decide the position of the elements. This increases the running

time at a factor of 2 (at most).

(b) Argue that you cannot have a Priority Queue in the comparison model with both the
following properties.

E XTRACT-M IN runs in    time.


BUILD -H EAP runs in    time.

Solution:

If such priority queues existed, then we could sort by running BUILD -H EAP ( )   

and then extracting the minimum times ( ). This algorithm would          

sort time in the comparison model, which violates the


   lower bound       

for comparison based sorting.


Handout 11: Practice Quiz 1 Solutions 4

(c) Given a heap in an array with as the maximum key (the heap is a max
 

      

heap), give pseudo-code to implement the following routine, while maintaining the
max heap property.
D ECREASE -K EY – Decrease the value of the key currently at by . Assume
  

  

.


Solution:

D ECREASE -K EY


 

    

  

M AX -H EAPIFY


 

(d) Given a sorted array of distinct integers, some of which may be negative, give an
algorithm to find an index such that and provided such an index
    

    

exists. If there are many such indices, the algorithm can return any one of them.

Solution:

The key observation is that if and , then . Similarly if


       

         

and , then . So if we look at the middle element of the array, then half
   

   

of the array can be eliminated. The algorithm below (I NDEX -S EARCH ) is similar to


binary search and runs in time. It returns -1 if there is no answer.  

I NDEX -S EARCH 
   

if 





return -1 

 

if


 

   

then return 

if


   

then return I NDEX -S EARCH 


  

 

else return I NDEX -S EARCH 





 

Problem -4. Suppose you are given a complete binary tree of height with leaves, where
 

each node and each leaf of this tree has an associated “value” (an arbitrary real number). 

If is a leaf, we denote by

the set of ancestors of (including as one of its own ancestors). 


That is, consists of , ’s parent, grandparent, etc. up to the root of the tree.

Similarly, if

and 

are distinct leaves we denote by 


 

 the ancestors of either

or . That


is,

  

        
Handout 11: Practice Quiz 1 Solutions 5

36

21 6

15 30 11 10

10 19 20 14 5 9 2 7

x y
A(x,y) shown in bold
f(x,y) = 19+15+21+36+20+30 = 141

Define the function 


;

/ 

 to be the sum of the values of the nodes in 2


;

/ 

 .
Give an algorithm (pseudo-code not necessary) that efficiently finds two leaves ;

( and 

( such that
 is as large as possible. What is the running time of your algorithm?
;

(
/ 

( 

Solution:

There are several different styles of solution to this problem. Since we studied divide-and-conquer
algorithms in class, we just give a divide-and-conquer solution here. There were also several
different quality algorithms, running in , , and . These were worth up to      
   



 

, , and points, respectively. A correct analysis is worth up to points.


   

First, let us look at an solution then show how to make it


 . For simplicity, the
 
    

solution given here just finds the maximum value, but it is not any harder to return the leaves
giving this value as well.
We define a recursive function M AX 1 to return the maximum value of —the sum of the   
;

ancestors of a single node—over all leaves in ’s subtree. Similarly, we define M AX 2 to be a ;

  
Handout 11: Practice Quiz 1 Solutions 6

function returning the maximum value of over all pairs of leaves 

 




 

in ’s subtree. Calling


M AX 2 on the root will return the answer to the problem.


First, let us implement M AX 1 . The maximum path can either be in ’s left subtree or ’s right
   

subtree, so we end up with a straightforward divide and conquer algorithm given as:

M AX 1  

1 return value M AX 1 left M AX 1 right



            

For M AX 2 , we note that there are three possible types of solutions: the two leaves are in ’s left
  

subtree, the two leaves are in ’s right subtree, or one leaf is in each subtree. We have the following


pseudocode:

M AX 2  

1 return value M AX 2 left M AX 2 right M AX 1 left M AX 1 right





 

                   

Analysis:
For M AX 1, we have the following recurrence


  

 

  
   


  (1)

by applying the Master Method.


For M AX 2, we have
 

     

  
     

 

 


  


   




     (2)

by case 2 of the Master Method.


To get an solution, we just define a single function, M AX B OTH , that returns a pair—the
  

answer to M AX 1 and the answer to M AX 2. With this simple change, the recurrence is the same as
M AX 1

Problem -5. Sorting small multisets


For this problem is an array of length objects that has at most distinct keys in it, where
 

. Our goal is to sort this array in time faster than . We will do so in two phases.


       

In the first phase, we will compute a sorted array that contains the distinct keys occuring in .

In the second phase we will sort the array using the array to help us.

Handout 11: Practice Quiz 1 Solutions 7

Note that might be very small, like a constant, and your running time should depend on
  as well
as . The objects have satellite data in addition to the keys.

 

  

Example: Let . Then and .


  

           

              

   


   

In the first phase we compute .





   


   

 

 

  
 

The output after the second phase should be .


  

           

        

   


   

Your goal is to design and analyse efficient algorithms and analyses for the two phases. Remember,
the more efficient your solutions, the better your grade!
(a) Design an algorithm for the first phase, that is computing the sorted array of length

containing the distinct keys. The value of is not provided as input to the algorithm.  

Solution:

The algorithm adds (non-duplicate) elements to array while maintaining sorted


at every intermediate stage. For , element is binary searched in




  

     

array . If occurs in , then it need not be inserted. Otherwise, binary search


also provides the location where should be inserted into array to maintain in

sorted order. All elements in to the right of this position are shifted by one place to

make place for .


(b) Analyse your algorithm for part (a).

Solution:

Binary search in array for each element of array takes time since size of

 


is at most . This takes a total of time. Also, a new element is inserted


  


into array exactly times, and the total time over all such insertions is


     

. Thus, the total time for the algorithm is


    


 
  


  


since .


(c) Design an algorithm for the second phase, that is, sorting the given array , using the
array that you created in part (a). Note that since the objects have satellite data, it

is not sufficient to count the number of elements with a given key and duplicate them.
Hint: Adapt Counting Sort.

Solution:

Build the array as in counting sort, with containing the number of elements in

that have values less than or equal to . Counting sort will not work as is since


Handout 11: Practice Quiz 1 Solutions 8

is necessarily an integer. Or, it may be some integer of very large value (there is


no restriction on our input range). Therefore is an invalid index into our array .


What we would like to do is assign an integral “label” for the value . The label we


choose is the index of the value in the array calculated in the last part of the


problem.
How do we find this index? We could search through from beginning to end, looking 

for the value , then returning the index of that contains . This would take



 

time. But, since is already sorted, we can use B INARY-S EARCH to speed this
  


up to . Let B INARY-S EARCH be a procedure that takes a sorted array


     




and an item within the array, and returns such that . The modified version
 

 

of C OUNTING S ORT is included below, with modified lines in bold:

C OUNTING -S ORT  

/* Uses Arrays , , and -out */




 

             

For to do ; /* Initialize */
 


  

For to do /* Count number of elements */




Location B INARY-S EARCH ;


 

 

  

C[Location] C[Location] ;


;



 

   

For to do /* Build cumulative counts */




;



  

   

For downto do /* Construct Sorted List A-Out */




Location B INARY-S EARCH ;


 

 

  

Out-Location D[Location];


D[Location] D[Location] ;


-out[Out-Location] ;
 

Output( -out);

(d) Analyse your algorithm for part (c).

Solution:

The running time of the modification to C OUNTING -S ORT we described can be bro-
ken down as follows:

First Loop: .   

Second Loop: iterations, each iteration performing a B INARY-S EARCH on  

an array of size . Total Work: .      

Third Loop: .   

Fourth Loop: iterations, each iteration performing a B INARY-S EARCH on  

an array of size . Total Work: .      


Handout 11: Practice Quiz 1 Solutions 9

The running time is dominated by the second and fourth loops, so the total running
time is   .  


You might also like