C 2 200 n c 3 300 c 2 200 n c 2 200 n denote α c 1 c

Info icon This preview shows pages 3–6. Sign up to view the full content.

View Full Document Right Arrow Icon
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 ) c n 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 final 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
Image of page 3

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

View Full Document Right Arrow Icon
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 first 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: The worst-case is the case with the maximum number of collisions. With double-hashing this happens if all keys hash to the same value with both hash functions. For our two hash functions, this will happen if all keys are congruent mod 7 · 3 = 21. So, for example, we can take 63 , 21 , 42 , 84 For each of these inputs, f hashes to 0 and g to 1. There are 0 + 1 + 2 + 3 = 6 collisions with this sequence of insertions. Afterwards, the table contains ------------------------------------ | | | | | | | | | 63 | 21 | 42 | 84 | | | | 4
Image of page 4
| | | | | | | | ------------------------------------ index 0 1 2 3 4 5 6 Grading guidelines: 5pts for just understanding open addressing. 12pts for also understanding double hashing but getting an incorrect answer (for example an answer with fewer than 6 collisions). 7. (25pts) Consider the method foo shown to the right.
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern