q1-f2009-sol

q1-f2009-sol - Introduction to Algorithms October 14, 2009...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: Introduction to Algorithms October 14, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Srini Devadas and Constantinos (Costis) Daskalakis Quiz 1 Solutions Quiz 1 Solutions Problem 1. Asymptotic orders of growth [9 points] (3 parts) For each of the three pairs of functions given below, rank the functions by increasing order of growth; that is, find any arrangement g 1 ,g 2 ,g 3 of the functions satisfying g 1 = O ( g 2 ) , g 2 = O ( g 3 ) . lg n represents logarithm of n to the base 2. (a) f 1 ( n ) = 8 n, f 2 ( n ) = 25 1000 , f 3 ( n ) = ( 3) lg n Solution: f 2 ( n ) ,f 1 ( n ) ,f 3 ( n ) (b) f 1 ( n ) = 1 100 , f 2 ( n ) = 1 n , f 3 ( n ) = lg n n Solution: f 2 ( n ) ,f 3 ( n ) ,f 1 ( n ) (c) f 1 ( n ) = 2 lg 3 n , f 2 ( n ) = n lg n , f 3 ( n ) = lg n ! Solution: f 3 ( n ) ,f 2 ( n ) ,f 1 ( n ) 6.006 Quiz 1 Solutions Name 2 Problem 2. Balanced Augmented BST [20 points] (2 parts) In this question, we use balanced binary search trees for keeping a directory of MIT students. We assume that the names of students have bounded constant length so that we can compare two different names in O (1) time. Let n denotes the number of students. (a) Say we have a binary search tree with students last names as the keys with lexico- graphic dictionary ordering. Let k be the number of students whose last name starts with a given prefix x . For example, if the tree contains 5 names { ABC, ABD, ADA, ADB, ADC } there are k=2 names, ABC and ABD respectively, starting with the prefix x=AB. How can we output a list of all those students in O ( k + log n ) time? Solution: Consider the following procedure for traversing the tree. traverse ( tree , prefix ): if tree is empty then return name := the last name at the root of tree if prefix is a prefix of name then traverse (left subtree of tree , prefix ) output( name ) traverse (right subtree of tree , prefix ) elseif prefix < name then traverse (left subtree of tree , prefix ) else traverse (right subtree of tree , prefix ) It suffices to run traverse (the entire balanced BST, x ) to find all last names with prefix x . The algorithm has the following properties: It finds all names with prefix x , because it never excludes a subtree that could contain a name with prefix x . It only outputs names with x as a prefix. The running time is O ( k +log n ) , because at each depth it visits at most two nodes for which x is not a prefix. Therefore, it visits at most 2 O (log n ) + k nodes. Another correct solution: Another correct solution is to find the lexicographically first last name with prefix x , and then to continue visiting consecutive nodes in lexi- cographic order as in the in-order tree traversal, until we encounter a name for which x is not a prefix. This procedure visits exactly the same nodes as the first solution, and also runs in O ( k + log n ) time. 6.006 Quiz 1 Solutions Name 3 Common errors: It is not true that the nodes with prefix x must constitute a connected component...
View Full Document

Page1 / 9

q1-f2009-sol - Introduction to Algorithms October 14, 2009...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online