Docsity

Practice Quiz 1 Name: Solutions, Exams of Algorithms and Programming

Toggle sidebar
bg1
Introduction to Algorithms March 3, 2004
Massachusetts Institute of Technology 6.046J/18.410J
Professors Erik Demaine and Shafi Goldwasser Practice Quiz 1
Practice Quiz 1
๎˜€
Do not open this quiz booklet until you are directed to do so. Read all the instructions first.
๎˜€
When the quiz begins, write your name on every page of this quiz booklet.
๎˜€
The quiz contains five multi-part problems. You have 80 minutes to earn 80 points.
๎˜€
This quiz booklet contains 13 pages, including this one. An extra sheet of scratch paper is
attached. Please detach it before turning in your quiz.
๎˜€
This quiz is closed book. You may use one handwritten A4 or
๎˜๎˜ƒ๎˜‚
๎˜„๎˜†๎˜… ๎˜…๎˜ˆ๎˜‡๎˜Š๎˜‰๎˜‹๎˜‰
๎˜… ๎˜…
crib sheet. No
calculators or programmable devices are permitted.
๎˜€
Write your solutions in the space provided. If you need more space, write on the back of the
sheet containing the problem. Do not put part of the answer to one problem on the back of
the sheet for another problem, since the pages may be separated for grading.
๎˜€
Do not waste time and paper rederiving facts that we have studied. It is sufficient to cite
known results.
๎˜€
Do not spend too much time on any one problem. Read them all through first, and attack
them in the order that allows you to make the most progress.
๎˜€
Show your work, as partial credit will be given. You will be graded not only on the correct-
ness of your answer, but also on the clarity with which you express it. Be neat.
๎˜€
Good luck!
Problem Points Grade Initials
1 11
2 19
3 10
4 20
5 20
Total 80
Name: Solutions
Circle your recitation letter and the name of your recitation instructor:
Brian 11 12 2 Jen 12 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Practice Quiz 1 Name: Solutions and more Exams Algorithms and Programming in PDF only on Docsity!

Introduction to Algorithms March 3, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Shafi Goldwasser Practice Quiz 1

Practice Quiz 1

Do not open this quiz booklet until you are directed to do so. Read all the instructions first.

When the quiz begins, write your name on every page of this quiz booklet.

The quiz contains five multi-part problems. You have 80 minutes to earn 80 points.

This quiz booklet contains 13 pages, including this one. An extra sheet of scratch paper is

attached. Please detach it before turning in your quiz.

This quiz is closed book. You may use one handwritten A4 or  

 crib sheet. No

calculators or programmable devices are permitted.

Write your solutions in the space provided. If you need more space, write on the back of the

sheet containing the problem. Do not put part of the answer to one problem on the back of the sheet for another problem, since the pages may be separated for grading.

Do not waste time and paper rederiving facts that we have studied. It is sufficient to cite

known results.

Do not spend too much time on any one problem. Read them all through first, and attack

them in the order that allows you to make the most progress.

Show your work, as partial credit will be given. You will be graded not only on the correct-

ness of your answer, but also on the clarity with which you express it. Be neat.

Good luck!

Problem Points Grade Initials

1 11

2 19

3 10

4 20

5 20

Total 80

Name: Solutions

Circle your recitation letter and the name of your recitation instructor:

Brian 11 12 2 Jen 12 1

Problem 1. Recurrences [11 points]

Solve the following recurrences. Give tight, i.e.  , bounds.

(a)  

    [2 points]

Solution:       by Case 3 of the Master Method. We have: ! #"%$'&( 

and ) + ,  -. Thus, if ./ , then ) 0 213

. The regularity condition,

