{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


pset3 - Introduction to Algorithms Massachusetts Institute...

Info iconThis 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 24, 2003 6.046J/18.410J Handout 10 Problem Set 3 This problem set is due in lecture on Monday, October 6. Reading: Chapter 9, Sections 11.1–11.3, Sections 12.1–12.3 There are four problems. Each problem is to be handed in separately. Mark the top of each sheet with your name, the course number, the problem number, your recitation section, 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. 2 Problem 3-1. Heap Operations Handout 10: Problem Set 3 For parts (a), (b), (c) and (d) of this problem, you may assume that the input heaps are nearly complete binary trees; for parts (a), (b) and (c) your output heaps need not be nearly complete. Note that the input heaps are not given to you in the form of arrays and your output heaps need not be in the form of arrays. . (b) Suppose you are given a heap of size and unordered elements, where What is the runtime to insert these elements into the heap ? What is the total runtime required to build a second heap of size and then merge and into a single heap? (c) Give an algorithm to merge heaps each of size . into a single heap of size (d) Given a min-heap of distinct elements and a real number , give an algorithm to determine whether the th smallest element is less than in time. Hint: You do not have to extract the th element. You only need to find its relation to . Problem 3-2. Political Machinations Noted actor Ranier Wolfcastle decides to hold a “town hall” meeting as part of his campaign for Governor of California. His political advisor, Rover Karlson, is in charge of hand-picking an audience and asks you for help. (a) Rover tells you that he only wants middle-class voters in the audience. He decides to eliminate anyone in the top or bottom th income bracket. Unfortunately, you cannot find out anyone’s actual income. You can only compare two people and see who makes more money. Given a list of potential audience members, give an time algorithm to find the people with the th largest and smallest incomes. (b) Rover decides to sort the list of potential audience members into equal-sized (to within 1) groups by income. Denote these groups as . The groups must have the property that , every person has a lower income than every person . Rover doesn’t care about the ordering within any particular group . Give an time algorithm to produce such groups. Problem 3-3. Set Operations Suppose you are given two sets of integers, and . Let and and suppose . You may assume any comparison or arithmetic operation (e.g. addition, multiplication) on two integers takes unit time. (a) Give a deterministic algorithm for finding in time. ¦£ §¡2¨ U a3 ! §¡  ¢¡ h fh g ¡  ! B 88 4 [email protected]@8997653 ¦&"!£©¨ % % h eh g  t¦b¡dr qpe s£ fi ! U3 SR VT21Q ¡ ! ¡  ¡ ¡  ¦¡ £ ©¨ f ¦!) &10('£ !) 1¡ ¡ e ! (a) Give an algorithm to merge two heaps of size heap of size in time . and (assume ¢¡ ¡ ), into a single I GE PHFD ! ¡ ¦¤ §¡ ¥ £ in time ¦ £ d!c1Vb¡2¨ Y3 SW `X$Q ¦¡ £ §$#"!©¨ % ¢¡ Handout 10: Problem Set 3 (b) Give a randomized algorithm for finding Problem 3-4. in expected 3 -ary Search Trees A -ary Search Tree ( -ST) is a Search Tree in which each non-leaf node has The function maps nodes to integers. for some integer . Suppose we build a complete -ary Search Tree (a) Let on nodes. What is the height of the tree in terms of and ? What is the runtime of a S EARCH operation? (b) Let be defined as “the number of nodes in the subtree rooted at , not including ”. For a leaf node , . Suppose . The figure below depicts such a tree in which the function has the following values: , A C E B D What is the height of a complete -ary Search Tree with nodes? What is the runtime of a S EARCH operation? For convenience, you may assume that is always an integer. ¦€y£‡1"„9ƒ ’ †E ¦£ dbyxw y ¦£ €yxw  ¦£ §¡2¨ ¡ (”¦€y£‡…„9ƒ “’‘g €byFw †E  ¦£ ¡ ¦fi vup"e£ w time. ˆng ” ˆ ' g @”' ¦£ €byFw g™—g ˜”ugk‡…„9ƒ ˜’mg aglxw g 2gkxw  ¦ £† E  ¦ £ ¦ £  ¦ £† E  ¦ £ ¦ £ ™ —g “”jgf‡1"„9ƒ ˜’—g ighxw g agfxw e Pg ” d "™—g (”–•‡1"„0ƒ ˜’—g –•xw  ¦ £† E  ¦ £ y ˆ ¦ £† E ‰g dby‡1…„9ƒ y ¦ £† E dby‡1…„9ƒ  ¦£ €byFw ¦£ €yxw £ ‚g ¦€yxw w ¡ ¦£ €byFw children. ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online