Quiz 1 Solutions, Summaries of Algorithms and Programming

Explain briefly why your algorithm is correct, and analyze its running time. (Hint: Use divide and conquer.) Solution: The idea is to partition ...

Typology: Summaries

2022/2023

Uploaded on 05/11/2023

lakshmirnarman
lakshmirnarman 🇺🇸

5

(5)

221 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
��
Introduction to Algorithms October 14, 2005
Massachusetts Institute of Technology 6.046J/18.410J
Professors Erik D. Demaine and Charles E. Leiserson Handout 14
Quiz 1 Solutions
Do not open this quiz booklet until you are directed to do so. Read all the instructions on
this page.
When the quiz begins, write your name on every page of this quiz booklet.
This quiz contains 4 problems, some with multiple parts. You have 80 minutes to earn 80
points.
This quiz booklet contains 13 pages, including this one. Two extra sheets of scratch paper
are attached. Please detach them before turning in your quiz at the end of the examination
period.
This quiz is closed book. You may use one handwritten A4 or 8 1 × 11�� crib sheet. No
2
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 Parts Points Grade Grader
1 4 12
2 1 7
3 11 44
4 3 17
Total 80
Name:
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Quiz 1 Solutions and more Summaries Algorithms and Programming in PDF only on Docsity!

��

Introduction to Algorithms October 14, 2005

Massachusetts Institute of Technology 6.046J/18.410J

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

Quiz 1 Solutions

  • Do not open this quiz booklet until you are directed to do so. Read all the instructions on this page.
  • When the quiz begins, write your name on every page of this quiz booklet.
  • This quiz contains 4 problems, some with multiple parts. You have 80 minutes to earn 80 points.
  • This quiz booklet contains 13 pages, including this one. Two extra sheets of scratch paper are attached. Please detach them before turning in your quiz at the end of the examination period. This quiz is closed book. You may use one handwritten A4 or 8 1 × 11 ��^ crib sheet. No 2

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 Parts Points Grade Grader

Total 80

Name:

Problem 1. Asymptotic Running Times [12 points] (4 parts)

For each algorithm listed below,

  • give a recurrence that describes its worst-case running time, and
  • give its worst-case running time using �-notation.

You need not justify your answers.

(a) Binary search

Solution: T (n) = T (n/2) + �(1) = �(lg n)

(b) Insertion sort

Solution: T (n) = T (n − 1) + �(n) = �(n^2 )

Problem 2. Substitution Method [7 points]

Consider the recurrence

T (n) = T (n/2) + T (n/4) + n ,

T (m) = 1 for m � 5.

Use the substitution method to give a tight upper bound on the solution to the recurrence using

O-notation.

Solution: We guess T (n) = O(n), which leads to the induction hypothesis T (m) � cm for all

m < n. For c � 1 , we have the base cases T (n) = 1 � cn for n � 5. The induction hypothesis

yields

T (n) = T (n/2) + T (n/4) + n � cn/2 + cn/4 + n = (3c/4 + 1)n.

If we choose c = 4, then T (n) � (3 + 1)n = 4n = cn. By induction on n, T (n) � cn for c � 4

and all n � 1.

Problem 3. True or False, and Justify [44 points] (11 parts)

Circle T or F for each of the following statements to indicate whether the statement is true or

false, respectively. If the statement is correct, briefly state why. If the statement is wrong, explain

why. The more content you provide in your justification, the higher your grade, but be brief. Your

justification is worth more points than your true-or-false designation.

T F The solution to the recurrence T (n) = 3 T (n/3) + O(lg n) is T (n) = �(n lg n).

Solution: False. Case 3 of the master theorem applies: f (n) = O(nlog^3 3 )= O(n) for f (n) = O(lg n), hence, T (n) = O(n).

T F Let Fk denote the kth Fibonacci number. Then, the n^2 th Fibonacci number Fn 2 can be computed in O(lg n) time.

