Questions(HW2)

2
Question 1:  Prove that the function f(N ) = 5 N + 13 N 2  + 175 is O(N 2  ). Solution: According to the definition of the Big-O, the function f(n) = 5n + 13n 2  + 175 is O(N 2 ) if there exist positive numbers c and N such that: 5n + 13n 2  + 175 cn 2  for all n N  Divide both sides by n 2  , we obtain:  +  + 13 c Then, we can easily calculate c and N : c 193 68.25 34.11 ……… ……… 13  N 1 2 3 ……… ………  Hence, the function f(n) = 5n + 13n 2  + 175 is O(N 2 ). Question 2: Show the result of inserting 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, 2, one at a time, into an initially empty binary heap (just show the resulting array).  Solution: I use min heap, so the resulting array: After inserting 10: {10} After inserting 12: {10, 12} After inserting 1: {1, 12, 10} After inserting 14: {1, 12, 10, 14} After inserting 6: {1, 6, 10, 14, 12} After inserting 5: {1, 6, 5, 14, 12, 10} After inserting 8: {1, 6, 5, 14, 12, 10, 8} After inserting 15: {1, 6, 5, 14, 12, 10, 8, 15} After inserting 3: {1, 3, 5, 6, 12, 10 , 8, 15, 14}

description

this is very bullshit. we upload it for fun. Have fun reading it

Transcript of Questions(HW2)

Question 1: Prove that the function f(N) = 5 N + 13 N2 + 175 is O(N2).Solution:According to the definition of the Big-O, the function f(n) = 5n + 13n2 + 175 is O(N2) if there exist positive numbers c and N such that: 5n + 13n2 + 175 cn2 for all n N Divide both sides by n2 , we obtain: + + 13 cThen, we can easily calculate c and N :c19368.2534.1113

N123

Hence, the function f(n) = 5n + 13n2 + 175 is O(N2).

Question 2: Show the result of inserting 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, 2, one at a time, into an initially empty binary heap (just show the resulting array).Solution: I use min heap, so the resulting array:After inserting 10: {10}After inserting 12: {10, 12}After inserting 1: {1, 12, 10}After inserting 14: {1, 12, 10, 14}After inserting 6: {1, 6, 10, 14, 12}After inserting 5: {1, 6, 5, 14, 12, 10}After inserting 8: {1, 6, 5, 14, 12, 10, 8}After inserting 15: {1, 6, 5, 14, 12, 10, 8, 15}After inserting 3: {1, 3, 5, 6, 12, 10 , 8, 15, 14}After inserting 9: {1, 3, 5, 6, 9, 10, 8, 15, 14, 12}After inserting 7: {1, 3, 5, 6, 7, 10, 8, 15, 14, 12, 9}After inserting 4: {1, 3, 4, 6, 7, 5, 8, 15, 14, 12, 9, 10}After inserting 11: {1, 3, 4, 6, 7, 5, 8, 15, 14, 12, 9, 10, 11}After inserting 13: {1, 3, 4, 6, 7, 5, 8, 15, 14, 12, 9, 10, 11, 13}After inserting 2: {1, 3, 2, 6, 7, 5, 4, 15, 14, 12, 9, 10, 11, 13, 8}

Question 3: Show the result of using the linear-time algorithm to build a binary heap using the same input as previous question (just show the resulting array).Solution:Firstly, we insert all data into the heap in the given order, then we have the initial array: {10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, 2}Next,we heapify the third last level, the array changes into:{10, 12, 1, 3, 6, 4, 2, 15, 14, 9, 7, 5, 11, 13, 8}Then, we heapify the second last level, the array changes into:{10, 3, 1, 12, 6, 4, 2, 15, 14, 9, 7, 5, 11, 13, 8}Finally, we heapify the top level, the array changes into:{1, 3, 2, 12, 6, 4, 8, 15, 14, 9, 7, 5, 11, 13, 10} Question 4: Show the result of performing three deleteMin operations in the heap of the previous question (just show the resulting array).Solution:The ordinary heap: {1, 3, 2, 6, 7, 5, 4, 15, 14, 12, 9, 10, 11, 13, 8}After the first deleteMin: {2, 3, 4, 6, 7, 5, 8, 15, 14, 12, 9, 10, 11, 13}After the second deleteMin: {3, 6, 4, 13, 7, 5 , 8, 15, 14, 12, 9, 10, 11}After the third deleteMin: {4, 6, 5, 13, 7, 10 , 8, 15, 14, 12, 9, 11}