8 9) :<;=?>A@BC)  holds here for @( EDGF<F.

You received one point if you mentioned Case 3 of the Master Method and showed that it complied with the regularity condition and one point if you gave the correct answer.

(b)   IH9    

[2 points]

Solution: Note that we can not use the Master Method to solve this recurrence. If we expand this recurrence, we obtain:

 J

D

KDKD

 L

!"MN 

O

 #P

Q

!"MN 

O

^ 

 LRTS

U Q

!"MN 

O

^ S

V /J- W=D

Problem 2. True or False, and Justify [19 points] 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. Your justification is worth more points than your true-or-false designation.

(a) T F Given a set of integers, the number of comparisons necessary to find the max-

imum element is R and the number of comparisons necessary to find the

minimum element is R. Therefore the number of comparisons necessary to

simultaneously find the smallest and largest elements is H9 LRH. [3 points]

Solution: False. In recitation, we gave an algorithm that uses < : H comparisons

to simultaneously find the maximum and minimum elements. A common error was to say, โ€You can just keep track of min and max at the same timeโ€, which does not say how many comparisons that would take or how it would work.

(b) T F There are people born between the year 0 A.D. and the year 2003 A.D. It is

possible to sort all of them by birthdate in  /-  time.

Solution: True. We can represent a birthdate using @ digits. This database can

then be sorted in





  time using RADIX SORT.

Common errors included saying, โ€Use Mergesortโ€ or โ€Use Radix Sortโ€ (with no explanation).

(c) T F There is some input for which RANDOMIZED QUICKSORT always runs in 

time. [3 points] Solution: False. The expected running time of RANDOMIZED QUICKSORT is

 / . This applies to any input.

A common error was to say, โ€RANDOMIZED QUICKSORT will take



 only

if itโ€™s very unluckyโ€, but you needed to say what itโ€™s expected runtime is.

(d) T F The following array is a Min Heap:

H  F

  H H H  

[3 points] Solution: True.

Problem 3. Short Answer [10 points]

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

(a) Explain the differences between average-case running time analysis and expected run- ning time analysis. For each type of running time analysis, name an algorithm we studied to which that analysis was applied. [5 points]

Solution: The average-case running time does not provide a guarantee for the worst- case, i.e. it only applies to a specific input distribution, while the expected running time provides a guarantee (in expectation) for every input. In the average-case running time, the probability is taken over the random choices over an input distribution. In the expected running time, the probability is taken over the random choices made by the algorithm.

The average-case running time of BUCKET SORT is   when the input is chosen at

random from the uniform distribution. The expected running time of QUICK SORT is

(b) Suppose we have a hash table with H slots with collisions resolved by chaining, and

suppose that :

keys are inserted into the table. Assume each key is equally likely to be hashed into each slot ( simple uniform hashing ). What is the expected number of keys for each slot? Justify your answer. [5 points] We have not covered Hashing yet this semester. Expect a problem of equivalent length and difficultly.

Solution: Define  (for ^ KDKD'D^  ) to be the indicator which is 1 if element 

hashes to slot S and 0 otherwise. Then  $ (  $  ( 0 :7 H D Then the

expected number of elements in slot S is  $ O (  O .$ ( :7

H ?

: by linearity of expectation.

(b) In addition to and , you are given an integer   . Design a more efficient

(than in part (a) ) algorithm to verify that: 





M

 

D

In other words, if there is some subset  such that  contains exactly  elements

and the sum of the elements in  is less than  , then your algorithm should output

โ€œnoโ€. Otherwise, it should output โ€œyesโ€. Argue (informally) that your algorithm is correct and analyze its running time. [10 points]

Solution: Find the  th smallest element using the linear time

DETERMINISTICSELECT algorithm. Then find the  smallest elements using

the PARTITION procedure. Check that the sum of these  smallest elements is greater

than . The total runtime is







-7J



CHECKSETSOFSIZEK(   )

1 Run SELECT(    to find 

 smallest element

2 Run PARTITION(   ) to put  smallest elements in $ DKDKD  (

3 sum  0

4 from S 1 to 

5 sum  sum + $!S-(

6 if sum > 

7 return โ€œnoโ€ 8 return โ€œyesโ€

Correctness: Again, as in part (a) , the key observation is that every set of  elements

has sum at least  iff the sum of the  smallest elements is at least .

Problem 5. Finding the Missing Number [20 points]

Suppose you are given an unsorted array of all integers in the range  to except for one integer,

denoted the missing number. Assume VH  R.

(a) Design a



  Divide and Conquer algorithm to find the missing number. Partial

credit will be given for non Divide and Conquer algorithms. Argue (informally) that your algorithm is correct and analyze its running time. [12 points]

Solution: We can use SELECT to find the median element and check to see if it is in the array. If it is not, then it is the missing number. Otherwise, we PARTITION the

array around the median element  into elements  and . If the first one has size

less than   , then we recurse on this subarray. Otherwise we recurse on the other

subarray.

The procedure MISSINGINTEGER(  $!S ,( ) takes as input an array and a range

$ !S ,( in which the missing number lies.

MISSINGINTEGER( $!S),(^ )

1 Determine median element  in range S DKD'D

2 Check to see if  is in

3 PARTITION into

, elements >  , and  , elements



4 If SIZE(

5 MISSINGINTEGER(

$!S ()

6 Else MISSINGINTEGER(  $ ,( )

The running time is



  because the recurrence for this algorithm is  

 : H   , which is



  by the Master Method.

Common errors included using a randomized, instead of deterministic, partitioning scheme and using COUNTING SORT and then stepping through the array to find adja- cent pairs that differ by two, which is not a Divide and Conquer approach.

DETERMINISTICSELECT(  S)

1 Divide the elements of the input array into groups of  elements.

2 Find median of each group of 3 elements and put them in array

3 Call DETERMINISTICSELECT(

: : ) to find median of the medians, .

4 Partition the input array around  into  containing  elements 

and  containing QR R elements



5 If S   , then return .

6 Else if S , DETERMINISTICSELECT(  S).

7 Else if S  , DETERMINISTICSELECT(  LR R SRU-  ).