{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

pset - Introduction to Algorithms Massachusetts Institute...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology Professors Shafi Goldwasser and Silvio Micali September 5, 2003 6.046J/18.410J Handout 6 Problem Set 1 This problem set is due in recitation on Friday, September 12. Reading: Chapters 1–4 (excluding 4.4); Kingston chapter. There are three problems. Each problem is to be done on a separate sheet (or sheets) of threehole punched paper. Mark the top of each sheet with your name, the course number, the problem number, your recitation section (i.e. letter), the date, and the names of any students with whom you collaborated. You will often be called upon to “give an algorithm” to solve a certain problem. Your write-up should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of your essay should provide the following: 1. A description of the algorithm in English and, if helpful, pseudocode. English explanations should be used liberally in your pseudocode. For convenience, pseudocode may use standard arithemetic and logical operations, as well as loops and data structures like arrays. Pseudocode should be understandable to anyone who can program–it should not be ready to compile! 2. At least one worked example or diagram to show more precisely how your algorithm works. 3. A proof (or indication) of the correctness of the algorithm. 4. An analysis of the running time of the algorithm. Remember, your goal is to communicate. Graders will be instructed to take off points for convoluted and obtuse descriptions. Problem 1-1. Recurrence Relations Solve the following recurrences. Give a bound for each problem. If you are unable to find a bound, provide as tight upper ( or ) and lower ( or ) bounds as you can find. Justify your answers. You may assume that . (b) (c) (d) (e) 7 )a`Y2Q ¦  (  © X § ¦  §  0 (#VW$ & §¨¦U 2$T H S R $ Q 32¦  ( §¦( §¦ § $ P IG$ E5  $ E¦ D ( H F D§ ¦ D§ [email protected])'&$ 65 32¦ 97 ( § ¦4  §  1)'&%#" ¦ 0  (  $§ ¦!  § (a) ¡ ¥ ¤ ¡ ©  ©§ ¨¦ £¢ 2 Handout 6: Problem Set 1 (g) Problem 1-2. Asymptotic Notation Rank the following functions by order of growth; that is, find an arrangement of the functions satisfying , , ..., . Partition your list into equivalence classes such that and are in the same class if and only if . (The function is discussed on pages 55-56 of CLRS.) Problem 1-3. Sieve of Eratosthenes The Sieve of Eratosthenes, invented circa 200 B.C., is an algorithm to find all prime numbers between 2 and an input number . The algorithm works as follows: we begin with a list of all integers from 2 to . For each , we cross out (i.e. mark as composite) each multiple of ( for ) that is less than or equal to . When this process terminates, only the prime numbers between 2 and are unmarked. Below, we give pseudocode for this algorithm. At the beginning of the algorithm, every entry in the array is initialized to true, i.e. is true for all . At the end of the algorithm, is true iff is prime. E RATOSTHENES -S IEVE : 1 Let for all from 2 to to : 2 for 3 . 4 while : 5 . 6 . 7 endwhile 8 endfor . —©  aŽk y rr u pk ssr ©vt The table below shows the values of the array elements (line 7) during an execution of the algorithm run on input .  § § ¡  § €32xiy" 32wv T prrr p 2H i tsssp & i qH i $ Wˆd3C@9§ e ™  B j[email protected] • § B at the end of each while loop B C@9 & k mw m {z5vD p w T §¤  2H iuf R H i H “ q’ ” $‘ 7 $—  k •© (  a5g2§ $ WˆdGWˆd8 e™e™ ˜WWW2H — ˜˜˜ H “ q’ ©” $ ‘  § 2xi wv §  7 iu5 & i  & iu" H i §¤ §¤ yu 2xwvt k k nm l k  h0 § ( § ¦ e  § &  3f $g & Gf ¦  ©§ ¢ (   B 9§ ¦  § dcb1[email protected]Q 32¦ ©Qg p (} ‚aEŠ‹‰xv 8ˆaˆˆct Œ }y lo u kf‡s† m lo D … } k D „l } ‚  €~ } u ƒ#82yxwvt $ ˆ‡†…&  ‰ „ƒ w k |2§ B9 C@f • – $D k k Dq sr w t (f) lo #p l ‚B I'6@9 2yxwvt u Handout 6: Problem Set 1 3 Prove that the E RATOSTHENES -S IEVE algorithm is correct; that is, prove that upon termination, is true iff is prime. Hint: You can use the following pre-condition and post-condition and you can prove the suggested loop invariant for the for loop (line 2). Let represent the statement: is true iff is prime. Loop Invariant: such that lQm `¨D wm k wm Qm `¤D Post-Condition: £¡ w p x £¡ w dp x Dq 5¢k Pre-Condition: ”• ‘ ”“ ‘ ” ‘ ”ž ‘ ” ‘ ” ‘ ” ‘ ” ‘ ” ‘ ” ‘ ” ‘ ” ‘ t%j’ t%j’ €aj’ Ÿqj– tˆœ’ tˆ›’ xš– tˆ™’ tˆ˜’ s— – tˆ•’ tˆ“’ T T T T T T T T T T T T T F F F F F T T T T T T T F F F F F T T T T T T T F F F F F T T F F F F T F F F F F T T T T T T T F F F F F T T T T T T . such that . . i 1 2 3 4 ... 13 w yu Eˆwct ¡  w 2yxwvt u ...
View Full Document

{[ snackBarMessage ]}