Practice Questions 1. Which of the following are true? (a) 3n = O(n2) (c) nlgn = Θ(20000nlgn) (b) 3n2 = O(nlgn) (d) 2n = Ω(n1000) Answers: (a), (c), (d) (a) n2 clearly grows larger than 3n as n gets large. (c) 20000 is a constant in front of nlgn (d) All exponential functions grow faster than all polynomial functions. (b) is false because 3n2 grows faster than nlgn. 2. What is the run-time of the following segment of code in terms of n? Give an upper bound and justify it. Only a tight upper bound will be accepted as a correct answer. int i=1; while (i <= n) { int j = i; while (j > 0) j = j/2; i++; } The outer loop runs n times. The inner loop will run log i times at most since there is repeated halving. Since i never exceeds n, it is safe to say the inner loop runs at most log n times. Hence an upper bound on the run time is O(nlgn). n 1 3. Determine the following sum: 3 i i 0 This is a geometric sum with a first term of 1, with a common ratio of 3, with n terms. Here's the sum: 1 3n 3n 1 3 1 3 2 . i 0 n 1 i n 4. Determine the following sum: log i 1 n n n i 1 i 1 2 4i log 2 4 i(log 2 4) 2i 2( i i 1 n( n 1) ) n( n 1) 2 5. Order the following functions from smallest to largest in terms of big-theta notation. n3 2 1 , n n , ,17, lg( n20 ), lg 2 n lg n n 1 n3 20 2 2 Answer: n ,17, lg( n ), lg n, n n , lg n As n grows large, 1/n tends to 0, so this is the smallest function. 17 is a constant, so it comes next. lg n20 = 20lg n which is Θ(lg n). The following function is slightly bigger than that. Finally to decide between the last two, note that n is a larger function than lg n. Thus, when we divide by lg n, we are dividing by something less than n . (The key here is that n 2 n can be rewritten as n3 n .) 6. Order the following functions from smallest to largest in terms of big-theta notation. 2n ,4 n ,23n ,7n , n15 , nlg n Answer: n15 , nlg n ,4 n ,2n ,7n ,23n All polynomial functions grow more slowly than exponential functions, so n15 comes first. We can rewrite the second function as (2 lg n ) lg n . This makes it comparable to other functions with an base of 2. The third function can be written as 2 2 n . Comparing the exponents of these two functions proves their order. Then the last three functions are each exponential functions raised to the n power with different bases. It's clear that these should be ordered by increasing value of the base. 7. Given the following experimental run-times, make the best guess for the theta-bound for the run-time of the algorithm described below: Input Size (n) 10 20 40 80 160 Run-Time (in ms) 5 21 78 325 1277 As the input size doubles, the run time increases by a factor of 4 each time, so the run-time is most likely Θ(n2). 8. Draw the result of inserting 31 into the AVL tree below: 20 / \ 10 40 / / \ 5 30 50 / \ 25 35 20 / \ 10 35 / / \ 5 30 40 / \ \ 25 31 50 20 / --- 10 / 5 \ 40 / \ 30 50 / \ 25 35 / 31 9. What is the result of inserting 19 into the following 2-4 Tree: 10, 20, 30 / / \ 5 13, 15, 17 27 \ 39 - 10, 20, 30 / / \ 5 13, 15, 17,19 27 \ 39 - 10, 17, 20, 30 / / \ 5 13, 15,19 27 \ 39 - 20 / 10, 17 / | \ 5 13, 15 19 \ 30 / \ 27 39 10. Draw the Heap created from running the Make Heap algorithm (shown in class on the following set of values – assume a minimum heap): 37 / 21 / 10 / 12 \ 58 \ 33 \ 9 / 19 \ 24 37 / 21 / \ 33 9 / 12 \ 58 / 19 \ - 24 \ 10 37 / 21 / \ 33 9 / 12 \ 19 / 58 \ - 24 \ 10 37 / \ 19 9 / 10 / 12 \ 33 / 58 \ - 24 \ 21 9 / 10 / 12 / 37 \ 19 \ 33 \ 21 / 58 \ 24 11. Consider a hash table of size 113. Imagine implementing quadratic probing on such a hash table. If our initial hash value to insert an item was index 95, list the first five indexes in which we would look AFTER index 95 in case of collisions. (Note: Your first answer should be 96.) The indexes are: 96, 99, 104, 111, and 7, since 120%113 = 7. 12. Here is an array representation of a Disjoint Set of the elements 1, 2, 3, …, 10. Draw the corresponding tree representation. 1 8 3 | \ 8 10 | 1 2 3 9 4 3 9 | 2 | 4 2 5 5 5 7 | 6 6 7 7 7 8 3 9 9 10 3