finsolgg

# Just give the upper bound you do not have to justify

This preview shows pages 3–5. Sign up to view the full content.

(Just give the upper bound; you do not have to justify how you got it.) Answer: O ( n 2 ) (b) Prove the upper bound on f ( n )+ g ( n ) that you have determined. Your proof should follow just from the deﬁnition of Big-Oh. You are NOT allowed to use the theorems about Big-Oh stated in the lecture notes. Answer: Since 100 f ( n ) is O ( g ( n ) · h ( n )), 200 g ( n ) is O ( n ) and 300 h ( n ) is O ( g ( n )) there exist N 1 ,N 2 ,N 3 ,c 1 ,c 2 ,c 3 > 0 such that n N 1 100 f ( n ) c 1 g ( n ) h ( n )) and n N 2 200 g ( n ) c 2 n and n N 3 300 h ( n ) c 3 g ( n ) Therefore, taking N = max( N 1 ,N 2 ,N 3 ) we have for any n N : f ( n ) + g ( n ) c 1 100 g ( n ) h ( n ) + g ( n ) c 1 100 g ( n ) c 3 300 g ( n ) + g ( n ) c 1 100 c 2 200 n c 3 300 c 2 200 n + c 2 200 n Denote α = c 1 c 2 2 c 3 6000000 and β = c 2 200 Therefore n N f ( n ) + g ( n ) αn 2 + β n Now let N 0 = max( N, 1) and c = α + β . For all n N 0 we have f ( n ) + g ( n ) cn 2 . This concludes the proof that f ( n ) + g ( n ) is O ( n 2 ). Grading guidelines: 10pts for part (a) and 15pts for part (b). In part (a) you lose 1pt for stating Big-Oh bound statements that are not crisp, such as writing O ( n 2 + n ). If you get part (a) wrong you still get 2-3pts if you state a bound that is not tight such as O ( n 3 ). In part (b) you get 3pts for correct statement of Big-Oh, 8pts for getting the calculations right and 4pts for stating the right ﬁnal N such as N = max( N 1 ,N 2 ,N 3 ). 5. (20pts) Give pseudocode for a method void koo(Stack s1, Stack s2) that copies the elements of s1 into s2 but under the existing elements of s2 . Moreover, after the execution of this method the state of s1 must be the same as before the execution. Example: 3

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

View Full Document
33 44 55 s 1 11 22 s 2 7→ 33 44 55 s 1 11 22 33 44 55 s 2 You can only use stacks (as many as you want) as auxilliary data structures. You may not as- sume anything about the underlying implementations of the stacks except that they provide all the operations in the stack interface. Answer: Here is an algorithm using two stacks s 3 and s 4: while s1 is not empty pop an element from s1 and push it to s3 while s2 is not empty pop an element from s2 and push it to s4 while s3 is not empty pop an element from s3 and push it to s1 and s2 while s4 is not empty pop an element from s4 and pust it to s2. Grading guidelines: You lose 5pts for not restoring s 1 to its prior state. You lose 5pts for producing the wrong order of elements in s 2 . 6. (20pts) On a hash table of size 7 we use double hashing with the ﬁrst hashing function f ( n ) = n mod7 and the second hashing function g ( n ) = n mod 3 + 1. Starting with an empty table, what is a worst- case sequence (that is, the sequence with the maximum total number of collisions) of 4 distinct insertion inputs, if each input is a number between 1 and 100. Show the content of the hash table after the 4 insertions. Answer:
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page3 / 11

Just give the upper bound you do not have to justify how...

This preview shows document pages 3 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online