Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

Similar documents
Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Introduction to Algorithms March 12, 2008 Massachusetts Institute of Technology Spring 2008 Professors Srini Devadas and Erik Demaine Quiz 1

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

Introduction to Algorithms April 15, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 2

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

6.006 Final Exam Name 2. Problem 1. True or False [30 points] (10 parts) For each of the following questions, circle either True, False or Unknown.

Quiz 2. Problem Parts Points Grade Grader. Total 120. Name: Recitation: Jayant 12 PM. Christina 10 AM. Jayant 1 PM. Jason 2 PM.

Final Exam. Problem Parts Points Grade Grader Problem Parts Points Grade Grader Total 180. Name: Athena username: Joe WF3a. Joe WF2.

Introduction to Algorithms April 16, 2008 Massachusetts Institute of Technology Spring 2008 Professors Srini Devadas and Erik Demaine Quiz 2

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

Introduction to Algorithms November 28, 2007 Massachusetts Institute of Technology Fall 2007 Professors Ron Rivest and Srini Devadas Quiz 2

Practice Midterm Exam Solutions

Quiz 1 Practice Problems

Quiz 1 Practice Problems

Introduction to Algorithms December 16, 2011 Massachusetts Institute of Technology Fall 2011 Professors Erik Demaine and Srini Devadas

Final Exam in Algorithms and Data Structures 1 (1DL210)

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Quiz 1 Solutions. (a) If f(n) = Θ(g(n)) and g(n) = Θ(h(n)), then h(n) = Θ(f(n)) Solution: True. Θ is transitive.

Introduction to Algorithms November 17, 2011 Massachusetts Institute of Technology Fall 2011 Professors Erik Demaine and Srini Devadas Quiz 2

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Final Examination CSE 100 UCSD (Practice)

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Basic Data Structures (Version 7) Name:

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Midterm solutions. n f 3 (n) = 3

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Final Exam in Algorithms and Data Structures 1 (1DL210)

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Solutions to Exam Data structures (X and NV)

Balanced Binary Search Trees. Victor Gao

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

CSL 201 Data Structures Mid-Semester Exam minutes

Second Examination Solution

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

CS 380 ALGORITHM DESIGN AND ANALYSIS

Course Review. Cpt S 223 Fall 2010

Introduction to Algorithms April 21, 2004 Massachusetts Institute of Technology. Quiz 2 Solutions

Course Review for Finals. Cpt S 223 Fall 2008

Course Review. Cpt S 223 Fall 2009

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

Technical University of Denmark

Sample Exam 1 Questions

Section 05: Solutions

x-fast and y-fast Tries

Trees. Eric McCreath

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Section 1: True / False (1 point each, 15 pts total)

ECE250: Algorithms and Data Structures Midterm Review

Problem Set 6 Due: 11:59 Sunday, April 29

CSE 100 Advanced Data Structures

Data Structures and Algorithms

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Lecture 6: Analysis of Algorithms (CS )

Test 1 - Closed Book Last 4 Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

CS-301 Data Structure. Tariq Hanif

Lecture Summary CSC 263H. August 5, 2016

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Quiz III

Problem Set 5. MIT students: Each problem should be done on a separate sheet (or sheets) of three-hole punched paper.

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

Section 05: Solutions

: Fundamental Data Structures and Algorithms. June 06, 2011 SOLUTIONS

Data Structures and Algorithms

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017

1. Attempt any three of the following: 15

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

UNIT III BALANCED SEARCH TREES AND INDEXING

Principles of Algorithm Design

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Properties of a heap (represented by an array A)

CISC 320 Midterm Exam

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

x-fast and y-fast Tries

Search Trees: BSTs and B-Trees

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

CSE wi: Practice Midterm

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

CSCE 2014 Final Exam Spring Version A

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Section 4 SOLUTION: AVL Trees & B-Trees

CS102 Binary Search Trees

Module 2: Classical Algorithm Design Techniques

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

( ) n 5. Test 1 - Closed Book

CSE373 Fall 2013, Midterm Examination October 18, 2013

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

CMSC351 - Fall 2014, Homework #2

Exam Data structures DIT960/DAT036

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

UNIVERSITY REGULATIONS