Solution: True. The n^2 th Fibonacci number can be computed in O(lg n^2 ) = O(lg n) time by using square and multiply method with matrix

T F The array

20 15 18 7 9 5 12 3 6 2

forms a max-heap.

Solution: True.

  • A[1] = 20 has children A[2] = 15 � 20 and A[3] = 18 � 20.
  • A[2] = 15 has children A[4] = 7 � 15 and A[5] = 9 � 15.
  • A[3] = 18 has children A[6] = 5 � 18 and A[7] = 12 � 18.
  • A[4] = 7 has children A[8] = 3 � 7 and A[9] = 6 � 7.
  • A[5] = 9 has child A[10] = 2.
  • A[6],... , A[10] have no children.

T F Heapsort can be used as the auxiliary sorting routine in radix sort, because it operates in

place.

Solution: False. The auxiliary sorting routine in radix sort needs to be stable, meaining that numbers with the same value appear in the output array in the same order as they do appear in the input array. Heapsort is not stable. It does operate in place, meaning that only a constant number of elements of the input array are ever stored outside the array.

T F There exists a comparison sort of 5 numbers that uses at most 6 comparisons in the worst

case.

Solution: False. The number of leaves of a decision tree which sorts 5 numbers is 5! and the height of the tree is at least lg(5!). Since 5! = 120 , 26 = 64 , and 27 = 128 , we have 6 < lg(5!) < 7. Thus at least 7 comparisons are required.

T F Suppose that a hash table with collisions resolved by chaining contains n items and has a

load factor of � = 1 / lg n. Assuming simple uniform hashing, the expected time to search for an item in the table is O(1/ lg n).

Solution: False. The expected time to search for an item in the table is O(1 + �) = O(1 + 1 / lg n) = O(1). At least a constant running time O(1) is needed to search for an item; subconstant running time O(1/ lg n) is not possible.

T F Let S be a set of n integers. One can create a data structure for S so that determining

whether an integer x belongs to S can be performed in O(1) time in the worst case.

Solution: True. Perfect hashing.

� �

Problem 4. Close Numbers [17 points] (3 parts)

Consider a set S of n � 2 distinct numbers. For simplicity, assume that n = 2k^ + 1 for some

k � 0. Call a pair of distinct numbers x, y ≤ S close in S if

|x − y| � max z − min z , n − 1 z�S^ z�S

that is, if the distance between x and y is at most the average distance between consecutive numbers

in the sorted order.

(a) Explain briefly why every set S of n � 2 distinct numbers contains a close pair of numbers.

Solution: Without loss of generality, assume S = {z 1 , z 2 ,... , zn}, with zi � zi+1. The average distance between two consecutive numbers zi and zi+1 is

n− 1 1 �^1

n − (^1) i=

(zi+1 − zi) = n − 1

(zn − z 1 ).

There exists at least one pair of consecutive numbers x and y whose distance between them is less than or equal to the avearge. The result then follows from the definition of the close pair.

(c) Describe an O(n)-time algorithm to find a close pair of numbers in S. Explain briefly

why your algorithm is correct, and analyze its running time. ( Hint: Use divide and conquer.)

Solution: The idea is to partition S recursively until we find a close pair.

  1. Determine the median of S and use it to partion S into S 1 and S 2.
  2. Use the result from Part (b) to determine the set Sk that contains a close pair of S.
  3. Recurse on Sk until Sk contains 2 elements.

Since each recursive step reduces the cardinality of the set by roughly a half, the recursion is guaranteed to terminate. After each recursive step, the remaining set contains a close pair of S.

Step 1 takes O(n) time in the worst case, if we use the deterministic median-finding algorithm. Step 2 takes O(n) time based on the result from Part (b). Therefore, the running time of the algorithm is given by the following recurrence:

T (n) = T (n/2) + O(n),

with the solution T (n) = O(n) according to the master theorem.

SCRATCH PAPER — Please detach this page before handing in your exam.