### CS302 Midterm Exam - October 14, 2014

Do your answers on the answer sheets provided. When you write code, you do not need to have "include" or "using" statements.

### Question 1

For each of the following, tell me the running time. Use the answer sheet. For disjoint sets, assume that you are using union by rank with path compression. Assume that m is smaller than n. If two answers are correct, please give me the one that is smaller -- in other words, if something is O(n) and also O(n2), then the answer should be O(n).
• A. Sorting a vector with m random numbers using merge sort.
• B. Deleting m elements from a map with n elements.
• C. Performing m Find() operations on a disjoint set instance with n elements.
• D. Creating a vector from the smallest m elements of a map with n elements.
• E. Creating a heap from the first m elements of a vector with n elements.
• F. Calling Find() m times on a multiset with n elements.
• G. Enumerating all permutations of the first m elements of a vector with n distinct elements.
• H. Performing m Pop() operations on a heap with n elements.
• I. Enumerating all vectors of length m, where each element of the vector can have one of n potential values.
• J. Sorting a vector with m random numbers using quicksort.
• K. Performing m Push() operations on a heap with n elements.
• L. Sorting a vector with m random numbers using insertion sort.
• M. Creating a map from the first m elements of a vector with n elements.
• N. Performing m Union() operations on a disjoint set instance with n elements.
Choose from the following multiple choice answers:

 a. O(1) g. O(n) m. O(m log(n)) s. O(nm) y. O(2m) b. O(α(m)) h. O(n α(n)) n. O(m log(m)) t. O(mn) z. O(2n) c. O(α(n)) i. O(m α(n)) o. O(n log(n)) u. O(mm) d. O(log(n)) j. O(m α(m)) p. O(nm) v. O(nn) e. O(log(m)) k. O(n α(n)) q. O(n2) w. O(n!) f. O(m) l. O(n log(m)) r. O(m2) x. O(m!)

### Question 2

To the right is an instance of disjoint sets. Answer the following. For each question, assume that the starting state is as pictured. Do not have the state of the instance change as you answer questions.

• A. What does Find(0) return?
• B. What does Find(1) return?
• C. What does Find(2) return?
• D. What does Find(3) return?
• E. Suppose we call Union(6, 10) and we are using Union By Size. Specify the links vector that after the union call.
• F. Suppose we call Union(6, 10) and we are using Union By Height. Specify the links vector that after the union call.
• G. Suppose we call Find(12) and we are using path compression. Tell me which elements of links change, and what they change to.