Transcription:

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1 Quiz 1 Do not open this quiz booklet until 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. You have 120 minutes to earn 120 points. 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. This quiz booklet contains 12 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 exam period. This quiz is closed book. You may use one 8 1 11 or A4 crib sheet (both sides). No 2 calculators or programmable devices are permitted. No cell phones or other communications 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. 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. Show your work, as partial credit will be given. You will be graded not only on the correctness of your answer, but also on the clarity with which you express it. Be neat. Good luck! Problem Parts Points Grade Grader 1 4 10 2 4 16 3 4 24 4 3 20 5 3 15 6 1 15 7 3 20 Total 120 Name: Friday Recitation: Rishabh 10 AM Rob 12 PM Chieu 1 PM Jason 2 PM Matthew 3 PM

6.006 Quiz 1 Name 2 Problem 1. Asymptotic orders of growth [10 points] (4 parts) For each pair of functions, circle the asymptotic relationships that apply. You do not need to give a proof. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g) (b) f(n) = 1 g(n) = 2 Circle all that apply: f = O(g) f = Θ(g) f = Ω(g) (c) f(n) = 1000 2 n g(n) = 3 n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g) (d) f(n) = 5n log n g(n) = n log 5n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

6.006 Quiz 1 Name 3 Problem 2. True or False [16 points] (4 parts) For each of the following questions, circle either T (True) or F (False). Explain your choice. (No credit if no explanation given.) (a) T F Performing a left rotation on a node and then a right rotation on the same node will not change the underlying tree structure. Explain: (b) T F While inserting an element into a BST, we will pass the element s predecessor and successor (if they exist). Explain: (c) T F For a hash table using open addressing, if we maintain m = Θ(n), then we can expect a good search and insert runtime. Explain: (d) T F If we know ahead of time all the keys that will ever be inserted into a hash table, it is possible to design a hash table that guarantees O(1) lookup and insertion times, while using O(n) space. Explain:

6.006 Quiz 1 Name 4 Problem 3. Short Answer [24 points] (4 parts) (a) Describe an efficient method to merge two balanced binary search trees with n elements each into a balanced BST. Give its running time. (b) Suppose you are given a list of n elements such that the location of each element is at most lg(lg n) elements away from the location it would be in if the list were sorted. Describe an o(n lg n)-time method to sort the list and give its asymptotic running time.

6.006 Quiz 1 Name 5 (c) Suppose we have a hash table that resolves collisions using open addressing with linear probing. Slots with no keys contain either an EMPTY marker or a DELETED marker. Alyssa P. Hacker tries to reduce the number of DELETED markers; she proposes to use the following rules in the delete method: i. If the object in the next slot is EMPTY, then a DELETED marker is not necessary. ii. If the object in the next slot has a different initial probe value, then a DELETED marker is not necessary. Determine whether each of the above rules guarantees that searches return a correct result. Explain. (d) An open-addressing hash table that resolves collisions using linear probing is initially empty. Key k 1 is inserted into the table first, followed by k 2, and then k 3 (the keys themselves are drawn randomly from a universal set of keys). i. Suppose k 2 is deleted from the hash table and replaced by a DELETED marker. What is the probability that searching for k 3 requires exactly three probes? ii. What is the probability that searching for k 1 takes exactly two probes?

6.006 Quiz 1 Name 6 Problem 4. Piles [20 points] (3 parts) The heaps that we discussed in class are binary trees that are stored in arrays; there are no explicit pointers to children, because the index of children in the array can be computed given the index of the parent. This problem explores heaps (priority queues) that are represented like search trees, in which each tree node is allocated separately and in which parent nodes have explicit pointers to their children. We will call this data structure a Pile. A node in a pile can have zero to two children, and the value stored in the node must be at least as large as the value stored at its children. (a) What is the asymptotic cost of INSERT and EXTRACT-MAX in a pile of height h with n nodes? Explain. (b) An AVL Pile is a pile in which every node also stores the height of the subtree rooted at the node, and in which the height of the children of a node can differ by at most one. (The height of a missing child is defined to be 1.) What is the maximum height of an AVL pile with n nodes? (c) Describe a simple algorithm to insert a value into an AVL Pile while maintaining the AVL property (ensuring that the height difference between siblings is at most one). Argue that your algorithm indeed maintains the AVL property.

