{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

solutions1

# solutions1 - MIT OpenCourseWare http/ocw.mit.edu 6.006...

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

MIT OpenCourseWare http://ocw.mit.edu 6.00 6 Introduction to Algorithms Spring 200 8 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms .

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

View Full Document
Introduction to Algorithms: 6.006 Massachusetts Institute of Technology March 7, 2008 Professors Srini Devadas and Erik Demaine Handout 6 Quiz 1 Practice Problems 1 Asymptotic Notation Decide whether these statements are True or False . You must brieﬂy justify all your answers to receive full credit. 1. If f ( n ) = Θ( g ( n )) and g ( n ) = Θ( h ( n )) , then h ( n ) = Θ( f ( n )) Solution: True. Θ is transitive. ± 2. If f ( n ) = O ( g ( n )) and g ( n ) = O ( h ( n )) , then h ( n ) = Ω( f ( n )) Solution: True. O is transitive, and h ( n ) = Ω( f ( n )) is the same as f ( n ) = O ( h ( n )) 3. If f ( n ) = O ( g ( n )) and g ( n ) = O ( f ( n )) then f ( n ) = g ( n ) Solution: False: f ( n ) = n and g ( n ) = n + 1 . ± n 4. = Ω( n ) 100 Solution: True. n < c n for c = 1 . 100 200
2 Handout 6: Quiz 1 Practice Problems 5. f ( n ) = Θ( n 2 ) , where f ( n ) is defined to be the running time of the program A(n) : def A(n): atuple = tuple(range(0, n)) # a tuple is an immutable version of a # list, so we can hash it S = set() for i in range(0, n): for j in range(i+1, n): S.add(atuple[i:j]) # add tuple (i,...,j-1) to set S Solution: False: Inside the two for loops, both slicing and hashing take linear time. 2 Linked List Equivalence Let S and T be two sets of numbers, represented as unordered linked lists of distinct numbers. All you have are pointers to the heads of the lists, but you do not know the list lengths . Describe an O (min {| S | , | T |} ) -expected-time algorithm to determine whether S = T . You may assume that any operation on one or two numbers can be performed in constant time. Solution: First, check that both sets are the same size. If they are not, then they cannot be equal.

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

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

{[ snackBarMessage ]}