6.006 Quiz 1 Name 7 Problem 5. Rolling hashes [15 points] (3 parts) Ben Bitdiddle, Louis Reasoner, and Alyssa P. Hacker are trying to solve the problem of searching for a given string of length m in a text of length n. They implement different algorithms that all follow the same general outline, and all make use of a hash function h(x, p) which maps a string x to a number in the range 0... p 1. The values returned by h(x, p) satisfy uniform hashing. 1 def string_search(text, substring): 2 m = len(substring) 3 n = len(text) 4 if m > n: return None 5 p =... # differs based on implementation 6 target = h(substring, p) 7 for start in xrange(0, n-m + 1): 8 # the +1 includes the endpoint n-m 9 hvalue =... # calculate h(x, p) 10 if hvalue == target: 11 if text[start:start+m] == substring: 12 return start 13 return None (a) Ben Bitdiddle chooses p so that 1/2 α 1, where α = n/p. He implements the hash operation on line 9 using a non-rolling hash: hvalue = h(text[start:start+m], p) In terms of m and n (not α), what is the asymptotic expected running time of Ben s algorithm? Explain your answer. Explain: T (m, n) = Θ( )

6.006 Quiz 1 Name 8 (b) Alyssa P. Hacker recognizes that this is a good case to use a rolling hash. She defines h so that she can calculate each hash value h(text[start:start+m], p) from the previous value h(text[start-1:start-1+m], p) using a constant number of arithmetic operations on values no larger than p. Instead of calling the h function on the substring every time like Ben Bitdiddle does, then, on line 9 she simply updates hvalue based on its previous value using these arithmetic operations. She chooses p in the same manner as Ben. In terms of m and n (not α), what is the asymptotic expected running time of Alyssa s algorithm? Explain your answer. Explain: T (m, n) = Θ( )

6.006 Quiz 1 Name 9 (c) Louis Reasoner doesn t want to worry about searching for a good prime number, so on line 5 he simply sets p = 1009 and lets α vary. He implements the rolling hash like Alyssa. In terms of m and n (not α), what is the asymptotic expected running time of Louis s algorithm? Explain your answer. Explain: T (m, n) = Θ( )

6.006 Quiz 1 Name 10 Problem 6. Treaps [15 points] (1 parts) Ben Bitdiddle recently learned about heaps and binary search trees in his algorithms class. He was so excited about getting to know about them, he thought of an interesting way to combine them to create a new hybrid binary tree called a treap. The treap T has a tuple value stored at each node (x, y) where he calls x the key, and y the priority of the node. The keys follow the BST property (maintain the BST invariant) while the priorities maintain the min-heap property. An example treap is shown below: Describe an efficient algorithm for INSERT((x, y), T ) that takes a new node with key value x and priority y and inserts it into the treap T. Analyze the running time of your algorithm on a treap with n nodes, and height h.

6.006 Quiz 1 Name 11 Problem 7. Amortized Successor [20 points] (3 parts) The successor of a node x in a binary search tree T is the node in T with the smallest value that is larger than the value of x. We assume in this question that all the values in the tree are distinct. Consider the following code for determining the successor of some node x. A node is an object with a value and pointers to its parent and left and right children. 1 # gets successor of x in tree rooted at root 2 def get_successor(x, root): 3 if x == None: 4 return None 5 elif x.right!= None: 6 return get_minimum(x.right) 7 else: 8 while x!= root and x.parent.right == x: 9 x = x.parent 10 return x.parent 11 12 # gets minimum node in tree rooted at x 13 def get_minimum(x): 14 if x == None: 15 return None 16 elif x.left == None: 17 return x 18 else: 19 return get_minimum(x.left) (a) Show that the worst-case running time of get successor is O(lg n) on a balanced BST and O(n) on a general BST.

6.006 Quiz 1 Name 12 The following procedure returns a list of all the values in a binary search tree by finding the minimum and then locating the successor of each node in turn. 1 # returns ordered list of values in tree rooted at root 2 def spell_out(root): 3 node_list = [] 4 current_node = get_minimum(root) 5 while current_node!= root.parent: 6 node_list.append(current_node.value) 7 current_node = get_successor(current_node, root) 8 return node_list (b) Since get successor is called once for each node in the tree, an upper bound for the worst-case running time of spell out is O(n lg n) on a balanced BST and O(n 2 ) on a general BST. This bound, however, is not asymptotically tight. Using amortized analysis, show that spell out runs in worst-case O(n) time on any BST. Hint: How many times is each edge encountered over the course of spell out? (c) Give an asymptotically tight bound on the amortized cost of get successor over the course of one call to spell out.

SCRATCH PAPER

SCRATCH PAPER